-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(micro-dash): add
intersection()
Closes #48
- Loading branch information
Showing
6 changed files
with
82 additions
and
1 deletion.
There are no files selected for viewing
3 changes: 3 additions & 0 deletions
3
projects/micro-dash-sizes/src/app/array/intersection.lodash.ts
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,3 @@ | ||
import intersection from 'lodash-es/intersection'; | ||
|
||
console.log(intersection([1, 2], [2, 3], null)); |
3 changes: 3 additions & 0 deletions
3
projects/micro-dash-sizes/src/app/array/intersection.microdash.ts
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,3 @@ | ||
import { intersection } from '@s-libs/micro-dash'; | ||
|
||
console.log(intersection([1, 2], [2, 3], null)); |
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,61 @@ | ||
import { constant, range, times } from 'lodash-es'; | ||
import { intersection } from './intersection'; | ||
|
||
describe('intersection()', () => { | ||
it('works with null and undefined', () => { | ||
const array = [0, 1, null, 3]; | ||
expect(intersection(array, null)).toEqual([]); | ||
expect(intersection(array, undefined)).toEqual([]); | ||
expect(intersection(null, array)).toEqual([]); | ||
expect(intersection(undefined, array)).toEqual([]); | ||
expect(intersection(null)).toEqual([]); | ||
expect(intersection(undefined)).toEqual([]); | ||
}); | ||
|
||
// | ||
// stolen from https://github.com/lodash/lodash | ||
// | ||
|
||
it('should return the intersection of two arrays', () => { | ||
expect(intersection([2, 1], [2, 3])).toEqual([2]); | ||
}); | ||
|
||
it('should return the intersection of multiple arrays', () => { | ||
expect(intersection([2, 1, 2, 3], [3, 4], [3, 2])).toEqual([3]); | ||
}); | ||
|
||
it('should return an array of unique values', () => { | ||
expect(intersection([1, 1, 3, 2, 2], [5, 2, 2, 1, 4], [2, 1, 1])).toEqual([ | ||
1, 2, | ||
]); | ||
}); | ||
|
||
it('should work with a single array', () => { | ||
expect(intersection([1, 1, 3, 2, 2])).toEqual([1, 3, 2]); | ||
}); | ||
|
||
it('should match `NaN`', () => { | ||
expect(intersection([1, NaN, 3], [NaN, 5, NaN])).toEqual([NaN]); | ||
}); | ||
|
||
it('should work with large arrays of `NaN`', () => { | ||
const largeArray = times(200, () => NaN); | ||
expect(intersection([1, NaN, 3], largeArray)).toEqual([NaN]); | ||
}); | ||
|
||
it('should work with large arrays of objects', () => { | ||
const object = {}; | ||
const largeArray = times(200, constant(object)); | ||
|
||
expect(intersection([object], largeArray)).toEqual([object]); | ||
expect(intersection(range(200), [1])).toEqual([1]); | ||
}); | ||
|
||
it('should return an array', () => { | ||
const array = [1, 2, 3]; | ||
const actual = intersection(array); | ||
|
||
expect(actual).toEqual(jasmine.any(Array)); | ||
expect(actual).not.toBe(array); | ||
}); | ||
}); |
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,13 @@ | ||
import { Nil } from '../interfaces'; | ||
|
||
/** | ||
* Creates an array of unique values that are included in all given arrays using SameValueZero for equality comparisons. The order and references of result values are determined by the first array. | ||
* | ||
* Contribution to minified bundle size, when it is the only function imported: | ||
* - Lodash: 5,789 bytes | ||
* - Micro-dash: 115 bytes | ||
*/ | ||
export function intersection<T>(...arrays: Array<readonly T[] | Nil>): T[] { | ||
const sets = arrays.map((array) => new Set(array)); | ||
return [...sets[0]].filter((value) => sets.every((set) => set.has(value))); | ||
} |