metarhia-common
Advanced tools
Comparing version 0.0.10 to 0.0.11
@@ -16,2 +16,3 @@ 'use strict'; | ||
'time', // Data and Time functions | ||
'functional', // Functional programming | ||
'misc', // Miscellaneous tools | ||
@@ -23,3 +24,2 @@ 'units', // Units conversion | ||
'cache', // Cache (enhanced Map) | ||
'functional' // Functional programming | ||
]; | ||
@@ -26,0 +26,0 @@ |
@@ -69,7 +69,4 @@ 'use strict'; | ||
const references = new Map(); | ||
return ( | ||
Array.isArray(obj) ? | ||
duplicateArray(obj, references) : | ||
duplicateObject(obj, references) | ||
); | ||
const dup = Array.isArray(obj) ? duplicateArray : duplicateObject; | ||
return dup(obj, references); | ||
}; | ||
@@ -99,5 +96,3 @@ | ||
const object = ( | ||
obj.constructor ? | ||
new obj.constructor() : | ||
Object.create(null) | ||
obj.constructor ? new obj.constructor() : Object.create(null) | ||
); | ||
@@ -104,0 +99,0 @@ references.set(obj, object); |
@@ -43,5 +43,5 @@ 'use strict'; | ||
if (arrays.length === 0) return []; | ||
let i, j; | ||
let minLen = arrays[0].length; | ||
let i; | ||
for (i = 1; i < arrays.length; i++) { | ||
@@ -54,3 +54,2 @@ minLen = Math.min(arrays[i].length, minLen); | ||
res[i] = new Array(arrays.length); | ||
let j; | ||
for (j = 0; j < res[i].length; j++) { | ||
@@ -84,2 +83,65 @@ res[i][j] = arrays[j][i]; | ||
api.common.curryUntil = ( | ||
// Curries function until the condition | ||
condition, // function(argsI, argsParts) returns boolean | ||
// argsI is arguments for i-th currying | ||
// argsParts is array of args given for currying from first to i-th currying | ||
fn, // function which will be curried | ||
...args1 // arguments for fn | ||
// Returns curried function | ||
) => { | ||
const argsParts = []; | ||
const curryMore = (...argsI) => { | ||
argsParts.push(argsI); | ||
if (condition(argsI, argsParts)) { | ||
const allArgs = [].concat(...argsParts); | ||
return fn(...allArgs); | ||
} | ||
return curryMore; | ||
}; | ||
return curryMore(...args1); | ||
}; | ||
api.common.curryN = ( | ||
// Curry fn count times. | ||
// First curry uses args as arguments for first currying | ||
fn, // curried function | ||
count, // number of times function should be curried | ||
...args1 // arguments for first currying | ||
// Returns curried count times function | ||
) => { | ||
let i = -1; | ||
const condition = () => (i++, i === count); | ||
return api.common.curryUntil(condition, fn, ...args1); | ||
}; | ||
api.common.curryTwice = fn => api.common.curry(api.common.curry, fn); | ||
api.common.fullCurry = (fn, ...args1) => { | ||
let argsTotalCount = 0; | ||
const condition = (argsI) => ( | ||
argsTotalCount += argsI.length, | ||
argsTotalCount >= fn.length | ||
); | ||
return api.common.curryUntil(condition, fn, ...args1); | ||
}; | ||
api.common.applyArgs = (...args) => fn => fn(...args); | ||
api.common.either = (fn) => (...args) => { | ||
let arg; | ||
let lastError; | ||
for (arg of args) { | ||
try { | ||
return fn(arg); | ||
} catch (error) { | ||
lastError = error; | ||
} | ||
} | ||
throw lastError; | ||
}; | ||
}; |
@@ -22,2 +22,17 @@ 'use strict'; | ||
api.common.cbExtract = ( | ||
// Exctracts callback and wraps it with api.common.cb | ||
// callback is last argument if it's function | ||
// otherwise it's api.common.falseness | ||
args // arguments | ||
// Returns wrapped callback | ||
) => { | ||
const lastArg = args[args.length - 1]; | ||
if (typeof(lastArg) !== 'function') return api.common.falseness; | ||
const cb = api.common.cb(lastArg); | ||
args.pop(); | ||
return api.common.cb(cb); | ||
}; | ||
api.common.override = ( | ||
@@ -93,2 +108,17 @@ obj, // object containing function to override | ||
api.common.restLeft = ( | ||
// Transforms function with (args, arg1 .. argN, callback) arguments | ||
// to function with (arg1 .. argN, ...args, callback) arguments | ||
fn // transforming function | ||
// Returns transformed function | ||
) => (...spreadArgs) => { | ||
const callback = api.common.cbExtract(spreadArgs); | ||
const namedArgsCount = fn.length - 2; | ||
const namedArgs = spreadArgs.slice(0, namedArgsCount); | ||
const args = spreadArgs.slice(namedArgsCount); | ||
fn(args, ...namedArgs, callback); | ||
}; | ||
api.common.requireEither = api.common.either(require); | ||
}; |
{ | ||
"name": "metarhia-common", | ||
"version": "0.0.10", | ||
"version": "0.0.11", | ||
"author": "Timur Shemsedinov <timur.shemsedinov@gmail.com>", | ||
@@ -31,5 +31,5 @@ "description": "Metarhia Common Library", | ||
"devDependencies": { | ||
"eslint": "3.x", | ||
"eslint": "^4.1.1", | ||
"eslint-plugin-metarhia": "0.0.2", | ||
"tap": "^10.0.0" | ||
"tap": "^10.7.0" | ||
}, | ||
@@ -36,0 +36,0 @@ "jshintConfig": { |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
38095
30
1119