Skip to content

v1.1.0 - More strongly-typed string methods 🎁

Compare
Choose a tag to compare
@gustavoguichard gustavoguichard released this 04 Oct 19:27
· 137 commits to main since this release
0a5f51f

What's Changed

This version adds a few more strongly-typed alternatives to the native string methods and by doing so we were able to simplify a lot of the internal methods' implementations.

With a lot of low level properly-typed utilities we can keep both type and runtime implementations in sync, like in this example:

// OLD Implementation
type CamelCase<T extends string> = T extends unknown
  ? PascalCase<T> extends `${infer first}${infer rest}`
    ? `${Lowercase<first>}${rest}`
    : T
  : never

function toCamelCase<T extends string>(str: T) {
  const res = toPascalCase(str)
  return (res.slice(0, 1).toLowerCase() + res.slice(1)) as CamelCase<T> // we needed to cast this type
}

// === πŸͺ„ ===

// NEW Implementation
type CamelCase<T extends string> = Uncapitalize<PascalCase<T>>

function toCamelCase<T extends string>(str: T): CamelCase<T> {
  return uncapitalize(toPascalCase(str)) // type-checks without type casting
}

These changes aim to achieve the goal of this library which is the perfect synchrony between type-level and runtime string functions ❀️.

Support for older JS runtimes

We are now also able to run replaceAll in older browsers that don't yet support it with a very simple polyfill .

Pull requests

Full Changelog: v1.0.0...v1.1.0