Skip to content
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
2 changes: 0 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,6 @@ export * from './redactCredentialsInURL.js';
export * from './resultify.js';
export * from './secToString.js';
export * from './set.js';
export * from './sort-factory.js';
export * from './sort.js';
export * from './stripWhitespace.js';
export * from './suffix.js';
export * from './toPascalCase.js';
Expand Down
1 change: 1 addition & 0 deletions src/predicate/factory/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export * from './matching.js';
export * from './maybeArray.js';
export * from './nonNullable.js';
export * from './nullable.js';
export * from './nullish.js';
export * from './optional.js';
export * from './range.js';
export * from './shape.js';
Expand Down
7 changes: 7 additions & 0 deletions src/predicate/factory/nullish.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import type { Nullish } from '../../types/common.js';
import type { TypePredicateFn } from '../../types/functions.js';

export const nullish =
<T>(predicate: TypePredicateFn<T>): TypePredicateFn<Nullish<T>> =>
(input: unknown): input is Nullish<T> =>
input == null || predicate(input);
17 changes: 0 additions & 17 deletions src/sort.ts

This file was deleted.

1 change: 1 addition & 0 deletions src/sort/byLocaleCompare.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const byLocaleCompare = (left: string, right: string): number => left.localeCompare(right);
3 changes: 3 additions & 0 deletions src/sort/bySimpleComparison.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const bySimpleComparison = <T>(left: T, right: T): number => {
return left === right ? 0 : left > right ? 1 : -1;
};
4 changes: 4 additions & 0 deletions src/sort/bySubtraction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { parseNumber } from '../parseNumber.js';

export const bySubtraction = <T extends number | bigint | string>(left: T, right: T): number =>
parseNumber(left) - parseNumber(right);
7 changes: 7 additions & 0 deletions src/sort/byTimestamp.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { getMilliseconds } from '../getMilliseconds.js';

export const byTimestamp = <T extends string | number | Date>(left: T, right: T): number => {
const timeDiff = getMilliseconds(left) - getMilliseconds(right);

return Number.isNaN(timeDiff) ? -1 : timeDiff;
};
7 changes: 1 addition & 6 deletions src/sort-factory.ts → src/sort/factory/byProperty.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
import type { CompareFn } from './types/functions.js';
import type { CompareFn } from '../../types/functions.js';

export const byProperty =
<T extends object, K extends keyof T = keyof T>(compareFn: CompareFn<T[K]>, property: K): CompareFn<T> =>
(left, right) =>
compareFn(left[property], right[property]);

export const byReverseOf =
<T>(compareFn: CompareFn<T>): CompareFn<T> =>
(left, right) =>
0 - compareFn(left, right);
6 changes: 6 additions & 0 deletions src/sort/factory/byReverseOf.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import type { CompareFn } from '../../types/functions.js';

export const byReverseOf =
<T>(compareFn: CompareFn<T>): CompareFn<T> =>
(left, right) =>
0 - compareFn(left, right);
2 changes: 2 additions & 0 deletions src/types/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,5 @@ export type AnyAsyncFunction = (...args: any[]) => Promise<any>;
export type AnyNewable = {
new (...args: any[]): any;
};

export type Nullish<T> = T | null | undefined;
22 changes: 22 additions & 0 deletions test/predicate/factory/nullish.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { describe, it, expect, vi } from 'vitest';

import { nullish } from '../../../src/predicate/factory/nullish.js';
import { isBoolean } from '../../../src/predicate/isBoolean.js';

describe('nullish()', () => {
it('Returns a type predicate function', () => {
expect(nullish(isBoolean)).toBeInstanceOf(Function);
});

it('Calls a type predicate function', () => {
const predicate = vi.fn(isBoolean) as unknown as typeof isBoolean;

const fn = nullish(predicate);

expect(fn(true)).toBeTruthy();
expect(fn(false)).toBeTruthy();
expect(fn(null)).toBeTruthy();
expect(fn(undefined)).toBeTruthy();
expect(predicate).toHaveBeenCalledTimes(2);
});
});
49 changes: 0 additions & 49 deletions test/sort.test.ts

This file was deleted.

9 changes: 9 additions & 0 deletions test/sort/byLocaleCompare.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { describe, expect, it } from 'vitest';

import { byLocaleCompare } from '../../src/sort/byLocaleCompare.js';

describe('byLocaleCompare()', () => {
it('compares by string localeCompare', () => {
expect(['c', 'b', 'a'].sort(byLocaleCompare)).toEqual(['a', 'b', 'c']);
});
});
13 changes: 13 additions & 0 deletions test/sort/bySimpleComparison.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { describe, expect, it } from 'vitest';

import { bySimpleComparison } from '../../src/sort/bySimpleComparison.js';

describe('bySimpleComparison()', () => {
it('compares using operators', () => {
expect([1, 0, 3, 2].sort(bySimpleComparison)).toEqual([0, 1, 2, 3]);

expect([true, false, true].sort(bySimpleComparison)).toEqual([false, true, true]);

expect(['c', 'b', 1, 'a', 'x', 'z', 'y', 1].sort(bySimpleComparison)).toEqual(['a', 1, 1, 'b', 'c', 'x', 'y', 'z']);
});
});
11 changes: 11 additions & 0 deletions test/sort/bySubtraction.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { describe, expect, it } from 'vitest';

import { bySubtraction } from '../../src/sort/bySubtraction.js';

describe('bySubtraction()', () => {
it('compares using subtraction', () => {
expect([1, 0, 3, 2].sort(bySubtraction)).toEqual([0, 1, 2, 3]);

expect(['1', 0n, 3, 2].sort(bySubtraction)).toEqual([0n, '1', 2, 3]);
});
});
25 changes: 25 additions & 0 deletions test/sort/byTimestamp.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { describe, expect, it } from 'vitest';

import { byTimestamp } from '../../src/sort/byTimestamp.js';

describe('byTimestamp()', () => {
it('sorts by timestamp', () => {
const now = new Date();
const past = new Date();

past.setDate(past.getDate() - 10);

const data = [now, 0, past, now.toISOString(), past.toISOString(), 1];

expect(data.sort(byTimestamp)).toEqual([0, 1, past, past.toISOString(), now, now.toISOString()]);
});

it('Handles bad dates', () => {
const now = new Date();
const bad = Date.parse('not a date');

const data = [now, bad];

expect(data.sort(byTimestamp)).toEqual([bad, now]);
});
});
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { describe, expect, it } from 'vitest';

import { byProperty, byReverseOf } from '../src/sort-factory.js';
import { byLocaleCompare } from '../src/sort.js';
import { byLocaleCompare } from '../../../src/sort/byLocaleCompare.js';
import { byProperty } from '../../../src/sort/factory/byProperty.js';

describe('byProperty()', () => {
it('returns a CompareFn', () => {
Expand All @@ -28,13 +28,3 @@ describe('byProperty()', () => {
]);
});
});

describe('byReverseOf()', () => {
it('returns a CompareFn', () => {
expect(byReverseOf(byLocaleCompare)).instanceOf(Function);
});

it('Sorts using the returned CompareFn', () => {
expect(['a', 'b', 'c'].sort(byReverseOf(byLocaleCompare))).toEqual(['c', 'b', 'a']);
});
});
14 changes: 14 additions & 0 deletions test/sort/factory/byReverseOf.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { describe, expect, it } from 'vitest';

import { byLocaleCompare } from '../../../src/sort/byLocaleCompare.js';
import { byReverseOf } from '../../../src/sort/factory/byReverseOf.js';

describe('byReverseOf()', () => {
it('returns a CompareFn', () => {
expect(byReverseOf(byLocaleCompare)).instanceOf(Function);
});

it('Sorts using the returned CompareFn', () => {
expect(['a', 'b', 'c'].sort(byReverseOf(byLocaleCompare))).toEqual(['c', 'b', 'a']);
});
});