diff --git a/packages/core/array.js b/packages/core/array.js new file mode 100644 index 0000000..b567689 --- /dev/null +++ b/packages/core/array.js @@ -0,0 +1,7 @@ +export function arrayUnique(array) { + return Array.from((new Set(array)).values()); +} + +export function flatten(array) { + return array.reduce((flattened, elm) => flattened.concat(elm), []); +} diff --git a/packages/core/fast-apply.js b/packages/core/fast-apply.js index 0e42784..e69de29 100644 --- a/packages/core/fast-apply.js +++ b/packages/core/fast-apply.js @@ -1,23 +0,0 @@ -export function fastApply(holder, fn, args) { - if (holder || args.length > 5) { - return fn.apply(holder, args); - } - if (0 === args.length) { - return fn(); - } - if (1 === args.length) { - return fn(args[0]); - } - if (2 === args.length) { - return fn(args[0], args[1]); - } - if (3 === args.length) { - return fn(args[0], args[1], args[2]); - } - if (4 === args.length) { - return fn(args[0], args[1], args[2], args[3]); - } - if (5 === args.length) { - return fn(args[0], args[1], args[2], args[3], args[4]); - } -} diff --git a/packages/core/function.js b/packages/core/function.js new file mode 100644 index 0000000..35d9ac7 --- /dev/null +++ b/packages/core/function.js @@ -0,0 +1,35 @@ +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))) +} + +export function fastApply(holder, fn, args) { + if (holder || args.length > 5) { + return fn.apply(holder, args); + } + if (0 === args.length) { + return fn(); + } + if (1 === args.length) { + return fn(args[0]); + } + if (2 === args.length) { + return fn(args[0], args[1]); + } + if (3 === args.length) { + return fn(args[0], args[1], args[2]); + } + if (4 === args.length) { + return fn(args[0], args[1], args[2], args[3]); + } + if (5 === args.length) { + return fn(args[0], args[1], args[2], args[3], args[4]); + } +} diff --git a/packages/core/index.js b/packages/core/index.js index 4c5f159..4c7027a 100644 --- a/packages/core/index.js +++ b/packages/core/index.js @@ -9,108 +9,7 @@ * (...args) => f(g(h(...args))). */ -export function arrayUnique(array) { - return Array.from((new Set(array)).values()); -} - -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))) -} - -export function flatten(array) { - return array.reduce((flattened, elm) => flattened.concat(elm), []); -} - -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; - } -} - -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; - } -} - -export class TickingPromise extends Promise { - - constructor(executor, ticker) { - let _reject, _resolve; - super((resolve, reject) => { - _reject = reject; - _resolve = resolve; - if (executor) { - executor(resolve, reject); - } - }); - this.executor = executor; - this.reject = _reject; - this.resolve = _resolve; - this.ticker = ticker; - } - - static all(promises) { - const tickingPromises = []; - for (let i = 0; i < promises.length; i++) { - const promise = promises[i]; - if (promise instanceof TickingPromise) { - tickingPromises.push(promise); - // After resolution, stop ticking the promise. - promise.then(() => { - tickingPromises.splice(tickingPromises.indexOf(promise), 1); - }) - } - } - if (0 === tickingPromises.length) { - return super.all(promises); - } - return new TickingPromise( - (resolve) => { - resolve(Promise.all(promises)); - }, - (elapsed) => { - for (let i = 0; i < tickingPromises.length; i++) { - tickingPromises[i].tick(elapsed); - } - }, - ); - } - - tick(elapsed) { - this.ticker(elapsed, this.resolve, this.reject); - } - -} - +export {arrayUnique, flatten} from './array'; export {EventEmitterMixin as EventEmitter} from './event-emitter'; export {merge} from './merge'; export { @@ -119,6 +18,8 @@ export { mergeDiffObject, mergeDiffPrimitive, } from './merge-diff'; -export {fastApply} from './fast-apply'; export {inflate, deflate} from './flate'; +export {compose, fastApply} from './function'; export {PropertyMixin as Property} from './property'; +export {TickingPromise} from './ticking-promise'; +export {virtualize, virtualizeStatic} from './virtualize'; diff --git a/packages/core/ticking-promise.js b/packages/core/ticking-promise.js new file mode 100644 index 0000000..b2e0d46 --- /dev/null +++ b/packages/core/ticking-promise.js @@ -0,0 +1,49 @@ +export class TickingPromise extends Promise { + + constructor(executor, ticker) { + let _reject, _resolve; + super((resolve, reject) => { + _reject = reject; + _resolve = resolve; + if (executor) { + executor(resolve, reject); + } + }); + this.executor = executor; + this.reject = _reject; + this.resolve = _resolve; + this.ticker = ticker; + } + + static all(promises) { + const tickingPromises = []; + for (let i = 0; i < promises.length; i++) { + const promise = promises[i]; + if (promise instanceof TickingPromise) { + tickingPromises.push(promise); + // After resolution, stop ticking the promise. + promise.then(() => { + tickingPromises.splice(tickingPromises.indexOf(promise), 1); + }) + } + } + if (0 === tickingPromises.length) { + return super.all(promises); + } + return new TickingPromise( + (resolve) => { + resolve(Promise.all(promises)); + }, + (elapsed) => { + for (let i = 0; i < tickingPromises.length; i++) { + tickingPromises[i].tick(elapsed); + } + }, + ); + } + + tick(elapsed) { + this.ticker(elapsed, this.resolve, this.reject); + } + +} diff --git a/packages/core/virtualize.js b/packages/core/virtualize.js new file mode 100644 index 0000000..74a1627 --- /dev/null +++ b/packages/core/virtualize.js @@ -0,0 +1,31 @@ +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; + } +} + +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; + } +}