diff --git a/client/dom.js b/client/dom.js index 72b5e557..2980f426 100644 --- a/client/dom.js +++ b/client/dom.js @@ -1139,18 +1139,22 @@ var CloudCmd, Util, DOM, CloudFunc; /** * check storage hash */ - this.checkStorageHash = function(name, callback) { - var Storage = DOM.Storage, - parallel = Util.exec.parallel, - loadHash = DOM.loadCurrentHash, - nameHash = name + '-hash', - getStoreHash = Util.exec.with(Storage.get, nameHash); + this.checkStorageHash = function(name, callback) { + var Storage = DOM.Storage; + var parallel = Util.exec.parallel; + var loadHash = DOM.loadCurrentHash; + var nameHash = name + '-hash'; + var getStoreHash = Util.exec.with(Storage.get, nameHash); - Util.check(arguments, ['name', 'callback']); + if (typeof name !== 'string') + throw Error('name should be a string!'); + + if (typeof callback !== 'function') + throw Error('callback should be a function!'); parallel([loadHash, getStoreHash], function(error, loadHash, storeHash) { - var equal, - isContain = /error/.test(loadHash); + var equal; + var isContain = /error/.test(loadHash); if (isContain) error = loadHash; diff --git a/client/events.js b/client/events.js index 5855cc59..2411bab1 100644 --- a/client/events.js +++ b/client/events.js @@ -8,81 +8,79 @@ var Util, DOM; DOMProto.Events = new EventsProto(); function EventsProto() { - var Events = this, + var Events = this; + var Type = Util.type; - Type = Util.type, - check = Util.check, + function parseArgs(eventName, element, listener, callback) { + var isFunc, isElement, error, + EVENT_NAME = 0, + ELEMENT = 1, + type = Type(eventName); - parseArgs = function(eventName, element, listener, callback) { - var isFunc, isElement, error, - EVENT_NAME = 0, - ELEMENT = 1, - type = Type(eventName); + switch(type) { + default: + isElement = type.match('element'); - switch(type) { - default: - isElement = type.match('element'); + if (!isElement) { + error = new Error('unknown eventName: ' + type); + throw(error); + } else { + eventName = arguments[ELEMENT]; + element = arguments[EVENT_NAME]; - if (!isElement) { - error = new Error('unknown eventName: ' + type); - throw(error); - } else { - eventName = arguments[ELEMENT]; - element = arguments[EVENT_NAME]; - - parseArgs( - eventName, - element, - listener, - callback - ); - } - break; - - case 'string': - isFunc = Type.function(element); - - if (isFunc) { - listener = element; - element = null; - } - - if (!element) - element = window; - - callback(element, [ + parseArgs( eventName, + element, listener, - false - ]); - break; - - case 'array': - eventName.forEach(function(eventName) { - parseArgs( - eventName, - element, - listener, - callback - ); - }); - break; - - case 'object': - Object.keys(eventName).forEach(function(name) { - var eventListener = eventName[name]; - - parseArgs( - name, - element, - eventListener, - callback - ); - }); - - break; + callback + ); } - }; + break; + + case 'string': + isFunc = Type.function(element); + + if (isFunc) { + listener = element; + element = null; + } + + if (!element) + element = window; + + callback(element, [ + eventName, + listener, + false + ]); + break; + + case 'array': + eventName.forEach(function(eventName) { + parseArgs( + eventName, + element, + listener, + callback + ); + }); + break; + + case 'object': + Object.keys(eventName).forEach(function(name) { + var eventListener = eventName[name]; + + parseArgs( + name, + element, + eventListener, + callback + ); + }); + + break; + } + } /** * safe add event listener @@ -91,8 +89,8 @@ var Util, DOM; * @param element {document by default} * @param listener */ - this.add = function(type, element, listener) { - check(arguments, ['type']); + this.add = function(type, element, listener) { + checkType(type); parseArgs(type, element, listener, function(element, args) { element.addEventListener.apply(element, args); @@ -132,7 +130,7 @@ var Util, DOM; * @param element {document by default} */ this.remove = function(type, element, listener) { - check(arguments, ['type']); + checkType(type); parseArgs(type, element, listener, function(element, args) { element.removeEventListener.apply(element, args); @@ -328,5 +326,10 @@ var Util, DOM; return ret; }; + + function checkType(type) { + if (!type) + throw Error('type could not be empty!'); + } } })(Util, DOM); diff --git a/client/files.js b/client/files.js index 088be990..4510e07e 100644 --- a/client/files.js +++ b/client/files.js @@ -23,10 +23,10 @@ DIR_JSON = '/json/', timeout = getTimeoutOnce(2000); - this.get = function(name, callback) { - var type = Util.type(name); + this.get = function(name, callback) { + var type = Util.type(name); - Util.check(arguments, ['name', 'callback']); + check(name, callback); switch(type) { case 'string': @@ -47,6 +47,14 @@ return Files; }; + function check(name, callback) { + if (!name) + throw Error('name could not be empty!'); + + if (typeof callback !== 'function') + throw Error('callback should be a function'); + } + function getModule(name, callback) { var path, diff --git a/client/operation.js b/client/operation.js index 18493048..0f35970d 100644 --- a/client/operation.js +++ b/client/operation.js @@ -469,6 +469,11 @@ return /\.tar\.gz$/; } + function checkEmpty(name, operation) { + if (!operation) + throw Error(name + ' could not be empty!'); + } + function twopack(operation, type) { var op, fileFrom, @@ -480,7 +485,7 @@ activeFiles = DOM.getActiveFiles(), names = DOM.getFilenames(activeFiles); - Util.check(arguments, ['operation']); + checkEmpty('operation', operation); if (!names.length) { Dialog.alert.noFiles(TITLE); diff --git a/client/view.js b/client/view.js index 2e5eb2eb..e2d6296a 100644 --- a/client/view.js +++ b/client/view.js @@ -280,7 +280,7 @@ var CloudCmd, Util, DOM, CloudFunc, $, exec; } function getMediaElement(src, callback) { - Util.check(arguments, ['src', 'callback']); + check(src, callback); DOM.Files.get('view/media-tmpl', function(error, template) { var rendered, element, type, is, @@ -307,6 +307,14 @@ var CloudCmd, Util, DOM, CloudFunc, $, exec; }); } + function check(src, callback) { + if (typeof src !== 'string') + throw Error('src should be a string!'); + + if (typeof callback !== 'function') + throw Error('callback should be a function'); + } + function onMediaKey(media, event) { var key = event.keyCode; diff --git a/common/util.js b/common/util.js index 80f194e0..1ff20f49 100644 --- a/common/util.js +++ b/common/util.js @@ -21,68 +21,9 @@ function UtilProto(exec) { var Util = this; - this.check = new checkProto(); - this.getStrBigFirst = getStrBigFirst; this.kebabToCamelCase = kebabToCamelCase; - function checkProto() { - /** - * Check is all arguments with names present - * - * @param name - * @param arg - */ - function check(args, names) { - var msg = '', - name = '', - template = '{{ name }} coud not be empty!', - - indexOf = Array.prototype.indexOf, - - lenNames = names.length, - lenArgs = args.length, - lessArgs = lenArgs < lenNames, - emptyIndex = indexOf.call(args), - isEmpty = ~emptyIndex && emptyIndex <= lenNames - 1; - - if (lessArgs || isEmpty) { - if (lessArgs) - name = names[lenNames - 1]; - else - name = names[emptyIndex]; - - msg = rendy(template, { - name: name - }); - - throw Error(msg); - } - - return check; - } - - check.check = check; - - /** - * 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 check; - } - /** * Copy properties from from to to *