From dc3ecd9f5688b9e31b6dfde8040a332125d477d4 Mon Sep 17 00:00:00 2001 From: coderaiser Date: Mon, 3 Nov 2014 04:06:36 -0500 Subject: [PATCH] feature(util) checkArgs -> check --- lib/client.js | 2 +- lib/client/dom.js | 6 +-- lib/client/edit.js | 4 +- lib/client/events.js | 4 +- lib/client/files.js | 4 +- lib/client/view.js | 2 +- lib/cloudcmd.js | 2 +- lib/cloudfunc.js | 2 +- lib/server/config.js | 2 +- lib/server/files.js | 4 +- lib/server/flop.js | 10 ++--- lib/server/ischanged.js | 2 +- lib/server/mellow.js | 2 +- lib/server/patch.js | 2 +- lib/server/pipe.js | 4 +- lib/server/ponse.js | 10 ++--- lib/server/readify.js | 10 ++--- lib/server/rest.js | 6 +-- lib/server/rest/fs/get.js | 2 +- lib/server/rest/fs/patch.js | 2 +- lib/server/rest/fs/put.js | 2 +- lib/server/route.js | 4 +- lib/server/size.js | 2 +- lib/server/terminal.js | 2 +- lib/server/timem.js | 2 +- lib/server/win.js | 6 +-- lib/util.js | 85 +++++++++++++++++++++---------------- 27 files changed, 99 insertions(+), 86 deletions(-) diff --git a/lib/client.js b/lib/client.js index 2c953954..c9da7ff8 100644 --- a/lib/client.js +++ b/lib/client.js @@ -150,7 +150,7 @@ var Util, DOM, CloudFunc, join; this.join = function(urls) { var prefix = CloudCmd.PREFIX; - Util.checkArgs(arguments, ['urls']); + Util.check(arguments, ['urls']); urls = urls.map(function(url) { return url.replace(prefix, ''); diff --git a/lib/client/dom.js b/lib/client/dom.js index d5fe94a7..84ce7ba5 100644 --- a/lib/client/dom.js +++ b/lib/client/dom.js @@ -425,7 +425,7 @@ var CloudCmd, Util, DOM, CloudFunc, Dialog; DOM.RESTful.write(path, file, callback); }; - Util.checkArgs(arguments, ['files']); + Util.check(arguments, ['files']); if (files.length) { func = func(files[0].name); @@ -495,7 +495,7 @@ var CloudCmd, Util, DOM, CloudFunc, Dialog; from : path }; - Util.checkArgs(arguments, ['operation']); + Util.check(arguments, ['operation']); if (operation.pack) { op = RESTful.pack; @@ -1260,7 +1260,7 @@ var CloudCmd, Util, DOM, CloudFunc, Dialog; nameHash = name + '-hash', getStoreHash = Util.exec.with(Storage.get, nameHash); - Util.checkArgs(arguments, ['name', 'callback']); + Util.check(arguments, ['name', 'callback']); parallel([loadHash, getStoreHash], function(error, loadHash, storeHash) { var equal, diff --git a/lib/client/edit.js b/lib/client/edit.js index 10210230..f4931cdc 100644 --- a/lib/client/edit.js +++ b/lib/client/edit.js @@ -114,7 +114,7 @@ var CloudCmd, Util, DOM, CloudFunc, ace, DiffProto, diff_match_patch, Zip, MenuI var name = Info.name, isDir = Info.isDir; - Util.checkArgs(arguments, ['callback']); + Util.check(arguments, ['callback']); if (isDir) Edit.setMode('json'); @@ -526,7 +526,7 @@ var CloudCmd, Util, DOM, CloudFunc, ace, DiffProto, diff_match_patch, Zip, MenuI function readWithFlag(flag) { var path = Info.path; - Util.checkArgs(arguments, ['flag']); + Util.check(arguments, ['flag']); RESTful.read(path + '?' + flag, function(data) { Ace.setValue(data); diff --git a/lib/client/events.js b/lib/client/events.js index 27624c62..aa64ecd3 100644 --- a/lib/client/events.js +++ b/lib/client/events.js @@ -89,7 +89,7 @@ var Util, DOM; * @param listener */ this.add = function(type, element, listener) { - Util.checkArgs(arguments, ['type']); + Util.check(arguments, ['type']); parseArgs(type, element, listener, function(element, args) { element.addEventListener.apply(element, args); @@ -124,7 +124,7 @@ var Util, DOM; * @param element {document by default} */ this.remove = function(type, element, listener) { - Util.checkArgs(arguments, ['type']); + Util.check(arguments, ['type']); parseArgs(type, element, listener, function(element, args) { element.removeEventListener.apply(element, args); diff --git a/lib/client/files.js b/lib/client/files.js index 3f5da0c5..5d9565c6 100644 --- a/lib/client/files.js +++ b/lib/client/files.js @@ -25,7 +25,7 @@ var Util, DOM, CloudCmd; this.get = function(name, callback) { var type = Util.type(name); - Util.checkArgs(arguments, ['name', 'callback']); + Util.check(arguments, ['name', 'callback']); switch(type) { case 'string': @@ -51,7 +51,7 @@ var Util, DOM, CloudCmd; regExp = new RegExp(strFiles), isFile = regExp.test(name); - Util.checkArgs(arguments, [name, data]); + Util.check(arguments, [name, data]); if (!isFile) { showError(name); diff --git a/lib/client/view.js b/lib/client/view.js index 3ae785a0..31b95b95 100644 --- a/lib/client/view.js +++ b/lib/client/view.js @@ -231,7 +231,7 @@ var CloudCmd, Util, DOM, CloudFunc, $; } function getMediaElement(src, callback) { - Util.checkArgs(arguments, ['src', 'callback']); + Util.check(arguments, ['src', 'callback']); DOM.Files.get('view/media-tmpl', function(error, template) { var rendered, element, type, is, diff --git a/lib/cloudcmd.js b/lib/cloudcmd.js index d5c93c76..b53b2300 100644 --- a/lib/cloudcmd.js +++ b/lib/cloudcmd.js @@ -51,7 +51,7 @@ }; module.exports.listen = function(socket) { - Util.checkArgs(arguments, ['socket']); + Util.check(arguments, ['socket']); webconsole({ socket: socket diff --git a/lib/cloudfunc.js b/lib/cloudfunc.js index df7db82f..236612b4 100644 --- a/lib/cloudfunc.js +++ b/lib/cloudfunc.js @@ -86,7 +86,7 @@ var Util; pathHTML = '', path = '/'; - Util.checkArgs(arguments, ['url', 'template']); + Util.check(arguments, ['url', 'template']); namesRaw = url.split('/') .slice(1, -1), diff --git a/lib/server/config.js b/lib/server/config.js index e20ab4c7..644bb703 100644 --- a/lib/server/config.js +++ b/lib/server/config.js @@ -49,7 +49,7 @@ function save(callback) { var data = Util.json.stringify(config); - Util.checkArgs(arguments, ['callback']); + Util.check(arguments, ['callback']); if (data) fs.writeFile(ConfigHome, data, callback); diff --git a/lib/server/files.js b/lib/server/files.js index 3b5df3b6..4ba336a9 100644 --- a/lib/server/files.js +++ b/lib/server/files.js @@ -27,7 +27,7 @@ } }; - Util.checkArgs(arguments, ['files', 'callback']); + Util.check(arguments, ['files', 'callback']); if (noOptions) { callback = options; @@ -98,7 +98,7 @@ bufferSize: 4 * 1024 }; - Util.checkArgs(arguments, ['read', 'write', 'callback']); + Util.check(arguments, ['read', 'write', 'callback']); if (isFunc) callback = options; diff --git a/lib/server/flop.js b/lib/server/flop.js index 288c419f..3616f800 100644 --- a/lib/server/flop.js +++ b/lib/server/flop.js @@ -21,13 +21,13 @@ mkdir = tryRequire('mkdirp') || fs.mkdir; exports.create = function(path, type, callback) { - Util.checkArgs(arguments, ['path', 'callback']); + Util.check(arguments, ['path', 'callback']); mkdir(path, callback); }; exports.read = function(path, type, callback) { - Util.checkArgs(arguments, ['path', 'callback']); + Util.check(arguments, ['path', 'callback']); if (!callback) callback = type; @@ -56,7 +56,7 @@ }; exports.delete = function(path, callback) { - Util.checkArgs(arguments, ['path', 'callback']); + Util.check(arguments, ['path', 'callback']); if (rimraf) rimraf(path, callback); @@ -72,7 +72,7 @@ }; exports.move = function(from, to, callback) { - Util.checkArgs(arguments, ['path', 'to', 'callback']); + Util.check(arguments, ['path', 'to', 'callback']); fs.rename(from, to, function(error) { var isExDev = error && error.code === 'EXDEV'; @@ -89,7 +89,7 @@ }; exports.copy = function(from, to, callback) { - Util.checkArgs(arguments, ['from', 'to', 'callback']); + Util.check(arguments, ['from', 'to', 'callback']); fs.lstat(from, function(error, stat) { var isDir = stat && stat.isDirectory(); diff --git a/lib/server/ischanged.js b/lib/server/ischanged.js index 454a2f98..40c2cac5 100644 --- a/lib/server/ischanged.js +++ b/lib/server/ischanged.js @@ -73,7 +73,7 @@ module.exports = function(name, callback) { - Util.checkArgs(arguments, ['name', 'callback']); + Util.check(arguments, ['name', 'callback']); getTimes(function(times) { var readTime = times[name]; diff --git a/lib/server/mellow.js b/lib/server/mellow.js index aad46d08..55cc0640 100644 --- a/lib/server/mellow.js +++ b/lib/server/mellow.js @@ -44,7 +44,7 @@ function convertPath(path) { var volume; - Util.checkArgs(arguments, ['path']); + Util.check(arguments, ['path']); if (isWin && path !== '/') { volume = path[1]; diff --git a/lib/server/patch.js b/lib/server/patch.js index b9fa75e5..96b29419 100644 --- a/lib/server/patch.js +++ b/lib/server/patch.js @@ -18,7 +18,7 @@ 'bigger then '; module.exports = function(name, patch, options, callback) { - Util.checkArgs(arguments, ['name', 'patch', 'callback']); + Util.check(arguments, ['name', 'patch', 'callback']); if (!callback) { callback = options; diff --git a/lib/server/pipe.js b/lib/server/pipe.js index c8f888cd..c5098574 100644 --- a/lib/server/pipe.js +++ b/lib/server/pipe.js @@ -23,7 +23,7 @@ function all(streams, options, callback) { var n, write, isFSWrite; - Util.checkArgs(arguments, ['streams', 'callback']); + Util.check(arguments, ['streams', 'callback']); n = streams.length - 1; write = streams[n]; @@ -71,7 +71,7 @@ function getBody(readStream, callback) { var sended, body = ''; - Util.checkArgs(arguments, ['readStream', 'callback']); + Util.check(arguments, ['readStream', 'callback']); readStream.on('data', function(chunk) { body += chunk; diff --git a/lib/server/ponse.js b/lib/server/ponse.js index 66774924..d83ca283 100644 --- a/lib/server/ponse.js +++ b/lib/server/ponse.js @@ -252,8 +252,8 @@ function checkParams(params) { var ret = true; - Util.checkArgs(arguments, ['params']); - Util.checkArgs([params.name, params.request, params.response], ['name', 'requst', 'response']); + Util.check(arguments, ['params']); + Util.check([params.name, params.request, params.response], ['name', 'requst', 'response']); return ret; } @@ -261,7 +261,7 @@ function getQuery(req) { var query, parsed; - Util.checkArgs(arguments, ['req']); + Util.check(arguments, ['req']); parsed = url.parse(req.url); query = parsed.query; @@ -272,7 +272,7 @@ function getPathName(req) { var pathname, parsed; - Util.checkArgs(arguments, ['req']); + Util.check(arguments, ['req']); parsed = url.parse(req.url); pathname = parsed.pathname; @@ -322,7 +322,7 @@ 'Location': url }; - Util.checkArgs(arguments, ['url', 'response']); + Util.check(arguments, ['url', 'response']); fillHeader(header, response); response.statusCode = MOVED_PERMANENTLY; diff --git a/lib/server/readify.js b/lib/server/readify.js index 76e7dc25..0d9a33cb 100644 --- a/lib/server/readify.js +++ b/lib/server/readify.js @@ -16,9 +16,9 @@ WIN = process.platform === 'win32'; module.exports = function(path, callback) { - Util.checkArgs(arguments, ['path', 'callback']) - .checkType('path', path, 'string') - .checkType('callback', callback, 'function'); + Util.check(arguments, ['path', 'callback']) + .type('path', path, 'string') + .type('callback', callback, 'function'); fs.readdir(path, function(error, names) { if (error) @@ -64,7 +64,7 @@ function parseStats(stats) { var files; - Util.checkArgs(arguments, ['stats']); + Util.check(arguments, ['stats']); files = Object.keys(stats).map(function(name) { var file, isDir, size, mode, modeStr, @@ -105,7 +105,7 @@ files : [] }; - Util.checkArgs(arguments, ['params']); + Util.check(arguments, ['params']); files = parseStats(stats); diff --git a/lib/server/rest.js b/lib/server/rest.js index 279b18b0..73b1fc7e 100644 --- a/lib/server/rest.js +++ b/lib/server/rest.js @@ -55,7 +55,7 @@ response : response, }; - Util.checkArgs(arguments, ['request', 'response', 'next']); + Util.check(arguments, ['request', 'response', 'next']); apiURL = CloudFunc.apiURL; name = ponse.getPathName(request); @@ -260,7 +260,7 @@ function onPUT(name, body, callback) { var cmd, files, data, from, to, error; - Util.checkArgs(arguments, ['name', 'body', 'callback']); + Util.check(arguments, ['name', 'body', 'callback']); if (name[0] === '/') cmd = name.replace('/', ''); @@ -406,7 +406,7 @@ }); }; - Util.checkArgs(arguments, ['files', 'processFunc', 'callback']); + Util.check(arguments, ['files', 'processFunc', 'callback']); copy(); } diff --git a/lib/server/rest/fs/get.js b/lib/server/rest/fs/get.js index 611b96a2..893dc8d3 100644 --- a/lib/server/rest/fs/get.js +++ b/lib/server/rest/fs/get.js @@ -15,7 +15,7 @@ module.exports = function(query, name, callback) { var hashStream, error; - Util.checkArgs(arguments, ['query', 'name', 'callback']); + Util.check(arguments, ['query', 'name', 'callback']); switch (query) { default: diff --git a/lib/server/rest/fs/patch.js b/lib/server/rest/fs/patch.js index cb2957b6..8a73d4a6 100644 --- a/lib/server/rest/fs/patch.js +++ b/lib/server/rest/fs/patch.js @@ -15,7 +15,7 @@ module.exports = function(name, readStream, callback) { var baseName = path.basename(name); - Util.checkArgs(arguments, ['name', 'readStream', 'callback']); + Util.check(arguments, ['name', 'readStream', 'callback']); pipe.getBody(readStream, function(error, data) { var options = { diff --git a/lib/server/rest/fs/put.js b/lib/server/rest/fs/put.js index 744d583b..b95ff763 100644 --- a/lib/server/rest/fs/put.js +++ b/lib/server/rest/fs/put.js @@ -26,7 +26,7 @@ onSave = Util.exec.with(onDone, 'save'), OnMakeDir = Util.exec.with(onDone, 'make dir'); - Util.checkArgs(arguments, ['query', 'name', 'readStream', 'callback']); + Util.check(arguments, ['query', 'name', 'readStream', 'callback']); switch(query) { default: diff --git a/lib/server/route.js b/lib/server/route.js index b405bb20..cb112360 100644 --- a/lib/server/route.js +++ b/lib/server/route.js @@ -82,7 +82,7 @@ lenthPath = TMPL_PATH.length, isRead = lengthTmpl === lenthPath; - Util.checkArgs(arguments, ['callback']); + Util.check(arguments, ['callback']); if (isRead) { callback(); @@ -116,7 +116,7 @@ function route(request, response, callback) { var name, p, isAuth, isFS, path; - Util.checkArgs(arguments, ['req', 'res', 'callback']); + Util.check(arguments, ['req', 'res', 'callback']); name = ponse.getPathName(request); isAuth = Util.strCmp(name, ['/auth', '/auth/github']); diff --git a/lib/server/size.js b/lib/server/size.js index f47d857e..1ed78a5a 100644 --- a/lib/server/size.js +++ b/lib/server/size.js @@ -21,7 +21,7 @@ var type, stopOnError, total = 0; - Util.checkArgs(arguments, ['dir', 'callback']); + Util.check(arguments, ['dir', 'callback']); if (!callback) { callback = options; diff --git a/lib/server/terminal.js b/lib/server/terminal.js index ac4692fc..b550debc 100644 --- a/lib/server/terminal.js +++ b/lib/server/terminal.js @@ -19,7 +19,7 @@ ConNum = 0; module.exports = function(socket) { - Util.checkArgs(arguments, ['socket']); + Util.check(arguments, ['socket']); if (pty) socket diff --git a/lib/server/timem.js b/lib/server/timem.js index 1fb58e02..299216c2 100644 --- a/lib/server/timem.js +++ b/lib/server/timem.js @@ -10,7 +10,7 @@ if (!callback) callback = option; - Util.checkArgs(arguments, ['filename', 'callback']); + Util.check(arguments, ['filename', 'callback']); fs.stat(filename, function(error, stat) { var time, timeRet; diff --git a/lib/server/win.js b/lib/server/win.js index 17e96c8e..8d419815 100644 --- a/lib/server/win.js +++ b/lib/server/win.js @@ -27,7 +27,7 @@ function getVolumes(callback) { var wmic = spawn('wmic', ['logicaldisk', 'get', 'name']); - Util.checkArgs(arguments, ['callback']); + Util.check(arguments, ['callback']); /* stream should be closed on win xp*/ wmic.stdin.end(); @@ -102,7 +102,7 @@ function isVolume(command) { var is, winVolume; - Util.checkArgs(arguments, ['command']); + Util.check(arguments, ['command']); if (WIN) { winVolume = new RegExp(WIN_VOLUME + '\\\\.*', 'i'); @@ -115,7 +115,7 @@ function isChangeVolume(command) { var is, winVolume; - Util.checkArgs(arguments, ['command']); + Util.check(arguments, ['command']); if (WIN) { winVolume = new RegExp(WIN_VOLUME, 'i'); diff --git a/lib/util.js b/lib/util.js index 40f1a5e7..01db0b03 100644 --- a/lib/util.js +++ b/lib/util.js @@ -59,44 +59,57 @@ return !!isMatch; }; - this.checkArgs = function(args, names) { - var msg = '', - template = '', - name = '', - - lenNames = names.length, - lenArgs = args.length, - lessArgs = lenArgs < lenNames; - - if (lessArgs) { - template = '{{ name }} coud not be empty!'; - name = names[lenNames - 1]; - - msg = Util.render(template, { - name: name - }); - - throw(Error(msg)); - } - - return this; - }; + this.check = new checkProto(); - /** - * Check is type of arg with name is equal to type - * - * @param name - * @param arg - * @param type - */ - this.checkType = function(name, arg, type) { - var is = Util.type(arg) === type; + function checkProto() { + /** + * Check is all arguments with names present + * + * @param name + * @param arg + * @param type + */ + var check = function check(args, names) { + var msg = '', + template = '', + name = '', + + lenNames = names.length, + lenArgs = args.length, + lessArgs = lenArgs < lenNames; + + if (lessArgs) { + template = '{{ name }} coud not be empty!'; + name = names[lenNames - 1]; + + msg = Util.render(template, { + name: name + }); + + throw(Error(msg)); + } + + return check; + }; - if (!is) - throw(Error(name + ' should be ' + type)); + /** + * Check is type of arg with name is equal to type + * + * @param name + * @param arg + * @param type + */ + check.type = function(name, arg, type) { + var is = Util.type(arg) === type; + + if (!is) + throw(Error(name + ' should be ' + type)); + + return check; + }; - return this; - }; + return check; + } /** * Copy properties from from to to @@ -553,7 +566,7 @@ countFuncs = 0, type = Util.type(funcs); - Util.checkArgs(arguments, ['funcs', 'callback']); + Util.check(arguments, ['funcs', 'callback']); switch(type) { case 'array':