Skip to content

Commit 23a0306

Browse files
jhnnssindresorhus
andauthored
Use locale-independent camel-case transformation (#84)
Co-authored-by: Sindre Sorhus <[email protected]>
1 parent cb703c4 commit 23a0306

File tree

4 files changed

+47
-2
lines changed

4 files changed

+47
-2
lines changed

fixtures/child-process-for-test.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
'use strict';
2+
const camelcaseKeys = require('..');
3+
4+
const camelcaseKeysArgs = JSON.parse(process.argv[2]);
5+
6+
console.log(JSON.stringify(camelcaseKeys(...camelcaseKeysArgs)));

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ const camelCaseConvert = (input, options) => {
5252
if (cache.has(cacheKey)) {
5353
key = cache.get(cacheKey);
5454
} else {
55-
const returnValue = camelCase(key, {pascalCase});
55+
const returnValue = camelCase(key, {pascalCase, locale: false});
5656

5757
if (key.length < 100) { // Prevent abuse
5858
cache.set(cacheKey, returnValue);

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
"recursive"
5151
],
5252
"dependencies": {
53-
"camelcase": "^6.2.0",
53+
"camelcase": "^6.3.0",
5454
"map-obj": "^4.1.0",
5555
"quick-lru": "^5.1.1",
5656
"type-fest": "^1.2.1"

test.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1+
import {promisify} from 'util';
2+
import {execFile} from 'child_process';
13
import test from 'ava';
24
import camelcaseKeys from '.';
35

6+
const execFilePromise = promisify(execFile);
7+
48
test('main', t => {
59
t.true(camelcaseKeys({'foo-bar': true}).fooBar);
610
});
@@ -106,3 +110,38 @@ test('handle array of non-objects with `deep` option', t => {
106110
input
107111
);
108112
});
113+
114+
test('use locale independent camel-case transformation', async t => {
115+
const input = {'user-id': 123};
116+
t.deepEqual(
117+
// Execute the library with Turkish locale.
118+
// A locale dependent implementation would return `{userİd: 123}`.
119+
// See https://github.com/sindresorhus/camelcase-keys/issues/81
120+
await runInTestProcess([input], {env: {...process.env, LC_ALL: 'tr'}}),
121+
{userId: 123}
122+
);
123+
});
124+
125+
/**
126+
Executes the library with the given arguments and resolves with the parsed result.
127+
128+
Input and output is serialized via `JSON.stringify()` and `JSON.parse()`.
129+
*/
130+
const runInTestProcess = async (camelcaseKeysArgs, childProcessOptions = {}) => {
131+
const {stdout, stderr} = await execFilePromise(
132+
process.execPath,
133+
['./fixtures/child-process-for-test.js', JSON.stringify(camelcaseKeysArgs)],
134+
childProcessOptions
135+
);
136+
137+
if (stderr) {
138+
throw new Error(stderr);
139+
}
140+
141+
try {
142+
return JSON.parse(stdout);
143+
} catch (error) {
144+
error.message = `Error parsing "${stdout}" as JSON: ${error.message}`;
145+
throw error;
146+
}
147+
};

0 commit comments

Comments
 (0)