From 789ba46cb8f8ddb4d60851785f4a96482289158e Mon Sep 17 00:00:00 2001 From: Thiago Silva Date: Thu, 2 Feb 2017 21:10:47 -0300 Subject: [PATCH 1/3] Issue #12 Fixed --- src/linq-to-type.ts | 15 ++++++++++++--- test/linq-to-type.spec.ts | 14 +++++++++----- 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/src/linq-to-type.ts b/src/linq-to-type.ts index 727b2c1..7661079 100644 --- a/src/linq-to-type.ts +++ b/src/linq-to-type.ts @@ -1,5 +1,4 @@ Array.prototype.first = function (expression) { - if (this.any()) { return expression ? this.filter(expression)[0] : this[0] } @@ -80,13 +79,23 @@ Array.prototype.groupBy = function (group, expression) { } Array.prototype.last = function (expression) { - return expression ? this.where(expression).last() : this[this.count() - 1] + if (this.any()) { + return expression ? this.where(expression).last() : this[this.count() - 1] + } + + throw new TypeError("The source sequence is empty.") } Array.prototype.lastOrDefault = function (expression) { if (this.any()) { - return expression ? this.where(expression).last() : this[this.count() - 1] + if (!expression) + return this[this.count() - 1] + + let filteredItems = this.where(expression); + + return filteredItems.any() ? this.where(expression).last() : undefined } + throw new TypeError("The source sequence is empty.") } diff --git a/test/linq-to-type.spec.ts b/test/linq-to-type.spec.ts index d26bb65..ee1eb7d 100644 --- a/test/linq-to-type.spec.ts +++ b/test/linq-to-type.spec.ts @@ -319,14 +319,19 @@ describe('Last test', () => { describe('Given an array', () => { describe('and requests the last item of the collection', () => { it('should return the last item that meets the expression', () => { - let result = items.last(x => x > 7) - expect(result).to.be.eq(8) + let result = items.last(x => x < 7) + expect(result).to.be.eq(6) }) it('should return the last item without an expression', () => { let result = items.last() expect(result).to.be.eq(8) }) + + it('should throws an exception if the collection is empty', () => { + let items = [] + expect(() => items.last()).to.throws(TypeError, 'The source sequence is empty.') + }) }) }) }) @@ -335,8 +340,8 @@ describe('LastOrDefault test', () => { describe('Given an array', () => { describe('and requests the last item of the collection', () => { it('should return the last item that meets the expression', () => { - let result = items.lastOrDefault(x => x > 7) - expect(result).to.be.eq(8) + let result = items.lastOrDefault(x => x < 7) + expect(result).to.be.eq(6) }) it('should return the last item without an expression', () => { @@ -344,7 +349,6 @@ describe('LastOrDefault test', () => { expect(result).to.be.eq(8) }) - it('should throws an exception if the collection is empty', () => { let items = [] expect(() => items.lastOrDefault()).to.throws(TypeError, 'The source sequence is empty.') From 8e1dc67f8633ebf8f75be646a2eb6b9448e1cf92 Mon Sep 17 00:00:00 2001 From: Thiago Silva Date: Thu, 2 Feb 2017 21:22:52 -0300 Subject: [PATCH 2/3] Making firstOrDefault analogous to lastOrDefault --- src/linq-to-type.ts | 2 +- test/linq-to-type.spec.ts | 20 +++++++++++++++----- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/src/linq-to-type.ts b/src/linq-to-type.ts index 7661079..6c05fbf 100644 --- a/src/linq-to-type.ts +++ b/src/linq-to-type.ts @@ -11,7 +11,7 @@ Array.prototype.firstOrDefault = function (expression) { return this.first(expression) } - return undefined + throw new TypeError("The source sequence is empty.") } Array.prototype.where = function (expression) { diff --git a/test/linq-to-type.spec.ts b/test/linq-to-type.spec.ts index ee1eb7d..2de9785 100644 --- a/test/linq-to-type.spec.ts +++ b/test/linq-to-type.spec.ts @@ -83,14 +83,24 @@ describe('First test', () => { describe('FirstOrDefault test', () => { describe('Given an array', () => { describe('and you are asked the first or default item in the collection', () => { - it('should return the first item', () => { - let result = items.firstOrDefault(x => x == 1) + it('should return the first item that meets the expression', () => { + let result = items.firstOrDefault(x => x > 1) + expect(result).to.be.eq(2) + }) + + it('sould return default value if not meet the expression', () => { + let result = items.firstOrDefault(x => x > 10) + expect(result).to.be.eq(undefined) + }) + + it('should return the first item without an expression', () => { + let result = items.firstOrDefault() expect(result).to.be.eq(1) }) - it('should return undefined if collection is empty', () => { + + it('should throws an exception if the collection is empty', () => { let items = [] - let result = items.firstOrDefault(x => x > 1) - expect(result).to.be.undefined + expect(() => items.firstOrDefault()).to.throws(TypeError, 'The source sequence is empty.') }) }) }) From 4ba0533ed65063e66b4778f9101acd296e9aa192 Mon Sep 17 00:00:00 2001 From: Thiago Silva Date: Thu, 2 Feb 2017 21:26:07 -0300 Subject: [PATCH 3/3] Formatting code --- src/linq-to-type.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/linq-to-type.ts b/src/linq-to-type.ts index 6c05fbf..7ed5311 100644 --- a/src/linq-to-type.ts +++ b/src/linq-to-type.ts @@ -34,6 +34,7 @@ Array.prototype.elementAt = function (index) { if (this.any()) { return this[index] } + throw new TypeError("The source sequence is empty.") } @@ -41,6 +42,7 @@ Array.prototype.elementAtOrDefault = function (index) { if (this.any()) { return this[index] } + throw new TypeError("The source sequence is empty.") } @@ -146,6 +148,7 @@ Array.prototype.zip = function (second, resultSelector) { const result = [] for (var i = 0; i < until; i++) result.push(resultSelector(this[i], second[i])) + return result } @@ -157,14 +160,15 @@ Array.prototype.intersect = function (source) { if (this.any() && source.any()) { return this.where(x => source.contains(x)) } + throw new TypeError("first or second is null.") } Array.prototype.groupJoin = function (inner, outerKeySelector, innerKeySelector, resultSelector) { - if (this.any() || inner.any()) { return this.select((x, y) => resultSelector(x, inner.where(a => outerKeySelector(x) === innerKeySelector(a)))); } + throw new TypeError("outer or inner or outerKeySelector or innerKeySelector or resultSelector is null.") }