-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* docs: Fix typo in JSDocs * feat: Add a shallow replaceKeys method * Regenerate package-lock * test: Add some additional tests for non-literals * docs: Fix mistake in JSDoc
- Loading branch information
1 parent
38f8875
commit 5a29efa
Showing
16 changed files
with
1,345 additions
and
842 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { type ReplaceKeys, replaceKeys } from './replace-keys.js' | ||
|
||
namespace TypeTransforms { | ||
type test = Expect< | ||
Equal< | ||
ReplaceKeys< | ||
{ | ||
'some-value': { 'deep-nested': true } | ||
'other-value': true | ||
}, | ||
'some-', | ||
'' | ||
>, | ||
{ value: { 'deep-nested': true }; 'other-value': true } | ||
> | ||
> | ||
type testWithUnion = Expect< | ||
Equal< | ||
ReplaceKeys<Record<'foo' | 'bar', string>, 'oo', 'izz'>, | ||
Record<'fizz' | 'bar', string> | ||
> | ||
> | ||
type test2 = Expect< | ||
Equal< | ||
ReplaceKeys<Record<'some nice string', string>, RegExp, '-'>, | ||
Record<string, string> | ||
> | ||
> | ||
type test3 = Expect< | ||
Equal<ReplaceKeys<Record<string, string>, ' ', '-'>, Record<string, string>> | ||
> | ||
type test4 = Expect< | ||
Equal< | ||
ReplaceKeys<Record<Uppercase<string>, string>, ' ', '-'>, | ||
Record<string, string> | ||
> | ||
> | ||
type test5 = Expect< | ||
Equal< | ||
ReplaceKeys<Record<'some nice string', string>, string, '-'>, | ||
Record<string, string> | ||
> | ||
> | ||
type test6 = Expect< | ||
Equal< | ||
ReplaceKeys<Record<'some nice string', string>, ' ', string>, | ||
Record<string, string> | ||
> | ||
> | ||
} | ||
|
||
test('replaceKeys', () => { | ||
const expected = { | ||
some: { deepNested: { value: true } }, | ||
value: true, | ||
} | ||
const result = replaceKeys( | ||
{ | ||
some: { deepNested: { value: true } }, | ||
other_value: true, | ||
}, | ||
'other_', | ||
'', | ||
) | ||
expect(result).toEqual(expected) | ||
type test = Expect<Equal<typeof result, typeof expected>> | ||
}) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { transformKeys } from './transform-keys.js' | ||
import { type Replace, replace } from '../../native/replace.js' | ||
|
||
/** | ||
* Shallowly transforms the keys of a Record with `replace`. | ||
* T: the type of the Record to transform. | ||
*/ | ||
export type ReplaceKeys< | ||
T, | ||
lookup extends string | RegExp, | ||
replacement extends string = '', | ||
> = T extends [] | ||
? T | ||
: { [K in keyof T as Replace<Extract<K, string>, lookup, replacement>]: T[K] } | ||
/** | ||
* A strongly typed function that shallowly transforms the keys of an object by running the `replace` method in every key. The transformation is done both at runtime and type level. | ||
* @param obj the object to transform. | ||
* @param lookup the lookup string to be replaced. | ||
* @param replacement the replacement string. | ||
* @returns the transformed object. | ||
* @example replaceKeys({ 'foo-bar': { 'fizz-buzz': true } }, 'f', 'b') // { booBar: { 'fizz-buz': true } } | ||
*/ | ||
export function replaceKeys< | ||
T, | ||
S extends string | RegExp, | ||
R extends string = '', | ||
>(obj: T, lookup: S, replacement: R = '' as R): ReplaceKeys<T, S, R> { | ||
return transformKeys(obj, (s) => replace(s, lookup, replacement)) as never | ||
} |
This file contains 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