tricks
Version:
ES6 modules
61 lines (51 loc) • 1.63 kB
JavaScript
// Makes it easier to assign parameters, where some are optional
// @param o object
// @param a arguments
export default (o, args) => {
const p = {};
let i = 0;
let t = null;
let x = null;
// 'x' is the first key in the list of object parameters
for (x in o) {
if (Object.prototype.hasOwnProperty.call(o, x)) {
break;
}
}
// Passing in hash object of arguments?
// Where the first argument can't be an object
if ((args.length === 1) && (typeof (args[0]) === 'object') && o[x] !== 'o!') {
// Could this object still belong to a property?
// Check the object keys if they match any of the property keys
for (x in args[0]) {
if (Object.prototype.hasOwnProperty.call(o, x)) {
// Does this key exist in the property list?
if (x in o) {
// Yes this key does exist so its most likely this function has been invoked with an object parameter
// Return first argument as the hash of all arguments
return args[0];
}
}
}
}
// Else loop through and account for the missing ones.
for (x in o) {
if (Object.prototype.hasOwnProperty.call(o, x)) {
t = typeof (args[i]);
if ((typeof (o[x]) === 'function' && o[x].test(args[i])) || (typeof (o[x]) === 'string' && (
(o[x].indexOf('s') > -1 && t === 'string') ||
(o[x].indexOf('o') > -1 && t === 'object') ||
(o[x].indexOf('i') > -1 && t === 'number') ||
(o[x].indexOf('a') > -1 && t === 'object') ||
(o[x].indexOf('f') > -1 && t === 'function')
))
) {
p[x] = args[i++];
}
else if (typeof (o[x]) === 'string' && o[x].indexOf('!') > -1) {
return false;
}
}
}
return p;
};