Comparing version 2.1.1 to 2.2.0
@@ -612,3 +612,3 @@ /** | ||
/** | ||
* Shallowly transforms the keys of an Record to camelCase. | ||
* Shallowly transforms the keys of a Record to camelCase. | ||
* T: the type of the Record to transform. | ||
@@ -628,3 +628,3 @@ */ | ||
/** | ||
* Shallowly transforms the keys of an Record to CONSTANT_CASE. | ||
* Shallowly transforms the keys of a Record to CONSTANT_CASE. | ||
* T: the type of the Record to transform. | ||
@@ -644,3 +644,3 @@ */ | ||
/** | ||
* Shallowly transforms the keys of an Record to a custom delimiter case. | ||
* Shallowly transforms the keys of a Record to a custom delimiter case. | ||
* T: the type of the Record to transform. | ||
@@ -662,3 +662,3 @@ * D: the delimiter to use. | ||
/** | ||
* Shallowly transforms the keys of an Record to kebab-case. | ||
* Shallowly transforms the keys of a Record to kebab-case. | ||
* T: the type of the Record to transform. | ||
@@ -678,3 +678,3 @@ */ | ||
/** | ||
* Shallowly transforms the keys of an Record to PascalCase. | ||
* Shallowly transforms the keys of a Record to PascalCase. | ||
* T: the type of the Record to transform. | ||
@@ -694,3 +694,3 @@ */ | ||
/** | ||
* Shallowly transforms the keys of an Record to snake_case. | ||
* Shallowly transforms the keys of a Record to snake_case. | ||
* T: the type of the Record to transform. | ||
@@ -710,5 +710,22 @@ */ | ||
/** | ||
* Recursively transforms the keys of an Record to camelCase. | ||
* Shallowly transforms the keys of a Record with `replace`. | ||
* T: the type of the Record to transform. | ||
*/ | ||
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 } } | ||
*/ | ||
declare function replaceKeys<T, S extends string | RegExp, R extends string = ''>(obj: T, lookup: S, replacement?: R): ReplaceKeys<T, S, R>; | ||
/** | ||
* Recursively transforms the keys of a Record to camelCase. | ||
* T: the type of the Record to transform. | ||
*/ | ||
type DeepCamelKeys<T> = T extends [any, ...any] ? { | ||
@@ -728,3 +745,3 @@ [I in keyof T]: DeepCamelKeys<T[I]>; | ||
/** | ||
* Recursively transforms the keys of an Record to CONSTANT_CASE. | ||
* Recursively transforms the keys of a Record to CONSTANT_CASE. | ||
* T: the type of the Record to transform. | ||
@@ -746,3 +763,3 @@ */ | ||
/** | ||
* Recursively transforms the keys of an Record to a custom delimiter case. | ||
* Recursively transforms the keys of a Record to a custom delimiter case. | ||
* T: the type of the Record to transform. | ||
@@ -766,3 +783,3 @@ * D: the delimiter to use. | ||
/** | ||
* Recursively transforms the keys of an Record to kebab-case. | ||
* Recursively transforms the keys of a Record to kebab-case. | ||
* T: the type of the Record to transform. | ||
@@ -784,3 +801,3 @@ */ | ||
/** | ||
* Recursively transforms the keys of an Record to PascalCase. | ||
* Recursively transforms the keys of a Record to PascalCase. | ||
* T: the type of the Record to transform. | ||
@@ -802,3 +819,3 @@ */ | ||
/** | ||
* Recursively transforms the keys of an Record to snake_case. | ||
* Recursively transforms the keys of a Record to snake_case. | ||
* T: the type of the Record to transform. | ||
@@ -830,2 +847,2 @@ */ | ||
export { type CamelCase, type CamelKeys, type CharAt, type Concat, type ConstantCase, type ConstantKeys, type DeepCamelKeys, type DeepConstantKeys, type DeepDelimiterKeys, type DeepKebabKeys, type DeepPascalKeys, type DeepSnakeKeys, type DelimiterCase, type DelimiterKeys, type EndsWith, type Includes, type IsDigit, type IsLetter, type IsLower, type IsSeparator, type IsSpecial, type IsUpper, type Join, type KebabCase, type KebabKeys, type Length, type PadEnd, type PadStart, type PascalCase, type PascalKeys, type Repeat, type Replace, type ReplaceAll, type Reverse, type Slice, type SnakeCase, type SnakeKeys, type Split, type StartsWith, type TitleCase, type Trim, type TrimEnd, type TrimStart, type Truncate, type Words, camelCase, camelKeys, capitalize, charAt, concat, constantCase, constantKeys, deepCamelKeys, deepConstantKeys, deepDelimiterKeys, deepKebabKeys, deepPascalKeys, deepSnakeKeys, deepTransformKeys, delimiterCase, delimiterKeys, endsWith, includes, join, kebabCase, kebabKeys, length, lowerCase, padEnd, padStart, pascalCase, pascalKeys, repeat, replace, replaceAll, reverse, slice, snakeCase, snakeKeys, split, startsWith, titleCase, toCamelCase, toConstantCase, toDelimiterCase, toKebabCase, toLowerCase, toPascalCase, toSnakeCase, toTitleCase, toUpperCase, trim, trimEnd, trimStart, truncate, uncapitalize, upperCase, words }; | ||
export { type CamelCase, type CamelKeys, type CharAt, type Concat, type ConstantCase, type ConstantKeys, type DeepCamelKeys, type DeepConstantKeys, type DeepDelimiterKeys, type DeepKebabKeys, type DeepPascalKeys, type DeepSnakeKeys, type DelimiterCase, type DelimiterKeys, type EndsWith, type Includes, type IsDigit, type IsLetter, type IsLower, type IsSeparator, type IsSpecial, type IsUpper, type Join, type KebabCase, type KebabKeys, type Length, type PadEnd, type PadStart, type PascalCase, type PascalKeys, type Repeat, type Replace, type ReplaceAll, type ReplaceKeys, type Reverse, type Slice, type SnakeCase, type SnakeKeys, type Split, type StartsWith, type TitleCase, type Trim, type TrimEnd, type TrimStart, type Truncate, type Words, camelCase, camelKeys, capitalize, charAt, concat, constantCase, constantKeys, deepCamelKeys, deepConstantKeys, deepDelimiterKeys, deepKebabKeys, deepPascalKeys, deepSnakeKeys, deepTransformKeys, delimiterCase, delimiterKeys, endsWith, includes, join, kebabCase, kebabKeys, length, lowerCase, padEnd, padStart, pascalCase, pascalKeys, repeat, replace, replaceAll, replaceKeys, reverse, slice, snakeCase, snakeKeys, split, startsWith, titleCase, toCamelCase, toConstantCase, toDelimiterCase, toKebabCase, toLowerCase, toPascalCase, toSnakeCase, toTitleCase, toUpperCase, trim, trimEnd, trimStart, truncate, uncapitalize, upperCase, words }; |
@@ -109,6 +109,4 @@ 'use strict'; | ||
function truncate(sentence, length2, omission = "...") { | ||
if (length2 < 0) | ||
return omission; | ||
if (sentence.length <= length2) | ||
return sentence; | ||
if (length2 < 0) return omission; | ||
if (sentence.length <= length2) return sentence; | ||
return join([ | ||
@@ -230,4 +228,3 @@ sentence.slice(0, length2 - omission.length), | ||
function transformKeys(obj, transform) { | ||
if (typeOf(obj) !== "object") | ||
return obj; | ||
if (typeOf(obj) !== "object") return obj; | ||
const res = {}; | ||
@@ -270,6 +267,10 @@ for (const key in obj) { | ||
// src/utils/object-keys/replace-keys.ts | ||
function replaceKeys(obj, lookup, replacement = "") { | ||
return transformKeys(obj, (s) => replace(s, lookup, replacement)); | ||
} | ||
// src/utils/object-keys/deep-transform-keys.ts | ||
function deepTransformKeys(obj, transform) { | ||
if (!["object", "array"].includes(typeOf(obj))) | ||
return obj; | ||
if (!["object", "array"].includes(typeOf(obj))) return obj; | ||
if (Array.isArray(obj)) { | ||
@@ -345,2 +346,3 @@ return obj.map((x) => deepTransformKeys(x, transform)); | ||
exports.replaceAll = replaceAll; | ||
exports.replaceKeys = replaceKeys; | ||
exports.reverse = reverse; | ||
@@ -347,0 +349,0 @@ exports.slice = slice; |
{ | ||
"name": "string-ts", | ||
"version": "2.1.1", | ||
"version": "2.2.0", | ||
"description": "Strongly-typed string functions.", | ||
@@ -5,0 +5,0 @@ "main": "./dist/index.js", |
@@ -151,2 +151,3 @@ <p align="center"> | ||
- [pascalKeys](#pascalkeys) | ||
- [replaceKeys](#replacekeys) | ||
- [snakeKeys](#snakekeys) | ||
@@ -681,2 +682,18 @@ - [Strongly-typed deep transformation of objects](#strongly-typed-deep-transformation-of-objects) | ||
### replaceKeys | ||
This function shallowly transforms the keys of an object by applying [`replace`](#replace) to each of its keys at both runtime and type levels. | ||
```ts | ||
import { replaceKeys } from 'string-ts' | ||
const data = { | ||
helloWorld: { | ||
fooBar: 'baz', | ||
}, | ||
} as const | ||
const result = replaceKeys(data, 'o', 'a') | ||
// ^ { 'hellaWorld': { 'fooBar': 'baz' } } | ||
``` | ||
## Strongly-typed deep transformation of objects | ||
@@ -683,0 +700,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
128472
1372
988