Skip to content

Commit

Permalink
feat(fp): move to conversion by @pakal/curry, add placeholder support
Browse files Browse the repository at this point in the history
The conversion of the methods to fp by "@pakal/curry"
All fp methods support placeholder, and exports of each package that supports fp have placeholder
  • Loading branch information
yisraelx committed Jul 4, 2018
1 parent 5107c31 commit ff18d23
Show file tree
Hide file tree
Showing 37 changed files with 58 additions and 36 deletions.
1 change: 1 addition & 0 deletions modules/-all/fp/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* @license MIT
*/

export { __ } from '@promises/_curry';
export {default as error} from '@promises/error/fp';
export {default as everyParallel} from '@promises/every-parallel/fp';
export {default as everySeries} from '@promises/every-series/fp';
Expand Down
1 change: 1 addition & 0 deletions modules/-all/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"url": "https://github.com/yisraelx/promises/issues"
},
"dependencies": {
"@promises/_curry": "^0.4.0",
"@promises/compose": "^0.4.0",
"@promises/core": "^0.3.1",
"@promises/create": "^0.4.0",
Expand Down
1 change: 1 addition & 0 deletions modules/-constructor/fp/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* @license MIT
*/

export { __ } from '@promises/_curry';
export {default as timesParallel} from '@promises/times-parallel/fp';
export {default as timesSeries} from '@promises/times-series/fp';

1 change: 1 addition & 0 deletions modules/-constructor/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"url": "https://github.com/yisraelx/promises/issues"
},
"dependencies": {
"@promises/_curry": "^0.4.0",
"@promises/compose": "^0.4.0",
"@promises/core": "^0.3.1",
"@promises/create": "^0.4.0",
Expand Down
1 change: 1 addition & 0 deletions modules/-parallel/fp/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* @license MIT
*/

export { __ } from '@promises/_curry';
export { default as everyParallel } from '@promises/every-parallel/fp';
export { default as filterParallel } from '@promises/filter-parallel/fp';
export { default as forEachParallel } from '@promises/for-each-parallel/fp';
Expand Down
1 change: 1 addition & 0 deletions modules/-parallel/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"url": "https://github.com/yisraelx/promises/issues"
},
"dependencies": {
"@promises/_curry": "^0.4.0",
"@promises/core": "^0.3.1",
"@promises/do-while-parallel": "^0.4.0",
"@promises/every-parallel": "^0.4.0",
Expand Down
1 change: 1 addition & 0 deletions modules/-prototype/fp/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* @license MIT
*/

export { __ } from '@promises/_curry';
export { default as error } from '@promises/error/fp';
export { default as everyParallel } from '@promises/every-parallel/fp';
export { default as everySeries } from '@promises/every-series/fp';
Expand Down
1 change: 1 addition & 0 deletions modules/-prototype/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"url": "https://github.com/yisraelx/promises/issues"
},
"dependencies": {
"@promises/_curry": "^0.4.0",
"@promises/core": "^0.3.1",
"@promises/error": "^0.4.0",
"@promises/every-parallel": "^0.4.0",
Expand Down
1 change: 1 addition & 0 deletions modules/-series/fp/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* @license MIT
*/

export { __ } from '@promises/_curry';
export { default as everySeries } from '@promises/every-series/fp';
export { default as filterSeries } from '@promises/filter-series/fp';
export { default as forEachSeries } from '@promises/for-each-series/fp';
Expand Down
1 change: 1 addition & 0 deletions modules/-series/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"url": "https://github.com/yisraelx/promises/issues"
},
"dependencies": {
"@promises/_curry": "^0.4.0",
"@promises/core": "^0.3.1",
"@promises/do-while-series": "^0.4.0",
"@promises/every-series": "^0.4.0",
Expand Down
32 changes: 13 additions & 19 deletions modules/_curry/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,27 @@
* @license MIT
*/

export interface ICurryOptions {
length?: number;
order?: number[];
}
import curry from '@pakal/curry';

/**
* @function
* @private
*/
export default function _curry(fn: Function, { length = fn.length, order}: ICurryOptions = {}) {
order = Array.isArray(order) ? order : [length - 1].concat(Array.apply(null, Array(length - 1)).map((v, i) => i));
function _curry(fn: Function, length: number = fn.length) {
return curry((function () {
let {length} = arguments;
let args = Array(length);

let next = (allArgs: any[]) => {
if (allArgs.length >= length) {
let execArgs = Array(length);
if (length) {
args[0] = arguments[--length];
for (let i = 0; i < length; i++) {
let index = order[i];
let value = allArgs[index];
execArgs[i] = value;
args[i + 1] = arguments[i];
}
return fn.apply(null, execArgs);
}
return (...currentArgs: any[]) => {
allArgs = allArgs.concat(currentArgs);
return next(allArgs);
};
};

return next([]);
return fn.apply(this, args);
}), length) as any;
}

export { __ } from '@pakal/curry';
export default _curry;
3 changes: 3 additions & 0 deletions modules/_curry/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,8 @@
},
"bugs": {
"url": "https://github.com/yisraelx/promises/issues"
},
"dependencies": {
"@pakal/curry": "^0.1.0"
}
}
5 changes: 3 additions & 2 deletions modules/error/fp/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import { IOptionalPromise } from '@promises/interfaces';
import error from '../';

export interface ICurriedError {
<R>(newValue: IOptionalPromise<any>, value: IOptionalPromise<R>): Promise<R>;
<R>(newValue: IOptionalPromise<any>): (value: IOptionalPromise<R>) => Promise<R>;
(newValue: any, value: IOptionalPromise<any>): Promise<never>;
(newValue: any): (value: IOptionalPromise<any>) => Promise<never>;
}

/**
Expand All @@ -32,4 +32,5 @@ export interface ICurriedError {
*/
let curriedError: ICurriedError = _curry(error);

export { __ } from '@promises/_curry';
export default curriedError;
1 change: 1 addition & 0 deletions modules/every-parallel/fp/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,5 @@ export interface ICurriedEveryParallel {
*/
let curriedEveryParallel: ICurriedEveryParallel = _curry(everyParallel);

export { __ } from '@promises/_curry';
export default curriedEveryParallel;
1 change: 1 addition & 0 deletions modules/every-series/fp/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,5 @@ export interface ICurriedEverySeries {
*/
let curriedEverySeries: ICurriedEverySeries = _curry(everySeries);

export { __ } from '@promises/_curry';
export default curriedEverySeries;
1 change: 1 addition & 0 deletions modules/filter-parallel/fp/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,5 @@ export interface ICurriedFilterParallel {
*/
let curriedFilterParallel: ICurriedFilterParallel = _curry(filterParallel);

export { __ } from '@promises/_curry';
export default curriedFilterParallel;
1 change: 1 addition & 0 deletions modules/filter-series/fp/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,5 @@ export interface ICurriedFilterSeries {
*/
let curriedFilterSeries: ICurriedFilterSeries = _curry(filterSeries);

export { __ } from '@promises/_curry';
export default curriedFilterSeries;
1 change: 1 addition & 0 deletions modules/finally/fp/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,5 @@ export interface ICurriedFinally {
*/
let curriedFinally: ICurriedFinally = _curry(_finally);

export { __ } from '@promises/_curry';
export default curriedFinally;
1 change: 1 addition & 0 deletions modules/for-each-parallel/fp/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,5 @@ export interface ICurriedForEachParallel {
*/
let curriedForEachParallel: ICurriedForEachParallel = _curry(forEachParallel);

export { __ } from '@promises/_curry';
export default curriedForEachParallel;
1 change: 1 addition & 0 deletions modules/for-each-right-series/fp/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,5 @@ export interface ICurriedForEachRightSeries {
*/
let curriedForEachRightSeries: ICurriedForEachRightSeries = _curry(forEachRightSeries);

export { __ } from '@promises/_curry';
export default curriedForEachRightSeries;
4 changes: 4 additions & 0 deletions modules/for-each-series/fp/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@ import forEachSeries from '../';

export interface ICurriedForEachSeries {
<T extends ArrayLike<any>>(iteratee: (value: T[keyof T & number], index: number, array: T) => IOptionalPromise<any>, array: IOptionalPromiseArray<T>): Promise<T>;

<T extends ArrayLike<any>>(iteratee: (value: T[keyof T & number], index: number, array: T) => IOptionalPromise<any>): (array: IOptionalPromiseArray<T>) => Promise<T>;

<T extends object>(iteratee: (value: T[keyof T], key: keyof T, object: T) => IOptionalPromise<any>, object: IOptionalPromiseDictionary<T>): Promise<T>;

<T extends object>(iteratee: (value: T[keyof T], key: keyof T, object: T) => IOptionalPromise<any>): (object: IOptionalPromiseDictionary<T>) => Promise<T>;
}

Expand Down Expand Up @@ -50,4 +53,5 @@ export interface ICurriedForEachSeries {
*/
let curriedForEachSeries: ICurriedForEachSeries = _curry(forEachSeries);

export { __ } from '@promises/_curry';
export default curriedForEachSeries;
1 change: 1 addition & 0 deletions modules/map-parallel/fp/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,5 @@ export interface ICurriedMapParallel {
*/
let curriedMapParallel: ICurriedMapParallel = _curry(mapParallel);

export { __ } from '@promises/_curry';
export default curriedMapParallel;
1 change: 1 addition & 0 deletions modules/map-series/fp/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,5 @@ export interface ICurriedMapSeries {
*/
let curriedMapSeries: ICurriedMapSeries = _curry(mapSeries);

export { __ } from '@promises/_curry';
export default curriedMapSeries;
1 change: 1 addition & 0 deletions modules/next/fp/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,5 @@ export interface ICurriedNext {
*/
let curriedNext: ICurriedNext = _curry(next);

export { __ } from '@promises/_curry';
export default curriedNext;
1 change: 1 addition & 0 deletions modules/reduce-right-series/fp/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,5 @@ export interface ICurriedReduceRightSeries {
*/
let curriedReduceRightSeries: ICurriedReduceRightSeries = _curry(reduceRightSeries);

export { __ } from '@promises/_curry';
export default curriedReduceRightSeries;
1 change: 1 addition & 0 deletions modules/reduce-series/fp/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,5 @@ export interface ICurriedReduceSeries {
*/
let curriedReduceSeries: ICurriedReduceSeries = _curry(reduceSeries);

export { __ } from '@promises/_curry';
export default curriedReduceSeries;
1 change: 1 addition & 0 deletions modules/reject-parallel/fp/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,5 @@ export interface ICurriedRejectParallel {
*/
let curriedRejectParallel: ICurriedRejectParallel = _curry(rejectParallel);

export { __ } from '@promises/_curry';
export default curriedRejectParallel;
1 change: 1 addition & 0 deletions modules/reject-series/fp/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,5 @@ export interface ICurriedRejectSeries {
*/
let curriedRejectSeries: ICurriedRejectSeries = _curry(rejectSeries);

export { __ } from '@promises/_curry';
export default curriedRejectSeries;
1 change: 1 addition & 0 deletions modules/reset/fp/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,5 @@ export interface ICurriedReset {
*/
let curriedReset: ICurriedReset = _curry(reset);

export { __ } from '@promises/_curry';
export default curriedReset;
1 change: 1 addition & 0 deletions modules/sleep/fp/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ export interface ICurriedSleep {
*/
let curriedSleep: ICurriedSleep = _curry(sleep);

export { __ } from '@promises/_curry';
export default curriedSleep;
1 change: 1 addition & 0 deletions modules/some-parallel/fp/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,5 @@ export interface ICurriedSomeParallel {
*/
let curriedSomeParallel: ICurriedSomeParallel = _curry(someParallel);

export { __ } from '@promises/_curry';
export default curriedSomeParallel;
1 change: 1 addition & 0 deletions modules/some-series/fp/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@ export interface ICurriedSomeSeries {
*/
let curriedSomeSeries: ICurriedSomeSeries = _curry(someSeries);

export { __ } from '@promises/_curry';
export default curriedSomeSeries;
1 change: 1 addition & 0 deletions modules/times-parallel/fp/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,5 @@ export interface ICurriedTimesParallel {
*/
let curriedTimesParallel: ICurriedTimesParallel = _curry(timesParallel);

export { __ } from '@promises/_curry';
export default curriedTimesParallel;
1 change: 1 addition & 0 deletions modules/times-series/fp/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,5 @@ export interface ICurriedTimesSeries {
*/
let curriedTimesSeries: ICurriedTimesSeries = _curry(timesSeries);

export { __ } from '@promises/_curry';
export default curriedTimesSeries;
1 change: 1 addition & 0 deletions modules/to-callback/fp/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ export interface ICurriedToCallback {
*/
let curriedToCallback: ICurriedToCallback = _curry(toCallback);

export { __ } from '@promises/_curry';
export default curriedToCallback;
3 changes: 2 additions & 1 deletion modules/wait/fp/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@ export interface ICurriedWait {
*/
let curriedWait: ICurriedWait = _curry(wait);

export default curriedWait;
export { __ } from '@promises/_curry';
export default curriedWait;
16 changes: 2 additions & 14 deletions test/_curry.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,26 +23,14 @@ describe('_curry', () => {
fn(b)()(void 0)(d)()(a);
});

it(`should be collect 2 args by n and order by array`, () => {
it(`should be collect 2 args by length`, () => {
let a = 1;
let b = 3;
let fn = _curry(function (...args) {
expect(args).toEqual([a, b]);
}, {length: 2, order: [1, 0]});
}, 2);

fn(b)(a);
});

it(`should be collect 4 args by n and order by array`, () => {
let a = 1;
let b = 2;
let c = 3;
let d = 4;
let fn = _curry(function (...args) {
expect(args).toEqual([a, b, c, d]);
}, {length: 4, order: [3, 0, 1, 2]});

fn(b)(c, d)(a);
});

});

0 comments on commit ff18d23

Please sign in to comment.