-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add intersect method: returns an intersection of two sequences.
- Loading branch information
1 parent
6db9320
commit c27b4cd
Showing
8 changed files
with
156 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { BaseLinqIterable } from "../base-linq-iterable"; | ||
import { defaultEqualityComparer, doneValue, iteratorResultCreator } from "../utils"; | ||
import { from } from "../creation"; | ||
import { WhereIterable } from "./where"; | ||
|
||
/** | ||
* Returns an intersect of two sequences, does not return duplicates | ||
*/ | ||
export class IntersectIterable extends BaseLinqIterable { | ||
constructor(source, other, comparer) { | ||
super(source); | ||
this.other = other; | ||
this.comparer = !!comparer ? comparer : defaultEqualityComparer; | ||
} | ||
|
||
__createContainingChecker() { | ||
class Checker { | ||
constructor(other, comparer) { | ||
this.other = from(other).distinct(comparer).toArray(); | ||
this.comparer = comparer; | ||
} | ||
|
||
has(value) { | ||
const index = from(this.other).firstIndex(item => this.comparer(value, item)); | ||
if (index > -1) { | ||
this.other.splice(index, 1); | ||
return true; | ||
} | ||
return false; | ||
} | ||
} | ||
return new Checker(this.other, this.comparer); | ||
} | ||
|
||
[Symbol.iterator]() { | ||
const checker = this.__createContainingChecker(this.other, this.comparer); | ||
const iterator = this._getSourceIterator(); | ||
return { | ||
next() { | ||
return WhereIterable.__findNext(iterator, (item) => checker.has(item)); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { expect } from 'chai'; | ||
import { from } from "../../src"; | ||
|
||
describe('intersect tests', () => { | ||
it('should do intersection of to sequences', () => { | ||
expect(from([1, 2, 2, 3]).intersect([2, 1, 5, 5]).toArray()).to.deep.equal([1, 2]); | ||
|
||
expect(from([ | ||
{ id: 1, x: 'a' }, | ||
{ id: 2, x: 'b' }, | ||
{ id: 3, x: 'c' }, | ||
]).intersect([ | ||
{ id: 4, x: 'a' }, | ||
{ id: 5, x: 'c' }, | ||
{ id: 2, x: 'b' }, | ||
{ id: 2, x: 'd' }, | ||
], (a, b) => a.id === b.id).toArray()).to.deep.equal([{ id: 2, x: 'b' }]); | ||
}); | ||
|
||
it('should return noting if second has no elements', () => { | ||
expect(from(new Set([1, 2, 3])).intersect([]).toArray()).to.deep.equal([]); | ||
}); | ||
|
||
it('should return noting if no matches', () => { | ||
expect(from(new Set([1, 2, 3])).intersect(new Set([4, 5, 6])).toArray()).to.deep.equal([]); | ||
expect(from(new Set([1, 2, 3])).intersect(new Set([4, 5, 6]), (a, b) => a === b).toArray()).to.deep.equal([]); | ||
}); | ||
}); | ||
|