Skip to content

Commit

Permalink
feat: Add a shallow replaceKeys method
Browse files Browse the repository at this point in the history
  • Loading branch information
gustavoguichard committed Jun 19, 2024
1 parent 29d90df commit 618719e
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ export type { PascalKeys } from './utils/object-keys/pascal-keys.js'
export { pascalKeys } from './utils/object-keys/pascal-keys.js'
export type { SnakeKeys } from './utils/object-keys/snake-keys.js'
export { snakeKeys } from './utils/object-keys/snake-keys.js'
// Object keys transformation
export type { ReplaceKeys } from './utils/object-keys/replace-keys.js'
export { replaceKeys } from './utils/object-keys/replace-keys.js'

// Object keys word casing (deep)
export type { DeepCamelKeys } from './utils/object-keys/deep-camel-keys.js'
Expand Down
34 changes: 34 additions & 0 deletions src/utils/object-keys/replace-keys.test.ts
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>>
})
29 changes: 29 additions & 0 deletions src/utils/object-keys/replace-keys.ts
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
}

0 comments on commit 618719e

Please sign in to comment.