Skip to content

Commit

Permalink
Merge pull request #7 from rpgeeganage/bug_in_fix_reduce
Browse files Browse the repository at this point in the history
fix bug in reduce right and reduce
  • Loading branch information
rpgeeganage authored Oct 25, 2018
2 parents 8f0a8d1 + 70f9ca1 commit a73e517
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 31 deletions.
9 changes: 5 additions & 4 deletions lib/methods/reduce.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,17 @@ export async function reduce<T, R>(
}

let reducedValue: T | R;
const copiedElements = [...elements];
let index = 0;

if (initialValue === undefined) {
reducedValue = copiedElements.shift() as T;
reducedValue = elements[0] as T;
index++;
} else {
reducedValue = initialValue;
}

for (const [index, element] of copiedElements.entries()) {
reducedValue = await cb(reducedValue, element, index, copiedElements);
for (; index < elements.length; index++) {
reducedValue = await cb(reducedValue, elements[index], index, elements);
}

return reducedValue;
Expand Down
9 changes: 5 additions & 4 deletions lib/methods/reduce_right.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,17 @@ export async function reduceRight<T, R>(
}

let reducedValue: T | R;
const reversedElements = [...elements].reverse();
let index = elements.length - 1;

if (initialValue === undefined) {
reducedValue = reversedElements.shift() as T;
reducedValue = elements[index] as T;
index--;
} else {
reducedValue = initialValue;
}

for (const [index, element] of reversedElements.entries()) {
reducedValue = await cb(reducedValue, element, index, reversedElements);
for (; index >= 0; index--) {
reducedValue = await cb(reducedValue, elements[index], index, elements);
}

return reducedValue;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"description": "Allow perform every, filter, find, findIndex, forEach, map, reduce, reduceRight and some on array using Async callback",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"version": "2.2.0",
"version": "2.2.1",
"scripts": {
"pretest": "npm run build",
"test": "mocha -r ts-node/register/type-check test/*.ts",
Expand Down
8 changes: 3 additions & 5 deletions test/find_index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,9 @@ describe('AsyncRay', () => {
});

it('should return -1 as index if array is empty', async () => {
const output: number = await AsyncRay([]).aFindIndex(
async () => {
return true;
}
);
const output: number = await AsyncRay([]).aFindIndex(async () => {
return true;
});

should(output).eql(-1);
});
Expand Down
23 changes: 6 additions & 17 deletions test/reduce_right.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,11 @@ describe('AsyncRay', () => {
describe('reduceRight', () => {
describe('with initial value', () => {
it('should return reduceRight values for number', async () => {
const reversedArray = [...inputArray].reverse();

const outputElement = await AsyncRay(inputArray).aReduceRight<number>(
async (acc, i, index, collection) => {
should(collection)
.instanceOf(Array)
.containDeepOrdered(reversedArray);
.containDeepOrdered(inputArray);
should(i)
.not.undefined()
.Number();
Expand All @@ -43,13 +41,12 @@ describe('AsyncRay', () => {

it('should return reduceRight values for string', async () => {
const inputString: string[] = ['s', 't', 'r', 'i', 'n', 'g'];
const reversedArray = [...inputString].reverse();

const outputElement = await AsyncRay(inputString).aReduceRight<string>(
async (acc, i, index, collection) => {
should(collection)
.instanceOf(Array)
.containDeepOrdered(reversedArray);
.containDeepOrdered(inputString);
should(i)
.not.undefined()
.String();
Expand All @@ -65,14 +62,13 @@ describe('AsyncRay', () => {

it('should return reduceRight values for array containing arrays', async () => {
const inputArrayOfArrays: [number, number][] = [[0, 1], [2, 3], [4, 5]];
const reversedArray = [...inputArrayOfArrays].reverse();

const outputElement = await AsyncRay(inputArrayOfArrays).aReduceRight<
number[]
>(async (acc, i, index, collection) => {
should(collection)
.instanceOf(Array)
.containDeepOrdered(reversedArray);
.containDeepOrdered(inputArrayOfArrays);
should(i)
.instanceOf(Array)
.containDeepOrdered(i);
Expand All @@ -87,14 +83,11 @@ describe('AsyncRay', () => {

describe('without initial value', () => {
it('should return reduceRight values for number', async () => {
const reversedArray = [...inputArray].reverse();
reversedArray.shift();

const outputElement = await AsyncRay(inputArray).aReduceRight<number>(
async (acc, i, index, collection) => {
should(collection)
.instanceOf(Array)
.containDeepOrdered(reversedArray);
.containDeepOrdered(inputArray);
should(i)
.not.undefined()
.Number();
Expand All @@ -109,14 +102,12 @@ describe('AsyncRay', () => {

it('should return reduceRight values for string', async () => {
const inputString: string[] = ['s', 't', 'r', 'i', 'n', 'g'];
const reversedArray = [...inputString].reverse();
reversedArray.shift();

const outputElement = await AsyncRay(inputString).aReduceRight<string>(
async (acc, i, index, collection) => {
should(collection)
.instanceOf(Array)
.containDeepOrdered(reversedArray);
.containDeepOrdered(inputString);
should(i)
.not.undefined()
.String();
Expand All @@ -131,15 +122,13 @@ describe('AsyncRay', () => {

it('should return reduceRight values for array containing arrays', async () => {
const inputArrayOfArrays: [number, number][] = [[0, 1], [2, 3], [4, 5]];
const reversedArray = [...inputArrayOfArrays].reverse();
reversedArray.shift();

const outputElement = await AsyncRay(inputArrayOfArrays).aReduceRight<
number[]
>(async (acc, i, index, collection) => {
should(collection)
.instanceOf(Array)
.containDeepOrdered(reversedArray);
.containDeepOrdered(inputArrayOfArrays);
should(i)
.instanceOf(Array)
.containDeepOrdered(i);
Expand Down

0 comments on commit a73e517

Please sign in to comment.