feature(util) Util.is* -> Util.type.*

This commit is contained in:
coderaiser 2014-09-29 07:11:07 -04:00
parent a4daff756f
commit 44a6ebab9a
21 changed files with 184 additions and 177 deletions

View file

@ -12,7 +12,8 @@ var Util, DOM, CloudFunc, join;
Images = DOM.Images,
Info = DOM.CurrentInfo,
CloudCmd = this,
Storage = DOM.Storage;
Storage = DOM.Storage,
type = Util.type;
this.PREFIX = '',
this.MousePosition = {};
@ -124,7 +125,7 @@ var Util, DOM, CloudFunc, join;
function(error) {
var Proto = CloudCmd[name];
if (!error && Util.isFunction(Proto))
if (!error && type.function(Proto))
CloudCmd[name] = Util.applyConstructor(Proto, args);
});
};
@ -235,7 +236,9 @@ var Util, DOM, CloudFunc, join;
modules = [];
modules.forEach(function(module) {
if (Util.isString(module))
var isStr = type.string(module);
if (isStr)
load(null, module, doBefore[module]);
});
@ -288,7 +291,7 @@ var Util, DOM, CloudFunc, join;
this.execFromModule = function(moduleName, funcName, params) {
var obj = CloudCmd[moduleName],
isObj = Util.isObject(obj);
isObj = Util.type.object(obj);
Util.exec.if(isObj,
function() {

View file

@ -99,7 +99,7 @@ var CloudCmd, Util, DOM;
function changeConfig(config) {
Object.keys(config).forEach(function(name) {
var item = config[name],
isBool = Util.isBoolean(item);
isBool = Util.type.boolean(item);
if (isBool)
config[name] = setState(item);

View file

@ -115,7 +115,7 @@ var CloudCmd, Util, DOM, CloudFunc, Dialog;
* show error image (usualy after error on ajax request)
*/
this.showError = function(jqXHR, isQuiet) {
var isStr = Util.isString(jqXHR),
var isStr = Util.type.string(jqXHR),
image = Images.error(),
response = '',
statusText = '',
@ -345,8 +345,8 @@ var CloudCmd, Util, DOM, CloudFunc, Dialog;
remoteObj = Util.findObjByNameInArr(modules, 'remote'),
module = Util.findObjByNameInArr(remoteObj, name),
isArray = Util.isArray(module.local),
version = module.version,
isArray = Util.type.array(module.local),
version = module.version,
funcON = function() {
load.parallel(remote, function(error) {
@ -442,7 +442,7 @@ var CloudCmd, Util, DOM, CloudFunc, Dialog;
lName = Cmd.getCurrentName(),
lDir = Cmd.getCurrentDirPath(),
lMsg = 'New ' + typeName || 'File',
lType = Util.isString(type) ? type : '';
lType = Util.type.string(type) ? type : '';
if (lName === '..')
lName = '';
@ -829,7 +829,7 @@ var CloudCmd, Util, DOM, CloudFunc, Dialog;
var length,
ONE_MEGABYTE = 1024 * 1024 * 1024;
if (Util.isObject(data))
if (Util.type.object(data))
data = Util.json.stringify(data);
length = data.length;

View file

@ -11,7 +11,7 @@ var CloudCmd, Util, DOM, CloudFunc, ace, DiffProto, diff_match_patch, Zip, MenuI
LIBDIR = CloudCmd.LIBDIR,
Info = DOM.CurrentInfo,
Files = DOM.Files,
type = Util.type,
join = CloudCmd.join,
Menu,
@ -52,7 +52,7 @@ var CloudCmd, Util, DOM, CloudFunc, ace, DiffProto, diff_match_patch, Zip, MenuI
this.show = function(value) {
var mode, htmlMode, jsMode, isHTML, isJS, modesByName,
isStr = Util.isString(value),
isStr = type.string(value),
name = Info.name,
isDir = Info.isDir;
@ -178,7 +178,7 @@ var CloudCmd, Util, DOM, CloudFunc, ace, DiffProto, diff_match_patch, Zip, MenuI
function setUseOfWorker(mode) {
var isMatch,
isStr = Util.isString(mode),
isStr = type.string(mode),
regStr = 'coffee|css|html|javascript|json|lua|php|xquery',
regExp = new RegExp(regStr);
@ -245,7 +245,7 @@ var CloudCmd, Util, DOM, CloudFunc, ace, DiffProto, diff_match_patch, Zip, MenuI
length = Value.length,
isLessMaxLength = length < MAX_SIZE,
isLessLength = isLessMaxLength && patchLength < length,
isStr = Util.isString(patch);
isStr = type.string(patch);
Value = value;

View file

@ -9,11 +9,12 @@ var Util, DOM;
function EventsProto() {
var Events = this,
parseArgs = function(eventName, element, listener, callback) {
var isFunc, isElement, error,
EVENT_NAME = 0,
ELEMENT = 1,
type = Util.getType(eventName);
type = Util.type(eventName);
switch(type) {
default:
@ -36,7 +37,7 @@ var Util, DOM;
break;
case 'string':
isFunc = Util.isFunction(element);
isFunc = Util.type.function(element);
if (isFunc) {
listener = element;
@ -272,7 +273,7 @@ var Util, DOM;
* @param pEvent
*/
this.dispatch = function(event, element) {
var isStr = Util.isString(event);
var isStr = Util.type.string(event);
if (isStr)
event = Events.create(event);

View file

@ -23,7 +23,7 @@ var Util, DOM, CloudCmd;
DIR_JSON = '/json/';
this.get = function(name, callback) {
var type = Util.getType(name);
var type = Util.type(name);
Util.checkArgs(arguments, ['name', 'callback']);

View file

@ -98,7 +98,7 @@ var Util, DOM;
}
if (p.attribute) {
type = Util.getType(p.attribute);
type = Util.type(p.attribute);
switch(type) {
case 'string':
@ -137,7 +137,7 @@ var Util, DOM;
*/
load.getIdBySrc = function(src) {
var num, sub, id,
isStr = Util.isString(src);
isStr = Util.type.string(src);
if (isStr) {
if (~src.indexOf(':'))
@ -163,8 +163,8 @@ var Util, DOM;
load.ajax = function(params) {
var data,
p = params, countProgress,
isObject = Util.isObject(p.data),
isArray = Util.isArray(p.data),
isObject = Util.type.object(p.data),
isArray = Util.type.array(p.data),
type = p.type || p.method || 'GET',
xhr = new XMLHttpRequest();

View file

@ -3,6 +3,8 @@ var Util, DOM, jQuery;
(function(window, document, Util, DOM, $) {
'use strict';
var type = Util.type;
if (!window.XMLHttpRequest || !document.head)
DOM.load.ajax = $.ajax;
@ -43,16 +45,13 @@ var Util, DOM, jQuery;
NOP.prototype = this.prototype;
fBound.prototype = new NOP();
return fBound;
};
if (!Array.isArray)
Array.isArray = function(arr) {
var type = Util.getType(arr),
is = type === 'array';
return is;
return type(arr) === 'array';
};
/*
@ -60,7 +59,7 @@ var Util, DOM, jQuery;
* as older browsers may report objects to be a function,
* which they are not
*/
Util.isFunction = $.isFunction;
Util.type.function = $.isFunction;
if (!document.addEventListener)
/**
@ -218,9 +217,9 @@ var Util, DOM, jQuery;
ret = '',
value = '',
json = [],
isStr = Util.isString(obj),
isObj = Util.isObject(obj),
isArray = Util.isArray(obj);
isStr = type.string(obj),
isObj = type.object(obj),
isArray = type.array(obj);
if (!isObj || obj === null) {
// simple data type
@ -235,8 +234,8 @@ var Util, DOM, jQuery;
has = obj.hasOwnProperty(n);
if (has) {
isStr = Util.isString(v);
isObj = Util.isObject(v);
isStr = type.string(v);
isObj = type.object(v);
if (isStr)
v = '"' + v + '"';

View file

@ -14,7 +14,7 @@ var Util, DOM, CloudFunc, CloudCmd;
var Images = DOM.Images;
this.delete = function(url, data, callback) {
var isFunc = Util.isFunction(data);
var isFunc = Util.type.function(data);
if (!callback && isFunc) {
callback = data;
@ -42,7 +42,7 @@ var Util, DOM, CloudFunc, CloudCmd;
this.read = function(url, dataType, callback) {
var isQuery = Util.isContainStr(url, '?'),
isFunc = Util.isFunction(dataType);
isFunc = Util.type.function(dataType);
if (!callback && isFunc) {
callback = dataType;

View file

@ -63,7 +63,7 @@ var Util, DOM;
this.set = function(name, data, callback) {
var str, ret = Allowed && name;
if (Util.isObject(data))
if (Util.type.object(data))
str = Util.json.stringify(data);
if (Allowed && name)

View file

@ -109,7 +109,7 @@ var CloudCmd, Util, DOM, CloudFunc, $;
isConfig = !!config[name],
series = Util.exec.series,
item = options[name],
isFunc = Util.isFunction(item);
isFunc = Util.type.function(item);
if (isFunc && isConfig) {
func = config[name];

View file

@ -10,7 +10,7 @@ var Util, Zip, pako;
function ZipProto() {
this.pack = function(str, callback) {
var buf, deflate, result,
isArrayBuffer = Util.isArrayBuffer(str);
isArrayBuffer = Util.type.arrayBuffer(str);
if (isArrayBuffer)
buf = str;

View file

@ -79,7 +79,7 @@ var Util;
var length, lastSlash, isStr, isEqual;
if (path) {
isStr = Util.isString(path);
isStr = Util.type.string(path);
length = path.length - 1;
lastSlash = path.lastIndexOf('/');
isEqual = lastSlash === length;

View file

@ -14,7 +14,7 @@
app = express && express();
exports.getApp = function(middleware) {
var isArray = Util.isArray(middleware);
var isArray = Util.type.array(middleware);
if (app) {
if (logger)

View file

@ -8,7 +8,7 @@
object.read = function(files, options, callback) {
var done = [],
isDone = false,
noOptions = Util.isFunction(options),
noOptions = Util.type.function(options),
readFiles = {},
doneFunc = function (name, error, data) {
done.pop();

View file

@ -37,7 +37,7 @@
if (!options)
options = {};
isFunc = Util.isFunction(options.minify);
isFunc = Util.type.function(options.minify);
if (isFunc)
isMinify = options.minify();
@ -56,7 +56,7 @@
names = parse(prefix, dir, path);
exec.if(!isMinify, function(error, namesObj) {
var is = Util.isObject(namesObj);
var is = Util.type.object(namesObj);
if (is)
names = names.map(function(key) {

View file

@ -26,7 +26,7 @@
else
createTar(from, function(readStream) {
var dir, name,
isStr = Util.isString(to);
isStr = Util.type.string(to);
if (isStr) {
dir = path.dirname(to),
@ -41,7 +41,7 @@
exports.gunzip = function(from, to, callback) {
var write,
isStr = Util.isString(from),
isStr = Util.type.string(from),
check = Util.checkExt,
isTarGz = isStr && check(from, 'tar.gz'),

View file

@ -2,6 +2,7 @@
'use strict';
var Util = require('../util'),
type = Util.type,
fs = require('fs'),
zlib = require('zlib');
@ -24,9 +25,9 @@
*/
function create(read, write, options, callback) {
var gzip, isFsWrite, func,
isStrRead = Util.isString(read),
isStrWrite = Util.isString(write),
isFunc = Util.isFunction(options),
isStrRead = type.string(read),
isStrWrite = type.string(write),
isFunc = type.function(options),
o = {},
optionsRead = {
bufferSize: 4 * 1024

View file

@ -177,7 +177,7 @@
}
function fillHeader(header, response) {
var isObject = Util.isObject(header),
var isObject = Util.type.object(header),
isSent = response.headersSent;
if (!isSent && isObject)
@ -337,7 +337,7 @@
name = path.join(dir, name);
if (Util.isFunction(o.cache))
if (Util.type.function(o.cache))
cache = o.cache();
else if (o.cache !== undefined)
cache = o.cache;

View file

@ -152,7 +152,7 @@
var str,
options = {},
isFile = error && error.code === 'ENOTDIR',
isStr = Util.isString(data),
isStr = Util.type.string(data),
params = {
gzip: true,
name: path,
@ -458,7 +458,7 @@
function formatMsg(msgParam, dataParam, status) {
var msg, data,
isObj = Util.isObject(dataParam);
isObj = Util.type.object(dataParam);
if (isObj)
data = Util.json.stringify(dataParam);

View file

@ -34,7 +34,7 @@
*/
this.checkExt = function(name, ext) {
var isMatch, str,
type = Util.getType(ext),
type = Util.type(ext),
regStr = '\\.({{ exts }})$',
regExp;
@ -89,7 +89,7 @@
* @param type
*/
this.checkType = function(name, arg, type) {
var is = Util.getType(arg) === type;
var is = Util.type(arg) === type;
if (!is)
throw(Error(name + ' should be ' + type));
@ -127,9 +127,9 @@
var obj,
keys,
proto,
isFunc = Util.isFunction(objFrom),
isArray = Util.isArray(objFrom),
isObj = Util.isObject(target),
isFunc = Util.type.function(objFrom),
isArray = Util.type.array(objFrom),
isObj = Util.type.object(target),
ret = isObj ? target : {};
if (isArray)
@ -204,7 +204,7 @@
*/
this.strCmp = function(str1, str2) {
var isEqual,
type = Util.getType(str2);
type = Util.type(str2);
switch(type) {
case 'array':
@ -226,7 +226,7 @@
};
this.getStrBigFirst = function(str) {
var isStr = Util.isString(str),
var isStr = Util.type.string(str),
ret = str;
if (isStr && str.length)
@ -243,8 +243,8 @@
this.isContainStr = function(str1, str2) {
var i, n, str, is, index,
isStr = Util.isString(str1),
type = Util.getType(str2);
isStr = Util.type.string(str1),
type = Util.type(str2);
if (isStr)
switch (type) {
@ -252,8 +252,8 @@
n = str2.length;
for (i = 0; i < n; i++) {
str = str2[i];
is = Util.isContainStr(str1, str);
str = str2[i];
is = Util.isContainStr(str1, str);
if (is)
break;
@ -275,10 +275,12 @@
* @param pStr2
*/
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;
for(i = 0; i < n; i++) {
@ -322,7 +324,7 @@
* @param array
*/
this.logArray = function(array) {
var isArray = Util.isArray(array);
var isArray = Util.type.array(array);
if (isArray)
array.forEach(function(item) {
@ -359,8 +361,8 @@
this.rmStr = function(str, substr, isOnce) {
var replace,
strArray = [],
isString = Util.isString(str),
isArray = Util.isArray(substr),
isString = Util.type.string(str),
isArray = Util.type.array(substr),
replaceStr = function(str, strItem) {
var ret = str.replace(strItem, '');
@ -405,7 +407,7 @@
*/
this.replaceStr = function(str, from, to, notEscape) {
var regExp,
isStr = Util.isString(str);
isStr = Util.type.string(str);
if (isStr && from) {
if (!notEscape)
@ -434,7 +436,7 @@
};
this.escapeRegExp = function(str) {
var isStr = Util.isString(str);
var isStr = Util.type.string(str);
if (isStr)
str = str.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&');
@ -506,113 +508,114 @@
return ret;
};
/**
* 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;
};
}
/**
* function return false
@ -663,7 +666,7 @@
*/
var exec = function(callback) {
var ret,
isFunc = Util.isFunction(callback),
isFunc = Util.type.function(callback),
args = Util.slice(arguments, 1);
if (isFunc)
@ -743,7 +746,7 @@
obj = {},
count = 0,
countFuncs = 0,
type = Util.getType(funcs);
type = Util.type(funcs);
Util.checkArgs(arguments, ['funcs', 'callback']);
@ -805,7 +808,7 @@
*/
exec.series = function(funcs) {
var func, callback,
isArray = Util.isArray(funcs);
isArray = Util.type.array(funcs);
if (isArray) {
func = funcs.shift();
@ -860,7 +863,7 @@
this.getExt = function(name) {
var ret = '',
dot,
isStr = Util.isString(name);
isStr = Util.type.string(name);
if (isStr) {
dot = name.lastIndexOf('.');
@ -879,7 +882,7 @@
*/
this.getNamesFromObjArray = function(arr) {
var ret = [],
isArray = Util.isArray(arr);
isArray = Util.type.array(arr);
if (arr && !isArray)
arr = arr.data;
@ -900,12 +903,12 @@
*/
this.findObjByNameInArr = function(array, name) {
var ret,
isArray = Util.isArray(array);
isArray = Util.type.array(array);
if (isArray) {
array.some(function(item) {
var is = item.name === name,
isArray = Util.isArray(item);
isArray = Util.type.array(item);
if (is)
ret = item;