| 1 | 'use strict';
|
|---|
| 2 |
|
|---|
| 3 | Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|---|
| 4 |
|
|---|
| 5 | function curryRight(func, arity = func.length, guard) {
|
|---|
| 6 | arity = guard ? func.length : arity;
|
|---|
| 7 | arity = Number.parseInt(arity, 10);
|
|---|
| 8 | if (Number.isNaN(arity) || arity < 1) {
|
|---|
| 9 | arity = 0;
|
|---|
| 10 | }
|
|---|
| 11 | const wrapper = function (...partialArgs) {
|
|---|
| 12 | const holders = partialArgs.filter(item => item === curryRight.placeholder);
|
|---|
| 13 | const length = partialArgs.length - holders.length;
|
|---|
| 14 | if (length < arity) {
|
|---|
| 15 | return makeCurryRight(func, arity - length, partialArgs);
|
|---|
| 16 | }
|
|---|
| 17 | if (this instanceof wrapper) {
|
|---|
| 18 | return new func(...partialArgs);
|
|---|
| 19 | }
|
|---|
| 20 | return func.apply(this, partialArgs);
|
|---|
| 21 | };
|
|---|
| 22 | wrapper.placeholder = curryRightPlaceholder;
|
|---|
| 23 | return wrapper;
|
|---|
| 24 | }
|
|---|
| 25 | function makeCurryRight(func, arity, partialArgs) {
|
|---|
| 26 | function wrapper(...providedArgs) {
|
|---|
| 27 | const holders = providedArgs.filter(item => item === curryRight.placeholder);
|
|---|
| 28 | const length = providedArgs.length - holders.length;
|
|---|
| 29 | providedArgs = composeArgs(providedArgs, partialArgs);
|
|---|
| 30 | if (length < arity) {
|
|---|
| 31 | return makeCurryRight(func, arity - length, providedArgs);
|
|---|
| 32 | }
|
|---|
| 33 | if (this instanceof wrapper) {
|
|---|
| 34 | return new func(...providedArgs);
|
|---|
| 35 | }
|
|---|
| 36 | return func.apply(this, providedArgs);
|
|---|
| 37 | }
|
|---|
| 38 | wrapper.placeholder = curryRightPlaceholder;
|
|---|
| 39 | return wrapper;
|
|---|
| 40 | }
|
|---|
| 41 | function composeArgs(providedArgs, partialArgs) {
|
|---|
| 42 | const placeholderLength = partialArgs.filter(arg => arg === curryRight.placeholder).length;
|
|---|
| 43 | const rangeLength = Math.max(providedArgs.length - placeholderLength, 0);
|
|---|
| 44 | const args = [];
|
|---|
| 45 | let providedIndex = 0;
|
|---|
| 46 | for (let i = 0; i < rangeLength; i++) {
|
|---|
| 47 | args.push(providedArgs[providedIndex++]);
|
|---|
| 48 | }
|
|---|
| 49 | for (let i = 0; i < partialArgs.length; i++) {
|
|---|
| 50 | const arg = partialArgs[i];
|
|---|
| 51 | if (arg === curryRight.placeholder) {
|
|---|
| 52 | if (providedIndex < providedArgs.length) {
|
|---|
| 53 | args.push(providedArgs[providedIndex++]);
|
|---|
| 54 | }
|
|---|
| 55 | else {
|
|---|
| 56 | args.push(arg);
|
|---|
| 57 | }
|
|---|
| 58 | }
|
|---|
| 59 | else {
|
|---|
| 60 | args.push(arg);
|
|---|
| 61 | }
|
|---|
| 62 | }
|
|---|
| 63 | return args;
|
|---|
| 64 | }
|
|---|
| 65 | const curryRightPlaceholder = Symbol('curryRight.placeholder');
|
|---|
| 66 | curryRight.placeholder = curryRightPlaceholder;
|
|---|
| 67 |
|
|---|
| 68 | exports.curryRight = curryRight;
|
|---|