camelcase-keys
Advanced tools
Comparing version 6.0.1 to 6.1.0
@@ -11,3 +11,3 @@ declare namespace camelcaseKeys { | ||
/** | ||
Exclude keys from being camelCased. | ||
Exclude keys from being camel-cased. | ||
@@ -17,2 +17,43 @@ @default [] | ||
readonly exclude?: ReadonlyArray<string | RegExp>; | ||
/** | ||
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 [] | ||
@example | ||
``` | ||
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?: ReadonlyArray<string>; | ||
/** | ||
Uppercase the first character as in `bye-bye` → `ByeBye`. | ||
@default false | ||
*/ | ||
readonly pascalCase?: boolean; | ||
} | ||
@@ -41,2 +82,5 @@ } | ||
// Convert object keys to pascal case | ||
camelcaseKeys({'foo-bar': true, nested: {unicorn_rainbow: true}}, {deep: true, pascalCase: true}); | ||
//=> {FooBar: true, Nested: {UnicornRainbow: true}} | ||
@@ -43,0 +87,0 @@ import minimist = require('minimist'); |
27
index.js
@@ -9,11 +9,28 @@ 'use strict'; | ||
// Reproduces behavior from `map-obj` | ||
const isObject = value => | ||
typeof value === 'object' && | ||
value !== null && | ||
!(value instanceof RegExp) && | ||
!(value instanceof Error) && | ||
!(value instanceof Date); | ||
const camelCaseConvert = (input, options) => { | ||
options = { | ||
deep: false, | ||
pascalCase: false, | ||
...options | ||
}; | ||
const {exclude} = options; | ||
const {exclude, pascalCase, stopPaths, deep} = options; | ||
return mapObj(input, (key, value) => { | ||
const stopPathsSet = stopPaths === undefined ? new Set() : new Set(stopPaths); | ||
const makeMapper = parentPath => (key, value) => { | ||
const path = parentPath === undefined ? key : `${parentPath}.${key}`; | ||
if (deep && isObject(value) && !stopPathsSet.has(path)) { | ||
value = mapObj(value, makeMapper(path)); | ||
} | ||
if (!(exclude && has(exclude, key))) { | ||
@@ -23,3 +40,3 @@ if (cache.has(key)) { | ||
} else { | ||
const ret = camelCase(key); | ||
const ret = camelCase(key, {pascalCase}); | ||
@@ -35,3 +52,5 @@ if (key.length < 100) { // Prevent abuse | ||
return [key, value]; | ||
}, {deep: options.deep}); | ||
}; | ||
return mapObj(input, makeMapper(undefined)); | ||
}; | ||
@@ -38,0 +57,0 @@ |
{ | ||
"name": "camelcase-keys", | ||
"version": "6.0.1", | ||
"version": "6.1.0", | ||
"description": "Convert object keys to camel case", | ||
@@ -57,4 +57,4 @@ "license": "MIT", | ||
"matcha": "^0.7.0", | ||
"tsd": "^0.7.3", | ||
"xo": "^0.24.0" | ||
"tsd": "^0.9.0", | ||
"xo": "^0.25.3" | ||
}, | ||
@@ -61,0 +61,0 @@ "xo": { |
@@ -28,2 +28,9 @@ # camelcase-keys [![Build Status](https://travis-ci.org/sindresorhus/camelcase-keys.svg?branch=master)](https://travis-ci.org/sindresorhus/camelcase-keys) | ||
//=> {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}} | ||
``` | ||
@@ -63,2 +70,37 @@ | ||
##### stopPaths | ||
Type: `string[]`<br> | ||
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 | ||
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 | ||
} | ||
} | ||
} | ||
*/ | ||
``` | ||
##### deep | ||
@@ -71,3 +113,10 @@ | ||
##### pascalCase | ||
Type: `boolean`<br> | ||
Default: `false` | ||
Uppercase the first character as in `bye-bye` → `ByeBye`. | ||
## Related | ||
@@ -74,0 +123,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
8555
130
136