From f7e7764c72d83f62a201fd3e9f9a2e2f8daa0241 Mon Sep 17 00:00:00 2001 From: Alexander Kurganov Date: Wed, 22 Nov 2023 21:17:49 +0400 Subject: [PATCH] chore: Refactoring --- __tests__/create-balanced-array.spec.ts | 7 +- __tests__/filter-by-same-key-value.test.ts | 2 +- __tests__/get-key-value.ts | 2 +- __tests__/get-unique-values.test.ts | 2 +- __tests__/is-sorted-by.test.ts | 2 +- __tests__/is-sorted-values.test.ts | 2 +- __tests__/sort-by.test.ts | 3 +- __tests__/split-by-key-value.test.ts | 2 +- src/compareValues.ts | 36 +++ src/constants.ts | 3 + ...lanced-array.ts => createBalancedArray.ts} | 6 +- src/filterBySameKeyValue.ts | 17 ++ src/getKeyValue.ts | 14 ++ src/getUniqueValues.ts | 18 ++ src/index.ts | 225 +----------------- src/isSortedBy.ts | 33 +++ src/isSortedValues.ts | 35 +++ src/sortBy.ts | 32 +++ src/splitByKeyValue.ts | 30 +++ src/types.ts | 3 + tsconfig.json | 3 +- 21 files changed, 248 insertions(+), 229 deletions(-) create mode 100644 src/compareValues.ts create mode 100644 src/constants.ts rename src/{create-balanced-array.ts => createBalancedArray.ts} (78%) create mode 100644 src/filterBySameKeyValue.ts create mode 100644 src/getKeyValue.ts create mode 100644 src/getUniqueValues.ts create mode 100644 src/isSortedBy.ts create mode 100644 src/isSortedValues.ts create mode 100644 src/sortBy.ts create mode 100644 src/splitByKeyValue.ts create mode 100644 src/types.ts diff --git a/__tests__/create-balanced-array.spec.ts b/__tests__/create-balanced-array.spec.ts index fcdb8c1..c345ae2 100644 --- a/__tests__/create-balanced-array.spec.ts +++ b/__tests__/create-balanced-array.spec.ts @@ -1,4 +1,4 @@ -import createBalancedArray from '../src/create-balanced-array' +import createBalancedArray from '../src/createBalancedArray' describe('createBalancedArray', () => { test('should be defined', () => { @@ -52,6 +52,11 @@ describe('createBalancedArray', () => { expect(createBalancedArray(3, -2)).toEqual([-1, -1, 0]) }) + test('should correctly handle large differences between length and sum', () => { + expect(createBalancedArray(13, 144)).toEqual([12, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11]) + expect(createBalancedArray(3, 487)).toEqual([163, 162, 162]) + }) + test('should maintain sum with large numbers', () => { const largeNumber = 1000000 const largeLength = 1000 diff --git a/__tests__/filter-by-same-key-value.test.ts b/__tests__/filter-by-same-key-value.test.ts index 0d14c82..1e75c75 100644 --- a/__tests__/filter-by-same-key-value.test.ts +++ b/__tests__/filter-by-same-key-value.test.ts @@ -1,4 +1,4 @@ -import { filterBySameKeyValue } from '../src' +import filterBySameKeyValue from '../src/filterBySameKeyValue' describe('filterBySameValue', () => { test('should filter by same value', () => { diff --git a/__tests__/get-key-value.ts b/__tests__/get-key-value.ts index 25c793d..9f676b1 100644 --- a/__tests__/get-key-value.ts +++ b/__tests__/get-key-value.ts @@ -1,4 +1,4 @@ -import { getKeyValue } from '../src' +import getKeyValue from '../src/getKeyValue' describe('getKeyValue', () => { test('getKeyValue', () => { diff --git a/__tests__/get-unique-values.test.ts b/__tests__/get-unique-values.test.ts index d557c2f..4440721 100644 --- a/__tests__/get-unique-values.test.ts +++ b/__tests__/get-unique-values.test.ts @@ -1,4 +1,4 @@ -import { getUniqueValues } from '../src' +import getUniqueValues from '../src/getUniqueValues' describe('getUniqueValues', () => { test('string', () => { diff --git a/__tests__/is-sorted-by.test.ts b/__tests__/is-sorted-by.test.ts index c4a4d79..366af15 100644 --- a/__tests__/is-sorted-by.test.ts +++ b/__tests__/is-sorted-by.test.ts @@ -1,4 +1,4 @@ -import { isSortedBy } from '../src' +import isSortedBy from '../src/isSortedBy' describe('isSortedBy', () => { test('empty', () => { diff --git a/__tests__/is-sorted-values.test.ts b/__tests__/is-sorted-values.test.ts index de3aaae..29ec577 100644 --- a/__tests__/is-sorted-values.test.ts +++ b/__tests__/is-sorted-values.test.ts @@ -1,4 +1,4 @@ -import { isSortedValues } from '../src' +import isSortedValues from '../src/isSortedValues' describe('isSortedValues', () => { test('empty', () => { diff --git a/__tests__/sort-by.test.ts b/__tests__/sort-by.test.ts index df192d3..075c60f 100644 --- a/__tests__/sort-by.test.ts +++ b/__tests__/sort-by.test.ts @@ -1,4 +1,5 @@ -import { sortBy, getKeyValue } from '../src' +import getKeyValue from '../src/getKeyValue' +import sortBy from '../src/sortBy' describe('sortBy', () => { test('empty', () => { diff --git a/__tests__/split-by-key-value.test.ts b/__tests__/split-by-key-value.test.ts index c2e0bf6..2c4060a 100644 --- a/__tests__/split-by-key-value.test.ts +++ b/__tests__/split-by-key-value.test.ts @@ -1,4 +1,4 @@ -import { splitByKeyValue } from '../src' +import splitByKeyValue from '../src/splitByKeyValue' describe('splitByKeyValue', () => { test('splits an array of objects into subarrays with the same value of the given key', () => { diff --git a/src/compareValues.ts b/src/compareValues.ts new file mode 100644 index 0000000..48f658a --- /dev/null +++ b/src/compareValues.ts @@ -0,0 +1,36 @@ +import { SortableOrder } from './types' +import { DEFAULT_ORDER } from './constants' + +/** + * Compares two values. + * @param {string | number} a The first value. + * @param {string | number} b The second value. + * @param {SortableOrder} [order='desc'] The order of sorting. + * @returns {number} The result of the comparison. + * + * This function compares two values `a` and `b`. Depending on the type of these values, the function uses different comparison approaches: + * - If the values are strings, the function uses the `localeCompare` method, which compares strings based on locale. This means that the strings are compared according to the sorting rules for the current language and region. + * - If the values are numbers, the function simply subtracts one number from another. This gives a negative number if `a` is less than `b`, a positive number if `a` is greater than `b`, and zero if `a` and `b` are equal. + * - If the values are of any other type, the function returns `0`. + * + * The function also takes into account the sorting order, which can be either 'asc' (ascending) or 'desc' (descending). If the sorting order is 'asc', the function returns the comparison result as is. If the sorting order is 'desc', the function returns the reverse comparison result. + * + * @example + * ['a', 'b'].sort((a, b) => compareValues(a, b, 'asc')); // Output: ['b', 'a'] + */ +export default function compareValues(a: T, b: T, order: SortableOrder = DEFAULT_ORDER): number { + const type = typeof a + + switch (type) { + case 'string': + return order === 'asc' + ? (a as string).localeCompare(b as string) + : (b as string).localeCompare(a as string) + case 'number': + return order === 'asc' + ? (a as number) - (b as number) + : (b as number) - (a as number) + default: + return 0 + } +} diff --git a/src/constants.ts b/src/constants.ts new file mode 100644 index 0000000..ba9da76 --- /dev/null +++ b/src/constants.ts @@ -0,0 +1,3 @@ +import { SortableOrder } from './types' + +export const DEFAULT_ORDER: SortableOrder = 'desc' diff --git a/src/create-balanced-array.ts b/src/createBalancedArray.ts similarity index 78% rename from src/create-balanced-array.ts rename to src/createBalancedArray.ts index 1af7c4e..b1a7463 100644 --- a/src/create-balanced-array.ts +++ b/src/createBalancedArray.ts @@ -1,9 +1,9 @@ /** * Creates an array of a specified length where the sum of all elements equals a given sum. * The function distributes the sum evenly across the array elements. - * If the sum is negative, the function creates an array of negative numbers. - * If the sum cannot be evenly distributed, the function distributes the remainder as evenly as possible. - * If the length is zero or negative, the function returns an empty array. + * - If the sum is negative, the function creates an array of negative numbers. + * - If the sum cannot be evenly distributed, the function distributes the remainder as evenly as possible. + * - If the length is zero or negative, the function returns an empty array. * * @param {number} length - The length of the array to be created. * @param {number} sum - The sum of all elements in the array. diff --git a/src/filterBySameKeyValue.ts b/src/filterBySameKeyValue.ts new file mode 100644 index 0000000..eb31c86 --- /dev/null +++ b/src/filterBySameKeyValue.ts @@ -0,0 +1,17 @@ +/** + * Filters an array of objects so that the value of a given key occurs only once in the array. + * @template T The type of the object. + * @param {T} value The current value. + * @param {number} index The index of the current value. + * @param {T[]} array The array of objects. + * @param {keyof T} key The key to filter by. + * @returns {boolean} Whether the value is the first occurrence of the key's value in the array. + * + * @example + * const array = [{ id: 1 }, { id: 2 }, { id: 1 }]; + * const filtered = array.filter((value, index, array) => filterBySameKeyValue(value, index, array, 'id')); + * // filtered: [{ id: 1 }, { id: 2 }] + */ +export default function filterBySameKeyValue(value: T, index: number, array: T[], key: keyof T): boolean { + return index === array.findIndex(v => v[key] === value[key]) +} diff --git a/src/getKeyValue.ts b/src/getKeyValue.ts new file mode 100644 index 0000000..08479fe --- /dev/null +++ b/src/getKeyValue.ts @@ -0,0 +1,14 @@ +/** + * Returns the array of values of a given key from an array of objects. + * @template T The type of the object. + * @param {T[]} arr The array of objects. + * @param {keyof T} key The key to get values by. + * @returns {T[keyof T][]} The array of values. + * + * @example + * const array = [{ id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }]; + * getKeyValue(array, 'name'); // Output: ['Alice', 'Bob'] + */ +export default function getKeyValue(arr: T[], key: keyof T): T[keyof T][] { + return arr.map(item => item[key]) +} diff --git a/src/getUniqueValues.ts b/src/getUniqueValues.ts new file mode 100644 index 0000000..afadeec --- /dev/null +++ b/src/getUniqueValues.ts @@ -0,0 +1,18 @@ +/** + * Returns an array of unique values from an array of objects. + * @template T The type of the object. + * @param {T[]} arr The array of objects. + * @param {keyof T} key The key to get unique values by. + * @returns {T[keyof T][]} The array of unique values. + * + * @example + * const array = [{ id: 1, name: 'Alice' }, { id: 2, name: 'Alice' }]; + * getUniqueValues(array, 'name'); // Output: ['Alice'] + */ +export default function getUniqueValues(arr: T[], key: keyof T): T[keyof T][] { + const uniqueValues = new Set() + + arr.forEach(item => uniqueValues.add(item[key])) + + return Array.from(uniqueValues) +} diff --git a/src/index.ts b/src/index.ts index d03e1d7..5e6b6a6 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,216 +1,9 @@ -import { isObject } from '@plq/is' - -export type Sortable = { [k in keyof T]: string | number | (() => string | number) } -export type SortableKey = T extends Sortable ? keyof T : never -export type SortableOrder = 'asc' | 'desc' - -const defaultOrder: SortableOrder = 'desc' - -/** - * Filters an array of objects so that the value of a given key occurs only once in the array. - * @template T The type of the object. - * @param {T} value The current value. - * @param {number} index The index of the current value. - * @param {T[]} array The array of objects. - * @param {keyof T} key The key to filter by. - * @returns {boolean} Whether the value is the first occurrence of the key's value in the array. - * - * @example - * const array = [{ id: 1 }, { id: 2 }, { id: 1 }]; - * const filtered = array.filter((value, index, array) => filterBySameKeyValue(value, index, array, 'id')); - * // filtered: [{ id: 1 }, { id: 2 }] - */ -export function filterBySameKeyValue(value: T, index: number, array: T[], key: keyof T): boolean { - return index === array.findIndex(v => v[key] === value[key]) -} - -/** - * Compares two values. - * @param {string | number} a The first value. - * @param {string | number} b The second value. - * @param {SortableOrder} [order='desc'] The order of sorting. - * @returns {number} The result of the comparison. - * - * This function compares two values `a` and `b`. Depending on the type of these values, the function uses different comparison approaches: - * - If the values are strings, the function uses the `localeCompare` method, which compares strings based on locale. This means that the strings are compared according to the sorting rules for the current language and region. - * - If the values are numbers, the function simply subtracts one number from another. This gives a negative number if `a` is less than `b`, a positive number if `a` is greater than `b`, and zero if `a` and `b` are equal. - * - If the values are of any other type, the function returns `0`. - * - * The function also takes into account the sorting order, which can be either 'asc' (ascending) or 'desc' (descending). If the sorting order is 'asc', the function returns the comparison result as is. If the sorting order is 'desc', the function returns the reverse comparison result. - * - * @example - * ['a', 'b'].sort((a, b) => compareValues(a, b, 'asc')); // Output: ['b', 'a'] - */ -export function compareValues(a: T, b: T, order: SortableOrder = defaultOrder): number { - const type = typeof a - - switch (type) { - case 'string': - return order === 'asc' - ? (a as string).localeCompare(b as string) - : (b as string).localeCompare(a as string) - case 'number': - return order === 'asc' - ? (a as number) - (b as number) - : (b as number) - (a as number) - default: - return 0 - } -} - -/** - * Sorts an array of objects by a given key. - * @template T The type of the object. - * @param {T[]} items The array of objects. - * @param {SortableKey} key The key to sort by. - * @param {SortableOrder} [order='desc'] The order of sorting. - * @returns {T[]} The sorted array. - * - * @example - * sortBy([{ id: 2 }, { id: 1 }], 'id', 'asc'); // Output: [{ id: 1 }, { id: 2 }] - */ -export function sortBy>(items: T[], key: SortableKey, order: SortableOrder = defaultOrder): T[] { - if (items.length <= 1) return items - - return items.sort((a, b) => { - if (typeof a[key] !== typeof b[key]) throw new Error(`Types are not equal (a: ${typeof a[key]}, b: ${typeof b[key]})`) - - let aValue = a[key] as string | number - let bValue = b[key] as string | number - - if (typeof aValue === 'function' && typeof bValue === 'function') { - aValue = (aValue as (() => string | number))() - bValue = (bValue as (() => string | number))() - } - - return compareValues(aValue, bValue, order) - }) -} - -/** - * Checks if an array of values is sorted. - * @template T The type of the values. - * @param {T[]} values The array of values. - * @param {SortableOrder} [order='desc'] The order of sorting. - * @returns {boolean} Whether the array is sorted. - * - * @example - * isSortedValues([1, 2, 3], 'asc'); // Output: true - */ -export function isSortedValues(values: T[], order: SortableOrder = defaultOrder): boolean { - if (values.length === 0) return true - - return values.every((value, index, valuesArray) => { - if (index === 0) return true - - if (typeof value !== typeof valuesArray[index - 1]) throw new Error(`Types are not equal (${valuesArray[index - 1]}: ${typeof valuesArray[index - 1]}, ${value}: ${typeof value})`) - - const prevValue = valuesArray[index - 1] - - switch (typeof value) { - case 'string': - return order === 'asc' - ? (value as string).localeCompare(prevValue as unknown as string) >= 0 - : (prevValue as unknown as string).localeCompare(value as string) >= 0 - case 'number': - return order === 'asc' - ? (value as number) >= (prevValue as unknown as number) - : (prevValue as unknown as number) >= (value as number) - } - }) -} - -/** - * Checks if an array of objects is sorted by a given key. - * @template T The type of the object. - * @param {T[]} array The array of objects. - * @param {SortableKey} key The key to check by. - * @param {SortableOrder} [order='desc'] The order of sorting. - * @returns {boolean} Whether the array is sorted by the key. - * - * @example - * isSortedBy([{ id: 1 }, { id: 2 }], 'id', 'asc'); // Output: true - */ -export function isSortedBy>(array: T[], key: SortableKey, order: SortableOrder = defaultOrder): boolean { - if (array.length <= 1) return true - - if (!isObject(array[0])) throw new Error(`Array is not an array of objects. (${typeof array[0]})`) - - const mapped = (array as Sortable[]) - .map(item => { - if (typeof item[key] === 'function') { - return (item[key] as (() => string | number))() as string | number - } else { - return item[key] as unknown as string | number - } - }) - - return isSortedValues(mapped, order) -} - -/** - * Returns the array of values of a given key from an array of objects. - * @template T The type of the object. - * @param {T[]} arr The array of objects. - * @param {keyof T} key The key to get values by. - * @returns {T[keyof T][]} The array of values. - * - * @example - * const array = [{ id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }]; - * getKeyValue(array, 'name'); // Output: ['Alice', 'Bob'] - */ -export function getKeyValue(arr: T[], key: keyof T): T[keyof T][] { - return arr.map(item => item[key]) -} - -/** - * Returns an array of unique values from an array of objects. - * @template T The type of the object. - * @param {T[]} arr The array of objects. - * @param {keyof T} key The key to get unique values by. - * @returns {T[keyof T][]} The array of unique values. - * - * @example - * const array = [{ id: 1, name: 'Alice' }, { id: 2, name: 'Alice' }]; - * getUniqueValues(array, 'name'); // Output: ['Alice'] - */ -export function getUniqueValues(arr: T[], key: keyof T): T[keyof T][] { - const uniqueValues = new Set() - - arr.forEach(item => uniqueValues.add(item[key])) - - return Array.from(uniqueValues) -} - -/** - * Splits an array of objects into subarrays with the same value of the given key. - * @template T The type of the object. - * @param {T[]} arr The array of objects. - * @param {keyof T} key The key to split by. - * @returns {T[][]} An array of subarrays, where each subarray contains objects with the same value for the given key. - * - * @example - * const array = [ - * { id: 1, name: 'Alice' }, - * { id: 2, name: 'Bob' }, - * { id: 3, name: 'Alice' }, - * ]; - * splitByKeyValue(array, 'name'); // Output: [[{ id: 1, name: 'Alice' }, { id: 3, name: 'Alice' }], [{ id: 2, name: 'Bob' }]] - */ -export function splitByKeyValue(arr: T[], key: keyof T): T[][] { - const groups: { [key: string]: T[] } = {} - - arr.forEach(item => { - const keyValue = String(item[key]) - - if (!groups[keyValue]) { - groups[keyValue] = [] - } - - groups[keyValue].push(item) - }) - - return Object.values(groups) -} - -export { default as createBalancedArray } from './create-balanced-array' +export { default as filterBySameKeyValue } from './filterBySameKeyValue' +export { default as sortBy } from './sortBy' +export { default as isSortedValues } from './isSortedValues' +export { default as isSortedBy } from './isSortedBy' +export { default as getKeyValue } from './getKeyValue' +export { default as getUniqueValues } from './getUniqueValues' +export { default as compareValues } from './compareValues' +export { default as splitByKeyValue } from './splitByKeyValue' +export { default as createBalancedArray } from './createBalancedArray' diff --git a/src/isSortedBy.ts b/src/isSortedBy.ts new file mode 100644 index 0000000..6904967 --- /dev/null +++ b/src/isSortedBy.ts @@ -0,0 +1,33 @@ +import { Sortable, SortableKey, SortableOrder } from './types' +import { DEFAULT_ORDER } from './constants' +import { isObject } from '@plq/is' + +import isSortedValues from './isSortedValues' + +/** + * Checks if an array of objects is sorted by a given key. + * @template T The type of the object. + * @param {T[]} array The array of objects. + * @param {SortableKey} key The key to check by. + * @param {SortableOrder} [order='desc'] The order of sorting. + * @returns {boolean} Whether the array is sorted by the key. + * + * @example + * isSortedBy([{ id: 1 }, { id: 2 }], 'id', 'asc'); // Output: true + */ +export default function isSortedBy>(array: T[], key: SortableKey, order: SortableOrder = DEFAULT_ORDER): boolean { + if (array.length <= 1) return true + + if (!isObject(array[0])) throw new Error(`Array is not an array of objects. (${typeof array[0]})`) + + const mapped = (array as Sortable[]) + .map(item => { + if (typeof item[key] === 'function') { + return (item[key] as (() => string | number))() as string | number + } else { + return item[key] as unknown as string | number + } + }) + + return isSortedValues(mapped, order) +} diff --git a/src/isSortedValues.ts b/src/isSortedValues.ts new file mode 100644 index 0000000..c91adfe --- /dev/null +++ b/src/isSortedValues.ts @@ -0,0 +1,35 @@ +import { SortableOrder } from './types' +import { DEFAULT_ORDER } from './constants' + +/** + * Checks if an array of values is sorted. + * @template T The type of the values. + * @param {T[]} values The array of values. + * @param {SortableOrder} [order='desc'] The order of sorting. + * @returns {boolean} Whether the array is sorted. + * + * @example + * isSortedValues([1, 2, 3], 'asc'); // Output: true + */ +export default function isSortedValues(values: T[], order: SortableOrder = DEFAULT_ORDER): boolean { + if (values.length === 0) return true + + return values.every((value, index, valuesArray) => { + if (index === 0) return true + + if (typeof value !== typeof valuesArray[index - 1]) throw new Error(`Types are not equal (${valuesArray[index - 1]}: ${typeof valuesArray[index - 1]}, ${value}: ${typeof value})`) + + const prevValue = valuesArray[index - 1] + + switch (typeof value) { + case 'string': + return order === 'asc' + ? (value as string).localeCompare(prevValue as unknown as string) >= 0 + : (prevValue as unknown as string).localeCompare(value as string) >= 0 + case 'number': + return order === 'asc' + ? (value as number) >= (prevValue as unknown as number) + : (prevValue as unknown as number) >= (value as number) + } + }) +} diff --git a/src/sortBy.ts b/src/sortBy.ts new file mode 100644 index 0000000..7413c1a --- /dev/null +++ b/src/sortBy.ts @@ -0,0 +1,32 @@ +import { Sortable, SortableKey, SortableOrder } from './types' +import { DEFAULT_ORDER } from './constants' +import compareValues from './compareValues' + +/** + * Sorts an array of objects by a given key. + * @template T The type of the object. + * @param {T[]} items The array of objects. + * @param {SortableKey} key The key to sort by. + * @param {SortableOrder} [order='desc'] The order of sorting. + * @returns {T[]} The sorted array. + * + * @example + * sortBy([{ id: 2 }, { id: 1 }], 'id', 'asc'); // Output: [{ id: 1 }, { id: 2 }] + */ +export default function sortBy>(items: T[], key: SortableKey, order: SortableOrder = DEFAULT_ORDER): T[] { + if (items.length <= 1) return items + + return items.sort((a, b) => { + if (typeof a[key] !== typeof b[key]) throw new Error(`Types are not equal (a: ${typeof a[key]}, b: ${typeof b[key]})`) + + let aValue = a[key] as string | number + let bValue = b[key] as string | number + + if (typeof aValue === 'function' && typeof bValue === 'function') { + aValue = (aValue as (() => string | number))() + bValue = (bValue as (() => string | number))() + } + + return compareValues(aValue, bValue, order) + }) +} diff --git a/src/splitByKeyValue.ts b/src/splitByKeyValue.ts new file mode 100644 index 0000000..fd705f1 --- /dev/null +++ b/src/splitByKeyValue.ts @@ -0,0 +1,30 @@ +/** + * Splits an array of objects into subarrays with the same value of the given key. + * @template T The type of the object. + * @param {T[]} arr The array of objects. + * @param {keyof T} key The key to split by. + * @returns {T[][]} An array of subarrays, where each subarray contains objects with the same value for the given key. + * + * @example + * const array = [ + * { id: 1, name: 'Alice' }, + * { id: 2, name: 'Bob' }, + * { id: 3, name: 'Alice' }, + * ]; + * splitByKeyValue(array, 'name'); // Output: [[{ id: 1, name: 'Alice' }, { id: 3, name: 'Alice' }], [{ id: 2, name: 'Bob' }]] + */ +export default function splitByKeyValue(arr: T[], key: keyof T): T[][] { + const groups: { [key: string]: T[] } = {} + + arr.forEach(item => { + const keyValue = String(item[key]) + + if (!groups[keyValue]) { + groups[keyValue] = [] + } + + groups[keyValue].push(item) + }) + + return Object.values(groups) +} diff --git a/src/types.ts b/src/types.ts new file mode 100644 index 0000000..4af0620 --- /dev/null +++ b/src/types.ts @@ -0,0 +1,3 @@ +export type Sortable = { [k in keyof T]: string | number | (() => string | number) } +export type SortableKey = T extends Sortable ? keyof T : never +export type SortableOrder = 'asc' | 'desc' diff --git a/tsconfig.json b/tsconfig.json index 46482c3..0884b1e 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -7,8 +7,7 @@ /* Modules */ "module": "node16", - "baseUrl": ".", - "rootDir": "./src", + "baseUrl": "./src", "moduleResolution": "node16", /* JavaScript Support */