Created
June 1, 2021 21:01
-
-
Save monkpit/9b3e27e9e345cb83b0289e44fc84e993 to your computer and use it in GitHub Desktop.
Typed Immutable Sort
This file contains hidden or 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
export const immutableSort = <T>(array: T[], comparatorFn?: (a: T, b: T) => number): T[] => | |
[...array].sort(comparatorFn); | |
describe('immutableSort', () => { | |
const testData = [4, 2, 5, -1, 0]; | |
it('can sort an array', () => { | |
const result = immutableSort(testData); | |
expect(result).toStrictEqual([-1, 0, 2, 4, 5]); | |
}); | |
it('does not modify the original array', () => { | |
const _ = immutableSort(testData); | |
expect(testData).toStrictEqual([4, 2, 5, -1, 0]); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment