UNPKG

3.81 kBMarkdownView Raw
1# camelcase
2
3> Convert a dash/dot/underscore/space separated string to camelCase or PascalCase: `foo-bar` → `fooBar`
4
5Correctly handles Unicode strings.
6
7If you use this on untrusted user input, don't forget to limit the length to something reasonable.
8
9## Install
10
11```sh
12npm install camelcase
13```
14
15## Usage
16
17```js
18import camelCase from 'camelcase';
19
20camelCase('foo-bar');
21//=> 'fooBar'
22
23camelCase('foo_bar');
24//=> 'fooBar'
25
26camelCase('Foo-Bar');
27//=> 'fooBar'
28
29camelCase('розовый_пушистый_единорог');
30//=> 'розовыйПушистыйЕдинорог'
31
32camelCase('Foo-Bar', {pascalCase: true});
33//=> 'FooBar'
34
35camelCase('--foo.bar', {pascalCase: false});
36//=> 'fooBar'
37
38camelCase('Foo-BAR', {preserveConsecutiveUppercase: true});
39//=> 'fooBAR'
40
41camelCase('fooBAR', {pascalCase: true, preserveConsecutiveUppercase: true}));
42//=> 'FooBAR'
43
44camelCase('foo bar');
45//=> 'fooBar'
46
47console.log(process.argv[3]);
48//=> '--foo-bar'
49camelCase(process.argv[3]);
50//=> 'fooBar'
51
52camelCase(['foo', 'bar']);
53//=> 'fooBar'
54
55camelCase(['__foo__', '--bar'], {pascalCase: true});
56//=> 'FooBar'
57
58camelCase(['foo', 'BAR'], {pascalCase: true, preserveConsecutiveUppercase: true})
59//=> 'FooBAR'
60
61camelCase('lorem-ipsum', {locale: 'en-US'});
62//=> 'loremIpsum'
63```
64
65## API
66
67### camelCase(input, options?)
68
69#### input
70
71Type: `string | string[]`
72
73String to convert to camel case.
74
75#### options
76
77Type: `object`
78
79##### pascalCase
80
81Type: `boolean`\
82Default: `false`
83
84Uppercase the first character: `foo-bar``FooBar`
85
86##### preserveConsecutiveUppercase
87
88Type: `boolean`\
89Default: `false`
90
91Preserve consecutive uppercase characters: `foo-BAR``FooBAR`.
92
93##### locale
94
95Type: `false | string | string[]`\
96Default: The host environment’s current locale.
97
98The locale parameter indicates the locale to be used to convert to upper/lower case according to any locale-specific case mappings. If multiple locales are given in an array, the best available locale is used.
99
100```js
101import camelCase from 'camelcase';
102
103camelCase('lorem-ipsum', {locale: 'en-US'});
104//=> 'loremIpsum'
105
106camelCase('lorem-ipsum', {locale: 'tr-TR'});
107//=> 'loremİpsum'
108
109camelCase('lorem-ipsum', {locale: ['en-US', 'en-GB']});
110//=> 'loremIpsum'
111
112camelCase('lorem-ipsum', {locale: ['tr', 'TR', 'tr-TR']});
113//=> 'loremİpsum'
114```
115
116Setting `locale: false` ignores the platform locale and uses the [Unicode Default Case Conversion](https://unicode-org.github.io/icu/userguide/transforms/casemappings.html#simple-single-character-case-mapping) algorithm:
117
118```js
119import camelCase from 'camelcase';
120
121// On a platform with 'tr-TR'
122
123camelCase('lorem-ipsum');
124//=> 'loremİpsum'
125
126camelCase('lorem-ipsum', {locale: false});
127//=> 'loremIpsum'
128```
129
130## camelcase for enterprise
131
132Available as part of the Tidelift Subscription.
133
134The maintainers of camelcase and thousands of other packages are working with Tidelift to deliver commercial support and maintenance for the open source dependencies you use to build your applications. Save time, reduce risk, and improve code health, while paying the maintainers of the exact dependencies you use. [Learn more.](https://tidelift.com/subscription/pkg/npm-camelcase?utm_source=npm-camelcase&utm_medium=referral&utm_campaign=enterprise&utm_term=repo)
135
136## Related
137
138- [decamelize](https://github.com/sindresorhus/decamelize) - The inverse of this module
139- [uppercamelcase](https://github.com/SamVerschueren/uppercamelcase) - Like this module, but to PascalCase instead of camelCase
140- [titleize](https://github.com/sindresorhus/titleize) - Capitalize every word in string
141- [humanize-string](https://github.com/sindresorhus/humanize-string) - Convert a camelized/dasherized/underscored string into a humanized one
142- [camelcase-keys](https://github.com/sindresorhus/camelcase-keys) - Convert object keys to camel case