Skip to content

Commit

Permalink
Merge 138b4ba into de60262
Browse files Browse the repository at this point in the history
  • Loading branch information
thiagosb13 committed Feb 3, 2017
2 parents de60262 + 138b4ba commit 0c801f7
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 15 deletions.
23 changes: 18 additions & 5 deletions src/linq-to-type.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
Array.prototype.first = function (expression) {

if (this.any()) {
return expression ? this.filter(expression)[0] : this[0]
}
Expand All @@ -12,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) {
Expand All @@ -35,13 +34,15 @@ Array.prototype.elementAt = function (index) {
if (this.any()) {
return this[index]
}

throw new TypeError("The source sequence is empty.")
}

Array.prototype.elementAtOrDefault = function (index) {
if (this.any()) {
return this[index]
}

throw new TypeError("The source sequence is empty.")
}

Expand Down Expand Up @@ -80,13 +81,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.")
}

Expand Down Expand Up @@ -137,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
}

Expand All @@ -148,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.")
}

Expand Down
34 changes: 24 additions & 10 deletions test/linq-to-type.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.')
})
})
})
Expand Down Expand Up @@ -319,14 +329,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.')
})
})
})
})
Expand All @@ -335,16 +350,15 @@ 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', () => {
let result = items.lastOrDefault()
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.')
Expand Down

0 comments on commit 0c801f7

Please sign in to comment.