Skip to content

Commit

Permalink
fix(reduceRightP): compensate for older versions of ramda
Browse files Browse the repository at this point in the history
Ref #114
  • Loading branch information
char0n committed Aug 14, 2017
1 parent 832b1ce commit 3e5b327
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions src/reduceRightP.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import { curryN, reduceRight, length } from 'ramda';
import { curryN, pipe, equals, reduceRight, length, concat } from 'ramda';

import isUndefined from './isUndefined';


// in older ramda versions the order of the arguments is flipped
const flipArgs = pipe(reduceRight(concat, ''), equals('ba'))(['a', 'b']);


/* eslint-disable max-len */
/**
* Given an `Iterable`(arrays are `Iterable`), or a promise of an `Iterable`,
Expand Down Expand Up @@ -72,16 +77,26 @@ const reduceRightP = curryN(3, (fn, acc, list) => {
}

return Promise.resolve(list).then((iterable) => {
const reducer = reduceRight((currentValueP, accP) =>
accP
const reducer = reduceRight((arg1, arg2) => {
let accP;
let currentValueP;

if (flipArgs) {
([accP, currentValueP] = [arg1, arg2]);
} else {
([accP, currentValueP] = [arg2, arg1]);
}

return accP
.then(previousValue => Promise.all([previousValue, currentValueP]))
.then(([previousValue, currentValue]) => {
if (isUndefined(previousValue) && listLength === 1) {
return currentValue;
}

return fn(currentValue, previousValue);
})
});
}
);

return reducer(originalAccP, iterable);
Expand Down

0 comments on commit 3e5b327

Please sign in to comment.