Skip to content

Commit

Permalink
Use locale-independent camel-case transformation (#84)
Browse files Browse the repository at this point in the history
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
  • Loading branch information
jhnns and sindresorhus authored Feb 1, 2022
1 parent cb703c4 commit 23a0306
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 2 deletions.
6 changes: 6 additions & 0 deletions fixtures/child-process-for-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
'use strict';
const camelcaseKeys = require('..');

const camelcaseKeysArgs = JSON.parse(process.argv[2]);

console.log(JSON.stringify(camelcaseKeys(...camelcaseKeysArgs)));
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ const camelCaseConvert = (input, options) => {
if (cache.has(cacheKey)) {
key = cache.get(cacheKey);
} else {
const returnValue = camelCase(key, {pascalCase});
const returnValue = camelCase(key, {pascalCase, locale: false});

if (key.length < 100) { // Prevent abuse
cache.set(cacheKey, returnValue);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
"recursive"
],
"dependencies": {
"camelcase": "^6.2.0",
"camelcase": "^6.3.0",
"map-obj": "^4.1.0",
"quick-lru": "^5.1.1",
"type-fest": "^1.2.1"
Expand Down
39 changes: 39 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import {promisify} from 'util';
import {execFile} from 'child_process';
import test from 'ava';
import camelcaseKeys from '.';

const execFilePromise = promisify(execFile);

test('main', t => {
t.true(camelcaseKeys({'foo-bar': true}).fooBar);
});
Expand Down Expand Up @@ -106,3 +110,38 @@ test('handle array of non-objects with `deep` option', t => {
input
);
});

test('use locale independent camel-case transformation', async t => {
const input = {'user-id': 123};
t.deepEqual(
// Execute the library with Turkish locale.
// A locale dependent implementation would return `{userİd: 123}`.
// See https://github.com/sindresorhus/camelcase-keys/issues/81
await runInTestProcess([input], {env: {...process.env, LC_ALL: 'tr'}}),
{userId: 123}
);
});

/**
Executes the library with the given arguments and resolves with the parsed result.
Input and output is serialized via `JSON.stringify()` and `JSON.parse()`.
*/
const runInTestProcess = async (camelcaseKeysArgs, childProcessOptions = {}) => {
const {stdout, stderr} = await execFilePromise(
process.execPath,
['./fixtures/child-process-for-test.js', JSON.stringify(camelcaseKeysArgs)],
childProcessOptions
);

if (stderr) {
throw new Error(stderr);
}

try {
return JSON.parse(stdout);
} catch (error) {
error.message = `Error parsing "${stdout}" as JSON: ${error.message}`;
throw error;
}
};

0 comments on commit 23a0306

Please sign in to comment.