From 618719e90a8469d7b792c6b7cf4086af8b96d8ed Mon Sep 17 00:00:00 2001 From: Guga Guichard Date: Wed, 19 Jun 2024 13:44:18 -0300 Subject: [PATCH] feat: Add a shallow replaceKeys method --- src/index.ts | 3 ++ src/utils/object-keys/replace-keys.test.ts | 34 ++++++++++++++++++++++ src/utils/object-keys/replace-keys.ts | 29 ++++++++++++++++++ 3 files changed, 66 insertions(+) create mode 100644 src/utils/object-keys/replace-keys.test.ts create mode 100644 src/utils/object-keys/replace-keys.ts diff --git a/src/index.ts b/src/index.ts index 0e5acec..cf2047c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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' diff --git a/src/utils/object-keys/replace-keys.test.ts b/src/utils/object-keys/replace-keys.test.ts new file mode 100644 index 0000000..2b9f264 --- /dev/null +++ b/src/utils/object-keys/replace-keys.test.ts @@ -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> +}) diff --git a/src/utils/object-keys/replace-keys.ts b/src/utils/object-keys/replace-keys.ts new file mode 100644 index 0000000..3e7f624 --- /dev/null +++ b/src/utils/object-keys/replace-keys.ts @@ -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, 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 { + return transformKeys(obj, (s) => replace(s, lookup, replacement)) as never +}