From 44a6ebab9a32219987ee7e0dcfc59182643a7b56 Mon Sep 17 00:00:00 2001 From: coderaiser Date: Mon, 29 Sep 2014 07:11:07 -0400 Subject: [PATCH] feature(util) Util.is* -> Util.type.* --- lib/client.js | 11 +- lib/client/config.js | 2 +- lib/client/dom.js | 10 +- lib/client/edit.js | 8 +- lib/client/events.js | 7 +- lib/client/files.js | 2 +- lib/client/load.js | 8 +- lib/client/polyfill.js | 21 ++-- lib/client/rest.js | 4 +- lib/client/storage.js | 2 +- lib/client/view.js | 2 +- lib/client/zip.js | 2 +- lib/cloudfunc.js | 2 +- lib/server/express.js | 2 +- lib/server/files.js | 2 +- lib/server/join.js | 4 +- lib/server/pack.js | 4 +- lib/server/pipe.js | 7 +- lib/server/ponse.js | 4 +- lib/server/rest.js | 4 +- lib/util.js | 253 +++++++++++++++++++++-------------------- 21 files changed, 184 insertions(+), 177 deletions(-) diff --git a/lib/client.js b/lib/client.js index 2e5ec7b7..a4d56ddb 100644 --- a/lib/client.js +++ b/lib/client.js @@ -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() { diff --git a/lib/client/config.js b/lib/client/config.js index cc338e14..61983a60 100644 --- a/lib/client/config.js +++ b/lib/client/config.js @@ -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); diff --git a/lib/client/dom.js b/lib/client/dom.js index aedae479..87f2470b 100644 --- a/lib/client/dom.js +++ b/lib/client/dom.js @@ -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; diff --git a/lib/client/edit.js b/lib/client/edit.js index 7897ba74..461fe39d 100644 --- a/lib/client/edit.js +++ b/lib/client/edit.js @@ -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; diff --git a/lib/client/events.js b/lib/client/events.js index 463886b4..27624c62 100644 --- a/lib/client/events.js +++ b/lib/client/events.js @@ -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); diff --git a/lib/client/files.js b/lib/client/files.js index 17de865a..c65e6fa3 100644 --- a/lib/client/files.js +++ b/lib/client/files.js @@ -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']); diff --git a/lib/client/load.js b/lib/client/load.js index a77115e5..ac197358 100644 --- a/lib/client/load.js +++ b/lib/client/load.js @@ -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(); diff --git a/lib/client/polyfill.js b/lib/client/polyfill.js index c2ce2794..f6a14af7 100644 --- a/lib/client/polyfill.js +++ b/lib/client/polyfill.js @@ -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 + '"'; diff --git a/lib/client/rest.js b/lib/client/rest.js index 3775d3ad..0fd53934 100644 --- a/lib/client/rest.js +++ b/lib/client/rest.js @@ -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; diff --git a/lib/client/storage.js b/lib/client/storage.js index 08af026e..b3ca9a17 100644 --- a/lib/client/storage.js +++ b/lib/client/storage.js @@ -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) diff --git a/lib/client/view.js b/lib/client/view.js index a1da444b..3ae785a0 100644 --- a/lib/client/view.js +++ b/lib/client/view.js @@ -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]; diff --git a/lib/client/zip.js b/lib/client/zip.js index 10f0548e..da45ce7b 100644 --- a/lib/client/zip.js +++ b/lib/client/zip.js @@ -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; diff --git a/lib/cloudfunc.js b/lib/cloudfunc.js index dee3eb5a..bd4dc768 100644 --- a/lib/cloudfunc.js +++ b/lib/cloudfunc.js @@ -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; diff --git a/lib/server/express.js b/lib/server/express.js index 8a1bb0d9..593b7dfd 100644 --- a/lib/server/express.js +++ b/lib/server/express.js @@ -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) diff --git a/lib/server/files.js b/lib/server/files.js index a585a0e0..026f50a6 100644 --- a/lib/server/files.js +++ b/lib/server/files.js @@ -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(); diff --git a/lib/server/join.js b/lib/server/join.js index 3cc6ff90..1feabae4 100644 --- a/lib/server/join.js +++ b/lib/server/join.js @@ -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) { diff --git a/lib/server/pack.js b/lib/server/pack.js index 1fc4242f..3225ba40 100644 --- a/lib/server/pack.js +++ b/lib/server/pack.js @@ -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'), diff --git a/lib/server/pipe.js b/lib/server/pipe.js index b6730a98..67b641e2 100644 --- a/lib/server/pipe.js +++ b/lib/server/pipe.js @@ -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 diff --git a/lib/server/ponse.js b/lib/server/ponse.js index df536097..d31b960a 100644 --- a/lib/server/ponse.js +++ b/lib/server/ponse.js @@ -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; diff --git a/lib/server/rest.js b/lib/server/rest.js index c05280d0..86b7f135 100644 --- a/lib/server/rest.js +++ b/lib/server/rest.js @@ -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); diff --git a/lib/util.js b/lib/util.js index 903f7d5b..242144fa 100644 --- a/lib/util.js +++ b/lib/util.js @@ -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;