From b23f04be7cbe12837a794f9d0f8f818552a65077 Mon Sep 17 00:00:00 2001 From: coderaiser Date: Sat, 10 May 2014 04:42:17 -0400 Subject: [PATCH] feature(util) add getType --- lib/util.js | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/lib/util.js b/lib/util.js index da9887e2..7af7d735 100644 --- a/lib/util.js +++ b/lib/util.js @@ -339,12 +339,12 @@ this.isContainStr = function(str1, str2) { var i, n, str, is, index, - isStr1 = Util.isString(str1), - isStr2 = Util.isString(str2), - isArray = Util.isArray(str2); + isStr = Util.isString(str1), + type = Util.getType(str2); - if (isStr1) - if (isArray) { + if (isStr) + switch (type) { + case 'array': n = str2.length; for (i = 0; i < n; i++) { @@ -354,9 +354,12 @@ if (is) break; } - } else if (isStr2) { + break; + + case 'string': index = str1.indexOf(str2); is = index >= 0; + break; } return is; @@ -686,6 +689,22 @@ return typeof variable === pType; }; + /** + * get type of variable + * + * @param variable + */ + this.getType = function(variable) { + var regExp = new RegExp('\\s([a-zA-Z]+)'), + obj = {}, + toStr = obj.toString, + str = toStr.call(variable), + typeBig = str.match(regExp)[1], + type = typeBig.toLowerCase(); + + return type; + }; + /** * return save exec function * @param callback