|
Last change
on this file since ba17441 was a762898, checked in by istevanoska <ilinastevanoska@…>, 5 months ago |
|
Added visualizations
|
-
Property mode
set to
100644
|
|
File size:
1.0 KB
|
| Line | |
|---|
| 1 | function partialRight(func, ...partialArgs) {
|
|---|
| 2 | return partialRightImpl(func, placeholderSymbol, ...partialArgs);
|
|---|
| 3 | }
|
|---|
| 4 | function partialRightImpl(func, placeholder, ...partialArgs) {
|
|---|
| 5 | const partialedRight = function (...providedArgs) {
|
|---|
| 6 | const placeholderLength = partialArgs.filter(arg => arg === placeholder).length;
|
|---|
| 7 | const rangeLength = Math.max(providedArgs.length - placeholderLength, 0);
|
|---|
| 8 | const remainingArgs = providedArgs.slice(0, rangeLength);
|
|---|
| 9 | let providedArgsIndex = rangeLength;
|
|---|
| 10 | const substitutedArgs = partialArgs
|
|---|
| 11 | .slice()
|
|---|
| 12 | .map(arg => (arg === placeholder ? providedArgs[providedArgsIndex++] : arg));
|
|---|
| 13 | return func.apply(this, remainingArgs.concat(substitutedArgs));
|
|---|
| 14 | };
|
|---|
| 15 | if (func.prototype) {
|
|---|
| 16 | partialedRight.prototype = Object.create(func.prototype);
|
|---|
| 17 | }
|
|---|
| 18 | return partialedRight;
|
|---|
| 19 | }
|
|---|
| 20 | const placeholderSymbol = Symbol('partialRight.placeholder');
|
|---|
| 21 | partialRight.placeholder = placeholderSymbol;
|
|---|
| 22 |
|
|---|
| 23 | export { partialRight, partialRightImpl };
|
|---|
Note:
See
TracBrowser
for help on using the repository browser.