From 743238d0844ee6c35299a9e2df03599b69009417 Mon Sep 17 00:00:00 2001 From: Alec Larson <1925840+aleclarson@users.noreply.github.com> Date: Thu, 27 Jun 2024 17:34:09 -0400 Subject: [PATCH] chore: upgrade typescript and use --isolatedDeclarations (#39) --- .github/workflows/publish-beta.yml | 2 +- .vscode/settings.json | 2 + package.json | 2 +- pnpm-lock.yaml | 18 ++--- src/array/alphabetical.ts | 2 +- src/array/boil.ts | 5 +- src/array/first.ts | 9 +-- src/array/iterate.ts | 2 +- src/array/last.ts | 9 +-- src/array/merge.ts | 6 +- src/array/replace.ts | 5 ++ src/array/replaceOrAppend.ts | 2 +- src/array/select.ts | 10 +-- src/array/sort.ts | 2 +- src/array/toggle.ts | 2 +- src/async/tests/retry.test.ts | 4 +- src/curry/callable.ts | 2 +- src/curry/debounce.ts | 2 +- src/curry/partial.ts | 5 +- src/curry/throttle.ts | 2 +- src/object/listify.ts | 12 ++-- src/series/series.ts | 110 +++++++++++++++-------------- src/string/template.ts | 4 +- src/string/trim.ts | 5 +- tsconfig.json | 5 +- 25 files changed, 124 insertions(+), 105 deletions(-) diff --git a/.github/workflows/publish-beta.yml b/.github/workflows/publish-beta.yml index be23d1c0..c5d6b633 100644 --- a/.github/workflows/publish-beta.yml +++ b/.github/workflows/publish-beta.yml @@ -151,6 +151,6 @@ jobs: - name: Publish to JSR run: | - deno run -A jsr:@david/publish-on-tag@0.1.4 --allow-slow-types --allow-dirty + deno run -A jsr:@david/publish-on-tag@0.1.4 --allow-dirty env: GITHUB_REF: refs/tags/v${{ needs.prepare-version.outputs.version }} diff --git a/.vscode/settings.json b/.vscode/settings.json index 04729476..4f81324d 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -6,6 +6,8 @@ "source.organizeImports", "source.formatDocument" ], + "typescript.tsdk": "./node_modules/typescript/lib", + "typescript.enablePromptUseWorkspaceTsdk": true, "typescript.preferences.importModuleSpecifier": "non-relative", "typescript.preferences.autoImportFileExcludePatterns": [ "**/node_modules/**", diff --git a/package.json b/package.json index 2cbf37e3..70d17a2d 100644 --- a/package.json +++ b/package.json @@ -25,7 +25,7 @@ "@vitest/coverage-v8": "^1.6.0", "prettier": "^2.7.1", "tsup": "^8.1.0", - "typescript": "^4.8.4", + "typescript": "^5.5.2", "vitest": "^1.6.0" }, "packageManager": "pnpm@9.1.3", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 40b563ad..151f545a 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -22,10 +22,10 @@ importers: version: 2.8.8 tsup: specifier: ^8.1.0 - version: 8.1.0(postcss@8.4.38)(typescript@4.9.5) + version: 8.1.0(postcss@8.4.38)(typescript@5.5.2) typescript: - specifier: ^4.8.4 - version: 4.9.5 + specifier: ^5.5.2 + version: 5.5.2 vitest: specifier: ^1.6.0 version: 1.6.0(@types/node@20.14.8) @@ -1066,9 +1066,9 @@ packages: resolution: {integrity: sha512-0fr/mIH1dlO+x7TlcMy+bIDqKPsw/70tVyeHW787goQjhmqaZe10uwLujubK9q9Lg6Fiho1KUKDYz0Z7k7g5/g==} engines: {node: '>=4'} - typescript@4.9.5: - resolution: {integrity: sha512-1FXk9E2Hm+QzZQ7z+McJiHL4NW1F2EzMu9Nq9i3zAaGqibafqYwCVU6WyWAuyQRRzOlxou8xZSyXLEN8oKj24g==} - engines: {node: '>=4.2.0'} + typescript@5.5.2: + resolution: {integrity: sha512-NcRtPEOsPFFWjobJEtfihkLCZCXZt/os3zf8nTxjVH3RvTSxjrCamJpbExGvYOF+tFHc3pA65qpdwPbzjohhew==} + engines: {node: '>=14.17'} hasBin: true ufo@1.5.3: @@ -2079,7 +2079,7 @@ snapshots: ts-interface-checker@0.1.13: {} - tsup@8.1.0(postcss@8.4.38)(typescript@4.9.5): + tsup@8.1.0(postcss@8.4.38)(typescript@5.5.2): dependencies: bundle-require: 4.2.1(esbuild@0.21.5) cac: 6.7.14 @@ -2097,14 +2097,14 @@ snapshots: tree-kill: 1.2.2 optionalDependencies: postcss: 8.4.38 - typescript: 4.9.5 + typescript: 5.5.2 transitivePeerDependencies: - supports-color - ts-node type-detect@4.0.8: {} - typescript@4.9.5: {} + typescript@5.5.2: {} ufo@1.5.3: {} diff --git a/src/array/alphabetical.ts b/src/array/alphabetical.ts index 1c309d19..93ddd8f4 100644 --- a/src/array/alphabetical.ts +++ b/src/array/alphabetical.ts @@ -6,7 +6,7 @@ export function alphabetical( array: readonly T[], getter: (item: T) => string, dir: 'asc' | 'desc' = 'asc', -) { +): T[] { if (!array) { return [] } diff --git a/src/array/boil.ts b/src/array/boil.ts index fc5d1358..dc76e2f0 100644 --- a/src/array/boil.ts +++ b/src/array/boil.ts @@ -7,7 +7,10 @@ * boil([1, 2, 3, 0], (a, b) => a > b ? a : b) // 3 * ``` */ -export function boil(array: readonly T[], compareFunc: (a: T, b: T) => T) { +export function boil( + array: readonly T[], + compareFunc: (a: T, b: T) => T, +): T | null { if (!array || (array.length ?? 0) === 0) { return null } diff --git a/src/array/first.ts b/src/array/first.ts index 76b9606e..ec7d37c6 100644 --- a/src/array/first.ts +++ b/src/array/first.ts @@ -9,9 +9,10 @@ * // 0 * ``` */ -export function first( - array: readonly T[], - defaultValue: T | null | undefined = undefined, -) { +export function first(array: readonly T[]): T | undefined + +export function first(array: readonly T[], defaultValue: U): T | U + +export function first(array: readonly unknown[], defaultValue?: unknown) { return array?.length > 0 ? array[0] : defaultValue } diff --git a/src/array/iterate.ts b/src/array/iterate.ts index 2fffa500..a965eb02 100644 --- a/src/array/iterate.ts +++ b/src/array/iterate.ts @@ -14,7 +14,7 @@ export function iterate( count: number, func: (currentValue: T, iteration: number) => T, initValue: T, -) { +): T { let value = initValue for (let i = 1; i <= count; i++) { value = func(value, i) diff --git a/src/array/last.ts b/src/array/last.ts index 9653c28a..dbe75dd7 100644 --- a/src/array/last.ts +++ b/src/array/last.ts @@ -9,9 +9,10 @@ * // 0 * ``` */ -export function last( - array: readonly T[], - defaultValue: T | null | undefined = undefined, -) { +export function last(array: readonly T[]): T | undefined + +export function last(array: readonly T[], defaultValue: U): T | U + +export function last(array: readonly unknown[], defaultValue?: unknown) { return array?.length > 0 ? array[array.length - 1] : defaultValue } diff --git a/src/array/merge.ts b/src/array/merge.ts index d522ff0a..09f35393 100644 --- a/src/array/merge.ts +++ b/src/array/merge.ts @@ -16,18 +16,18 @@ export function merge( root: readonly T[], others: readonly T[], matcher: (item: T) => any, -) { +): T[] { if (!others && !root) { return [] } if (!others) { - return root + return [...root] } if (!root) { return [] } if (!matcher) { - return root + return [...root] } return root.reduce((acc, r) => { const matched = others.find(o => matcher(r) === matcher(o)) diff --git a/src/array/replace.ts b/src/array/replace.ts index d0057b5f..5887787a 100644 --- a/src/array/replace.ts +++ b/src/array/replace.ts @@ -1,6 +1,11 @@ /** * Replace an element in an array with a new item without modifying * the array and return the new value + * + * ```ts + * replace([1, 2, 3], 4, (n) => n === 2) + * // [1, 4, 3] + * ``` */ export function replace( list: readonly T[], diff --git a/src/array/replaceOrAppend.ts b/src/array/replaceOrAppend.ts index b7b444ad..b3009287 100644 --- a/src/array/replaceOrAppend.ts +++ b/src/array/replaceOrAppend.ts @@ -15,7 +15,7 @@ export function replaceOrAppend( list: readonly T[], newItem: T, match: (a: T, idx: number) => boolean, -) { +): T[] { if (!list && !newItem) { return [] } diff --git a/src/array/select.ts b/src/array/select.ts index 14211055..1ba9ece8 100644 --- a/src/array/select.ts +++ b/src/array/select.ts @@ -11,15 +11,15 @@ * // => [9, 16] * ``` */ -export function select( +export function select( array: readonly T[], - mapper: (item: T, index: number) => K, + mapper: (item: T, index: number) => U, condition?: (item: T, index: number) => boolean, -) { +): U[] { if (!array) { return [] } - let mapped: K + let mapped: U return array.reduce((acc, item, index) => { if (condition) { condition(item, index) && acc.push(mapper(item, index)) @@ -27,5 +27,5 @@ export function select( acc.push(mapped) } return acc - }, [] as K[]) + }, [] as U[]) } diff --git a/src/array/sort.ts b/src/array/sort.ts index 0f051ebe..256e1c10 100644 --- a/src/array/sort.ts +++ b/src/array/sort.ts @@ -17,7 +17,7 @@ export function sort( array: readonly T[], getter: (item: T) => number, desc = false, -) { +): T[] { if (!array) { return [] } diff --git a/src/array/toggle.ts b/src/array/toggle.ts index 88eb0a54..6b22415d 100644 --- a/src/array/toggle.ts +++ b/src/array/toggle.ts @@ -29,7 +29,7 @@ export function toggle( options?: { strategy?: 'prepend' | 'append' }, -) { +): T[] { if (!list && !item) { return [] } diff --git a/src/async/tests/retry.test.ts b/src/async/tests/retry.test.ts index 06c2d68a..168eb802 100644 --- a/src/async/tests/retry.test.ts +++ b/src/async/tests/retry.test.ts @@ -8,7 +8,7 @@ describe('_.retry', () => { vi.useFakeTimers({ shouldAdvanceTime: true }) }) test('returns result of given function', async () => { - const result = await _.retry(cast(null), async bail => { + const result = await _.retry(cast(null), async _bail => { return 'hello' }) expect(result).toBe('hello') @@ -21,7 +21,7 @@ describe('_.retry', () => { }) test('retries on failure', async () => { let failedOnce = false - const result = await _.retry(cast(null), async bail => { + const result = await _.retry(cast(null), async _bail => { if (!failedOnce) { failedOnce = true throw 'Failing for test' diff --git a/src/curry/callable.ts b/src/curry/callable.ts index 700cd0d0..f3aee488 100644 --- a/src/curry/callable.ts +++ b/src/curry/callable.ts @@ -24,6 +24,6 @@ export function callable< ;(target as any)[key] = value return true }, - apply: (target, self, args) => fn(Object.assign({}, target))(...args), + apply: (target, _, args) => fn(Object.assign({}, target))(...args), }) as unknown as TObj & TFunc } diff --git a/src/curry/debounce.ts b/src/curry/debounce.ts index 47bb11d7..47e263fc 100644 --- a/src/curry/debounce.ts +++ b/src/curry/debounce.ts @@ -29,7 +29,7 @@ export type DebounceFunction = { export function debounce( { delay }: { delay: number }, func: (...args: TArgs) => any, -) { +): DebounceFunction { let timer: unknown = undefined let active = true diff --git a/src/curry/partial.ts b/src/curry/partial.ts index d6dee9e3..b8f0dabe 100644 --- a/src/curry/partial.ts +++ b/src/curry/partial.ts @@ -30,7 +30,6 @@ type RemoveItemsInFront< export function partial, R>( fn: (...args: T) => R, ...args: TA -) { - return (...rest: RemoveItemsInFront) => - fn(...([...args, ...rest] as T)) +): (...rest: RemoveItemsInFront) => R { + return (...rest) => fn(...([...args, ...rest] as T)) } diff --git a/src/curry/throttle.ts b/src/curry/throttle.ts index ea659f7d..cc7c6244 100644 --- a/src/curry/throttle.ts +++ b/src/curry/throttle.ts @@ -16,7 +16,7 @@ export type ThrottledFunction = { export function throttle( { interval }: { interval: number }, func: (...args: TArgs) => any, -) { +): ThrottledFunction { let ready = true let timer: unknown = undefined diff --git a/src/object/listify.ts b/src/object/listify.ts index 683e6f5a..82d38477 100644 --- a/src/object/listify.ts +++ b/src/object/listify.ts @@ -1,10 +1,10 @@ /** * Convert an object to a list, mapping each entry into a list item */ -export function listify( - obj: Record, - toItem: (key: TKey, value: TValue) => KResult, -) { +export function listify( + obj: Record, + toItem: (key: Key, value: Value) => Item, +): Item[] { if (!obj) { return [] } @@ -13,7 +13,7 @@ export function listify( return [] } return entries.reduce((acc, entry) => { - acc.push(toItem(entry[0] as TKey, entry[1] as TValue)) + acc.push(toItem(entry[0] as Key, entry[1] as Value)) return acc - }, [] as KResult[]) + }, [] as Item[]) } diff --git a/src/series/series.ts b/src/series/series.ts index 25941863..84a2836a 100644 --- a/src/series/series.ts +++ b/src/series/series.ts @@ -1,5 +1,15 @@ import { list } from 'radashi' +export interface Series { + min: (a: T, b: T) => T + max: (a: T, b: T) => T + first: () => T + last: () => T + next: (current: T, defaultValue?: T) => T + previous: (current: T, defaultValue?: T) => T + spin: (current: T, num: number) => T +} + /** * Creates a series object around a list of values that should be * treated with order. @@ -7,7 +17,7 @@ import { list } from 'radashi' export const series = ( items: readonly T[], toKey: (item: T) => string | symbol = item => `${item}`, -) => { +): Series => { const { indexesByKey, itemsByIndex } = items.reduce( (acc, item, idx) => ({ indexesByKey: { @@ -24,78 +34,70 @@ export const series = ( itemsByIndex: {} as Record, }, ) - /** - * Given two values in the series, returns the value that occurs - * earlier in the series. - */ - const min = (a: T, b: T): T => { - return indexesByKey[toKey(a)] < indexesByKey[toKey(b)] ? a : b - } - /** - * Given two values in the series, returns the value that occurs - * later in the series. - */ - const max = (a: T, b: T): T => { - return indexesByKey[toKey(a)] > indexesByKey[toKey(b)] ? a : b - } + /** * Returns the first item from the series. */ - const first = (): T => { - return itemsByIndex[0] - } + const first = (): T => itemsByIndex[0] + /** * Returns the last item in the series. */ - const last = (): T => { - return itemsByIndex[items.length - 1] - } + const last = (): T => itemsByIndex[items.length - 1] + /** * Given an item in the series, returns the next item in the series * or `defaultValue` if the given value is the last item in the series. */ - const next = (current: T, defaultValue?: T): T => { - return ( - itemsByIndex[indexesByKey[toKey(current)] + 1] ?? defaultValue ?? first() - ) - } + const next = (current: T, defaultValue?: T): T => + itemsByIndex[indexesByKey[toKey(current)] + 1] ?? defaultValue ?? first() + /** * Given an item in the series, returns the previous item in the * series or `defaultValue` if the given value is the first item in * the series. */ - const previous = (current: T, defaultValue?: T): T => { - return ( - itemsByIndex[indexesByKey[toKey(current)] - 1] ?? defaultValue ?? last() - ) - } - /** - * A more dynamic method than `next` and `previous` that lets you move - * many times in either direction. - * - * ```ts - * series(weekdays).spin('wednesday', 3) // => 'monday' - * series(weekdays).spin('wednesday', -3) // => 'friday' - * ``` - */ - const spin = (current: T, num: number): T => { - if (num === 0) { - return current - } - const abs = Math.abs(num) - const rel = abs > items.length ? abs % items.length : abs - return list(0, rel - 1).reduce( - acc => (num > 0 ? next(acc) : previous(acc)), - current, - ) - } + const previous = (current: T, defaultValue?: T): T => + itemsByIndex[indexesByKey[toKey(current)] - 1] ?? defaultValue ?? last() + return { - min, - max, + /** + * Given two values in the series, returns the value that occurs + * earlier in the series. + */ + min(a, b) { + return indexesByKey[toKey(a)] < indexesByKey[toKey(b)] ? a : b + }, + /** + * Given two values in the series, returns the value that occurs + * later in the series. + */ + max(a, b) { + return indexesByKey[toKey(a)] > indexesByKey[toKey(b)] ? a : b + }, first, last, next, previous, - spin, + /** + * A more dynamic method than `next` and `previous` that lets you move + * many times in either direction. + * + * ```ts + * series(weekdays).spin('wednesday', 3) // => 'monday' + * series(weekdays).spin('wednesday', -3) // => 'friday' + * ``` + */ + spin(current, num) { + if (num === 0) { + return current + } + const abs = Math.abs(num) + const rel = abs > items.length ? abs % items.length : abs + return list(0, rel - 1).reduce( + acc => (num > 0 ? next(acc) : previous(acc)), + current, + ) + }, } } diff --git a/src/string/template.ts b/src/string/template.ts index b77ee4ae..a7f640ee 100644 --- a/src/string/template.ts +++ b/src/string/template.ts @@ -13,8 +13,8 @@ export function template( str: string, data: Record, - regex = /\{\{(.+?)\}\}/g, -) { + regex: RegExp = /\{\{(.+?)\}\}/g, +): string { return Array.from(str.matchAll(regex)).reduce((acc, match) => { return acc.replace(match[0], data[match[1]]) }, str) diff --git a/src/string/trim.ts b/src/string/trim.ts index 2d9cf92b..ad24dac1 100644 --- a/src/string/trim.ts +++ b/src/string/trim.ts @@ -10,7 +10,10 @@ * trim('222222__hello__1111111', '12_') // => 'hello' * ``` */ -export function trim(str: string | null | undefined, charsToTrim = ' ') { +export function trim( + str: string | null | undefined, + charsToTrim = ' ', +): string { if (!str) { return '' } diff --git a/tsconfig.json b/tsconfig.json index 786f3ac3..4ca7b181 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -3,7 +3,10 @@ "exclude": ["**/*.test.ts"], "compilerOptions": { "moduleResolution": "node", - "noEmit": true, + "outDir": "./dist/tmp", + "noEmitOnError": true, + "declaration": true, + "isolatedDeclarations": true, "target": "esnext", "lib": ["es2020"], "esModuleInterop": true,