camelcase-keys
- Version 10.0.0
- Published
- 14.7 kB
- 4 dependencies
- MIT license
Install
npm i camelcase-keys
yarn add camelcase-keys
pnpm add camelcase-keys
Overview
Convert object keys to camel case
Index
Functions
function camelcaseKeys
camelcaseKeys: < T extends ObjectLike | readonly ObjectLike[], O extends Options = Options>( input: T, options?: O) => CamelCaseKeys< T, O['deep'] extends true ? true : false, O['pascalCase'] extends true ? true : false, O['preserveConsecutiveUppercase'] extends true ? true : false, O['exclude'] extends readonly unknown[] ? O['exclude'] : readonly never[], O['stopPaths'] extends readonly string[] ? O['stopPaths'] : readonly never[]>;
Convert object keys to camel case using [
camelcase
](https://github.com/sindresorhus/camelcase).Parameter input
Object or array of objects to camel-case.
Example 1
import camelcaseKeys from 'camelcase-keys';// Convert an objectcamelcaseKeys({'foo-bar': true});//=> {fooBar: true}// Convert an array of objectscamelcaseKeys([{'foo-bar': true}, {'bar-foo': false}]);//=> [{fooBar: true}, {barFoo: false}]Example 2
import {parseArgs} from 'node:util';import camelcaseKeys from 'camelcase-keys';const commandLineArguments = parseArgs();//=> {_: [], 'foo-bar': true}camelcaseKeys(commandLineArguments);//=> {_: [], fooBar: true}
Type Aliases
type AppendPath
type AppendPath<Base extends string, Key extends string> = Base extends '' ? Key : `${Base}.${Key}`;
type CamelCaseKeys
type CamelCaseKeys< T extends ObjectLike | readonly unknown[], Deep extends boolean = false, IsPascalCase extends boolean = false, PreserveConsecutiveUppercase extends boolean = false, Exclude extends readonly unknown[] = readonly never[], StopPaths extends readonly string[] = readonly never[], Path extends string = ''> = T extends readonly any[] ? // Handle arrays { [K in keyof T]: ProcessArrayElement< T[K], Deep, IsPascalCase, PreserveConsecutiveUppercase, Exclude, StopPaths >; } : T extends ObjectLike ? // Handle objects { [K in keyof T as IsExcluded<K, Exclude> extends true ? K : IsPascalCase extends true ? PascalCase<K> : CamelCase< K, { preserveConsecutiveUppercase: PreserveConsecutiveUppercase } >]: ProcessValue< T[K], K & string, Path, Deep, IsPascalCase, PreserveConsecutiveUppercase, Exclude, StopPaths >; } : T;
Convert keys of an object to camelcase strings.
Note: Opaque types from
type-fest
may expose underlying primitive methods in the result type due to TypeScript's mapped type distribution. Runtime behavior is correct. Workaround: Useresult.key as OpaqueType
if needed.
type IsExcluded
type IsExcluded< K, Exclude extends readonly unknown[]> = Exclude extends readonly never[] ? false : Exclude extends readonly [infer First, ...infer Rest] ? K extends First ? true : IsExcluded<K, Rest> : false;
type IsStopPath
type IsStopPath< Path extends string, StopPaths extends readonly string[]> = StopPaths extends readonly never[] ? false : StopPaths extends readonly [ infer First, ...infer Rest extends readonly string[] ] ? Path extends First ? true : IsStopPath<Path, Rest> : false;
type ObjectLike
type ObjectLike = { [key: string]: any };
type Options
type Options = { /** Exclude keys from being camel-cased.
If this option can be statically determined, it's recommended to add `as const` to it.
@default [] */ readonly exclude?: ReadonlyArray<string | RegExp>;
/** Recurse nested objects and objects in arrays.
@default false
@example ``` import camelcaseKeys from 'camelcase-keys';
const object = { 'foo-bar': true, nested: { unicorn_rainbow: true } };
camelcaseKeys(object, {deep: true}); //=> {fooBar: true, nested: {unicornRainbow: true}}
camelcaseKeys(object, {deep: false}); //=> {fooBar: true, nested: {unicorn_rainbow: true}} ``` */ readonly deep?: boolean;
/** Uppercase the first character: `bye-bye` → `ByeBye`
@default false
@example ``` import camelcaseKeys from 'camelcase-keys';
camelcaseKeys({'foo-bar': true}, {pascalCase: true}); //=> {FooBar: true}
camelcaseKeys({'foo-bar': true}, {pascalCase: false}); //=> {fooBar: true} ```` */ readonly pascalCase?: boolean;
/** Preserve consecutive uppercase characters: `foo-BAR` → `FooBAR`
@default false
@example ``` import camelcaseKeys from 'camelcase-keys';
camelcaseKeys({'foo-BAR': true}, {preserveConsecutiveUppercase: true}); //=> {fooBAR: true}
camelcaseKeys({'foo-BAR': true}, {preserveConsecutiveUppercase: false}); //=> {fooBar: true} ```` */ readonly preserveConsecutiveUppercase?: boolean;
/** Exclude children at the given object paths in dot-notation from being camel-cased. For example, with an object like `{a: {b: '🦄'}}`, the object path to reach the unicorn is `'a.b'`.
If this option can be statically determined, it's recommended to add `as const` to it.
@default []
@example ``` import camelcaseKeys from 'camelcase-keys';
const object = { a_b: 1, a_c: { c_d: 1, c_e: { e_f: 1 } } };
camelcaseKeys(object, { deep: true, stopPaths: [ 'a_c.c_e' ] }), // { // aB: 1, // aC: { // cD: 1, // cE: { // e_f: 1 // } // } // } ``` */ readonly stopPaths?: readonly string[];};
type ProcessArrayElement
type ProcessArrayElement< E, Deep extends boolean, IsPascalCase extends boolean, PreserveConsecutiveUppercase extends boolean, Exclude extends readonly unknown[], StopPaths extends readonly string[]> = Deep extends true ? E extends ObjectLike | readonly any[] ? CamelCaseKeys< E, Deep, IsPascalCase, PreserveConsecutiveUppercase, Exclude, StopPaths > : E : E extends ObjectLike ? CamelCaseKeys< E, false, IsPascalCase, PreserveConsecutiveUppercase, Exclude, StopPaths > : E;
type ProcessValue
type ProcessValue< V, K extends string, Path extends string, Deep extends boolean, IsPascalCase extends boolean, PreserveConsecutiveUppercase extends boolean, Exclude extends readonly unknown[], StopPaths extends readonly string[]> = IsStopPath<AppendPath<Path, K>, StopPaths> extends true ? V // Stop recursion at this path : Deep extends true ? V extends ObjectLike | readonly any[] ? CamelCaseKeys< V, Deep, IsPascalCase, PreserveConsecutiveUppercase, Exclude, StopPaths, AppendPath<Path, K> > : V : V;
Package Files (1)
Dependencies (4)
Dev Dependencies (4)
Peer Dependencies (0)
No peer dependencies.
Badge
To add a badge like this oneto your package's README, use the codes available below.
You may also use Shields.io to create a custom badge linking to https://www.jsdocs.io/package/camelcase-keys
.
- Markdown[](https://www.jsdocs.io/package/camelcase-keys)
- HTML<a href="https://www.jsdocs.io/package/camelcase-keys"><img src="https://img.shields.io/badge/jsDocs.io-reference-blue" alt="jsDocs.io"></a>
- Updated .
Package analyzed in 3026 ms. - Missing or incorrect documentation? Open an issue for this package.