pax_global_header00006660000000000000000000000064150601232530014507gustar00rootroot0000000000000052 comment=fbe25dd494e172f717dc491f0f5c27b8e5661a84 sindresorhus-camelcase-keys-1f19a3a/000077500000000000000000000000001506012325300175325ustar00rootroot00000000000000sindresorhus-camelcase-keys-1f19a3a/.editorconfig000066400000000000000000000002571506012325300222130ustar00rootroot00000000000000root = 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-1f19a3a/.gitattributes000066400000000000000000000000231506012325300224200ustar00rootroot00000000000000* text=auto eol=lf sindresorhus-camelcase-keys-1f19a3a/.github/000077500000000000000000000000001506012325300210725ustar00rootroot00000000000000sindresorhus-camelcase-keys-1f19a3a/.github/security.md000066400000000000000000000002631506012325300232640ustar00rootroot00000000000000# 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-1f19a3a/.github/workflows/000077500000000000000000000000001506012325300231275ustar00rootroot00000000000000sindresorhus-camelcase-keys-1f19a3a/.github/workflows/main.yml000066400000000000000000000006451506012325300246030ustar00rootroot00000000000000name: 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-1f19a3a/.gitignore000066400000000000000000000000271506012325300215210ustar00rootroot00000000000000node_modules yarn.lock sindresorhus-camelcase-keys-1f19a3a/.npmrc000066400000000000000000000000231506012325300206450ustar00rootroot00000000000000package-lock=false sindresorhus-camelcase-keys-1f19a3a/bench/000077500000000000000000000000001506012325300206115ustar00rootroot00000000000000sindresorhus-camelcase-keys-1f19a3a/bench/bench.js000066400000000000000000000005431506012325300222300ustar00rootroot00000000000000/* 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-1f19a3a/bench/fixture.json000066400000000000000000000023721506012325300231760ustar00rootroot00000000000000{ "_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-1f19a3a/bench/package.json000066400000000000000000000000651506012325300231000ustar00rootroot00000000000000{ "devDependencies": { "camelcase-keys": "*" } } sindresorhus-camelcase-keys-1f19a3a/fixtures/000077500000000000000000000000001506012325300214035ustar00rootroot00000000000000sindresorhus-camelcase-keys-1f19a3a/fixtures/child-process-for-test.js000066400000000000000000000003101506012325300262330ustar00rootroot00000000000000import 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-1f19a3a/index.d.ts000066400000000000000000000144631506012325300214430ustar00rootroot00000000000000import 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. If this option can be statically determined, it's recommended to add `as const` to it. @default [] */ 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'`. 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[]; }; /** 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-1f19a3a/index.js000066400000000000000000000061731506012325300212060ustar00rootroot00000000000000import 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 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 // Only transform string keys (preserve symbols and numbers) if (typeof key === 'string' && !(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, preserveConsecutiveUppercase}); // Only cache reasonable length keys to prevent memory abuse if (key.length < 100) { cache.set(cacheKey, returnValue); } key = returnValue; } } 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, index) => isObject(item) ? transform(item, options, isSeen, String(index)) : item); } return transform(input, options, isSeen); } sindresorhus-camelcase-keys-1f19a3a/index.test-d.ts000066400000000000000000000345551506012325300224240ustar00rootroot00000000000000/* 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; sindresorhus-camelcase-keys-1f19a3a/license000066400000000000000000000021351506012325300211000ustar00rootroot00000000000000MIT 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-1f19a3a/package.json000066400000000000000000000022051506012325300220170ustar00rootroot00000000000000{ "name": "camelcase-keys", "version": "10.0.0", "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" } } sindresorhus-camelcase-keys-1f19a3a/readme.md000066400000000000000000000054061506012325300213160ustar00rootroot00000000000000# 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} ``` ## 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. ##### 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 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 } } } */ ``` ## 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-1f19a3a/test.js000066400000000000000000000200211506012325300210420ustar00rootroot00000000000000import 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('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}, ); }); /** 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; } };