ts-normify
v1.0.1
Published
Fix those unconventional names coming from outside your beautiful codebase. With Typescript!
Maintainers
Readme
Fix those unconventional names coming from outside your beautiful codebase. With Typescript!
Table of Contents
Features
- Super easy to use normalization functions.
- Guaranteed type safety by exhaustive testing comparing types with runtime results.
- Offers multiple normalization (and denormalization) strategies.
API
stringNormify(param: string, casing?: "Pascal" | "Camel") → stringConverts a single string to camelCase or PascalCasetsNormify(param: object | array, casing?: "Pascal" | "Camel") → object | arrayDeep: recursively transforms ALL nested keys to camelCase or PascalCasekeyNormify(param: object | array, casing?: "Pascal" | "Camel") → object | arrayShallow: only transforms top-level keys to camelCase or PascalCasemapNormify(mapping: Record<string, string>) → { normalize, denormalize }More strict key mapping with reversible transform
Installation
You can install ts-normify like any other npm package. Simply use your favorite package manager, we recommend pnpm:
pnpm add ts-normifyWho is this for?
- Projects with backends that return inconsistently named keys (snake_case, kebab-case, PascalCase, etc.)
- Projects that already have a transformation layer and are looking for a better, more tested utility to replace/enhance it
- Devs who want type-safe normalization of data without manually redefining interfaces
- Devs who don't want ugly key or string transformations scattered throughout their codebase
Who is this NOT for?
- If you have control over the backend: it's way better to not add a frontend dependency and just fix the naming scheme there instead
- Large codebases with tsc performance issues: ts-normify's deep types can slow down TypeScript's language server on very large projects. Meaning, type errors become sluggish, IDE seems unresponsive to type changes, worsening the developer experience. In those cases the brute force approach of having to name your own internal interface for every response is probably your best bet.
Things to consider
Since this package is based on the idea of normalizing responses from sources you don't control, there's a set of things to consider:
- tsNormify, keyNormify and mapNormify all work with strictly serializeable data structures
- All key/string normalizations treat special characters as spaces, so something like "hansel & gretel" will be normalized to "hanzelGretel". See
src/tests/stringNormify.test.tsfor more examples. - In the current implementation, special characters are defined as "characters that don't change between lowercase and uppercase".
Contributing
We are open to contributions from the community! Please make sure to follow the Code of Conduct while contributing.
