Comparing version 1.3.0 to 1.4.0
445
lib/util.js
@@ -30,69 +30,2 @@ (function(scope) { | ||
/** | ||
* add listener to observer | ||
* | ||
* @param name - event name | ||
* @param func - function | ||
* @param allListeners - listeners array | ||
* @param observer - observer object | ||
*/ | ||
this.addListener = function(name, func, allListeners, observer) { | ||
var listeners, obj, | ||
type = Util.getType(name); | ||
switch(type) { | ||
case 'string': | ||
listeners = allListeners[name]; | ||
if (!listeners) | ||
listeners = allListeners[name] = []; | ||
listeners.push(func); | ||
if (func && observer) | ||
observer.on(name, func); | ||
break; | ||
case 'object': | ||
obj = name; | ||
Object.keys(obj).forEach(function(name) { | ||
func = obj[name]; | ||
Util.addListener(name, func, allListeners, observer); | ||
}); | ||
break; | ||
} | ||
return this; | ||
}; | ||
/** | ||
* remove listener from observer | ||
* | ||
* @param name - event name | ||
* @param func - function | ||
* @param allListeners - listeners array | ||
* @param observer - observer object | ||
*/ | ||
this.removeListener = function(name, func, allListeners, observer) { | ||
var listeners; | ||
if (observer) | ||
observer.removeListener(name, func); | ||
listeners = allListeners[name]; | ||
if (listeners) | ||
listeners = listeners.map(function(listener) { | ||
if (listener === func) | ||
listener = null; | ||
return listener; | ||
}); | ||
return this; | ||
}; | ||
/** | ||
* Функция ищет в имени файла расширение | ||
@@ -105,3 +38,3 @@ * и если находит возвращает true | ||
var isMatch, str, | ||
type = Util.getType(ext), | ||
type = Util.type(ext), | ||
regStr = '\\.({{ exts }})$', | ||
@@ -117,3 +50,3 @@ regExp; | ||
regExp = new RegExp(regStr, 'i'); | ||
isMatch = name.match(regExp); | ||
isMatch = regExp.test(name); | ||
@@ -137,13 +70,15 @@ break; | ||
n = names.length, | ||
isLength = n === argsParam.length, | ||
template = '{{ name }} coud not be empty!'; | ||
for (i = 0; i < n; i++) | ||
if (!argsParam[i]) { | ||
msg = Util.render(template, { | ||
name: names[i] | ||
}); | ||
error = new Error(msg); | ||
throw(error); | ||
} | ||
if (!isLength) | ||
for (i = 0; i < n; i++) | ||
if (!argsParam[i]) { | ||
msg = Util.render(template, { | ||
name: names[i] | ||
}); | ||
error = new Error(msg); | ||
throw(error); | ||
} | ||
@@ -161,3 +96,3 @@ return this; | ||
this.checkType = function(name, arg, type) { | ||
var is = Util.getType(arg) === type; | ||
var is = Util.type(arg) === type; | ||
@@ -191,23 +126,34 @@ if (!is) | ||
/** | ||
* copy pObj properties to pTargetObject | ||
* copy objFrom properties to target | ||
* | ||
* @pTarget | ||
* @pObj | ||
* @target | ||
* @objFrom | ||
*/ | ||
this.extend = function(pTarget, PObj) { | ||
var i, n, lObj, | ||
isFunc = Util.isFunction(PObj), | ||
isArray = Util.isArray(PObj), | ||
isObj = Util.isObject(pTarget), | ||
ret = isObj ? pTarget : {}; | ||
this.extend = function(target, objFrom) { | ||
var obj, | ||
keys, | ||
proto, | ||
isFunc = Util.type.function(objFrom), | ||
isArray = Util.type.array(objFrom), | ||
isObj = Util.type.object(target), | ||
ret = isObj ? target : {}; | ||
if (isArray) | ||
for (i = 0, n = PObj.length; i < n; i++) | ||
ret = Util.extend(pTarget, PObj[i]); | ||
objFrom.forEach(function(item) { | ||
ret = Util.extend(target, item); | ||
}); | ||
else if (PObj) { | ||
lObj = isFunc ? new PObj() : PObj; | ||
else if (objFrom) { | ||
obj = isFunc ? new objFrom() : objFrom; | ||
keys = Object.keys(obj); | ||
for(i in lObj) | ||
ret[i] = lObj[i]; | ||
if (!keys.length) { | ||
proto = Object.getPrototypeOf(objFrom); | ||
keys = Object.keys(proto); | ||
} | ||
keys.forEach(function(name) { | ||
ret[name] = obj[name]; | ||
}); | ||
} | ||
@@ -231,28 +177,31 @@ | ||
/** | ||
* @param pJSON | ||
*/ | ||
this.parseJSON = function(str) { | ||
var obj; | ||
Util.exec.try(function() { | ||
obj = JSON.parse(str); | ||
}); | ||
return obj; | ||
}; | ||
this.json = new JsonProto(); | ||
/** | ||
* @param pObj | ||
*/ | ||
this.stringifyJSON = function(obj) { | ||
var str; | ||
function JsonProto() { | ||
/** | ||
* @param str | ||
*/ | ||
this.parse = function(str) { | ||
var obj; | ||
Util.exec.try(function() { | ||
obj = JSON.parse(str); | ||
}); | ||
return obj; | ||
}, | ||
Util.exec.tryLog(function() { | ||
str = JSON.stringify(obj, null, 4); | ||
}); | ||
return str; | ||
}; | ||
/** | ||
* @param obj | ||
*/ | ||
this.stringify = function(obj) { | ||
var str; | ||
Util.exec.tryLog(function() { | ||
str = JSON.stringify(obj, null, 4); | ||
}); | ||
return str; | ||
}; | ||
} | ||
/** | ||
@@ -265,3 +214,3 @@ * function check is strings are equal | ||
var isEqual, | ||
type = Util.getType(str2); | ||
type = Util.type(str2); | ||
@@ -287,10 +236,8 @@ switch(type) { | ||
this.getStrBigFirst = function(pStr) { | ||
var ret; | ||
this.getStrBigFirst = function(str) { | ||
var isStr = Util.type.string(str), | ||
ret = str; | ||
if (Util.isString(pStr) && pStr.length > 0) | ||
ret = pStr[0].toUpperCase() + | ||
pStr.substring(1); | ||
else | ||
ret = pStr; | ||
if (isStr && str.length) | ||
ret = str[0].toUpperCase() + str.substring(1); | ||
@@ -308,4 +255,4 @@ return ret; | ||
var i, n, str, is, index, | ||
isStr = Util.isString(str1), | ||
type = Util.getType(str2); | ||
isStr = Util.type.string(str1), | ||
type = Util.type(str2); | ||
@@ -318,4 +265,4 @@ if (isStr) | ||
for (i = 0; i < n; i++) { | ||
str = str2[i]; | ||
is = Util.isContainStr(str1, str); | ||
str = str2[i]; | ||
is = Util.isContainStr(str1, str); | ||
@@ -342,6 +289,8 @@ if (is) | ||
this.isContainStrAtBegin = function(pStr1, pStr2) { | ||
var i, n, length, subStr, ret; | ||
var i, n, length, subStr, ret, | ||
isStr1 = Util.type.string(pStr1), | ||
isArr2 = Util.type.array(pStr2); | ||
if (Util.isString(pStr1)) | ||
if (Util.isArray(pStr2)) { | ||
if (isStr1) | ||
if (isArr2) { | ||
n = pStr2.length; | ||
@@ -390,3 +339,3 @@ | ||
this.logArray = function(array) { | ||
var isArray = Util.isArray(array); | ||
var isArray = Util.type.array(array); | ||
@@ -428,4 +377,4 @@ if (isArray) | ||
strArray = [], | ||
isString = Util.isString(str), | ||
isArray = Util.isArray(substr), | ||
isString = Util.type.string(str), | ||
isArray = Util.type.array(substr), | ||
replaceStr = function(str, strItem) { | ||
@@ -475,3 +424,3 @@ var ret = str.replace(strItem, ''); | ||
var regExp, | ||
isStr = Util.isString(str); | ||
isStr = Util.type.string(str); | ||
@@ -505,3 +454,3 @@ if (isStr && from) { | ||
this.escapeRegExp = function(str) { | ||
var isStr = Util.isString(str); | ||
var isStr = Util.type.string(str); | ||
@@ -578,109 +527,110 @@ if (isStr) | ||
/** | ||
* functions check is variable is array | ||
* @param variable | ||
*/ | ||
this.isArray = function(variable) { | ||
return Array.isArray(variable); | ||
}; | ||
this.type = new TypeProto(); | ||
/** | ||
* functions check is variable is arrayBuffer | ||
* @param variable | ||
*/ | ||
this.isArrayBuffer = function(variable) { | ||
var type = this.getType(variable), | ||
is = type === 'arraybuffer'; | ||
function TypeProto() { | ||
/** | ||
* get type of variable | ||
* | ||
* @param variable | ||
*/ | ||
function type(variable) { | ||
var regExp = new RegExp('\\s([a-zA-Z]+)'), | ||
str = {}.toString.call(variable), | ||
typeBig = str.match(regExp)[1], | ||
result = typeBig.toLowerCase(); | ||
return result; | ||
} | ||
return is; | ||
}; | ||
/** | ||
* functions check is variable is boolean | ||
* @param variable | ||
*/ | ||
this.isBoolean = function(variable) { | ||
return Util.isType(variable, 'boolean'); | ||
}; | ||
/** | ||
* functions check is variable is function | ||
* @param variable | ||
*/ | ||
this.isFunction = function(variable) { | ||
return Util.isType(variable, 'function'); | ||
}; | ||
/** | ||
* functions check is variable is number | ||
* @param variable | ||
*/ | ||
this.isNumber = function(variable) { | ||
return Util.isType(variable, 'number'); | ||
}; | ||
/** | ||
* functions check is variable is object | ||
* @param variable | ||
*/ | ||
this.isObject = function(variable) { | ||
var type = Util.getType(variable), | ||
is = type === 'object'; | ||
/** | ||
* functions check is variable is array | ||
* @param variable | ||
*/ | ||
type.array = function(variable) { | ||
var result; | ||
if (Array.isArray) | ||
result = Array.isArray(variable) ; | ||
else | ||
result = type(variable) === 'array'; | ||
return result; | ||
}; | ||
return is; | ||
}; | ||
/** | ||
* functions check is variable is string | ||
* @param variable | ||
*/ | ||
this.isString = function(variable) { | ||
return Util.isType(variable, 'string'); | ||
}; | ||
/** | ||
* functions check is variable is string | ||
* @param variable | ||
*/ | ||
this.isUndefined = function(variable) { | ||
return Util.isType(variable, 'undefined'); | ||
}; | ||
/** | ||
* functions check is variable is File | ||
* @param variable | ||
*/ | ||
this.isFile = function(variable) { | ||
var FILE = '[object File]', | ||
name, is; | ||
/** | ||
* functions check is variable is arrayBuffer | ||
* @param variable | ||
*/ | ||
type.arrayBuffer = function(variable) { | ||
return type(variable) === 'arraybuffer'; | ||
}; | ||
name = Util.exec.ifExist(variable, 'toString'); | ||
/** | ||
* functions check is variable is boolean | ||
* @param variable | ||
*/ | ||
type.boolean = function(variable) { | ||
return typeof variable === 'boolean'; | ||
}; | ||
is = name === FILE; | ||
/** | ||
* functions check is variable is function | ||
* @param variable | ||
*/ | ||
type.function = function(variable) { | ||
return typeof variable === 'function'; | ||
}; | ||
return is; | ||
}; | ||
/** | ||
* functions check is variable is type | ||
* @param variable | ||
* @param type | ||
*/ | ||
this.isType = function(variable, type) { | ||
return typeof variable === type; | ||
}; | ||
/** | ||
* get type of variable | ||
* | ||
* @param variable | ||
*/ | ||
this.getType = function(variable) { | ||
var regExp = new RegExp('\\s([a-zA-Z]+)'), | ||
str = {}.toString.call(variable), | ||
typeBig = str.match(regExp)[1], | ||
type = typeBig.toLowerCase(); | ||
/** | ||
* functions check is variable is number | ||
* @param variable | ||
*/ | ||
type.number = function(variable) { | ||
return typeof variable === 'number'; | ||
}; | ||
/** | ||
* functions check is variable is object | ||
* @param variable | ||
*/ | ||
type.object = function(variable) { | ||
var type = Util.type(variable), | ||
is = type === 'object'; | ||
return is; | ||
}; | ||
/** | ||
* functions check is variable is string | ||
* @param variable | ||
*/ | ||
type.string = function(variable) { | ||
return typeof variable === 'string'; | ||
}; | ||
/** | ||
* functions check is variable is string | ||
* @param variable | ||
*/ | ||
type.undefined = function(variable) { | ||
return typeof variable === 'undefined'; | ||
}; | ||
/** | ||
* functions check is variable is File | ||
* @param variable | ||
*/ | ||
type.file = function(variable) { | ||
var FILE = '[object File]', | ||
name, is; | ||
name = Util.exec.ifExist(variable, 'toString'); | ||
is = name === FILE; | ||
return is; | ||
}; | ||
return type; | ||
}; | ||
} | ||
@@ -697,9 +647,2 @@ /** | ||
/** | ||
* function return param | ||
*/ | ||
this.retParam = function(pParam) { | ||
return pParam; | ||
}; | ||
/** | ||
* function makes new array based on first | ||
@@ -744,3 +687,3 @@ * | ||
var ret, | ||
isFunc = Util.isFunction(callback), | ||
isFunc = Util.type.function(callback), | ||
args = Util.slice(arguments, 1); | ||
@@ -825,3 +768,3 @@ | ||
countFuncs = 0, | ||
type = Util.getType(funcs); | ||
type = Util.type(funcs); | ||
@@ -888,3 +831,3 @@ Util.checkArgs(arguments, ['funcs', 'callback']); | ||
var func, callback, | ||
isArray = Util.isArray(funcs); | ||
isArray = Util.type.array(funcs); | ||
@@ -944,3 +887,3 @@ if (isArray) { | ||
dot, | ||
isStr = Util.isString(name); | ||
isStr = Util.type.string(name); | ||
@@ -964,3 +907,3 @@ if (isStr) { | ||
var ret = [], | ||
isArray = Util.isArray(arr); | ||
isArray = Util.type.array(arr); | ||
@@ -986,3 +929,3 @@ if (arr && !isArray) | ||
var ret, | ||
isArray = Util.isArray(array); | ||
isArray = Util.type.array(array); | ||
@@ -992,3 +935,3 @@ if (isArray) { | ||
var is = item.name === name, | ||
isArray = Util.isArray(item); | ||
isArray = Util.type.array(item); | ||
@@ -995,0 +938,0 @@ if (is) |
{ | ||
"name": "util-io", | ||
"version": "1.3.0", | ||
"version": "1.4.0", | ||
"author": "coderaiser <mnemonic.enemy@gmail.com> (https://github.com/coderaiser)", | ||
@@ -5,0 +5,0 @@ "description": "Util.io - utilites for vanila js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
35048
797