Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 1 | declare namespace camelcaseKeys { |
| 2 | interface Options { |
| 3 | /** |
| 4 | Recurse nested objects and objects in arrays. |
| 5 | |
| 6 | @default false |
| 7 | */ |
| 8 | readonly deep?: boolean; |
| 9 | |
| 10 | /** |
| 11 | Exclude keys from being camel-cased. |
| 12 | |
| 13 | @default [] |
| 14 | */ |
| 15 | readonly exclude?: ReadonlyArray<string | RegExp>; |
| 16 | |
| 17 | /** |
| 18 | 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'`. |
| 19 | |
| 20 | @default [] |
| 21 | |
| 22 | @example |
| 23 | ``` |
| 24 | camelcaseKeys({ |
| 25 | a_b: 1, |
| 26 | a_c: { |
| 27 | c_d: 1, |
| 28 | c_e: { |
| 29 | e_f: 1 |
| 30 | } |
| 31 | } |
| 32 | }, { |
| 33 | deep: true, |
| 34 | stopPaths: [ |
| 35 | 'a_c.c_e' |
| 36 | ] |
| 37 | }), |
| 38 | // { |
| 39 | // aB: 1, |
| 40 | // aC: { |
| 41 | // cD: 1, |
| 42 | // cE: { |
| 43 | // e_f: 1 |
| 44 | // } |
| 45 | // } |
| 46 | // } |
| 47 | ``` |
| 48 | */ |
| 49 | readonly stopPaths?: ReadonlyArray<string>; |
| 50 | |
| 51 | /** |
| 52 | Uppercase the first character as in `bye-bye` → `ByeBye`. |
| 53 | |
| 54 | @default false |
| 55 | */ |
| 56 | readonly pascalCase?: boolean; |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | Convert object keys to camel case using [`camelcase`](https://github.com/sindresorhus/camelcase). |
| 62 | |
| 63 | @param input - Object or array of objects to camel-case. |
| 64 | |
| 65 | @example |
| 66 | ``` |
| 67 | import camelcaseKeys = require('camelcase-keys'); |
| 68 | |
| 69 | // Convert an object |
| 70 | camelcaseKeys({'foo-bar': true}); |
| 71 | //=> {fooBar: true} |
| 72 | |
| 73 | // Convert an array of objects |
| 74 | camelcaseKeys([{'foo-bar': true}, {'bar-foo': false}]); |
| 75 | //=> [{fooBar: true}, {barFoo: false}] |
| 76 | |
| 77 | camelcaseKeys({'foo-bar': true, nested: {unicorn_rainbow: true}}, {deep: true}); |
| 78 | //=> {fooBar: true, nested: {unicornRainbow: true}} |
| 79 | |
| 80 | // Convert object keys to pascal case |
| 81 | camelcaseKeys({'foo-bar': true, nested: {unicorn_rainbow: true}}, {deep: true, pascalCase: true}); |
| 82 | //=> {FooBar: true, Nested: {UnicornRainbow: true}} |
| 83 | |
| 84 | import minimist = require('minimist'); |
| 85 | |
| 86 | const argv = minimist(process.argv.slice(2)); |
| 87 | //=> {_: [], 'foo-bar': true} |
| 88 | |
| 89 | camelcaseKeys(argv); |
| 90 | //=> {_: [], fooBar: true} |
| 91 | ``` |
| 92 | */ |
| 93 | declare function camelcaseKeys<T extends ReadonlyArray<{[key: string]: any}>>( |
| 94 | input: T, |
| 95 | options?: camelcaseKeys.Options, |
| 96 | ): T; |
| 97 | |
| 98 | declare function camelcaseKeys<T extends {[key: string]: any}>( |
| 99 | input: T, |
| 100 | options?: camelcaseKeys.Options, |
| 101 | ): T; |
| 102 | |
| 103 | export = camelcaseKeys; |