Skip to content

Commit

Permalink
feat: Rename casing functions and deprecate old ones
Browse files Browse the repository at this point in the history
  • Loading branch information
gustavoguichard committed Oct 10, 2023
1 parent 46c6af8 commit b5c7621
Show file tree
Hide file tree
Showing 36 changed files with 244 additions and 193 deletions.
56 changes: 28 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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'
```

Expand Down
38 changes: 22 additions & 16 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand All @@ -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'

Expand Down
2 changes: 1 addition & 1 deletion src/utils/object-keys/camel-keys.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { transformKeys } from './transform-keys.js'
import { type CamelCase, toCamelCase } from '../word-case/to-camel-case.js'
import { type CamelCase, toCamelCase } from '../word-case/camel-case.js'

/**
* Shallowly transforms the keys of an Record to camelCase.
Expand Down
2 changes: 1 addition & 1 deletion src/utils/object-keys/constant-keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { transformKeys } from './transform-keys.js'
import {
type ConstantCase,
toConstantCase,
} from '../word-case/to-constant-case.js'
} from '../word-case/constant-case.js'

/**
* Shallowly transforms the keys of an Record to CONSTANT_CASE.
Expand Down
2 changes: 1 addition & 1 deletion src/utils/object-keys/deep-camel-keys.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { type CamelCase, toCamelCase } from '../word-case/to-camel-case.js'
import { type CamelCase, toCamelCase } from '../word-case/camel-case.js'
import { deepTransformKeys } from './deep-transform-keys.js'

/**
Expand Down
2 changes: 1 addition & 1 deletion src/utils/object-keys/deep-constant-keys.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
type ConstantCase,
toConstantCase,
} from '../word-case/to-constant-case.js'
} from '../word-case/constant-case.js'
import { deepTransformKeys } from './deep-transform-keys.js'

/**
Expand Down
2 changes: 1 addition & 1 deletion src/utils/object-keys/deep-delimiter-keys.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
type DelimiterCase,
toDelimiterCase,
} from '../word-case/to-delimiter-case.js'
} from '../word-case/delimiter-case.js'
import { deepTransformKeys } from './deep-transform-keys.js'

/**
Expand Down
2 changes: 1 addition & 1 deletion src/utils/object-keys/deep-kebab-keys.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { type KebabCase, toKebabCase } from '../word-case/to-kebab-case.js'
import { type KebabCase, toKebabCase } from '../word-case/kebab-case.js'
import { deepTransformKeys } from './deep-transform-keys.js'

/**
Expand Down
2 changes: 1 addition & 1 deletion src/utils/object-keys/deep-pascal-keys.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { type PascalCase, toPascalCase } from '../word-case/to-pascal-case.js'
import { type PascalCase, toPascalCase } from '../word-case/pascal-case.js'
import { deepTransformKeys } from './deep-transform-keys.js'

/**
Expand Down
2 changes: 1 addition & 1 deletion src/utils/object-keys/deep-snake-keys.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { type SnakeCase, toSnakeCase } from '../word-case/to-snake-case.js'
import { type SnakeCase, toSnakeCase } from '../word-case/snake-case.js'
import { deepTransformKeys } from './deep-transform-keys.js'

/**
Expand Down
2 changes: 1 addition & 1 deletion src/utils/object-keys/delimiter-keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { transformKeys } from './transform-keys.js'
import {
type DelimiterCase,
toDelimiterCase,
} from '../word-case/to-delimiter-case.js'
} from '../word-case/delimiter-case.js'

/**
* Shallowly transforms the keys of an Record to a custom delimiter case.
Expand Down
2 changes: 1 addition & 1 deletion src/utils/object-keys/kebab-keys.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { type KebabCase, toKebabCase } from '../word-case/to-kebab-case.js'
import { type KebabCase, toKebabCase } from '../word-case/kebab-case.js'
import { transformKeys } from './transform-keys.js'

/**
Expand Down
2 changes: 1 addition & 1 deletion src/utils/object-keys/pascal-keys.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { type PascalCase, toPascalCase } from '../word-case/to-pascal-case.js'
import { type PascalCase, toPascalCase } from '../word-case/pascal-case.js'
import { transformKeys } from './transform-keys.js'

/**
Expand Down
2 changes: 1 addition & 1 deletion src/utils/object-keys/snake-keys.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { transformKeys } from './transform-keys.js'
import { type SnakeCase, toSnakeCase } from '../word-case/to-snake-case.js'
import { type SnakeCase, toSnakeCase } from '../word-case/snake-case.js'

/**
* Shallowly transforms the keys of an Record to snake_case.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<
Expand All @@ -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<Equal<typeof result, typeof expected>>
})
test('with various separators', () => {
const result = toCamelCase(SEPARATORS_TEXT)
const result = camelCase(SEPARATORS_TEXT)
const expected = 'oneTwoThreeFourFiveSixSevenEightNineTen'
expect(result).toEqual(expected)
type test = Expect<Equal<typeof result, typeof expected>>
Expand Down
24 changes: 24 additions & 0 deletions src/utils/word-case/camel-case.ts
Original file line number Diff line number Diff line change
@@ -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<T extends string> = Uncapitalize<PascalCase<T>>

/**
* 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<T extends string>(str: T): CamelCase<T> {
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
Original file line number Diff line number Diff line change
Expand Up @@ -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<
Expand All @@ -14,19 +14,19 @@ 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)
type test = Expect<Equal<typeof result, typeof expected>>
})

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<Equal<typeof result, typeof expected>>
Expand Down
23 changes: 23 additions & 0 deletions src/utils/word-case/constant-case.ts
Original file line number Diff line number Diff line change
@@ -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<T extends string> = Uppercase<DelimiterCase<T, '_'>>
/**
* 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<T extends string>(str: T): ConstantCase<T> {
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
Loading

0 comments on commit b5c7621

Please sign in to comment.