From 933d74de911db439520f653bff51f3cc243b8c8a Mon Sep 17 00:00:00 2001 From: Guga Guichard Date: Tue, 10 Oct 2023 16:55:52 -0300 Subject: [PATCH] feat: Rename casing functions and deprecate old ones --- README.md | 56 +++++++++---------- src/index.ts | 38 +++++++------ src/utils/object-keys/camel-keys.ts | 4 +- src/utils/object-keys/constant-keys.ts | 7 +-- src/utils/object-keys/deep-camel-keys.ts | 4 +- src/utils/object-keys/deep-constant-keys.ts | 7 +-- src/utils/object-keys/deep-delimiter-keys.ts | 8 +-- src/utils/object-keys/deep-kebab-keys.ts | 4 +- src/utils/object-keys/deep-pascal-keys.ts | 4 +- src/utils/object-keys/deep-snake-keys.ts | 4 +- src/utils/object-keys/deep-transform-keys.ts | 2 +- src/utils/object-keys/delimiter-keys.ts | 6 +- src/utils/object-keys/kebab-keys.ts | 4 +- src/utils/object-keys/pascal-keys.ts | 4 +- src/utils/object-keys/snake-keys.ts | 4 +- src/utils/object-keys/transform-keys.ts | 2 +- ...-camel-case.test.ts => camel-case.test.ts} | 8 +-- src/utils/word-case/camel-case.ts | 24 ++++++++ ...ant-case.test.ts => constant-case.test.ts} | 8 +-- src/utils/word-case/constant-case.ts | 23 ++++++++ ...er-case.test.ts => delimiter-case.test.ts} | 8 +-- ...to-delimiter-case.ts => delimiter-case.ts} | 17 ++++-- ...-kebab-case.test.ts => kebab-case.test.ts} | 8 +-- src/utils/word-case/kebab-case.ts | 23 ++++++++ src/utils/word-case/lower-case.ts | 4 +- ...ascal-case.test.ts => pascal-case.test.ts} | 8 +-- src/utils/word-case/pascal-case.ts | 24 ++++++++ ...-snake-case.test.ts => snake-case.test.ts} | 8 +-- src/utils/word-case/snake-case.ts | 23 ++++++++ ...-title-case.test.ts => title-case.test.ts} | 8 +-- src/utils/word-case/title-case.ts | 23 ++++++++ src/utils/word-case/to-camel-case.ts | 17 ------ src/utils/word-case/to-constant-case.ts | 16 ------ src/utils/word-case/to-kebab-case.ts | 16 ------ src/utils/word-case/to-pascal-case.ts | 19 ------- src/utils/word-case/to-snake-case.ts | 16 ------ src/utils/word-case/to-title-case.ts | 16 ------ src/utils/word-case/upper-case.ts | 4 +- 38 files changed, 261 insertions(+), 218 deletions(-) rename src/utils/word-case/{to-camel-case.test.ts => camel-case.test.ts} (79%) create mode 100644 src/utils/word-case/camel-case.ts rename src/utils/word-case/{to-constant-case.test.ts => constant-case.test.ts} (80%) create mode 100644 src/utils/word-case/constant-case.ts rename src/utils/word-case/{to-delimiter-case.test.ts => delimiter-case.test.ts} (77%) rename src/utils/word-case/{to-delimiter-case.ts => delimiter-case.ts} (53%) rename src/utils/word-case/{to-kebab-case.test.ts => kebab-case.test.ts} (80%) create mode 100644 src/utils/word-case/kebab-case.ts rename src/utils/word-case/{to-pascal-case.test.ts => pascal-case.test.ts} (78%) create mode 100644 src/utils/word-case/pascal-case.ts rename src/utils/word-case/{to-snake-case.test.ts => snake-case.test.ts} (80%) create mode 100644 src/utils/word-case/snake-case.ts rename src/utils/word-case/{to-title-case.test.ts => title-case.test.ts} (80%) create mode 100644 src/utils/word-case/title-case.ts delete mode 100644 src/utils/word-case/to-camel-case.ts delete mode 100644 src/utils/word-case/to-constant-case.ts delete mode 100644 src/utils/word-case/to-kebab-case.ts delete mode 100644 src/utils/word-case/to-pascal-case.ts delete mode 100644 src/utils/word-case/to-snake-case.ts delete mode 100644 src/utils/word-case/to-title-case.ts diff --git a/README.md b/README.md index 53b61f3..43ccdc6 100644 --- a/README.md +++ b/README.md @@ -128,13 +128,13 @@ It also only work with common ASCII characters characters. We don't plan to supp - [trimEnd](#trimend) - [trimStart](#trimstart) - [Strongly-typed alternatives to common loosely-typed functions](#strongly-typed-alternatives-to-common-loosely-typed-functions) - - [toCamelCase](#tocamelcase) - - [toConstantCase](#toconstantcase) - - [toDelimiterCase](#todelimitercase) - - [toKebabCase](#tokebabcase) - - [toPascalCase](#topascalcase) - - [toSnakeCase](#tosnakecase) - - [toTitleCase](#totitlecase) + - [camelCase](#camelcase) + - [constantCase](#constantcase) + - [delimiterCase](#delimitercase) + - [kebabCase](#kebabcase) + - [pascalCase](#pascalcase) + - [snakeCase](#snakecase) + - [titleCase](#titlecase) - [truncate](#truncate) - [words](#words) - [Strongly-typed shallow transformation of objects](#strongly-typed-shallow-transformation-of-objects) @@ -441,87 +441,87 @@ const result = lowerCase(str) // ^ 'hello world' ``` -### toCamelCase +### camelCase This function converts a string to `camelCase` at both runtime and type levels. ```ts -import { toCamelCase } from 'string-ts' +import { camelCase } from 'string-ts' const str = 'hello-world' -const result = toCamelCase(str) +const result = camelCase(str) // ^ 'helloWorld' ``` -### toConstantCase +### constantCase This function converts a string to `CONSTANT_CASE` at both runtime and type levels. ```ts -import { toConstantCase } from 'string-ts' +import { constantCase } from 'string-ts' const str = 'helloWorld' -const result = toConstantCase(str) +const result = constantCase(str) // ^ 'HELLO_WORLD' ``` -### toDelimiterCase +### delimiterCase This function converts a string to a new case with a custom delimiter at both runtime and type levels. ```ts -import { toDelimiterCase } from 'string-ts' +import { delimiterCase } from 'string-ts' const str = 'helloWorld' -const result = toDelimiterCase(str, '.') +const result = delimiterCase(str, '.') // ^ 'hello.World' ``` -### toKebabCase +### kebabCase This function converts a string to `kebab-case` at both runtime and type levels. ```ts -import { toKebabCase } from 'string-ts' +import { kebabCase } from 'string-ts' const str = 'helloWorld' -const result = toKebabCase(str) +const result = kebabCase(str) // ^ 'hello-world' ``` -### toPascalCase +### pascalCase This function converts a string to `PascalCase` at both runtime and type levels. ```ts -import { toPascalCase } from 'string-ts' +import { pascalCase } from 'string-ts' const str = 'hello-world' -const result = toPascalCase(str) +const result = pascalCase(str) // ^ 'HelloWorld' ``` -### toSnakeCase +### snakeCase This function converts a string to `snake_case` at both runtime and type levels. ```ts -import { toSnakeCase } from 'string-ts' +import { snakeCase } from 'string-ts' const str = 'helloWorld' -const result = toSnakeCase(str) +const result = snakeCase(str) // ^ 'hello_world' ``` -### toTitleCase +### titleCase This function converts a string to `Title Case` at both runtime and type levels. ```ts -import { toTitleCase } from 'string-ts' +import { titleCase } from 'string-ts' const str = 'helloWorld' -const result = toTitleCase(str) +const result = titleCase(str) // ^ 'Hello World' ``` diff --git a/src/index.ts b/src/index.ts index 54a08f9..7876626 100644 --- a/src/index.ts +++ b/src/index.ts @@ -31,6 +31,8 @@ export { replaceAll } from './native/replace-all.js' export { slice } from './native/slice.js' export { split } from './native/split.js' export { startsWith } from './native/starts-with.js' +export { toLowerCase } from './native/to-lower-case.js' +export { toUpperCase } from './native/to-upper-case.js' export { trimStart } from './native/trim-start.js' export { trimEnd } from './native/trim-end.js' @@ -48,25 +50,29 @@ export type { IsSpecial } from './utils/characters/special.js' export type { Separator, IsSeparator } from './utils/characters/separators.js' // Word casing -export type { CamelCase } from './utils/word-case/to-camel-case.js' -export type { ConstantCase } from './utils/word-case/to-constant-case.js' -export type { DelimiterCase } from './utils/word-case/to-delimiter-case.js' -export type { KebabCase } from './utils/word-case/to-kebab-case.js' -export type { PascalCase } from './utils/word-case/to-pascal-case.js' -export type { SnakeCase } from './utils/word-case/to-snake-case.js' -export type { TitleCase } from './utils/word-case/to-title-case.js' +export type { CamelCase } from './utils/word-case/camel-case.js' +export type { ConstantCase } from './utils/word-case/constant-case.js' +export type { DelimiterCase } from './utils/word-case/delimiter-case.js' +export type { KebabCase } from './utils/word-case/kebab-case.js' +export type { PascalCase } from './utils/word-case/pascal-case.js' +export type { SnakeCase } from './utils/word-case/snake-case.js' +export type { TitleCase } from './utils/word-case/title-case.js' export { capitalize } from './utils/capitalize.js' export { lowerCase } from './utils/word-case/lower-case.js' -export { toCamelCase } from './utils/word-case/to-camel-case.js' -export { toConstantCase } from './utils/word-case/to-constant-case.js' -export { toDelimiterCase } from './utils/word-case/to-delimiter-case.js' -export { toKebabCase } from './utils/word-case/to-kebab-case.js' -export { toLowerCase } from './native/to-lower-case.js' -export { toPascalCase } from './utils/word-case/to-pascal-case.js' -export { toSnakeCase } from './utils/word-case/to-snake-case.js' -export { toTitleCase } from './utils/word-case/to-title-case.js' -export { toUpperCase } from './native/to-upper-case.js' +export { camelCase, toCamelCase } from './utils/word-case/camel-case.js' +export { + constantCase, + toConstantCase, +} from './utils/word-case/constant-case.js' +export { + delimiterCase, + toDelimiterCase, +} from './utils/word-case/delimiter-case.js' +export { kebabCase, toKebabCase } from './utils/word-case/kebab-case.js' +export { pascalCase, toPascalCase } from './utils/word-case/pascal-case.js' +export { snakeCase, toSnakeCase } from './utils/word-case/snake-case.js' +export { titleCase, toTitleCase } from './utils/word-case/title-case.js' export { uncapitalize } from './utils/uncapitalize.js' export { upperCase } from './utils/word-case/upper-case.js' diff --git a/src/utils/object-keys/camel-keys.ts b/src/utils/object-keys/camel-keys.ts index 9b3dd7f..1fbd909 100644 --- a/src/utils/object-keys/camel-keys.ts +++ b/src/utils/object-keys/camel-keys.ts @@ -1,5 +1,5 @@ import { transformKeys } from './transform-keys.js' -import { type CamelCase, toCamelCase } from '../word-case/to-camel-case.js' +import { type CamelCase, camelCase } from '../word-case/camel-case.js' /** * Shallowly transforms the keys of an Record to camelCase. @@ -15,5 +15,5 @@ export type CamelKeys = T extends [] * @example camelKeys({ 'foo-bar': { 'fizz-buzz': true } }) // { fooBar: { 'fizz-buz': true } } */ export function camelKeys(obj: T): CamelKeys { - return transformKeys(obj, toCamelCase) as never + return transformKeys(obj, camelCase) as never } diff --git a/src/utils/object-keys/constant-keys.ts b/src/utils/object-keys/constant-keys.ts index bb7ffa1..197c548 100644 --- a/src/utils/object-keys/constant-keys.ts +++ b/src/utils/object-keys/constant-keys.ts @@ -1,8 +1,5 @@ import { transformKeys } from './transform-keys.js' -import { - type ConstantCase, - toConstantCase, -} from '../word-case/to-constant-case.js' +import { type ConstantCase, constantCase } from '../word-case/constant-case.js' /** * Shallowly transforms the keys of an Record to CONSTANT_CASE. @@ -18,5 +15,5 @@ export type ConstantKeys = T extends [] * @example constantKeys({ 'foo-bar': { 'fizz-buzz': true } }) // { FOO_BAR: { 'fizz-buzz': true } } */ export function constantKeys(obj: T): ConstantKeys { - return transformKeys(obj, toConstantCase) as never + return transformKeys(obj, constantCase) as never } diff --git a/src/utils/object-keys/deep-camel-keys.ts b/src/utils/object-keys/deep-camel-keys.ts index ab911c7..ef93bc4 100644 --- a/src/utils/object-keys/deep-camel-keys.ts +++ b/src/utils/object-keys/deep-camel-keys.ts @@ -1,4 +1,4 @@ -import { type CamelCase, toCamelCase } from '../word-case/to-camel-case.js' +import { type CamelCase, camelCase } from '../word-case/camel-case.js' import { deepTransformKeys } from './deep-transform-keys.js' /** @@ -19,5 +19,5 @@ export type DeepCamelKeys = T extends [any, ...any] * @example deepCamelKeys({ 'foo-bar': { 'fizz-buzz': true } }) // { fooBar: { fizzBuzz: true } } */ export function deepCamelKeys(obj: T): DeepCamelKeys { - return deepTransformKeys(obj, toCamelCase) as never + return deepTransformKeys(obj, camelCase) as never } diff --git a/src/utils/object-keys/deep-constant-keys.ts b/src/utils/object-keys/deep-constant-keys.ts index c45d49c..57d6ef2 100644 --- a/src/utils/object-keys/deep-constant-keys.ts +++ b/src/utils/object-keys/deep-constant-keys.ts @@ -1,7 +1,4 @@ -import { - type ConstantCase, - toConstantCase, -} from '../word-case/to-constant-case.js' +import { type ConstantCase, constantCase } from '../word-case/constant-case.js' import { deepTransformKeys } from './deep-transform-keys.js' /** @@ -22,5 +19,5 @@ export type DeepConstantKeys = T extends [any, ...any] * @example deepConstantKeys({ 'foo-bar': { 'fizz-buzz': true } }) // { FOO_BAR: { FIZZ_BUZZ: true } } */ export function deepConstantKeys(obj: T): DeepConstantKeys { - return deepTransformKeys(obj, toConstantCase) as never + return deepTransformKeys(obj, constantCase) as never } diff --git a/src/utils/object-keys/deep-delimiter-keys.ts b/src/utils/object-keys/deep-delimiter-keys.ts index 2f99c07..438add2 100644 --- a/src/utils/object-keys/deep-delimiter-keys.ts +++ b/src/utils/object-keys/deep-delimiter-keys.ts @@ -1,7 +1,7 @@ import { type DelimiterCase, - toDelimiterCase, -} from '../word-case/to-delimiter-case.js' + delimiterCase, +} from '../word-case/delimiter-case.js' import { deepTransformKeys } from './deep-transform-keys.js' /** @@ -30,7 +30,5 @@ export function deepDelimiterKeys( obj: T, delimiter: D, ): DeepDelimiterKeys { - return deepTransformKeys(obj, (str) => - toDelimiterCase(str, delimiter), - ) as never + return deepTransformKeys(obj, (str) => delimiterCase(str, delimiter)) as never } diff --git a/src/utils/object-keys/deep-kebab-keys.ts b/src/utils/object-keys/deep-kebab-keys.ts index 790524a..402a8b9 100644 --- a/src/utils/object-keys/deep-kebab-keys.ts +++ b/src/utils/object-keys/deep-kebab-keys.ts @@ -1,4 +1,4 @@ -import { type KebabCase, toKebabCase } from '../word-case/to-kebab-case.js' +import { type KebabCase, kebabCase } from '../word-case/kebab-case.js' import { deepTransformKeys } from './deep-transform-keys.js' /** @@ -19,5 +19,5 @@ export type DeepKebabKeys = T extends [any, ...any] * @example deepKebabKeys({ 'foo-bar': { 'fizz-buzz': true } }) // { 'foo-bar': { 'fizz-buzz': true } } */ export function deepKebabKeys(obj: T): DeepKebabKeys { - return deepTransformKeys(obj, toKebabCase) as never + return deepTransformKeys(obj, kebabCase) as never } diff --git a/src/utils/object-keys/deep-pascal-keys.ts b/src/utils/object-keys/deep-pascal-keys.ts index 12f3c50..cc3036d 100644 --- a/src/utils/object-keys/deep-pascal-keys.ts +++ b/src/utils/object-keys/deep-pascal-keys.ts @@ -1,4 +1,4 @@ -import { type PascalCase, toPascalCase } from '../word-case/to-pascal-case.js' +import { type PascalCase, pascalCase } from '../word-case/pascal-case.js' import { deepTransformKeys } from './deep-transform-keys.js' /** @@ -19,5 +19,5 @@ export type DeepPascalKeys = T extends [any, ...any] * @example deepPascalKeys({ 'foo-bar': { 'fizz-buzz': true } }) // { FooBar: { FizzBuzz: true } } */ export function deepPascalKeys(obj: T): DeepPascalKeys { - return deepTransformKeys(obj, toPascalCase) as never + return deepTransformKeys(obj, pascalCase) as never } diff --git a/src/utils/object-keys/deep-snake-keys.ts b/src/utils/object-keys/deep-snake-keys.ts index 91ee0db..fb416e1 100644 --- a/src/utils/object-keys/deep-snake-keys.ts +++ b/src/utils/object-keys/deep-snake-keys.ts @@ -1,4 +1,4 @@ -import { type SnakeCase, toSnakeCase } from '../word-case/to-snake-case.js' +import { type SnakeCase, snakeCase } from '../word-case/snake-case.js' import { deepTransformKeys } from './deep-transform-keys.js' /** @@ -19,5 +19,5 @@ export type DeepSnakeKeys = T extends [any, ...any] * @example deepSnakeKeys({ 'foo-bar': { 'fizz-buzz': true } }) // { 'foo_bar': { 'fizz_buzz': true } } */ export function deepSnakeKeys(obj: T): DeepSnakeKeys { - return deepTransformKeys(obj, toSnakeCase) as never + return deepTransformKeys(obj, snakeCase) as never } diff --git a/src/utils/object-keys/deep-transform-keys.ts b/src/utils/object-keys/deep-transform-keys.ts index 8bda552..4c534ff 100644 --- a/src/utils/object-keys/deep-transform-keys.ts +++ b/src/utils/object-keys/deep-transform-keys.ts @@ -6,7 +6,7 @@ import { typeOf } from '../../internal/internals.js' * @param obj the object to transform. * @param transform the function to transform the keys from string to string. * @returns the transformed object. - * @example deepTransformKeys({ 'foo-bar': { 'fizz-buzz': true } }, toCamelCase) + * @example deepTransformKeys({ 'foo-bar': { 'fizz-buzz': true } }, camelCase) * // { fooBar: { fizzBuzz: true } } */ export function deepTransformKeys( diff --git a/src/utils/object-keys/delimiter-keys.ts b/src/utils/object-keys/delimiter-keys.ts index bc374c6..7ba913f 100644 --- a/src/utils/object-keys/delimiter-keys.ts +++ b/src/utils/object-keys/delimiter-keys.ts @@ -1,8 +1,8 @@ import { transformKeys } from './transform-keys.js' import { type DelimiterCase, - toDelimiterCase, -} from '../word-case/to-delimiter-case.js' + delimiterCase, +} from '../word-case/delimiter-case.js' /** * Shallowly transforms the keys of an Record to a custom delimiter case. @@ -23,5 +23,5 @@ export function delimiterKeys( obj: T, delimiter: D, ): DelimiterKeys { - return transformKeys(obj, (str) => toDelimiterCase(str, delimiter)) as never + return transformKeys(obj, (str) => delimiterCase(str, delimiter)) as never } diff --git a/src/utils/object-keys/kebab-keys.ts b/src/utils/object-keys/kebab-keys.ts index d041c93..66f6f66 100644 --- a/src/utils/object-keys/kebab-keys.ts +++ b/src/utils/object-keys/kebab-keys.ts @@ -1,4 +1,4 @@ -import { type KebabCase, toKebabCase } from '../word-case/to-kebab-case.js' +import { type KebabCase, kebabCase } from '../word-case/kebab-case.js' import { transformKeys } from './transform-keys.js' /** @@ -17,5 +17,5 @@ export type KebabKeys = T extends [] * @example kebabKeys({ fooBar: { fizzBuzz: true } }) // { 'foo-bar': { fizzBuzz: true } } */ export function kebabKeys(obj: T): KebabKeys { - return transformKeys(obj, toKebabCase) as never + return transformKeys(obj, kebabCase) as never } diff --git a/src/utils/object-keys/pascal-keys.ts b/src/utils/object-keys/pascal-keys.ts index fb92907..b5cf9dc 100644 --- a/src/utils/object-keys/pascal-keys.ts +++ b/src/utils/object-keys/pascal-keys.ts @@ -1,4 +1,4 @@ -import { type PascalCase, toPascalCase } from '../word-case/to-pascal-case.js' +import { type PascalCase, pascalCase } from '../word-case/pascal-case.js' import { transformKeys } from './transform-keys.js' /** @@ -15,5 +15,5 @@ export type PascalKeys = T extends [] * @example pascalKeys({ 'foo-bar': { 'fizz-buzz': true } }) // { FooBar: { 'fizz-buzz': true } } */ export function pascalKeys(obj: T): PascalKeys { - return transformKeys(obj, toPascalCase) as never + return transformKeys(obj, pascalCase) as never } diff --git a/src/utils/object-keys/snake-keys.ts b/src/utils/object-keys/snake-keys.ts index a5d2641..d98c937 100644 --- a/src/utils/object-keys/snake-keys.ts +++ b/src/utils/object-keys/snake-keys.ts @@ -1,5 +1,5 @@ import { transformKeys } from './transform-keys.js' -import { type SnakeCase, toSnakeCase } from '../word-case/to-snake-case.js' +import { type SnakeCase, snakeCase } from '../word-case/snake-case.js' /** * Shallowly transforms the keys of an Record to snake_case. @@ -15,5 +15,5 @@ export type SnakeKeys = T extends [] * @example snakeKeys({ 'foo-bar': { 'fizz-buzz': true } }) // { 'foo_bar': { 'fizz-buzz': true } } */ export function snakeKeys(obj: T): SnakeKeys { - return transformKeys(obj, toSnakeCase) as never + return transformKeys(obj, snakeCase) as never } diff --git a/src/utils/object-keys/transform-keys.ts b/src/utils/object-keys/transform-keys.ts index 986ac11..71df7bf 100644 --- a/src/utils/object-keys/transform-keys.ts +++ b/src/utils/object-keys/transform-keys.ts @@ -6,7 +6,7 @@ import { typeOf } from '../../internal/internals.js' * @param obj the object to transform. * @param transform the function to transform the keys from string to string. * @returns the transformed object. - * @example transformKeys({ 'foo-bar': { 'fizz-buzz': true } }, toCamelCase) + * @example transformKeys({ 'foo-bar': { 'fizz-buzz': true } }, camelCase) * // { fooBar: { 'fizz-buzz': true } } */ export function transformKeys(obj: T, transform: (s: string) => string): T { diff --git a/src/utils/word-case/to-camel-case.test.ts b/src/utils/word-case/camel-case.test.ts similarity index 79% rename from src/utils/word-case/to-camel-case.test.ts rename to src/utils/word-case/camel-case.test.ts index 916ef36..0573ab1 100644 --- a/src/utils/word-case/to-camel-case.test.ts +++ b/src/utils/word-case/camel-case.test.ts @@ -3,7 +3,7 @@ import { WEIRD_TEXT, SEPARATORS_TEXT, } from '../../internal/fixtures.js' -import { type CamelCase, toCamelCase } from './to-camel-case.js' +import { type CamelCase, camelCase } from './camel-case.js' namespace TypeTransforms { type test = Expect< @@ -14,15 +14,15 @@ namespace TypeTransforms { > } -describe('toCamelCase', () => { +describe('camelCase', () => { test('casing functions', () => { const expected = 'someWeirdCased$*String1986FooBarWForWumbo' as const - const result = toCamelCase(WEIRD_TEXT) + const result = camelCase(WEIRD_TEXT) expect(result).toEqual(expected) type test = Expect> }) test('with various separators', () => { - const result = toCamelCase(SEPARATORS_TEXT) + const result = camelCase(SEPARATORS_TEXT) const expected = 'oneTwoThreeFourFiveSixSevenEightNineTen' expect(result).toEqual(expected) type test = Expect> diff --git a/src/utils/word-case/camel-case.ts b/src/utils/word-case/camel-case.ts new file mode 100644 index 0000000..9de84e7 --- /dev/null +++ b/src/utils/word-case/camel-case.ts @@ -0,0 +1,24 @@ +import { type PascalCase, pascalCase } from './pascal-case.js' +import { uncapitalize } from '../uncapitalize.js' + +/** + * Transforms a string to camelCase. + */ +export type CamelCase = Uncapitalize> + +/** + * A strongly typed version of `camelCase` that works in both runtime and type level. + * @param str the string to convert to camel case. + * @returns the camel cased string. + * @example camelCase('hello world') // 'helloWorld' + */ +export function camelCase(str: T): CamelCase { + return uncapitalize(pascalCase(str)) +} + +/** + * @deprecated + * Use `camelCase` instead. + * Read more about the deprecation [here](https://github.com/gustavoguichard/string-ts/issues/44). + */ +export const toCamelCase = camelCase diff --git a/src/utils/word-case/to-constant-case.test.ts b/src/utils/word-case/constant-case.test.ts similarity index 80% rename from src/utils/word-case/to-constant-case.test.ts rename to src/utils/word-case/constant-case.test.ts index 830ccba..f6e5ee1 100644 --- a/src/utils/word-case/to-constant-case.test.ts +++ b/src/utils/word-case/constant-case.test.ts @@ -2,7 +2,7 @@ import { type WeirdTextUnion, SEPARATORS_TEXT, } from '../../internal/fixtures.js' -import { type ConstantCase, toConstantCase } from './to-constant-case.js' +import { type ConstantCase, constantCase } from './constant-case.js' namespace TypeTransforms { type test = Expect< @@ -14,11 +14,11 @@ namespace TypeTransforms { > } -describe('toConstantCase', () => { +describe('constantCase', () => { test('casing functions', () => { const expected = 'SOME_WEIRD_CASED_$*_STRING_1986_FOO_BAR_W_FOR_WUMBO' as const - const result = toConstantCase( + const result = constantCase( ' someWeird-cased$*String1986Foo Bar W_FOR_WUMBO', ) expect(result).toEqual(expected) @@ -26,7 +26,7 @@ describe('toConstantCase', () => { }) test('with various separators', () => { - const result = toConstantCase(SEPARATORS_TEXT) + const result = constantCase(SEPARATORS_TEXT) const expected = 'ONE_TWO_THREE_FOUR_FIVE_SIX_SEVEN_EIGHT_NINE_TEN' expect(result).toEqual(expected) type test = Expect> diff --git a/src/utils/word-case/constant-case.ts b/src/utils/word-case/constant-case.ts new file mode 100644 index 0000000..48dc987 --- /dev/null +++ b/src/utils/word-case/constant-case.ts @@ -0,0 +1,23 @@ +import { type DelimiterCase, delimiterCase } from './delimiter-case.js' +import { toUpperCase } from '../../native/to-upper-case.js' + +/** + * Transforms a string to CONSTANT_CASE. + */ +export type ConstantCase = Uppercase> +/** + * A strongly typed version of `constantCase` that works in both runtime and type level. + * @param str the string to convert to constant case. + * @returns the constant cased string. + * @example constantCase('hello world') // 'HELLO_WORLD' + */ +export function constantCase(str: T): ConstantCase { + return toUpperCase(delimiterCase(str, '_')) +} + +/** + * @deprecated + * Use `constantCase` instead. + * Read more about the deprecation [here](https://github.com/gustavoguichard/string-ts/issues/44). + */ +export const toConstantCase = constantCase diff --git a/src/utils/word-case/to-delimiter-case.test.ts b/src/utils/word-case/delimiter-case.test.ts similarity index 77% rename from src/utils/word-case/to-delimiter-case.test.ts rename to src/utils/word-case/delimiter-case.test.ts index 95b4e4e..2f6c831 100644 --- a/src/utils/word-case/to-delimiter-case.test.ts +++ b/src/utils/word-case/delimiter-case.test.ts @@ -3,7 +3,7 @@ import { WEIRD_TEXT, SEPARATORS_TEXT, } from '../../internal/fixtures.js' -import { type DelimiterCase, toDelimiterCase } from './to-delimiter-case.js' +import { type DelimiterCase, delimiterCase } from './delimiter-case.js' namespace TypeTransforms { type test = Expect< @@ -15,16 +15,16 @@ namespace TypeTransforms { > } -describe('toDelimiterCase', () => { +describe('delimiterCase', () => { test('casing functions', () => { const expected = 'some@Weird@cased@$*@String@1986@Foo@Bar@W@FOR@WUMBO' as const - const result = toDelimiterCase(WEIRD_TEXT, '@') + const result = delimiterCase(WEIRD_TEXT, '@') expect(result).toEqual(expected) type test = Expect> }) test('with various separators', () => { - const result = toDelimiterCase(SEPARATORS_TEXT, '.') + const result = delimiterCase(SEPARATORS_TEXT, '.') const expected = 'one.two.three.four.five.six.seven.eight.nine.ten' expect(result).toEqual(expected) type test = Expect> diff --git a/src/utils/word-case/to-delimiter-case.ts b/src/utils/word-case/delimiter-case.ts similarity index 53% rename from src/utils/word-case/to-delimiter-case.ts rename to src/utils/word-case/delimiter-case.ts index 21f4df0..6334495 100644 --- a/src/utils/word-case/to-delimiter-case.ts +++ b/src/utils/word-case/delimiter-case.ts @@ -1,7 +1,5 @@ -import { join } from '../../native/join.js' -import type { Join } from '../../native/join.js' -import { words } from '../words.js' -import type { Words } from '../words.js' +import { type Join, join } from '../../native/join.js' +import { type Words, words } from '../words.js' /** * Transforms a string with the specified separator (delimiter). @@ -15,11 +13,18 @@ export type DelimiterCase = Join< * @param str the string to transform. * @param delimiter the delimiter to use. * @returns the transformed string. - * @example toDelimiterCase('hello world', '.') // 'hello.world' + * @example delimiterCase('hello world', '.') // 'hello.world' */ -export function toDelimiterCase( +export function delimiterCase( str: T, delimiter: D, ): DelimiterCase { return join(words(str), delimiter) } + +/** + * @deprecated + * Use `delimiterCase` instead. + * Read more about the deprecation [here](https://github.com/gustavoguichard/string-ts/issues/44). + */ +export const toDelimiterCase = delimiterCase diff --git a/src/utils/word-case/to-kebab-case.test.ts b/src/utils/word-case/kebab-case.test.ts similarity index 80% rename from src/utils/word-case/to-kebab-case.test.ts rename to src/utils/word-case/kebab-case.test.ts index a2597e7..de76d9f 100644 --- a/src/utils/word-case/to-kebab-case.test.ts +++ b/src/utils/word-case/kebab-case.test.ts @@ -3,7 +3,7 @@ import { WEIRD_TEXT, SEPARATORS_TEXT, } from '../../internal/fixtures.js' -import { type KebabCase, toKebabCase } from './to-kebab-case.js' +import { type KebabCase, kebabCase } from './kebab-case.js' namespace TypeTransforms { type test = Expect< @@ -15,16 +15,16 @@ namespace TypeTransforms { > } -describe('toKebabCase', () => { +describe('kebabCase', () => { test('casing functions', () => { const expected = 'some-weird-cased-$*-string-1986-foo-bar-w-for-wumbo' as const - const result = toKebabCase(WEIRD_TEXT) + const result = kebabCase(WEIRD_TEXT) expect(result).toEqual(expected) type test = Expect> }) test('with various separators', () => { - const result = toKebabCase(SEPARATORS_TEXT) + const result = kebabCase(SEPARATORS_TEXT) const expected = 'one-two-three-four-five-six-seven-eight-nine-ten' expect(result).toEqual(expected) type test = Expect> diff --git a/src/utils/word-case/kebab-case.ts b/src/utils/word-case/kebab-case.ts new file mode 100644 index 0000000..fc03a8b --- /dev/null +++ b/src/utils/word-case/kebab-case.ts @@ -0,0 +1,23 @@ +import { type DelimiterCase, delimiterCase } from './delimiter-case.js' +import { toLowerCase } from '../../native/to-lower-case.js' + +/** + * Transforms a string to kebab-case. + */ +export type KebabCase = Lowercase> +/** + * A strongly typed version of `kebabCase` that works in both runtime and type level. + * @param str the string to convert to kebab case. + * @returns the kebab cased string. + * @example kebabCase('hello world') // 'hello-world' + */ +export function kebabCase(str: T): KebabCase { + return toLowerCase(delimiterCase(str, '-')) +} + +/** + * @deprecated + * Use `kebabCase` instead. + * Read more about the deprecation [here](https://github.com/gustavoguichard/string-ts/issues/44). + */ +export const toKebabCase = kebabCase diff --git a/src/utils/word-case/lower-case.ts b/src/utils/word-case/lower-case.ts index c4d8e9b..e694669 100644 --- a/src/utils/word-case/lower-case.ts +++ b/src/utils/word-case/lower-case.ts @@ -1,5 +1,5 @@ import { toLowerCase } from '../../native/to-lower-case.js' -import { type DelimiterCase, toDelimiterCase } from './to-delimiter-case.js' +import { type DelimiterCase, delimiterCase } from './delimiter-case.js' /** * A strongly-typed version of `lowerCase` that works in both runtime and type level. @@ -10,5 +10,5 @@ import { type DelimiterCase, toDelimiterCase } from './to-delimiter-case.js' export function lowerCase( str: T, ): Lowercase> { - return toLowerCase(toDelimiterCase(str, ' ')) + return toLowerCase(delimiterCase(str, ' ')) } diff --git a/src/utils/word-case/to-pascal-case.test.ts b/src/utils/word-case/pascal-case.test.ts similarity index 78% rename from src/utils/word-case/to-pascal-case.test.ts rename to src/utils/word-case/pascal-case.test.ts index 71b1dfe..1de1ae8 100644 --- a/src/utils/word-case/to-pascal-case.test.ts +++ b/src/utils/word-case/pascal-case.test.ts @@ -3,7 +3,7 @@ import { WEIRD_TEXT, SEPARATORS_TEXT, } from '../../internal/fixtures.js' -import { type PascalCase, toPascalCase } from './to-pascal-case.js' +import { type PascalCase, pascalCase } from './pascal-case.js' namespace TypeTransforms { type test = Expect< @@ -14,15 +14,15 @@ namespace TypeTransforms { > } -describe('toPascelCase', () => { +describe('pascalCase', () => { test('casing functions', () => { const expected = 'SomeWeirdCased$*String1986FooBarWForWumbo' as const - const result = toPascalCase(WEIRD_TEXT) + const result = pascalCase(WEIRD_TEXT) expect(result).toEqual(expected) type test = Expect> }) test('with various separators', () => { - const result = toPascalCase(SEPARATORS_TEXT) + const result = pascalCase(SEPARATORS_TEXT) const expected = 'OneTwoThreeFourFiveSixSevenEightNineTen' expect(result).toEqual(expected) type test = Expect> diff --git a/src/utils/word-case/pascal-case.ts b/src/utils/word-case/pascal-case.ts new file mode 100644 index 0000000..96c346f --- /dev/null +++ b/src/utils/word-case/pascal-case.ts @@ -0,0 +1,24 @@ +import { type PascalCaseAll, pascalCaseAll } from '../../internal/internals.js' +import { type Join, join } from '../../native/join.js' +import { type Words, words } from '../words.js' + +/** + * Transforms a string to PascalCase. + */ +export type PascalCase = Join>> +/** + * A strongly typed version of `pascalCase` that works in both runtime and type level. + * @param str the string to convert to pascal case. + * @returns the pascal cased string. + * @example pascalCase('hello world') // 'HelloWorld' + */ +export function pascalCase(str: T): PascalCase { + return join(pascalCaseAll(words(str))) +} + +/** + * @deprecated + * Use `pascalCase` instead. + * Read more about the deprecation [here](https://github.com/gustavoguichard/string-ts/issues/44). + */ +export const toPascalCase = pascalCase diff --git a/src/utils/word-case/to-snake-case.test.ts b/src/utils/word-case/snake-case.test.ts similarity index 80% rename from src/utils/word-case/to-snake-case.test.ts rename to src/utils/word-case/snake-case.test.ts index 5c6f345..f0ada95 100644 --- a/src/utils/word-case/to-snake-case.test.ts +++ b/src/utils/word-case/snake-case.test.ts @@ -3,7 +3,7 @@ import { WEIRD_TEXT, SEPARATORS_TEXT, } from '../../internal/fixtures.js' -import { type SnakeCase, toSnakeCase } from './to-snake-case.js' +import { type SnakeCase, snakeCase } from './snake-case.js' namespace TypeTransforms { type test = Expect< @@ -15,16 +15,16 @@ namespace TypeTransforms { > } -describe('toSnakeCase', () => { +describe('snakeCase', () => { test('casing functions', () => { const expected = 'some_weird_cased_$*_string_1986_foo_bar_w_for_wumbo' as const - const result = toSnakeCase(WEIRD_TEXT) + const result = snakeCase(WEIRD_TEXT) expect(result).toEqual(expected) type test = Expect> }) test('with various separators', () => { - const result = toSnakeCase(SEPARATORS_TEXT) + const result = snakeCase(SEPARATORS_TEXT) const expected = 'one_two_three_four_five_six_seven_eight_nine_ten' expect(result).toEqual(expected) type test = Expect> diff --git a/src/utils/word-case/snake-case.ts b/src/utils/word-case/snake-case.ts new file mode 100644 index 0000000..572659c --- /dev/null +++ b/src/utils/word-case/snake-case.ts @@ -0,0 +1,23 @@ +import { type DelimiterCase, delimiterCase } from './delimiter-case.js' +import { toLowerCase } from '../../native/to-lower-case.js' + +/** + * Transforms a string to snake_case. + */ +export type SnakeCase = Lowercase> +/** + * A strongly typed version of `snakeCase` that works in both runtime and type level. + * @param str the string to convert to snake case. + * @returns the snake cased string. + * @example snakeCase('hello world') // 'hello_world' + */ +export function snakeCase(str: T): SnakeCase { + return toLowerCase(delimiterCase(str, '_')) +} + +/** + * @deprecated + * Use `snakeCase` instead. + * Read more about the deprecation [here](https://github.com/gustavoguichard/string-ts/issues/44). + */ +export const toSnakeCase = snakeCase diff --git a/src/utils/word-case/to-title-case.test.ts b/src/utils/word-case/title-case.test.ts similarity index 80% rename from src/utils/word-case/to-title-case.test.ts rename to src/utils/word-case/title-case.test.ts index 02545b1..06e6d82 100644 --- a/src/utils/word-case/to-title-case.test.ts +++ b/src/utils/word-case/title-case.test.ts @@ -3,7 +3,7 @@ import { WEIRD_TEXT, SEPARATORS_TEXT, } from '../../internal/fixtures.js' -import { type TitleCase, toTitleCase } from './to-title-case.js' +import { type TitleCase, titleCase } from './title-case.js' namespace TypeTransforms { type test = Expect< @@ -15,16 +15,16 @@ namespace TypeTransforms { > } -describe('toTitleCase', () => { +describe('titleCase', () => { test('casing functions', () => { const expected = 'Some Weird Cased $* String 1986 Foo Bar W For Wumbo' as const - const result = toTitleCase(WEIRD_TEXT) + const result = titleCase(WEIRD_TEXT) expect(result).toEqual(expected) type test = Expect> }) test('with various separators', () => { - const result = toTitleCase(SEPARATORS_TEXT) + const result = titleCase(SEPARATORS_TEXT) const expected = 'One Two Three Four Five Six Seven Eight Nine Ten' expect(result).toEqual(expected) type test = Expect> diff --git a/src/utils/word-case/title-case.ts b/src/utils/word-case/title-case.ts new file mode 100644 index 0000000..5b4c1f5 --- /dev/null +++ b/src/utils/word-case/title-case.ts @@ -0,0 +1,23 @@ +import { type PascalCase, pascalCase } from './pascal-case.js' +import { type DelimiterCase, delimiterCase } from './delimiter-case.js' + +/** + * Transforms a string to "Title Case". + */ +export type TitleCase = DelimiterCase, ' '> +/** + * A strongly typed version of `titleCase` that works in both runtime and type level. + * @param str the string to convert to title case. + * @returns the title cased string. + * @example titleCase('hello world') // 'Hello World' + */ +export function titleCase(str: T): TitleCase { + return delimiterCase(pascalCase(str), ' ') +} + +/** + * @deprecated + * Use `titleCase` instead. + * Read more about the deprecation [here](https://github.com/gustavoguichard/string-ts/issues/44). + */ +export const toTitleCase = titleCase diff --git a/src/utils/word-case/to-camel-case.ts b/src/utils/word-case/to-camel-case.ts deleted file mode 100644 index bf815ca..0000000 --- a/src/utils/word-case/to-camel-case.ts +++ /dev/null @@ -1,17 +0,0 @@ -import { type PascalCase, toPascalCase } from './to-pascal-case.js' -import { uncapitalize } from '../uncapitalize.js' - -/** - * Transforms a string to camelCase. - */ -export type CamelCase = Uncapitalize> - -/** - * A strongly typed version of `toCamelCase` that works in both runtime and type level. - * @param str the string to convert to camel case. - * @returns the camel cased string. - * @example toCamelCase('hello world') // 'helloWorld' - */ -export function toCamelCase(str: T): CamelCase { - return uncapitalize(toPascalCase(str)) -} diff --git a/src/utils/word-case/to-constant-case.ts b/src/utils/word-case/to-constant-case.ts deleted file mode 100644 index a0393c7..0000000 --- a/src/utils/word-case/to-constant-case.ts +++ /dev/null @@ -1,16 +0,0 @@ -import { type DelimiterCase, toDelimiterCase } from './to-delimiter-case.js' -import { toUpperCase } from '../../native/to-upper-case.js' - -/** - * Transforms a string to CONSTANT_CASE. - */ -export type ConstantCase = Uppercase> -/** - * A strongly typed version of `toConstantCase` that works in both runtime and type level. - * @param str the string to convert to constant case. - * @returns the constant cased string. - * @example toConstantCase('hello world') // 'HELLO_WORLD' - */ -export function toConstantCase(str: T): ConstantCase { - return toUpperCase(toDelimiterCase(str, '_')) -} diff --git a/src/utils/word-case/to-kebab-case.ts b/src/utils/word-case/to-kebab-case.ts deleted file mode 100644 index 5f61ea7..0000000 --- a/src/utils/word-case/to-kebab-case.ts +++ /dev/null @@ -1,16 +0,0 @@ -import { type DelimiterCase, toDelimiterCase } from './to-delimiter-case.js' -import { toLowerCase } from '../../native/to-lower-case.js' - -/** - * Transforms a string to kebab-case. - */ -export type KebabCase = Lowercase> -/** - * A strongly typed version of `toKebabCase` that works in both runtime and type level. - * @param str the string to convert to kebab case. - * @returns the kebab cased string. - * @example toKebabCase('hello world') // 'hello-world' - */ -export function toKebabCase(str: T): KebabCase { - return toLowerCase(toDelimiterCase(str, '-')) -} diff --git a/src/utils/word-case/to-pascal-case.ts b/src/utils/word-case/to-pascal-case.ts deleted file mode 100644 index 5571a00..0000000 --- a/src/utils/word-case/to-pascal-case.ts +++ /dev/null @@ -1,19 +0,0 @@ -import { pascalCaseAll, type PascalCaseAll } from '../../internal/internals.js' -import type { Join } from '../../native/join.js' -import { join } from '../../native/join.js' -import type { Words } from '../words.js' -import { words } from '../words.js' - -/** - * Transforms a string to PascalCase. - */ -export type PascalCase = Join>> -/** - * A strongly typed version of `toPascalCase` that works in both runtime and type level. - * @param str the string to convert to pascal case. - * @returns the pascal cased string. - * @example toPascalCase('hello world') // 'HelloWorld' - */ -export function toPascalCase(str: T): PascalCase { - return join(pascalCaseAll(words(str))) -} diff --git a/src/utils/word-case/to-snake-case.ts b/src/utils/word-case/to-snake-case.ts deleted file mode 100644 index 2ac6950..0000000 --- a/src/utils/word-case/to-snake-case.ts +++ /dev/null @@ -1,16 +0,0 @@ -import { type DelimiterCase, toDelimiterCase } from './to-delimiter-case.js' -import { toLowerCase } from '../../native/to-lower-case.js' - -/** - * Transforms a string to snake_case. - */ -export type SnakeCase = Lowercase> -/** - * A strongly typed version of `toSnakeCase` that works in both runtime and type level. - * @param str the string to convert to snake case. - * @returns the snake cased string. - * @example toSnakeCase('hello world') // 'hello_world' - */ -export function toSnakeCase(str: T): SnakeCase { - return toLowerCase(toDelimiterCase(str, '_')) -} diff --git a/src/utils/word-case/to-title-case.ts b/src/utils/word-case/to-title-case.ts deleted file mode 100644 index 1fbc0ec..0000000 --- a/src/utils/word-case/to-title-case.ts +++ /dev/null @@ -1,16 +0,0 @@ -import { type PascalCase, toPascalCase } from './to-pascal-case.js' -import { type DelimiterCase, toDelimiterCase } from './to-delimiter-case.js' - -/** - * Transforms a string to "Title Case". - */ -export type TitleCase = DelimiterCase, ' '> -/** - * A strongly typed version of `toTitleCase` that works in both runtime and type level. - * @param str the string to convert to title case. - * @returns the title cased string. - * @example toTitleCase('hello world') // 'Hello World' - */ -export function toTitleCase(str: T): TitleCase { - return toDelimiterCase(toPascalCase(str), ' ') -} diff --git a/src/utils/word-case/upper-case.ts b/src/utils/word-case/upper-case.ts index b4b977b..b813c0a 100644 --- a/src/utils/word-case/upper-case.ts +++ b/src/utils/word-case/upper-case.ts @@ -1,5 +1,5 @@ import { toUpperCase } from '../../native/to-upper-case.js' -import { type DelimiterCase, toDelimiterCase } from './to-delimiter-case.js' +import { type DelimiterCase, delimiterCase } from './delimiter-case.js' /** * A strongly-typed version of `upperCase` that works in both runtime and type level. @@ -10,5 +10,5 @@ import { type DelimiterCase, toDelimiterCase } from './to-delimiter-case.js' export function upperCase( str: T, ): Uppercase> { - return toUpperCase(toDelimiterCase(str, ' ')) + return toUpperCase(delimiterCase(str, ' ')) }