UNPKG

3.53 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```
12$ npm install camelcase
13```
14
15*If you need to support Firefox < 78, stay on version 5 as version 6 uses regex features not available in Firefox < 78.*
16
17## Usage
18
19```js
20const camelCase = require('camelcase');
21
22camelCase('foo-bar');
23//=> 'fooBar'
24
25camelCase('foo_bar');
26//=> 'fooBar'
27
28camelCase('Foo-Bar');
29//=> 'fooBar'
30
31camelCase('розовый_пушистый_единорог');
32//=> 'розовыйПушистыйЕдинорог'
33
34camelCase('Foo-Bar', {pascalCase: true});
35//=> 'FooBar'
36
37camelCase('--foo.bar', {pascalCase: false});
38//=> 'fooBar'
39
40camelCase('Foo-BAR', {preserveConsecutiveUppercase: true});
41//=> 'fooBAR'
42
43camelCase('fooBAR', {pascalCase: true, preserveConsecutiveUppercase: true}));
44//=> 'FooBAR'
45
46camelCase('foo bar');
47//=> 'fooBar'
48
49console.log(process.argv[3]);
50//=> '--foo-bar'
51camelCase(process.argv[3]);
52//=> 'fooBar'
53
54camelCase(['foo', 'bar']);
55//=> 'fooBar'
56
57camelCase(['__foo__', '--bar'], {pascalCase: true});
58//=> 'FooBar'
59
60camelCase(['foo', 'BAR'], {pascalCase: true, preserveConsecutiveUppercase: true})
61//=> 'FooBAR'
62
63camelCase('lorem-ipsum', {locale: 'en-US'});
64//=> 'loremIpsum'
65```
66
67## API
68
69### camelCase(input, options?)
70
71#### input
72
73Type: `string | string[]`
74
75String to convert to camel case.
76
77#### options
78
79Type: `object`
80
81##### pascalCase
82
83Type: `boolean`\
84Default: `false`
85
86Uppercase the first character: `foo-bar``FooBar`
87
88##### preserveConsecutiveUppercase
89
90Type: `boolean`\
91Default: `false`
92
93Preserve the consecutive uppercase characters: `foo-BAR``FooBAR`.
94
95##### locale
96
97Type: `string | string[]`\
98Default: The host environment’s current locale.
99
100The 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.
101
102```js
103const camelCase = require('camelcase');
104
105camelCase('lorem-ipsum', {locale: 'en-US'});
106//=> 'loremIpsum'
107
108camelCase('lorem-ipsum', {locale: 'tr-TR'});
109//=> 'loremİpsum'
110
111camelCase('lorem-ipsum', {locale: ['en-US', 'en-GB']});
112//=> 'loremIpsum'
113
114camelCase('lorem-ipsum', {locale: ['tr', 'TR', 'tr-TR']});
115//=> 'loremİpsum'
116```
117
118## camelcase for enterprise
119
120Available as part of the Tidelift Subscription.
121
122The 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)
123
124## Related
125
126- [decamelize](https://github.com/sindresorhus/decamelize) - The inverse of this module
127- [uppercamelcase](https://github.com/SamVerschueren/uppercamelcase) - Like this module, but to PascalCase instead of camelCase
128- [titleize](https://github.com/sindresorhus/titleize) - Capitalize every word in string
129- [humanize-string](https://github.com/sindresorhus/humanize-string) - Convert a camelized/dasherized/underscored string into a humanized one
130- [camelcase-keys](https://github.com/sindresorhus/camelcase-keys) - Convert object keys to camel case