-
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.
feat: Add a shallow replaceKeys method
- Loading branch information
1 parent
29d90df
commit 618719e
Showing
3 changed files
with
66 additions
and
0 deletions.
There are no files selected for viewing
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,34 @@ | ||
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 } | ||
> | ||
> | ||
} | ||
|
||
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: { 'bizz-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 | ||
} |