Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add makeNumberCompareFn #43

Merged
merged 2 commits into from
Jun 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions src/array.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
sortByArray,
toggleElement,
withoutIndex,
makeNumberCompareFn,
} from './array';
import { Maybe } from './types';

Expand Down Expand Up @@ -229,6 +230,59 @@ describe('makeStringCompareFn', () => {
});
});

describe('makeNumberCompareFn', () => {
type MaybeKeyNumber = { key?: Maybe<number> };

it('creates a compare function that sorts numbers', () => {
const compareFn = makeNumberCompareFn<MaybeKeyNumber>(
(record) => record.key,
);
const original: Array<MaybeKeyNumber> = [
{ key: 3 },
{ key: 0 },
{ key: undefined },
{ key: 1 },
{ key: null },
{ key: 2 },
{},
];
expect(original.sort(compareFn)).toEqual([
{ key: 0 },
{ key: undefined },
{ key: null },
{},
{ key: 1 },
{ key: 2 },
{ key: 3 },
]);
});

it('accepts different fallback values', () => {
const compareFn = makeNumberCompareFn<MaybeKeyNumber>(
(record) => record.key,
9001,
);
const original: Array<MaybeKeyNumber> = [
{ key: 3 },
{ key: 0 },
{ key: undefined },
{ key: 1 },
{ key: null },
{ key: 2 },
{},
];
expect(original.sort(compareFn)).toEqual([
{ key: 0 },
{ key: 1 },
{ key: 2 },
{ key: 3 },
{ key: undefined },
{ key: null },
{},
]);
});
});

describe('localeCompareStrings', () => {
it('sorts strings', () => {
const original: Array<string> = ['c', '', 'a', 'b'];
Expand Down
20 changes: 19 additions & 1 deletion src/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ export function sortByArray<T extends string | number>(
}

/**
* Takes a function that maps the values to sort and returns a compare function
* Takes a function that maps the values to sort to strings and returns a compare function
* using `String.prototype.localeCompare`, usable in `Array.toSorted` or similar APIs.
*
* null, undefined and the empty string are not distinguished and first in sort order.
Expand All @@ -109,6 +109,24 @@ export function makeStringCompareFn<TSortable>(
};
}

/**
* Takes a function that maps the values to sort to numbers and returns a compare function
* using subtraction, usable in `Array.toSorted` or similar APIs.
*
* null and undefined are coalesced to `fallbackValue`, by default 0, and thus not distinguished and first in sort order.
*/
export function makeNumberCompareFn<TSortable>(
map: (sortable: TSortable) => Maybe<number>,
fallbackValue: number = 0,
): (a: TSortable, b: TSortable) => number {
return (a, b) => {
const mappedA = map(a) ?? fallbackValue;
const mappedB = map(b) ?? fallbackValue;

return mappedA - mappedB;
};
}

/**
* Returns a compare function for values that are string, null or undefined,
* using `String.prototype.localeCompare`, usable in `Array.toSorted` or similar APIs.
Expand Down
Loading