feature(util) rm checkType

This commit is contained in:
coderaiser 2016-12-28 17:03:52 +02:00
parent f4332c779e
commit fc058fcfa7
6 changed files with 113 additions and 144 deletions

View file

@ -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;

View file

@ -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);

View file

@ -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,

View file

@ -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);

View file

@ -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;

View file

@ -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
*