Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
85 additions
and
0 deletions.
There are no files selected for viewing
3 changes: 3 additions & 0 deletions
3
projects/micro-dash-sizes/src/app/collection/shuffle.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 shuffle from 'lodash-es/shuffle'; | ||
|
||
console.log(shuffle([1, 2]), shuffle({ a: 1, b: 2 }), shuffle(null)); |
3 changes: 3 additions & 0 deletions
3
projects/micro-dash-sizes/src/app/collection/shuffle.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 { shuffle } from '@s-libs/micro-dash'; | ||
|
||
console.log(shuffle([1, 2]), shuffle({ a: 1, b: 2 }), shuffle(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,35 @@ | ||
import { sortBy, times, uniqBy } from 'lodash-es'; | ||
import { shuffle } from './shuffle'; | ||
|
||
describe('shuffle()', () => { | ||
it('accepts nil values for the collection, returning an empty array', () => { | ||
expect(shuffle(null)).toEqual([]); | ||
expect(shuffle(undefined)).toEqual([]); | ||
}); | ||
|
||
// | ||
// stolen from https://github.com/lodash/lodash | ||
// | ||
|
||
const array = [1, 2, 3]; | ||
const object = { a: 1, b: 2, c: 3 }; | ||
|
||
it('should return a new array', () => { | ||
const actual = shuffle(array); | ||
expect(actual).toEqual(jasmine.arrayWithExactContents(array)); | ||
expect(actual).not.toBe(array); | ||
}); | ||
|
||
it('should contain the same elements after a collection is shuffled', () => { | ||
expect(shuffle(array).sort()).toEqual(array); | ||
expect(shuffle(object).sort()).toEqual(array); | ||
}); | ||
|
||
it('should shuffle small collections', () => { | ||
const actual = times(1000, () => shuffle([1, 2])); | ||
expect(sortBy(uniqBy(actual, String), '0')).toEqual([ | ||
[1, 2], | ||
[2, 1], | ||
]); | ||
}); | ||
}); |
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,18 @@ | ||
import { Nil } from '../interfaces'; | ||
import { sampleSize } from './sample-size'; | ||
import { size } from './size'; | ||
|
||
/** | ||
* Creates an array of shuffled values, using a version of the [Fisher-Yates shuffle](https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle). | ||
* | ||
* Contribution to minified bundle size, when it is the only function imported: | ||
* - Lodash: 3,846 bytes | ||
* - Micro-dash: 842 bytes | ||
*/ | ||
|
||
export function shuffle<T>(array: T[] | Nil): T[]; | ||
export function shuffle<T>(object: T | Nil): Array<T[keyof T]>; | ||
|
||
export function shuffle(collection: any): any { | ||
return sampleSize(collection, size(collection)); | ||
} |
21 changes: 21 additions & 0 deletions
21
projects/micro-dash/src/typing-tests/collection/shuffle.dts-spec.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,21 @@ | ||
import { shuffle } from '../../lib/collection'; | ||
|
||
declare const array: number[]; | ||
declare const arrayOrNull: number[] | null; | ||
declare const arrayOrUndefined: number[] | undefined; | ||
declare const object: { a: number; b: string }; | ||
declare const objectOrNull: { a: number; b: string } | null; | ||
declare const objectOrUndefined: { a: number; b: string } | undefined; | ||
|
||
// $ExpectType number[] | ||
shuffle(array); | ||
// $ExpectType (string | number)[] | ||
shuffle(object); | ||
// $ExpectType number[] | ||
shuffle(arrayOrNull); | ||
// $ExpectType number[] | ||
shuffle(arrayOrUndefined); | ||
// $ExpectType (string | number)[] | ||
shuffle(objectOrNull); | ||
// $ExpectType (string | number)[] | ||
shuffle(objectOrUndefined); |