pax_global_header00006660000000000000000000000064143030647450014520gustar00rootroot0000000000000052 comment=015d7901a509642a6fda41d6bae56e7c06793a7c camelcase-keys-8.0.2/000077500000000000000000000000001430306474500144155ustar00rootroot00000000000000camelcase-keys-8.0.2/.editorconfig000066400000000000000000000002571430306474500170760ustar00rootroot00000000000000root = true [*] indent_style = tab end_of_line = lf charset = utf-8 trim_trailing_whitespace = true insert_final_newline = true [*.yml] indent_style = space indent_size = 2 camelcase-keys-8.0.2/.gitattributes000066400000000000000000000000231430306474500173030ustar00rootroot00000000000000* text=auto eol=lf camelcase-keys-8.0.2/.github/000077500000000000000000000000001430306474500157555ustar00rootroot00000000000000camelcase-keys-8.0.2/.github/funding.yml000066400000000000000000000001701430306474500201300ustar00rootroot00000000000000github: sindresorhus open_collective: sindresorhus tidelift: npm/camelcase-keys custom: https://sindresorhus.com/donate camelcase-keys-8.0.2/.github/security.md000066400000000000000000000002631430306474500201470ustar00rootroot00000000000000# Security Policy To report a security vulnerability, please use the [Tidelift security contact](https://tidelift.com/security). Tidelift will coordinate the fix and disclosure. camelcase-keys-8.0.2/.github/workflows/000077500000000000000000000000001430306474500200125ustar00rootroot00000000000000camelcase-keys-8.0.2/.github/workflows/main.yml000066400000000000000000000006641430306474500214670ustar00rootroot00000000000000name: CI on: - push - pull_request jobs: test: name: Node.js ${{ matrix.node-version }} runs-on: ubuntu-latest strategy: fail-fast: false matrix: node-version: - 18 - 16 - 14 steps: - uses: actions/checkout@v3 - uses: actions/setup-node@v3 with: node-version: ${{ matrix.node-version }} - run: npm install - run: npm test camelcase-keys-8.0.2/.gitignore000066400000000000000000000000271430306474500164040ustar00rootroot00000000000000node_modules yarn.lock camelcase-keys-8.0.2/.npmrc000066400000000000000000000000231430306474500155300ustar00rootroot00000000000000package-lock=false camelcase-keys-8.0.2/bench/000077500000000000000000000000001430306474500154745ustar00rootroot00000000000000camelcase-keys-8.0.2/bench/bench.js000066400000000000000000000005431430306474500171130ustar00rootroot00000000000000/* globals bench suite set */ import camelcaseKeysNpm from 'camelcase-keys'; import camelcaseKeys from '../index.js'; import fixture from './fixture.js'; suite('camelcaseKeys', () => { set('mintime', 1000); bench('npm', () => { camelcaseKeysNpm(fixture, {deep: true}); }); bench('master', () => { camelcaseKeys(fixture, {deep: true}); }); }); camelcase-keys-8.0.2/bench/fixture.json000066400000000000000000000023721430306474500200610ustar00rootroot00000000000000{ "_id": "58ca523329b7997ab7b741a2", "index": 0, "guid": "e58a19f6-5dc3-45a6-bf57-2db5d32171d6", "is_active": false, "balance": "$3,245.09", "picture": "https://placehold.it/32x32", "age": 21, "eye_color": "blue", "name": "Murphy Rivers", "gender": "male", "company": "OVOLO", "email": "murphyrivers@ovolo.com", "phone": "+1 (840) 432-3958", "address": "950 Wortman Avenue, Axis, New Jersey, 9085", "about": "Qui nulla proident voluptate ut qui laborum est qui aute pariatur consequat reprehenderit. Esse nulla aute sunt esse exercitation ipsum consequat consequat anim ipsum tempor non ex excepteur. Dolor sit id incididunt cillum veniam sint veniam et aliqua velit aute qui magna esse. Qui id minim mollit est magna sunt esse eiusmod. Amet cillum irure est fugiat. Ipsum consectetur Lorem excepteur laboris.", "registered": "2016-04-11T11:47:47 -04:00", "latitude": -79.956954, "longitude": 21.129594, "tags": [ "et", "occaecat", "aliqua", "ex", "Lorem", "excepteur", "do" ], "friends": [ { "id": 0, "name": "Penelope Castro" }, { "id": 1, "name": "Henderson Cannon" }, { "id": 2, "name": "Alicia Howard" } ], "greeting": "Hello, Murphy Rivers! You have 8 unread messages.", "favorite_fruit": "banana" } camelcase-keys-8.0.2/bench/package.json000066400000000000000000000000651430306474500177630ustar00rootroot00000000000000{ "devDependencies": { "camelcase-keys": "*" } } camelcase-keys-8.0.2/fixtures/000077500000000000000000000000001430306474500162665ustar00rootroot00000000000000camelcase-keys-8.0.2/fixtures/child-process-for-test.js000066400000000000000000000003101430306474500231160ustar00rootroot00000000000000import process from 'node:process'; import camelcaseKeys from '../index.js'; const camelcaseKeysArgs = JSON.parse(process.argv[2]); console.log(JSON.stringify(camelcaseKeys(...camelcaseKeysArgs))); camelcase-keys-8.0.2/index.d.ts000066400000000000000000000113201430306474500163130ustar00rootroot00000000000000import {CamelCase, PascalCase} from 'type-fest'; // eslint-disable-next-line @typescript-eslint/ban-types type EmptyTuple = []; /** Return a default type if input type is nil. @template T - Input type. @template U - Default type. */ type WithDefault = T extends undefined | void | null ? U : T; // eslint-disable-line @typescript-eslint/ban-types // TODO: Replace this with https://github.com/sindresorhus/type-fest/blob/main/source/includes.d.ts /** Check if an element is included in a tuple. */ type IsInclude = List extends undefined ? false : List extends Readonly ? false : List extends readonly [infer First, ...infer Rest] ? First extends Target ? true : IsInclude : boolean; /** Append a segment to dot-notation path. */ type AppendPath = S extends '' ? Last : `${S}.${Last}`; /** Convert keys of an object to camelcase strings. */ export type CamelCaseKeys< T extends Record | readonly any[], Deep extends boolean = false, IsPascalCase extends boolean = false, Exclude extends readonly unknown[] = EmptyTuple, StopPaths extends readonly string[] = EmptyTuple, Path extends string = '', > = T extends readonly any[] // Handle arrays or tuples. ? { [P in keyof T]: T[P] extends Record | readonly any[] // eslint-disable-next-line @typescript-eslint/ban-types ? {} extends CamelCaseKeys ? T[P] : CamelCaseKeys< T[P], Deep, IsPascalCase, Exclude, StopPaths > : T[P]; } : T extends Record // Handle objects. ? { [P in keyof T as [IsInclude] extends [true] ? P : [IsPascalCase] extends [true] ? PascalCase

: CamelCase

]: [IsInclude>] extends [ true, ] ? T[P] // eslint-disable-next-line @typescript-eslint/ban-types : {} extends CamelCaseKeys ? T[P] : [Deep] extends [true] ? CamelCaseKeys< T[P], Deep, IsPascalCase, Exclude, StopPaths, AppendPath > : T[P]; } // Return anything else as-is. : T; type Options = { /** Recurse nested objects and objects in arrays. @default false */ readonly deep?: boolean; /** 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; /** 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'; camelcaseKeys({ a_b: 1, a_c: { c_d: 1, c_e: { e_f: 1 } } }, { deep: true, stopPaths: [ 'a_c.c_e' ] }), // { // aB: 1, // aC: { // cD: 1, // cE: { // e_f: 1 // } // } // } ``` */ readonly stopPaths?: readonly string[]; /** Uppercase the first character as in `bye-bye` → `ByeBye`. @default false */ readonly pascalCase?: boolean; }; /** Convert object keys to camel case using [`camelcase`](https://github.com/sindresorhus/camelcase). @param input - Object or array of objects to camel-case. @example ``` import camelcaseKeys from 'camelcase-keys'; // Convert an object camelcaseKeys({'foo-bar': true}); //=> {fooBar: true} // Convert an array of objects camelcaseKeys([{'foo-bar': true}, {'bar-foo': false}]); //=> [{fooBar: true}, {barFoo: false}] camelcaseKeys({'foo-bar': true, nested: {unicorn_rainbow: true}}, {deep: true}); //=> {fooBar: true, nested: {unicornRainbow: true}} camelcaseKeys({a_b: 1, a_c: {c_d: 1, c_e: {e_f: 1}}}, {deep: true, stopPaths: ['a_c.c_e']}), //=> {aB: 1, aC: {cD: 1, cE: {e_f: 1}}} // Convert object keys to pascal case camelcaseKeys({'foo-bar': true, nested: {unicorn_rainbow: true}}, {deep: true, pascalCase: true}); //=> {FooBar: true, Nested: {UnicornRainbow: true}} ``` @example ``` import {parseArgs} from 'node:utils'; import camelcaseKeys from 'camelcase-keys'; const commandLineArguments = parseArgs(); //=> {_: [], 'foo-bar': true} camelcaseKeys(commandLineArguments); //=> {_: [], fooBar: true} ``` */ export default function camelcaseKeys< T extends Record | readonly any[], OptionsType extends Options = Options, >( input: T, options?: OptionsType ): CamelCaseKeys< T, WithDefault, WithDefault, WithDefault, WithDefault >; camelcase-keys-8.0.2/index.js000066400000000000000000000032421430306474500160630ustar00rootroot00000000000000import mapObject from 'map-obj'; import camelCase from 'camelcase'; import QuickLru from 'quick-lru'; const has = (array, key) => array.some(element => { if (typeof element === 'string') { return element === key; } element.lastIndex = 0; return element.test(key); }); const cache = new QuickLru({maxSize: 100_000}); // Reproduces behavior from `map-obj`. const isObject = value => typeof value === 'object' && value !== null && !(value instanceof RegExp) && !(value instanceof Error) && !(value instanceof Date); const camelCaseConvert = (input, options) => { if (!isObject(input)) { return input; } options = { deep: false, pascalCase: false, ...options, }; const {exclude, pascalCase, stopPaths, deep} = options; const stopPathsSet = new Set(stopPaths); const makeMapper = parentPath => (key, value) => { if (deep && isObject(value)) { const path = parentPath === undefined ? key : `${parentPath}.${key}`; if (!stopPathsSet.has(path)) { value = mapObject(value, makeMapper(path)); } } if (!(exclude && has(exclude, key))) { const cacheKey = pascalCase ? `${key}_` : key; if (cache.has(cacheKey)) { key = cache.get(cacheKey); } else { const returnValue = camelCase(key, {pascalCase, locale: false}); if (key.length < 100) { // Prevent abuse cache.set(cacheKey, returnValue); } key = returnValue; } } return [key, value]; }; return mapObject(input, makeMapper(undefined)); }; export default function camelcaseKeys(input, options) { if (Array.isArray(input)) { return Object.keys(input).map(key => camelCaseConvert(input[key], options)); } return camelCaseConvert(input, options); } camelcase-keys-8.0.2/index.test-d.ts000066400000000000000000000231131430306474500172730ustar00rootroot00000000000000/* eslint-disable @typescript-eslint/naming-convention, @typescript-eslint/indent */ import {expectType, expectAssignable, expectNotType} from 'tsd'; import camelcaseKeys, {type CamelCaseKeys} from './index.js'; const fooBarObject = {'foo-bar': true}; const camelFooBarObject = camelcaseKeys(fooBarObject); expectType<{fooBar: boolean}>(camelFooBarObject); const fooBarArray = [{'foo-bar': true}]; const camelFooBarArray = camelcaseKeys(fooBarArray); expectType>(camelFooBarArray); expectType>(camelcaseKeys([{'foo-bar': true}])); expectType(camelcaseKeys(['name 1', 'name 2'])); expectType(camelcaseKeys(['name 1', 'name 2'], {deep: true})); expectType( camelcaseKeys([{'foo-bar': true}, {'foo-baz': true}] as const), ); expectType<{fooBar: boolean}>(camelcaseKeys({'foo-bar': true})); expectType<{fooBar: boolean}>(camelcaseKeys({'--foo-bar': true})); expectType<{fooBar: boolean}>(camelcaseKeys({foo_bar: true})); expectType<{fooBar: boolean}>(camelcaseKeys({'foo bar': true})); expectType<{readonly fooBar: true}>(camelcaseKeys({'foo-bar': true} as const)); expectType<{readonly fooBar: true}>(camelcaseKeys({'--foo-bar': true} as const)); expectType<{readonly fooBar: true}>(camelcaseKeys({foo_bar: true} as const)); expectType<{readonly fooBar: true}>(camelcaseKeys({'foo bar': true} as const)); expectType<{fooBar: {fooBar: {fooBar: boolean}}}>( camelcaseKeys({'foo-bar': {foo_bar: {'foo bar': true}}}, {deep: true}), ); interface ObjectOrUndefined { foo_bar: { foo_bar: | { foo_bar: boolean; } | undefined; }; } const objectOrUndefined: ObjectOrUndefined = { foo_bar: { foo_bar: { foo_bar: true, }, }, }; expectType<{fooBar: {fooBar: {fooBar: boolean} | undefined}}>( camelcaseKeys(objectOrUndefined, {deep: true}), ); expectType<{FooBar: boolean}>( camelcaseKeys({'foo-bar': true}, {pascalCase: true}), ); expectType<{readonly FooBar: true}>( camelcaseKeys({'foo-bar': true} as const, {pascalCase: true}), ); expectType<{FooBar: boolean}>( camelcaseKeys({'--foo-bar': true}, {pascalCase: true}), ); expectType<{FooBar: boolean}>( camelcaseKeys({foo_bar: true}, {pascalCase: true}), ); expectType<{FooBar: boolean}>( camelcaseKeys({'foo bar': true}, {pascalCase: true}), ); expectType<{FooBar: {FooBar: {FooBar: boolean}}}>( camelcaseKeys( {'foo-bar': {foo_bar: {'foo bar': true}}}, {deep: true, pascalCase: true}, ), ); expectType<{fooBar: boolean; foo_bar: true}>( camelcaseKeys( {'foo-bar': true, foo_bar: true}, {exclude: ['foo', 'foo_bar', /bar/] as const}, ), ); expectType<{fooBar: boolean}>( camelcaseKeys({'foo-bar': true}, {stopPaths: ['foo']}), ); expectType<{topLevel: {fooBar: {'bar-baz': boolean}}; fooFoo: boolean}>( camelcaseKeys( {'top-level': {'foo-bar': {'bar-baz': true}}, 'foo-foo': true}, {deep: true, stopPaths: ['top-level.foo-bar'] as const}, ), ); expectAssignable>( camelcaseKeys({} as Record), ); expectAssignable>( camelcaseKeys({} as Record, {deep: true}), ); interface SomeObject { someProperty: string; } const someObject: SomeObject = { someProperty: 'hello', }; expectType(camelcaseKeys(someObject)); expectType(camelcaseKeys([someObject])); type SomeTypeAlias = { someProperty: string; }; const objectWithTypeAlias = { someProperty: 'this should also work', }; expectType(camelcaseKeys(objectWithTypeAlias)); expectType(camelcaseKeys([objectWithTypeAlias])); // Using exported type expectType>(camelFooBarArray); const arrayItems = [{fooBar: true}, {fooBaz: true}] as const; expectType>(camelcaseKeys(arrayItems)); expectType>( camelcaseKeys({'foo-bar': true}), ); expectType>( camelcaseKeys({'--foo-bar': true}), ); expectType>( camelcaseKeys({foo_bar: true}), ); expectType>( camelcaseKeys({'foo bar': true}), ); expectType>( camelcaseKeys({'foo-bar': true} as const), ); expectType>( camelcaseKeys({'--foo-bar': true} as const), ); expectType>( camelcaseKeys({foo_bar: true} as const), ); expectType>( camelcaseKeys({'foo bar': true} as const), ); const nestedItem = {'foo-bar': {foo_bar: {'foo bar': true}}}; expectType>( camelcaseKeys(nestedItem, {deep: true}), ); expectType>( camelcaseKeys(objectOrUndefined, {deep: true}), ); expectType>( camelcaseKeys({'foo-bar': true}, {pascalCase: true}), ); expectType>( camelcaseKeys({'foo-bar': true} as const, {pascalCase: true}), ); expectType>( camelcaseKeys({'foo-bar': true}, {pascalCase: true}), ); expectType>( camelcaseKeys({'foo-bar': true}, {pascalCase: true}), ); expectType>( camelcaseKeys({'foo-bar': true}, {pascalCase: true}), ); expectType>( camelcaseKeys(nestedItem, {deep: true, pascalCase: true}), ); const data = {'foo-bar': true, foo_bar: true}; const exclude = ['foo', 'foo_bar', /bar/] as const; expectType>( camelcaseKeys(data, {exclude}), ); const nonNestedWithStopPathData = {'foo-bar': true, foo_bar: true}; expectType< CamelCaseKeys >(camelcaseKeys({'foo-bar': true}, {stopPaths: ['foo']})); const nestedWithStopPathData = { 'top-level': {'foo-bar': {'bar-baz': true}}, 'foo-foo': true, }; const stopPaths = ['top-level.foo-bar'] as const; expectType< CamelCaseKeys< typeof nestedWithStopPathData, true, false, // eslint-disable-next-line @typescript-eslint/ban-types [], typeof stopPaths > >(camelcaseKeys(nestedWithStopPathData, {deep: true, stopPaths})); expectAssignable>>( camelcaseKeys({} as Record), ); expectAssignable, true>>( camelcaseKeys({} as Record, {deep: true}), ); expectType>(camelcaseKeys(someObject)); expectType>(camelcaseKeys([someObject])); expectType>(camelcaseKeys(objectWithTypeAlias)); expectType>( camelcaseKeys([objectWithTypeAlias]), ); // Verify exported type `CamelcaseKeys` // Mapping types and retaining properties of keys // https://github.com/microsoft/TypeScript/issues/13224 type ObjectDataType = { foo_bar?: string; bar_baz?: string; baz: string; }; type InvalidConvertedObjectDataType = { fooBar: string; barBaz: string; baz: string; }; type ConvertedObjectDataType = { fooBar?: string; barBaz?: string; baz: string; }; const objectInputData: ObjectDataType = { foo_bar: 'foo_bar', baz: 'baz', }; expectType(camelcaseKeys(objectInputData)); expectNotType(camelcaseKeys(objectInputData)); // Array type ArrayDataType = ObjectDataType[]; const arrayInputData: ArrayDataType = [ { foo_bar: 'foo_bar', baz: 'baz', }, ]; expectType(camelcaseKeys(arrayInputData)); expectNotType(camelcaseKeys(arrayInputData)); // Deep type DeepObjectType = { foo_bar?: string; bar_baz?: string; baz: string; first_level: { foo_bar?: string; bar_baz?: string; second_level: { foo_bar: string; bar_baz?: string; }; }; }; type InvalidConvertedDeepObjectDataType = { fooBar?: string; barBaz?: string; baz: string; first_level?: { fooBar?: string; barBaz?: string; second_level?: { fooBar: string; barBaz?: string; }; }; }; type ConvertedDeepObjectDataType = { fooBar?: string; barBaz?: string; baz: string; firstLevel: { foo_bar?: string; bar_baz?: string; second_level: { foo_bar: string; bar_baz?: string; }; }; }; const deepInputData: DeepObjectType = { foo_bar: 'foo_bar', baz: 'baz', first_level: { bar_baz: 'bar_baz', second_level: { foo_bar: 'foo_bar', }, }, }; expectType( camelcaseKeys(deepInputData, {deep: false}), ); expectNotType( camelcaseKeys(deepInputData, {deep: false}), ); // Exclude type InvalidConvertedExcludeObjectDataType = { foo_bar?: string; bar_baz?: string; baz: string; }; type ConvertedExcludeObjectDataType = { foo_bar?: string; barBaz?: string; baz: string; }; const excludeInputData: ObjectDataType = { foo_bar: 'foo_bar', bar_baz: 'bar_baz', baz: 'baz', }; expectType( camelcaseKeys(excludeInputData, { exclude, }), ); expectNotType( camelcaseKeys(excludeInputData, { exclude, }), ); expectType<{ funcFoo: () => 'foo'; recordBar: {foo: string}; promiseBaz: Promise; }>( camelcaseKeys({ func_foo: () => 'foo', record_bar: {foo: 'bar'}, promise_baz: new Promise(resolve => { resolve(true); }), }), ); expectType<[ () => 'foo', {foo: string}, Promise, ]>( camelcaseKeys([ () => 'foo', {foo: 'bar'}, new Promise(resolve => { resolve(true); }), ]), ); camelcase-keys-8.0.2/license000066400000000000000000000021351430306474500157630ustar00rootroot00000000000000MIT License Copyright (c) Sindre Sorhus (https://sindresorhus.com) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. camelcase-keys-8.0.2/package.json000066400000000000000000000023641430306474500167100ustar00rootroot00000000000000{ "name": "camelcase-keys", "version": "8.0.2", "description": "Convert object keys to camel case", "license": "MIT", "repository": "sindresorhus/camelcase-keys", "funding": "https://github.com/sponsors/sindresorhus", "author": { "name": "Sindre Sorhus", "email": "sindresorhus@gmail.com", "url": "https://sindresorhus.com" }, "type": "module", "exports": "./index.js", "types": "./index.d.ts", "engines": { "node": ">=14.16" }, "scripts": { "test": "xo && ava && tsd", "bench": "matcha bench/bench.js" }, "files": [ "index.js", "index.d.ts" ], "keywords": [ "map", "obj", "object", "key", "keys", "value", "values", "val", "iterate", "camelcase", "camel-case", "camel", "case", "dash", "hyphen", "dot", "underscore", "separator", "string", "text", "convert", "pascalcase", "pascal-case", "deep", "recurse", "recursive" ], "dependencies": { "camelcase": "^7.0.0", "map-obj": "^4.3.0", "quick-lru": "^6.1.1", "type-fest": "^2.13.0" }, "devDependencies": { "ava": "^4.3.0", "matcha": "^0.7.0", "tsd": "^0.23.0", "xo": "^0.49.0" }, "xo": { "overrides": [ { "files": "bench/bench.js", "rules": { "import/no-unresolved": "off" } } ] } } camelcase-keys-8.0.2/readme.md000066400000000000000000000052431430306474500162000ustar00rootroot00000000000000# camelcase-keys > Convert object keys to camel case using [`camelcase`](https://github.com/sindresorhus/camelcase) ## Install ```sh npm install camelcase-keys ``` ## Usage ```js import camelcaseKeys from 'camelcase-keys'; // Convert an object camelcaseKeys({'foo-bar': true}); //=> {fooBar: true} // Convert an array of objects camelcaseKeys([{'foo-bar': true}, {'bar-foo': false}]); //=> [{fooBar: true}, {barFoo: false}] camelcaseKeys({'foo-bar': true, nested: {unicorn_rainbow: true}}, {deep: true}); //=> {fooBar: true, nested: {unicornRainbow: true}} camelcaseKeys({a_b: 1, a_c: {c_d: 1, c_e: {e_f: 1}}}, {deep: true, stopPaths: ['a_c.c_e']}), //=> {aB: 1, aC: {cD: 1, cE: {e_f: 1}}} // Convert object keys to pascal case camelcaseKeys({'foo-bar': true, nested: {unicorn_rainbow: true}}, {deep: true, pascalCase: true}); //=> {FooBar: true, Nested: {UnicornRainbow: true}} ``` ```js import {parseArgs} from 'node:utils'; import camelcaseKeys from 'camelcase-keys'; const commandLineArguments = parseArgs(); //=> {_: [], 'foo-bar': true} camelcaseKeys(commandLineArguments); //=> {_: [], fooBar: true} ``` ## API ### camelcaseKeys(input, options?) #### input Type: `object | object[]` An object or array of objects to camel-case. #### options Type: `object` ##### exclude Type: `Array`\ Default: `[]` Exclude keys from being camel-cased. ##### stopPaths Type: `string[]`\ Default: `[]` 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'`. ```js camelcaseKeys({ a_b: 1, a_c: { c_d: 1, c_e: { e_f: 1 } } }, { deep: true, stopPaths: [ 'a_c.c_e' ] }), /* { aB: 1, aC: { cD: 1, cE: { e_f: 1 } } } */ ``` ##### deep Type: `boolean`\ Default: `false` Recurse nested objects and objects in arrays. ##### pascalCase Type: `boolean`\ Default: `false` Uppercase the first character as in `bye-bye` → `ByeBye`. ## camelcase-keys for enterprise Available as part of the Tidelift Subscription. The maintainers of camelcase-keys and thousands of other packages are working with Tidelift to deliver commercial support and maintenance for the open source dependencies you use to build your applications. Save time, reduce risk, and improve code health, while paying the maintainers of the exact dependencies you use. [Learn more.](https://tidelift.com/subscription/pkg/npm-camelcase-keys?utm_source=npm-camelcase-keys&utm_medium=referral&utm_campaign=enterprise&utm_term=repo) ## Related - [snakecase-keys](https://github.com/bendrucker/snakecase-keys) - [kebabcase-keys](https://github.com/mattiloh/kebabcase-keys) camelcase-keys-8.0.2/test.js000066400000000000000000000111241430306474500157310ustar00rootroot00000000000000import process from 'node:process'; import {promisify} from 'node:util'; import {execFile} from 'node:child_process'; import test from 'ava'; import camelcaseKeys from './index.js'; const execFilePromise = promisify(execFile); test('main', t => { t.true(camelcaseKeys({'foo-bar': true}).fooBar); }); test('exclude option', t => { t.true(camelcaseKeys({'--': true}, {exclude: ['--']})['--']); t.deepEqual(camelcaseKeys({'foo-bar': true}, {exclude: [/^f/]}), {'foo-bar': true}); }); test('deep option', t => { t.deepEqual( // eslint-disable-next-line camelcase camelcaseKeys({foo_bar: true, obj: {one_two: false, arr: [{three_four: true}]}}, {deep: true}), {fooBar: true, obj: {oneTwo: false, arr: [{threeFour: true}]}}, ); }); test('stopPaths option', t => { t.deepEqual( // eslint-disable-next-line camelcase camelcaseKeys({foo_bar: true, obj: {one_two: false, arr: [{three_four: true}]}}, {deep: true, stopPaths: ['obj']}), // eslint-disable-next-line camelcase {fooBar: true, obj: {one_two: false, arr: [{three_four: true}]}}, ); t.deepEqual( // eslint-disable-next-line camelcase camelcaseKeys({foo_bar: true, obj: {one_two: false, arr: [{three_four: true}]}}, {deep: true, stopPaths: ['obj.arr']}), // eslint-disable-next-line camelcase {fooBar: true, obj: {oneTwo: false, arr: [{three_four: true}]}}, ); t.deepEqual( // eslint-disable-next-line camelcase camelcaseKeys({q_w_e: [[{foo_bar: 1}, {one_two: 2}, {foo_bar: 3, one_two: 4}]]}, {deep: true, stopPaths: ['q_w_e.foo_bar']}), {qWE: [[{fooBar: 1}, {oneTwo: 2}, {fooBar: 3, oneTwo: 4}]]}, ); t.deepEqual( // eslint-disable-next-line camelcase camelcaseKeys({a_b: 1, a_c: {c_d: 1, c_e: {e_f: 1}}}, {deep: true, stopPaths: ['a_c.c_e']}), // eslint-disable-next-line camelcase {aB: 1, aC: {cD: 1, cE: {e_f: 1}}}, ); }); test('pascalCase option only', t => { t.true(camelcaseKeys({'new-foo-bar': true}, {pascalCase: true}).NewFooBar); }); test('pascalCase and deep options', t => { t.deepEqual( // eslint-disable-next-line camelcase camelcaseKeys({p_foo_bar: true, p_obj: {p_two: false, p_arr: [{p_three_four: true}]}}, {deep: true, pascalCase: true}), {PFooBar: true, PObj: {PTwo: false, PArr: [{PThreeFour: true}]}}, ); }); test('handles nested arrays', t => { t.deepEqual( // eslint-disable-next-line camelcase camelcaseKeys({q_w_e: [['a', 'b']]}, {deep: true}), {qWE: [['a', 'b']]}, ); }); test('accepts an array of objects', t => { t.deepEqual( // eslint-disable-next-line camelcase camelcaseKeys([{foo_bar: true}, {bar_foo: false}, {'bar-foo': 'false'}]), [{fooBar: true}, {barFoo: false}, {barFoo: 'false'}], ); }); test('different pascalCase option values', t => { // eslint-disable-next-line camelcase t.true(camelcaseKeys({foo_bar_UPPERCASE: true}).fooBarUppercase); // eslint-disable-next-line camelcase t.true(camelcaseKeys({foo_bar_UPPERCASE: true}, {pascalCase: true}).FooBarUppercase); t.deepEqual( camelcaseKeys({'p-foo-bar': true, 'p-obj': {'p-two': false, 'p-arr': [{'p-three-four': true}]}}, {deep: true, pascalCase: true}), {PFooBar: true, PObj: {PTwo: false, PArr: [{PThreeFour: true}]}}, ); t.deepEqual( camelcaseKeys({'p-foo-bar': true, 'p-obj': {'p-two': false, 'p-arr': [{'p-three-four': true}]}}, {deep: true}), {pFooBar: true, pObj: {pTwo: false, pArr: [{pThreeFour: true}]}}, ); }); test('handle array of non-objects', t => { const input = ['name 1', 'name 2']; t.deepEqual( camelcaseKeys(input), input, ); }); test('handle array of non-objects with `deep` option', t => { const input = ['name 1', 'name 2']; t.deepEqual( camelcaseKeys(input, {deep: true}), input, ); }); test('use locale independent camel-case transformation', async t => { const input = {'user-id': 123}; t.deepEqual( // Execute the library with Turkish locale. // A locale dependent implementation would return `{userİd: 123}`. // See https://github.com/sindresorhus/camelcase-keys/issues/81 await runInTestProcess([input], {env: {...process.env, LC_ALL: 'tr'}}), {userId: 123}, ); }); /** Executes the library with the given arguments and resolves with the parsed result. Input and output is serialized via `JSON.stringify()` and `JSON.parse()`. */ const runInTestProcess = async (camelcaseKeysArgs, childProcessOptions = {}) => { const {stdout, stderr} = await execFilePromise( process.execPath, ['./fixtures/child-process-for-test.js', JSON.stringify(camelcaseKeysArgs)], childProcessOptions, ); if (stderr) { throw new Error(stderr); } try { return JSON.parse(stdout); } catch (error) { error.message = `Error parsing "${stdout}" as JSON: ${error.message}`; throw error; } };