From e445b77c0f6a4884801bd5ed3260a02184cc3864 Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Mon, 6 Jun 2022 12:19:43 +0700 Subject: [PATCH] Require Node.js 14 and move to ESM --- .github/workflows/main.yml | 6 +- bench/bench.js | 7 +- fixtures/child-process-for-test.js | 4 +- index.d.ts | 146 +++++++++++----------- index.js | 44 +++---- index.test-d.ts | 188 ++++++++++++++--------------- package.json | 20 +-- readme.md | 13 +- test.js | 35 +++--- 9 files changed, 235 insertions(+), 228 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 3b8aa86..d50ada6 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -10,12 +10,12 @@ jobs: fail-fast: false matrix: node-version: + - 18 - 16 - 14 - - 12 steps: - - uses: actions/checkout@v2 - - uses: actions/setup-node@v2 + - uses: actions/checkout@v3 + - uses: actions/setup-node@v3 with: node-version: ${{ matrix.node-version }} - run: npm install diff --git a/bench/bench.js b/bench/bench.js index b423c7f..c5dbe44 100644 --- a/bench/bench.js +++ b/bench/bench.js @@ -1,8 +1,7 @@ /* globals bench suite set */ -'use strict'; -const camelcaseKeysNpm = require('camelcase-keys'); -const fixture = require('./fixture'); -const camelcaseKeys = require('..'); +import camelcaseKeysNpm from 'camelcase-keys'; +import camelcaseKeys from '../index.js'; +import fixture from './fixture.js'; suite('camelcaseKeys', () => { set('mintime', 1000); diff --git a/fixtures/child-process-for-test.js b/fixtures/child-process-for-test.js index dd86978..ac3da37 100644 --- a/fixtures/child-process-for-test.js +++ b/fixtures/child-process-for-test.js @@ -1,5 +1,5 @@ -'use strict'; -const camelcaseKeys = require('..'); +import process from 'node:process'; +import camelcaseKeys from '../index.js'; const camelcaseKeysArgs = JSON.parse(process.argv[2]); diff --git a/index.d.ts b/index.d.ts index 91e3407..e1e9416 100644 --- a/index.d.ts +++ b/index.d.ts @@ -9,12 +9,11 @@ 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; +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. - -TODO: Remove this once https://github.com/sindresorhus/type-fest/pull/217 is merged. */ type IsInclude = List extends undefined ? false @@ -42,7 +41,7 @@ export type CamelCaseKeys< IsPascalCase extends boolean = false, Exclude extends readonly unknown[] = EmptyTuple, StopPaths extends readonly string[] = EmptyTuple, - Path extends string = '' + Path extends string = '', > = T extends readonly any[] // Handle arrays or tuples. ? { @@ -62,7 +61,7 @@ export type CamelCaseKeys< : [IsPascalCase] extends [true] ? PascalCase

: CamelCase

]: [IsInclude>] extends [ - true + true, ] ? T[P] : [Deep] extends [true] @@ -79,68 +78,68 @@ export type CamelCaseKeys< // Return anything else as-is. : T; -declare namespace camelcaseKeys { - interface Options { - /** - Recurse nested objects and objects in arrays. +type Options = { + /** + Recurse nested objects and objects in arrays. + + @default false + */ + readonly deep?: boolean; - @default false - */ - readonly deep?: boolean; + /** + Exclude keys from being camel-cased. - /** - Exclude keys from being camel-cased. + If this option can be statically determined, it's recommended to add `as const` to it. - If this option can be statically determined, it's recommended to add `as const` to it. + @default [] + */ + readonly exclude?: ReadonlyArray; - @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'`. - /** - 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. - If this option can be statically determined, it's recommended to add `as const` to it. + @default [] - @default [] + @example + ``` + import camelcaseKeys from 'camelcase-keys'; - @example - ``` - camelcaseKeys({ - a_b: 1, - a_c: { - c_d: 1, - c_e: { - e_f: 1 - } + 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; - } -} + } + }, { + 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). @@ -149,7 +148,7 @@ Convert object keys to camel case using [`camelcase`](https://github.com/sindres @example ``` -import camelcaseKeys = require('camelcase-keys'); +import camelcaseKeys from 'camelcase-keys'; // Convert an object camelcaseKeys({'foo-bar': true}); @@ -162,31 +161,36 @@ camelcaseKeys([{'foo-bar': true}, {'bar-foo': 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}} +``` -import minimist = require('minimist'); +@example +``` +import {parseArgs} from 'node:utils'; +import camelcaseKeys from 'camelcase-keys'; -const argv = minimist(process.argv.slice(2)); +const commandLineArguments = parseArgs(); //=> {_: [], 'foo-bar': true} -camelcaseKeys(argv); +camelcaseKeys(commandLineArguments); //=> {_: [], fooBar: true} ``` */ -declare function camelcaseKeys< +export default function camelcaseKeys< T extends Record | readonly any[], - Options extends camelcaseKeys.Options = camelcaseKeys.Options + OptionsType extends Options = Options, >( input: T, - options?: Options + options?: OptionsType ): CamelCaseKeys< T, -WithDefault, -WithDefault, -WithDefault, -WithDefault +WithDefault, +WithDefault, +WithDefault, +WithDefault >; - -export default camelcaseKeys; diff --git a/index.js b/index.js index 9f4e261..ce85f1a 100644 --- a/index.js +++ b/index.js @@ -1,26 +1,26 @@ -'use strict'; -const mapObj = require('map-obj'); -const camelCase = require('camelcase'); -const QuickLru = require('quick-lru'); - -const has = (array, key) => array.some(x => { - if (typeof x === 'string') { - return x === key; +import 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; } - x.lastIndex = 0; - return x.test(key); + element.lastIndex = 0; + + return element.test(key); }); -const cache = new QuickLru({maxSize: 100000}); +const cache = new QuickLru({maxSize: 100_000}); -// Reproduces behavior from `map-obj` +// Reproduces behavior from `map-obj`. const isObject = value => - typeof value === 'object' && - value !== null && - !(value instanceof RegExp) && - !(value instanceof Error) && - !(value instanceof Date); + typeof value === 'object' + && value !== null + && !(value instanceof RegExp) + && !(value instanceof Error) + && !(value instanceof Date); const camelCaseConvert = (input, options) => { if (!isObject(input)) { @@ -30,7 +30,7 @@ const camelCaseConvert = (input, options) => { options = { deep: false, pascalCase: false, - ...options + ...options, }; const {exclude, pascalCase, stopPaths, deep} = options; @@ -42,7 +42,7 @@ const camelCaseConvert = (input, options) => { const path = parentPath === undefined ? key : `${parentPath}.${key}`; if (!stopPathsSet.has(path)) { - value = mapObj(value, makeMapper(path)); + value = mapObject(value, makeMapper(path)); } } @@ -65,13 +65,13 @@ const camelCaseConvert = (input, options) => { return [key, value]; }; - return mapObj(input, makeMapper(undefined)); + return mapObject(input, makeMapper(undefined)); }; -module.exports = (input, options) => { +export default function camelcaseKeys(input, options) { if (Array.isArray(input)) { return Object.keys(input).map(key => camelCaseConvert(input[key], options)); } return camelCaseConvert(input, options); -}; +} diff --git a/index.test-d.ts b/index.test-d.ts index 054eb27..f2b2f99 100644 --- a/index.test-d.ts +++ b/index.test-d.ts @@ -1,37 +1,37 @@ +/* eslint-disable @typescript-eslint/naming-convention */ import {expectType, expectAssignable, expectNotType} from 'tsd'; -import type {CamelCaseKeys} from '.'; -import camelcaseKeys from '.'; +import camelcaseKeys, {type CamelCaseKeys} from './index.js'; const fooBarObject = {'foo-bar': true}; const camelFooBarObject = camelcaseKeys(fooBarObject); -expectType<{ fooBar: boolean }>(camelFooBarObject); +expectType<{fooBar: boolean}>(camelFooBarObject); const fooBarArray = [{'foo-bar': true}]; const camelFooBarArray = camelcaseKeys(fooBarArray); -expectType>(camelFooBarArray); +expectType>(camelFooBarArray); -expectType>(camelcaseKeys([{'foo-bar': true}])); +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( + 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<{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<{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: {fooBar: {fooBar: boolean}}}>( + camelcaseKeys({'foo-bar': {foo_bar: {'foo bar': true}}}, {deep: true}), ); interface ObjectOrUndefined { @@ -47,60 +47,60 @@ interface ObjectOrUndefined { const objectOrUndefined: ObjectOrUndefined = { foo_bar: { foo_bar: { - foo_bar: true - } - } + foo_bar: true, + }, + }, }; -expectType<{ fooBar: { fooBar: { fooBar: boolean } | undefined } }>( - camelcaseKeys(objectOrUndefined, {deep: true}) +expectType<{fooBar: {fooBar: {fooBar: boolean} | undefined}}>( + camelcaseKeys(objectOrUndefined, {deep: true}), ); -expectType<{ FooBar: boolean }>( - camelcaseKeys({'foo-bar': true}, {pascalCase: true}) +expectType<{FooBar: boolean}>( + camelcaseKeys({'foo-bar': true}, {pascalCase: true}), ); -expectType<{ readonly FooBar: true }>( - camelcaseKeys({'foo-bar': true} as const, {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: 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 } } }>( +expectType<{FooBar: {FooBar: {FooBar: boolean}}}>( camelcaseKeys( {'foo-bar': {foo_bar: {'foo bar': true}}}, - {deep: true, pascalCase: true} - ) + {deep: true, pascalCase: true}, + ), ); -expectType<{ fooBar: boolean; foo_bar: true }>( +expectType<{fooBar: boolean; foo_bar: true}>( camelcaseKeys( {'foo-bar': true, foo_bar: true}, - {exclude: ['foo', 'foo_bar', /bar/] as const} - ) + {exclude: ['foo', 'foo_bar', /bar/] as const}, + ), ); -expectType<{ fooBar: boolean }>( - camelcaseKeys({'foo-bar': true}, {stopPaths: ['foo']}) +expectType<{fooBar: boolean}>( + camelcaseKeys({'foo-bar': true}, {stopPaths: ['foo']}), ); -expectType<{ topLevel: { fooBar: { 'bar-baz': boolean } }; fooFoo: boolean }>( +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} - ) + {deep: true, stopPaths: ['top-level.foo-bar'] as const}, + ), ); expectAssignable>( - camelcaseKeys({} as Record) + camelcaseKeys({} as Record), ); expectAssignable>( - camelcaseKeys({} as Record, {deep: true}) + camelcaseKeys({} as Record, {deep: true}), ); interface SomeObject { @@ -108,7 +108,7 @@ interface SomeObject { } const someObject: SomeObject = { - someProperty: 'hello' + someProperty: 'hello', }; expectType(camelcaseKeys(someObject)); @@ -119,7 +119,7 @@ type SomeTypeAlias = { }; const objectWithTypeAlias = { - someProperty: 'this should also work' + someProperty: 'this should also work', }; expectType(camelcaseKeys(objectWithTypeAlias)); @@ -131,65 +131,65 @@ 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}) +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), ); -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}) + camelcaseKeys(nestedItem, {deep: true}), ); expectType>( - camelcaseKeys(objectOrUndefined, {deep: true}) + camelcaseKeys(objectOrUndefined, {deep: true}), ); -expectType>( - camelcaseKeys({'foo-bar': true}, {pascalCase: true}) +expectType>( + camelcaseKeys({'foo-bar': true}, {pascalCase: true}), ); -expectType>( - camelcaseKeys({'foo-bar': true} as const, {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({'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}) + 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}) + camelcaseKeys(data, {exclude}), ); const nonNestedWithStopPathData = {'foo-bar': true, foo_bar: true}; @@ -198,7 +198,7 @@ CamelCaseKeys >(camelcaseKeys({'foo-bar': true}, {stopPaths: ['foo']})); const nestedWithStopPathData = { 'top-level': {'foo-bar': {'bar-baz': true}}, - 'foo-foo': true + 'foo-foo': true, }; const stopPaths = ['top-level.foo-bar'] as const; expectType< @@ -213,11 +213,11 @@ false, >(camelcaseKeys(nestedWithStopPathData, {deep: true, stopPaths})); expectAssignable>>( - camelcaseKeys({} as Record) + camelcaseKeys({} as Record), ); expectAssignable, true>>( - camelcaseKeys({} as Record, {deep: true}) + camelcaseKeys({} as Record, {deep: true}), ); expectType>(camelcaseKeys(someObject)); @@ -225,7 +225,7 @@ expectType>(camelcaseKeys([someObject])); expectType>(camelcaseKeys(objectWithTypeAlias)); expectType>( - camelcaseKeys([objectWithTypeAlias]) + camelcaseKeys([objectWithTypeAlias]), ); // Verify exported type `CamelcaseKeys` @@ -250,7 +250,7 @@ type ConvertedObjectDataType = { const objectInputData: ObjectDataType = { foo_bar: 'foo_bar', - baz: 'baz' + baz: 'baz', }; expectType(camelcaseKeys(objectInputData)); expectNotType(camelcaseKeys(objectInputData)); @@ -261,8 +261,8 @@ type ArrayDataType = ObjectDataType[]; const arrayInputData: ArrayDataType = [ { foo_bar: 'foo_bar', - baz: 'baz' - } + baz: 'baz', + }, ]; expectType(camelcaseKeys(arrayInputData)); expectNotType(camelcaseKeys(arrayInputData)); @@ -313,15 +313,15 @@ const deepInputData: DeepObjectType = { first_level: { bar_baz: 'bar_baz', second_level: { - foo_bar: 'foo_bar' - } - } + foo_bar: 'foo_bar', + }, + }, }; expectType( - camelcaseKeys(deepInputData, {deep: false}) + camelcaseKeys(deepInputData, {deep: false}), ); expectNotType( - camelcaseKeys(deepInputData, {deep: false}) + camelcaseKeys(deepInputData, {deep: false}), ); // Exclude @@ -338,15 +338,15 @@ type ConvertedExcludeObjectDataType = { const excludeInputData: ObjectDataType = { foo_bar: 'foo_bar', bar_baz: 'bar_baz', - baz: 'baz' + baz: 'baz', }; expectType( camelcaseKeys(excludeInputData, { - exclude - }) + exclude, + }), ); expectNotType( camelcaseKeys(excludeInputData, { - exclude - }) + exclude, + }), ); diff --git a/package.json b/package.json index 888a138..eb2a38b 100644 --- a/package.json +++ b/package.json @@ -10,8 +10,11 @@ "email": "sindresorhus@gmail.com", "url": "https://sindresorhus.com" }, + "type": "module", + "exports": "./index.js", + "types": "./index.d.ts", "engines": { - "node": ">=12" + "node": ">=14.16" }, "scripts": { "test": "xo && ava && tsd", @@ -50,17 +53,16 @@ "recursive" ], "dependencies": { - "camelcase": "^6.3.0", - "map-obj": "^4.1.0", - "quick-lru": "^5.1.1", - "type-fest": "^1.2.1" + "camelcase": "^7.0.0", + "map-obj": "^4.3.0", + "quick-lru": "^6.1.1", + "type-fest": "^2.13.0" }, "devDependencies": { - "ava": "^2.4.0", + "ava": "^4.3.0", "matcha": "^0.7.0", - "tsd": "^0.17.0", - "typescript": "^4.3.5", - "xo": "^0.36.1" + "tsd": "^0.20.0", + "xo": "^0.49.0" }, "xo": { "overrides": [ diff --git a/readme.md b/readme.md index 7874e55..d4abe56 100644 --- a/readme.md +++ b/readme.md @@ -4,14 +4,14 @@ ## Install -``` -$ npm install camelcase-keys +```sh +npm install camelcase-keys ``` ## Usage ```js -const camelcaseKeys = require('camelcase-keys'); +import camelcaseKeys from 'camelcase-keys'; // Convert an object camelcaseKeys({'foo-bar': true}); @@ -33,12 +33,13 @@ camelcaseKeys({'foo-bar': true, nested: {unicorn_rainbow: true}}, {deep: true, p ``` ```js -const camelcaseKeys = require('camelcase-keys'); +import {parseArgs} from 'node:utils'; +import camelcaseKeys from 'camelcase-keys'; -const argv = require('minimist')(process.argv.slice(2)); +const commandLineArguments = parseArgs(); //=> {_: [], 'foo-bar': true} -camelcaseKeys(argv); +camelcaseKeys(commandLineArguments); //=> {_: [], fooBar: true} ``` diff --git a/test.js b/test.js index 5c37ae2..2fbc895 100644 --- a/test.js +++ b/test.js @@ -1,7 +1,8 @@ -import {promisify} from 'util'; -import {execFile} from 'child_process'; +import process from 'node:process'; +import {promisify} from 'node:util'; +import {execFile} from 'node:child_process'; import test from 'ava'; -import camelcaseKeys from '.'; +import camelcaseKeys from './index.js'; const execFilePromise = promisify(execFile); @@ -18,7 +19,7 @@ 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}]}} + {fooBar: true, obj: {oneTwo: false, arr: [{threeFour: true}]}}, ); }); @@ -27,27 +28,27 @@ test('stopPaths option', t => { // 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}]}} + {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}]}} + {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}]]} + {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}}} + {aB: 1, aC: {cD: 1, cE: {e_f: 1}}}, ); }); @@ -59,7 +60,7 @@ 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}]}} + {PFooBar: true, PObj: {PTwo: false, PArr: [{PThreeFour: true}]}}, ); }); @@ -67,7 +68,7 @@ test('handles nested arrays', t => { t.deepEqual( // eslint-disable-next-line camelcase camelcaseKeys({q_w_e: [['a', 'b']]}, {deep: true}), - {qWE: [['a', 'b']]} + {qWE: [['a', 'b']]}, ); }); @@ -75,7 +76,7 @@ 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'}] + [{fooBar: true}, {barFoo: false}, {barFoo: 'false'}], ); }); @@ -87,11 +88,11 @@ test('different pascalCase option values', t => { 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}]}} + {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}]}} + {pFooBar: true, pObj: {pTwo: false, pArr: [{pThreeFour: true}]}}, ); }); @@ -99,7 +100,7 @@ test('handle array of non-objects', t => { const input = ['name 1', 'name 2']; t.deepEqual( camelcaseKeys(input), - input + input, ); }); @@ -107,7 +108,7 @@ test('handle array of non-objects with `deep` option', t => { const input = ['name 1', 'name 2']; t.deepEqual( camelcaseKeys(input, {deep: true}), - input + input, ); }); @@ -118,7 +119,7 @@ test('use locale independent camel-case transformation', async t => { // 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} + {userId: 123}, ); }); @@ -131,7 +132,7 @@ const runInTestProcess = async (camelcaseKeysArgs, childProcessOptions = {}) => const {stdout, stderr} = await execFilePromise( process.execPath, ['./fixtures/child-process-for-test.js', JSON.stringify(camelcaseKeysArgs)], - childProcessOptions + childProcessOptions, ); if (stderr) {