pax_global_header00006660000000000000000000000064151017022110014500gustar00rootroot0000000000000052 comment=6a35bb190a494f233864a0d958fad162869e9603 sindresorhus-camelcase-keys-439f9fa/000077500000000000000000000000001510170221100175435ustar00rootroot00000000000000sindresorhus-camelcase-keys-439f9fa/.editorconfig000066400000000000000000000002571510170221100222240ustar00rootroot00000000000000root = 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 sindresorhus-camelcase-keys-439f9fa/.gitattributes000066400000000000000000000000231510170221100224310ustar00rootroot00000000000000* text=auto eol=lf sindresorhus-camelcase-keys-439f9fa/.github/000077500000000000000000000000001510170221100211035ustar00rootroot00000000000000sindresorhus-camelcase-keys-439f9fa/.github/security.md000066400000000000000000000002631510170221100232750ustar00rootroot00000000000000# Security Policy To report a security vulnerability, please use the [Tidelift security contact](https://tidelift.com/security). Tidelift will coordinate the fix and disclosure. sindresorhus-camelcase-keys-439f9fa/.github/workflows/000077500000000000000000000000001510170221100231405ustar00rootroot00000000000000sindresorhus-camelcase-keys-439f9fa/.github/workflows/main.yml000066400000000000000000000006451510170221100246140ustar00rootroot00000000000000name: CI on: - push - pull_request jobs: test: name: Node.js ${{ matrix.node-version }} runs-on: ubuntu-latest strategy: fail-fast: false matrix: node-version: - 24 - 20 steps: - uses: actions/checkout@v5 - uses: actions/setup-node@v5 with: node-version: ${{ matrix.node-version }} - run: npm install - run: npm test sindresorhus-camelcase-keys-439f9fa/.gitignore000066400000000000000000000000271510170221100215320ustar00rootroot00000000000000node_modules yarn.lock sindresorhus-camelcase-keys-439f9fa/.npmrc000066400000000000000000000000231510170221100206560ustar00rootroot00000000000000package-lock=false sindresorhus-camelcase-keys-439f9fa/bench/000077500000000000000000000000001510170221100206225ustar00rootroot00000000000000sindresorhus-camelcase-keys-439f9fa/bench/bench.js000066400000000000000000000005431510170221100222410ustar00rootroot00000000000000/* 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}); }); }); sindresorhus-camelcase-keys-439f9fa/bench/fixture.json000066400000000000000000000023721510170221100232070ustar00rootroot00000000000000{ "_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" } sindresorhus-camelcase-keys-439f9fa/bench/package.json000066400000000000000000000000651510170221100231110ustar00rootroot00000000000000{ "devDependencies": { "camelcase-keys": "*" } } sindresorhus-camelcase-keys-439f9fa/fixtures/000077500000000000000000000000001510170221100214145ustar00rootroot00000000000000sindresorhus-camelcase-keys-439f9fa/fixtures/child-process-for-test.js000066400000000000000000000003101510170221100262440ustar00rootroot00000000000000import process from 'node:process'; import camelcaseKeys from '../index.js'; const camelcaseKeysArgs = JSON.parse(process.argv[2]); console.log(JSON.stringify(camelcaseKeys(...camelcaseKeysArgs))); sindresorhus-camelcase-keys-439f9fa/index.d.ts000066400000000000000000000153731510170221100214550ustar00rootroot00000000000000import type {CamelCase, PascalCase} from 'type-fest'; // Type that accepts both interfaces and type aliases // eslint-disable-next-line @typescript-eslint/consistent-indexed-object-style type ObjectLike = {[key: string]: any}; // Helper to check if a key is in the exclude list type IsExcluded = Exclude extends readonly never[] ? false : Exclude extends readonly [infer First, ...infer Rest] ? K extends First ? true : IsExcluded : false; // Helper to check if a path should stop transformation type IsStopPath = StopPaths extends readonly never[] ? false : StopPaths extends readonly [infer First, ...infer Rest extends readonly string[]] ? Path extends First ? true : IsStopPath : false; // Build dot-notation path type AppendPath = Base extends '' ? Key : `${Base}.${Key}`; /** 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: Use `result.key as OpaqueType` if needed. */ export 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 extends ObjectLike ? // Handle objects { [K in keyof T as IsExcluded extends true ? K : IsPascalCase extends true ? PascalCase : CamelCase ]: ProcessValue } : T; // Return non-objects as-is // Process a value, checking if we should recurse 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, StopPaths> extends true ? V // Stop recursion at this path : Deep extends true ? V extends ObjectLike | readonly any[] ? CamelCaseKeys> : V : V; // Process array elements 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 : E extends ObjectLike ? CamelCaseKeys : E; export type Options = { /** Exclude keys from being camel-cased. @default [] For correct TypeScript types when using this option with a string array, add `as const` to the array. */ readonly exclude?: ReadonlyArray; /** 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'`. @default [] For correct TypeScript types when using this option, add `as const` to the array. @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 // } // } // } ``` When an object is inside an array, the path is specified without array indices. A `stopPath` will apply to all items in the array. ``` import camelcaseKeys from 'camelcase-keys'; const object = { foo: [ { bar: { baz_qux: 'value' } } ] }; camelcaseKeys(object, { deep: true, stopPaths: [ 'foo.bar' ] }), // { // foo: [ // { // bar: { // baz_qux: 'value' // } // } // ] // } ``` */ readonly stopPaths?: readonly string[]; }; /** 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}] ``` @example ``` import {parseArgs} from 'node:util'; import camelcaseKeys from 'camelcase-keys'; const commandLineArguments = parseArgs(); //=> {_: [], 'foo-bar': true} camelcaseKeys(commandLineArguments); //=> {_: [], fooBar: true} ``` */ export default function 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[] >; sindresorhus-camelcase-keys-439f9fa/index.js000066400000000000000000000074711510170221100212210ustar00rootroot00000000000000import 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); }); // Preserve numeric keys. The trim check excludes empty/whitespace strings, // which Number() would incorrectly convert to 0. const isNumericKey = key => key.trim() !== '' && !Number.isNaN(Number(key)); const cache = new QuickLru({maxSize: 100_000}); const isBuiltIn = value => ArrayBuffer.isView(value) // Check for known built-in types || value instanceof Date || value instanceof RegExp || value instanceof Error || value instanceof Map || value instanceof Set || value instanceof WeakMap || value instanceof WeakSet || value instanceof Promise; // Reproduces behavior from `map-obj`. const isObject = value => typeof value === 'object' && value !== null && !isBuiltIn(value); const transform = (input, options = {}, isSeen = new WeakMap(), parentPath) => { if (!isObject(input)) { return input; } // Check for circular references if (isSeen.has(input)) { return isSeen.get(input); } const { exclude, pascalCase = false, stopPaths, deep = false, preserveConsecutiveUppercase = false, } = options; const stopPathsSet = new Set(stopPaths); // Handle arrays directly if (Array.isArray(input)) { const result = []; isSeen.set(input, result); for (const item of input) { result.push(isObject(item) ? transform(item, options, isSeen, parentPath) : item); } return result; } // Pre-allocate the result object for circular reference handling const result = {}; isSeen.set(input, result); const makeMapper = currentParentPath => (key, value) => { // Handle deep transformation if (deep && isObject(value)) { const path = currentParentPath === undefined ? key : `${currentParentPath}.${key}`; if (!stopPathsSet.has(path)) { // Handle arrays and objects recursively value = Array.isArray(value) ? value.map(item => isObject(item) ? transform(item, options, isSeen, path) : item) : transform(value, options, isSeen, path); } } // Skip transformation for excluded keys, numeric keys // Only transform string keys (preserve symbols and numbers) if (typeof key === 'string' && !isNumericKey(key) && !(exclude && has(exclude, key))) { const cacheKey = pascalCase ? `${key}_` : key; if (cache.has(cacheKey)) { key = cache.get(cacheKey); } else { // Preserve leading `_` and `$` as they typically have semantic meaning // This should eventually be fixed in the `camelcase` package itself const leadingPrefix = key.match(/^[_$]*/)[0]; const transformed = camelCase(key.slice(leadingPrefix.length), {pascalCase, locale: false, preserveConsecutiveUppercase}); key = leadingPrefix + transformed; // Only cache reasonable length keys to prevent memory abuse if (key.length < 100) { cache.set(cacheKey, key); } } } return [key, value]; }; const mappedResult = mapObject(input, makeMapper(parentPath), {deep: false}); // Copy properties to the pre-allocated result for circular reference handling Object.assign(result, mappedResult); // Preserve symbol keys (mapObject doesn't handle them) const symbols = Object.getOwnPropertySymbols(input); for (const symbol of symbols) { result[symbol] = deep && isObject(input[symbol]) ? transform(input[symbol], options, isSeen, parentPath) : input[symbol]; } return result; }; export default function camelcaseKeys(input, options) { const isSeen = new WeakMap(); if (Array.isArray(input)) { // More efficient array handling - directly map the array return input.map(item => isObject(item) ? transform(item, options, isSeen, undefined) : item); } return transform(input, options, isSeen); } sindresorhus-camelcase-keys-439f9fa/index.test-d.ts000066400000000000000000000417341510170221100224320ustar00rootroot00000000000000/* eslint-disable @typescript-eslint/naming-convention */ 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([{'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})); expectType<{fooBar: Array<{fooBar: {fooBar: boolean}}>}>(camelcaseKeys({'foo-bar': [{foo_bar: {'foo bar': true}}]}, {deep: true})); type 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}>(camelcaseKeys({'foo-BAR': true}, {preserveConsecutiveUppercase: true})); expectType<{readonly fooBAR: true}>(camelcaseKeys({foo_BAR: true} as const, {preserveConsecutiveUppercase: true})); expectType<{fooBAR: boolean}>(camelcaseKeys({'--foo-BAR': true}, {preserveConsecutiveUppercase: true})); expectType<{fooBAR: boolean}>(camelcaseKeys({foo_BAR: true}, {preserveConsecutiveUppercase: true})); expectType<{fooBAR: boolean}>(camelcaseKeys({'foo BAR': true}, {preserveConsecutiveUppercase: true})); expectType<{FooBAR: boolean}>(camelcaseKeys({'foo BAR': true}, {preserveConsecutiveUppercase: true, pascalCase: true})); expectType<{fooBAR: {fooBAR: {fooBAR: boolean}}}>(camelcaseKeys( {'foo-BAR': {foo_BAR: {'foo BAR': true}}}, {deep: true, preserveConsecutiveUppercase: 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})); type SomeObject = { someProperty: string; }; const someObject: SomeObject = { someProperty: 'hello', }; expectType(camelcaseKeys(someObject)); expectType(camelcaseKeys([someObject])); expectType<{someObject: SomeObject}>(camelcaseKeys({some_object: someObject})); expectType>(camelcaseKeys([{some_object: 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, false, readonly never[], 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; }; }; optional_first_level?: { foo_bar?: string; bar_baz?: true; optional_second_level?: { foo_bar: number; }; }; }; type InvalidConvertedDeepObjectDataType = { fooBar?: string; barBaz?: string; baz: string; first_level?: { fooBar?: string; barBaz?: string; second_level?: { fooBar: string; barBaz?: string; }; }; optional_first_level?: { fooBar?: string; barBaz?: true; optional_second_level?: { fooBar: number; }; }; }; type ConvertedDeepObjectDataType = { fooBar?: string; barBaz?: string; baz: string; firstLevel: { foo_bar?: string; bar_baz?: string; second_level: { foo_bar: string; bar_baz?: string; }; }; optionalFirstLevel?: { foo_bar?: string; bar_baz?: true; optional_second_level?: { foo_bar: number; }; }; }; type ConvertedDeepObjectDataTypeWithDeepTrue = { fooBar?: string | undefined; barBaz?: string | undefined; baz: string; firstLevel: { fooBar?: string | undefined; barBaz?: string | undefined; secondLevel: { fooBar: string; barBaz?: string | undefined; }; }; optionalFirstLevel?: { fooBar?: string; barBaz?: true; optionalSecondLevel?: { fooBar: number; }; }; }; 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})); expectType(camelcaseKeys(deepInputData, {deep: true})); 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); }), })); // Test for function with inferred type function camelcaseKeysDeep< T extends Record | ReadonlyArray>, >(response: T): CamelCaseKeys { return camelcaseKeys(response, {deep: true}); } function camelcaseKeysPascalCase< T extends Record | ReadonlyArray>, >(response: T): CamelCaseKeys { return camelcaseKeys(response, {pascalCase: true}); } expectType<{fooBar: {hogeHoge: string}}>(camelcaseKeysDeep({foo_bar: {hoge_hoge: 'hoge_hoge'}})); expectType<{FooBar: string}>(camelcaseKeysPascalCase({foo_bar: 'foo_bar'})); // Test for union type const objectCamelcased: CamelCaseKeys<{foo_bar: {foo_prop: string} | undefined}, true> = camelcaseKeys({foo_bar: {foo_prop: 'foo_props'}}, {deep: true}); const nullCamelcased: CamelCaseKeys<{foo_bar: {foo_prop: string} | undefined}, true> = camelcaseKeys({foo_bar: undefined}, {deep: true}); expectType<{fooBar: {fooProp: string} | undefined}>(objectCamelcased); expectType<{fooBar: {fooProp: string} | undefined}>(nullCamelcased); // Test for union type in arrays (Issue #130) const arrayOfUnionType: CamelCaseKeys< { foo_bar: Array<{foo_prop: string} | undefined>; }, true > = camelcaseKeys({foo_bar: [{foo_prop: 'foo_prop'}]}, {deep: true}); // This should work without TypeScript errors expectType<{fooBar: Array<{fooProp: string} | undefined>}>(arrayOfUnionType); // Test with null union (similar to original issue but following project style) const arrayOfUnionWithNull: CamelCaseKeys< { foo_bar: Array<{foo_prop: string} | undefined>; }, true > = camelcaseKeys({foo_bar: [{foo_prop: 'foo_prop'}]}, {deep: true}); expectType<{fooBar: Array<{fooProp: string} | undefined>}>(arrayOfUnionWithNull); // Test for issue #114 - Interface type constraint issue (Fixed) // TypeScript interfaces can now be used directly with camelcase-keys // Using interface specifically to test interface support (not type alias) // eslint-disable-next-line @typescript-eslint/consistent-type-definitions interface TestInterface { snake_case_prop: string; another_prop: number; } // Direct usage with interface now works after removing the constraint const interfaceTest: TestInterface = {snake_case_prop: 'test', another_prop: 123}; const interfaceResult = camelcaseKeys(interfaceTest); // Check that the transformation works (runtime will transform keys correctly) // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment const _typeCheck: typeof interfaceResult = {} as any; // Test nested interfaces // eslint-disable-next-line @typescript-eslint/consistent-type-definitions interface NestedInterface { outer_key: { inner_key: string; }; } const nestedInterfaceTest: NestedInterface = {outer_key: {inner_key: 'value'}}; const nestedInterfaceResult = camelcaseKeys(nestedInterfaceTest, {deep: true}); // Check that nested transformation works // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment const _nestedCheck: typeof nestedInterfaceResult = {} as any; // Test arrays of interfaces const interfaceArray: TestInterface[] = [ {snake_case_prop: 'test1', another_prop: 1}, {snake_case_prop: 'test2', another_prop: 2}, ]; const interfaceArrayResult = camelcaseKeys(interfaceArray); // Check that array transformation works // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment const _arrayCheck: typeof interfaceArrayResult = {} as any; // Test numeric string keys are preserved (issue #68) // eslint-disable-next-line @stylistic/quote-props expectType<{'4.2': string}>(camelcaseKeys({'4.2': 'foo'})); expectType<{42: string}>(camelcaseKeys({42: 'foo'})); // eslint-disable-next-line @stylistic/quote-props expectType<{'42': string}>(camelcaseKeys({'42': 'foo'})); // Numeric keys should be preserved with deep option // eslint-disable-next-line @stylistic/quote-props expectType<{fooBar: {'4.2': string}}>(camelcaseKeys({foo_bar: {'4.2': 'nested'}}, {deep: true})); // Numeric keys in arrays // eslint-disable-next-line @stylistic/quote-props expectType>(camelcaseKeys([{'4.2': 'foo'}])); // Non-numeric keys starting with numbers should still be transformed expectType<{'42Foo': string}>(camelcaseKeys({'42-foo': 'value'})); // Test for issue #83 - exclude without `as const` type Language = { iso_639_1: string; name: string; }; type Image = { aspect_ratio: number; file_path: string; height: number; iso_639_1: string | undefined; vote_average: number; vote_count: number; width: number; }; type MovieDetails = { id: number; spoken_languages: Language[]; images: Image[]; }; const movieData: MovieDetails = { id: 1, spoken_languages: [ {iso_639_1: 'en', name: 'English'}, ], images: [ { aspect_ratio: 1.5, file_path: '/path.jpg', height: 100, iso_639_1: 'en', vote_average: 5, vote_count: 10, width: 150, }, ], }; // WITHOUT `as const` - the problematic case const result1 = camelcaseKeys(movieData, { exclude: ['iso_639_1', 'iso_3166_1'], deep: true, }); // Since exclude is string[], TypeScript can't properly exclude specific keys // It will convert everything to camelCase expectType<{ id: number; spokenLanguages: Array<{ iso6391: string; // Bug: should be iso_639_1 but TypeScript doesn't know name: string; }>; images: Array<{ aspectRatio: number; filePath: string; height: number; iso6391: string | undefined; // Bug: should be iso_639_1 but TypeScript doesn't know voteAverage: number; voteCount: number; width: number; }>; }>(result1); // WITH `as const` - this works correctly const result2 = camelcaseKeys(movieData, { exclude: ['iso_639_1', 'iso_3166_1'] as const, deep: true, }); // With as const, TypeScript knows the exact strings in the exclude array expectType<{ id: number; spokenLanguages: Array<{ iso_639_1: string; // Correctly preserved name: string; }>; images: Array<{ aspectRatio: number; filePath: string; height: number; iso_639_1: string | undefined; // Correctly preserved voteAverage: number; voteCount: number; width: number; }>; }>(result2); sindresorhus-camelcase-keys-439f9fa/license000066400000000000000000000021351510170221100211110ustar00rootroot00000000000000MIT 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. sindresorhus-camelcase-keys-439f9fa/package.json000066400000000000000000000023251510170221100220330ustar00rootroot00000000000000{ "name": "camelcase-keys", "version": "10.0.1", "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": { "types": "./index.d.ts", "default": "./index.js" }, "sideEffects": false, "engines": { "node": ">=20" }, "scripts": { "test": "xo && ava && tsd", "bench": "matcha bench/bench.js" }, "files": [ "index.js", "index.d.ts" ], "keywords": [ "map", "object", "key", "keys", "value", "values", "iterate", "camelcase", "camel-case", "camel", "case", "dash", "hyphen", "dot", "underscore", "separator", "string", "text", "convert", "pascalcase", "pascal-case", "deep", "recurse", "recursive" ], "dependencies": { "camelcase": "^8.0.0", "map-obj": "5.0.2", "quick-lru": "^7.1.0", "type-fest": "^4.41.0" }, "devDependencies": { "ava": "^6.4.1", "matcha": "^0.7.0", "tsd": "^0.33.0", "xo": "^1.2.2" }, "xo": { "rules": { "@typescript-eslint/unified-signatures": "off" } } } sindresorhus-camelcase-keys-439f9fa/readme.md000066400000000000000000000067611510170221100213340ustar00rootroot00000000000000# 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}] ``` ```js import {parseArgs} from 'node:util'; import camelcaseKeys from 'camelcase-keys'; const commandLineArguments = parseArgs(); //=> {_: [], 'foo-bar': true} camelcaseKeys(commandLineArguments); //=> {_: [], fooBar: true} ``` Leading `_` and `$` are preserved as they have semantic meaning. ```js camelcaseKeys({_foo_bar: true, $baz_qux: true}); //=> {_fooBar: true, $bazQux: true} ``` ## API ### camelcaseKeys(input, options?) #### input Type: `Record | ReadonlyArray>` A plain object or array of plain objects to camel-case. #### options Type: `object` ##### exclude Type: `Array`\ Default: `[]` Exclude keys from being camel-cased. For correct TypeScript types when using this option with a string array, add `as const` to the array. ##### deep Type: `boolean`\ Default: `false` Recurse nested objects and objects in arrays. ```js 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}} ``` ##### pascalCase Type: `boolean`\ Default: `false` Uppercase the first character: `bye-bye` → `ByeBye` ```js import camelcaseKeys from 'camelcase-keys'; camelcaseKeys({'foo-bar': true}, {pascalCase: true}); //=> {FooBar: true} camelcaseKeys({'foo-bar': true}, {pascalCase: false}); //=> {fooBar: true} ```` ##### preserveConsecutiveUppercase Type: `boolean`\ Default: `false` Preserve consecutive uppercase characters: `foo-BAR` → `FooBAR` ```js import camelcaseKeys from 'camelcase-keys'; camelcaseKeys({'foo-BAR': true}, {preserveConsecutiveUppercase: true}); //=> {fooBAR: true} camelcaseKeys({'foo-BAR': true}, {preserveConsecutiveUppercase: false}); //=> {fooBar: true} ```` ##### stopPaths Type: `string[]`\ Default: `[]` Exclude children at the given object paths in dot-notation from being camel-cased. For correct TypeScript types when using this option, add `as const` to the array. For example, with an object like `{a: {b: '🦄'}}`, the object path to reach the unicorn is `'a.b'`. ```js 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 } } } */ ``` When an object is inside an array, the path is specified without array indices. A `stopPath` will apply to all items in the array. ```js import camelcaseKeys from 'camelcase-keys'; const object = { foo: [ { bar: { baz_qux: 'value' } } ] }; camelcaseKeys(object, { deep: true, stopPaths: [ 'foo.bar' ] }), /* { foo: [ { bar: { baz_qux: 'value' } } ] } */ ``` ## Related - [decamelize-keys](https://github.com/sindresorhus/decamelize-keys) - The inverse of this package - [snakecase-keys](https://github.com/bendrucker/snakecase-keys) - [kebabcase-keys](https://github.com/mattiloh/kebabcase-keys) sindresorhus-camelcase-keys-439f9fa/test.js000066400000000000000000000337031510170221100210660ustar00rootroot00000000000000import 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('stopPaths works with arrays', t => { // Issue #65: stopPaths should work across array boundaries t.deepEqual( camelcaseKeys({ foo: [ { bar: { // eslint-disable-next-line camelcase baz_qux: 'value', }, }, ], }, {deep: true, stopPaths: ['foo.bar']}), { foo: [ { bar: { // eslint-disable-next-line camelcase baz_qux: 'value', }, }, ], }, ); // StopPaths applies to all items in the array t.deepEqual( camelcaseKeys({ // eslint-disable-next-line camelcase my_items: [ // eslint-disable-next-line camelcase {nested_obj: {deep_value: 1}}, // eslint-disable-next-line camelcase {nested_obj: {deep_value: 2}}, ], }, {deep: true, stopPaths: ['my_items.nested_obj']}), { myItems: [ // eslint-disable-next-line camelcase {nestedObj: {deep_value: 1}}, // eslint-disable-next-line camelcase {nestedObj: {deep_value: 2}}, ], }, ); // Top-level arrays work consistently with nested arrays t.deepEqual( camelcaseKeys([ { // eslint-disable-next-line camelcase user_name: 'john', // eslint-disable-next-line camelcase user_meta: { // eslint-disable-next-line camelcase created_at: '2023', }, }, { // eslint-disable-next-line camelcase user_name: 'jane', // eslint-disable-next-line camelcase user_meta: { // eslint-disable-next-line camelcase created_at: '2024', }, }, ], {deep: true, stopPaths: ['user_meta']}), [ { userName: 'john', userMeta: { // eslint-disable-next-line camelcase created_at: '2023', }, }, { userName: 'jane', userMeta: { // eslint-disable-next-line camelcase created_at: '2024', }, }, ], ); }); test('preserveConsecutiveUppercase option only', t => { // eslint-disable-next-line camelcase t.true(camelcaseKeys({new_foo_BAR: true}, {preserveConsecutiveUppercase: true}).newFooBAR); }); test('preserveConsecutiveUppercase 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, preserveConsecutiveUppercase: true}), {pFOOBar: true, pObj: {pTwo: false, pArr: [{pTHREEFour: true}]}}, ); }); 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('handle null and undefined inputs gracefully', t => { // These should not throw errors and should return the input as-is t.is(camelcaseKeys(null), null); t.is(camelcaseKeys(undefined), undefined); t.is(camelcaseKeys(123), 123); t.is(camelcaseKeys('hello'), 'hello'); t.is(camelcaseKeys(true), true); t.is(camelcaseKeys(false), false); // With options t.is(camelcaseKeys(null, {deep: true}), null); t.is(camelcaseKeys(undefined, {deep: true}), undefined); t.is(camelcaseKeys(123, {pascalCase: true}), 123); t.is(camelcaseKeys('hello', {deep: true}), 'hello'); // Arrays with mixed null/undefined values // eslint-disable-next-line camelcase const mixedArray = [null, undefined, 'string', 123, true, {snake_case: 'value'}]; const expected = [null, undefined, 'string', 123, true, {snakeCase: 'value'}]; t.deepEqual(camelcaseKeys(mixedArray), expected); t.deepEqual(camelcaseKeys(mixedArray, {deep: true}), expected); }); test('handle circular references', t => { // Simple self-reference const simple = {}; simple.self = simple; const simpleResult = camelcaseKeys(simple, {deep: true}); t.is(simpleResult.self, simpleResult); // With key transformation // eslint-disable-next-line camelcase const withSnakeCase = {some_key: 'value'}; // eslint-disable-next-line camelcase withSnakeCase.self_ref = withSnakeCase; const snakeResult = camelcaseKeys(withSnakeCase, {deep: true}); t.is(snakeResult.someKey, 'value'); t.is(snakeResult.selfRef, snakeResult); // Nested circular reference // eslint-disable-next-line camelcase const nested = {outer_key: {inner_key: 'value'}}; // eslint-disable-next-line camelcase nested.outer_key.back_ref = nested; const nestedResult = camelcaseKeys(nested, {deep: true}); t.is(nestedResult.outerKey.innerKey, 'value'); t.is(nestedResult.outerKey.backRef, nestedResult); // Multiple circular references const object1 = {name: 'object1'}; const object2 = {name: 'object2'}; // eslint-disable-next-line camelcase object1.other_obj = object2; // eslint-disable-next-line camelcase object2.other_obj = object1; // eslint-disable-next-line camelcase object1.self_ref = object1; const multiResult1 = camelcaseKeys(object1, {deep: true}); const multiResult2 = multiResult1.otherObj; t.is(multiResult1.name, 'object1'); t.is(multiResult2.name, 'object2'); t.is(multiResult1.selfRef, multiResult1); t.is(multiResult2.otherObj, multiResult1); // Circular reference in array // eslint-disable-next-line camelcase const arrayCircular = {some_items: []}; arrayCircular.some_items.push(arrayCircular); const arrayResult = camelcaseKeys(arrayCircular, {deep: true}); t.is(arrayResult.someItems[0], arrayResult); // Without deep option should not cause issues const shallowCircular = {}; shallowCircular.self = shallowCircular; t.notThrows(() => { const result = camelcaseKeys(shallowCircular, {deep: false}); t.is(result.self, shallowCircular); // Points to original, not transformed }); }); 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}, ); }); test('do not deep convert built-in types', t => { const date = new Date('2024-01-01'); const regex = /test/; const typedArray = new Uint8Array([1, 2, 3]); // eslint-disable-next-line camelcase const result = camelcaseKeys({foo_date: date, foo_regex: regex, foo_buffer: typedArray}, {deep: true}); t.is(result.fooDate, date); t.is(result.fooRegex, regex); t.is(result.fooBuffer, typedArray); }); test('deep convert custom class instances', t => { class CustomClass { constructor() { // eslint-disable-next-line camelcase this.foo_bar = 'value'; } } // eslint-disable-next-line camelcase const result = camelcaseKeys({my_obj: new CustomClass()}, {deep: true}); t.is(result.myObj.fooBar, 'value'); }); test('preserve numeric string keys', t => { // Float strings should be preserved (issue #68) // eslint-disable-next-line @stylistic/quote-props t.deepEqual(camelcaseKeys({'4.2': 'foo'}), {'4.2': 'foo'}); // Integer strings should be preserved // eslint-disable-next-line @stylistic/quote-props t.deepEqual(camelcaseKeys({'42': 'foo'}), {'42': 'foo'}); // Negative numbers should be preserved t.deepEqual( camelcaseKeys({'-42': 'foo', '-4.2': 'bar'}), {'-42': 'foo', '-4.2': 'bar'}, ); // Scientific notation should be preserved t.deepEqual( camelcaseKeys({'1e5': 'foo'}), {'1e5': 'foo'}, ); // Special numeric values should be preserved t.deepEqual( camelcaseKeys({Infinity: 'foo'}), {Infinity: 'foo'}, ); // Keys starting with numbers but containing non-numeric characters should be transformed t.deepEqual( camelcaseKeys({'42-foo': 'value'}), {'42Foo': 'value'}, ); // With deep option // eslint-disable-next-line camelcase, @stylistic/quote-props t.deepEqual(camelcaseKeys({foo_bar: {'4.2': 'nested'}}, {deep: true}), {fooBar: {'4.2': 'nested'}}); // In arrays // eslint-disable-next-line @stylistic/quote-props t.deepEqual(camelcaseKeys([{'4.2': 'foo'}, {42: 'bar'}]), [{'4.2': 'foo'}, {42: 'bar'}]); }); test('preserve leading underscores and dollar signs', t => { // Single leading underscore t.deepEqual(camelcaseKeys({_name: true}), {_name: true}); // eslint-disable-next-line camelcase t.deepEqual(camelcaseKeys({_name_obj: 'value'}), {_nameObj: 'value'}); // eslint-disable-next-line camelcase t.deepEqual(camelcaseKeys({_foo_bar: true}), {_fooBar: true}); // Single leading dollar sign // eslint-disable-next-line camelcase t.deepEqual(camelcaseKeys({$foo_bar: true}), {$fooBar: true}); t.deepEqual(camelcaseKeys({$element: true}), {$element: true}); // Multiple leading underscores // eslint-disable-next-line camelcase t.deepEqual(camelcaseKeys({__foo_bar: 'value'}), {__fooBar: 'value'}); // eslint-disable-next-line camelcase t.deepEqual(camelcaseKeys({___foo_bar: 'value'}), {___fooBar: 'value'}); // Multiple leading dollar signs // eslint-disable-next-line camelcase t.deepEqual(camelcaseKeys({$$foo_bar: 'value'}), {$$fooBar: 'value'}); // Mixed leading characters // eslint-disable-next-line camelcase t.deepEqual(camelcaseKeys({$_foo_bar: 'value'}), {$_fooBar: 'value'}); // eslint-disable-next-line camelcase t.deepEqual(camelcaseKeys({_$foo_bar: 'value'}), {_$fooBar: 'value'}); // eslint-disable-next-line camelcase t.deepEqual(camelcaseKeys({__$foo_bar: 'value'}), {__$fooBar: 'value'}); // With pascalCase option // eslint-disable-next-line camelcase t.deepEqual(camelcaseKeys({_foo_bar: true}, {pascalCase: true}), {_FooBar: true}); // eslint-disable-next-line camelcase t.deepEqual(camelcaseKeys({$foo_bar: true}, {pascalCase: true}), {$FooBar: true}); // With deep option t.deepEqual( // eslint-disable-next-line camelcase camelcaseKeys({_outer_key: {$inner_key: 'value'}}, {deep: true}), {_outerKey: {$innerKey: 'value'}}, ); // In arrays t.deepEqual( // eslint-disable-next-line camelcase camelcaseKeys([{_foo_bar: true}, {$bar_foo: false}]), [{_fooBar: true}, {$barFoo: false}], ); // With preserveConsecutiveUppercase option // eslint-disable-next-line camelcase t.deepEqual(camelcaseKeys({_foo_BAR: true}, {preserveConsecutiveUppercase: true}), {_fooBAR: true}); // eslint-disable-next-line camelcase t.deepEqual(camelcaseKeys({$foo_BAR: true}, {preserveConsecutiveUppercase: true}), {$fooBAR: true}); }); /** 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; } };