avocado-old/packages/core/index.js

77 lines
2.0 KiB
JavaScript
Raw Normal View History

2019-03-17 23:45:48 -05:00
/**
* Composes single-argument functions from right to left. The rightmost
* function can take multiple arguments as it provides the signature for
* the resulting composite function.
*
* @param {...Function} funcs The functions to compose.
* @returns {Function} A function obtained by composing the argument functions
* from right to left. For example, compose(f, g, h) is identical to doing
* (...args) => f(g(h(...args))).
*/
2019-03-22 14:46:01 -05:00
export function arrayUnique(array) {
const uniques = new Map();
array.forEach((element) => {
if (uniques.has(element)) {
return;
}
uniques.set(element, true);
})
return Array.from(uniques.keys());
}
2019-03-17 23:45:48 -05:00
export function compose(...funcs) {
if (funcs.length === 0) {
return arg => arg
}
if (funcs.length === 1) {
return funcs[0]
}
return funcs.reduce((a, b) => (...args) => a(b(...args)))
}
2019-04-07 14:34:22 -05:00
export function flatten(array) {
return array.reduce((flattened, elm) => flattened.concat(elm), []);
}
2019-03-17 23:45:48 -05:00
export function virtualize(fields) {
return (Superclass) => {
class Virtualized extends Superclass {}
fields.forEach((field) => {
Virtualized.prototype[field] = function() {
const prototype = Object.getPrototypeOf(this);
const className = prototype.constructor.name;
throw new ReferenceError(
`"${className}" has undefined pure virtual method "${field}"`
);
}
});
return Virtualized;
}
}
2019-03-24 01:16:04 -05:00
export function virtualizeStatic(fields) {
return (Superclass) => {
class Virtualized extends Superclass {}
fields.forEach((field) => {
Virtualized[field] = function() {
const prototype = Virtualized.prototype;
const className = prototype.constructor.name;
throw new ReferenceError(
`"${className}" has undefined pure virtual static method "${field}"`
);
}
});
return Virtualized;
}
}
2019-03-17 23:45:48 -05:00
export class TickingPromise extends Promise {
constructor(resolve, reject) {
super(resolve, reject);
this.ticker = null;
}
}