From 3910b1b440d2056747619d5857b054e06d96108e Mon Sep 17 00:00:00 2001 From: gerneio Date: Fri, 11 Jan 2019 17:24:54 -0600 Subject: [PATCH 1/4] Aggregate Bug #79 Proposed fix for: https://github.com/ENikS/LINQ/issues/79 Untested with code in this form since I do not have a Node.js or Angular environment setup. However it was tested with alterations to the Browersified version (https://unpkg.com/linq-es2015@2.4.33/dist/linq.js). --- lib/linq.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/linq.ts b/lib/linq.ts index 13db3d8..de14f02 100644 --- a/lib/linq.ts +++ b/lib/linq.ts @@ -190,7 +190,9 @@ class EnumerableImpl implements Enumerable, Iterable, IEnumerable { } let result: A = zero; for (let value of this) { - if (!result) result = Constant.getDefaultVal(typeof (value)); + if ([null, undefined].indexOf(result) > -1 || (isNaN(result) && !result)) + result = Constant.getDefaultVal(typeof (value)); + result = method(result, value); } return selector(result); From 2f45ea41bfee743e06750c75bb8129304f59b007 Mon Sep 17 00:00:00 2001 From: gerneio Date: Fri, 11 Jan 2019 18:11:53 -0600 Subject: [PATCH 2/4] Update linq.ts TypeScript type checking --- lib/linq.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/linq.ts b/lib/linq.ts index de14f02..d03a911 100644 --- a/lib/linq.ts +++ b/lib/linq.ts @@ -190,7 +190,7 @@ class EnumerableImpl implements Enumerable, Iterable, IEnumerable { } let result: A = zero; for (let value of this) { - if ([null, undefined].indexOf(result) > -1 || (isNaN(result) && !result)) + if ([null, undefined].indexOf(result) > -1 || (isNaN(result as any) && !result)) result = Constant.getDefaultVal(typeof (value)); result = method(result, value); From 764190c3e05a32758ce00388aa627549fe35cdcb Mon Sep 17 00:00:00 2001 From: gerneio Date: Fri, 11 Jan 2019 18:39:56 -0600 Subject: [PATCH 3/4] Aggregate Test Cases Test cases for https://github.com/ENikS/LINQ/pull/80 on https://github.com/ENikS/LINQ/issues/79 --- test/immediate.ts | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/test/immediate.ts b/test/immediate.ts index 42b23da..3a24565 100644 --- a/test/immediate.ts +++ b/test/immediate.ts @@ -33,6 +33,14 @@ describe('Immediate Execution -', function () { it('Aggregate() - Transform', function () { assert.equal(1814400, Linq.From(simpleArray).Aggregate(1, (a, b) => a * b, o => o / 2)); }); + + it('Aggregate() - Default Value [String]', function () { + assert.equal("123", Linq.From([1, 2, 3]).Aggregate("", (c, n) => c.toString() + n.toString())); + }); + + it('Aggregate() - Default Value [Number]', function () { + assert.equal("0123", Linq.From(["1", "2", "3"]).Aggregate(0, (c, n) => c.toString() + n.toString())); + }); From 03696119fd188a663f5c5743e9764324debc5fba Mon Sep 17 00:00:00 2001 From: gerneio Date: Fri, 11 Jan 2019 18:51:01 -0600 Subject: [PATCH 4/4] Update immediate.ts Remove Number test case. Only applicable to browserfied code. --- test/immediate.ts | 5 ----- 1 file changed, 5 deletions(-) diff --git a/test/immediate.ts b/test/immediate.ts index 3a24565..6269c72 100644 --- a/test/immediate.ts +++ b/test/immediate.ts @@ -37,11 +37,6 @@ describe('Immediate Execution -', function () { it('Aggregate() - Default Value [String]', function () { assert.equal("123", Linq.From([1, 2, 3]).Aggregate("", (c, n) => c.toString() + n.toString())); }); - - it('Aggregate() - Default Value [Number]', function () { - assert.equal("0123", Linq.From(["1", "2", "3"]).Aggregate(0, (c, n) => c.toString() + n.toString())); - }); - // All