Skip to content

Commit 1d2e9e1

Browse files
committedJun 6, 2022
Require Node.js 14 and move to ESM
Closes #83
1 parent 6f12d6d commit 1d2e9e1

File tree

7 files changed

+80
-90
lines changed

7 files changed

+80
-90
lines changed
 

‎.github/workflows/main.yml

-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ jobs:
1313
- 18
1414
- 16
1515
- 14
16-
- 12
17-
- 10
1816
steps:
1917
- uses: actions/checkout@v3
2018
- uses: actions/setup-node@v3

‎index.d.ts

+44-45
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,44 @@
1-
declare namespace camelcase {
2-
interface Options {
3-
/**
4-
Uppercase the first character: `foo-bar` → `FooBar`.
5-
6-
@default false
7-
*/
8-
readonly pascalCase?: boolean;
9-
10-
/**
11-
Preserve the consecutive uppercase characters: `foo-BAR` → `FooBAR`.
12-
13-
@default false
14-
*/
15-
readonly preserveConsecutiveUppercase?: boolean;
16-
17-
/**
18-
The 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.
19-
20-
Setting `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.
21-
22-
Default: The host environment’s current locale.
23-
24-
@example
25-
```
26-
import camelCase = require('camelcase');
27-
28-
camelCase('lorem-ipsum', {locale: 'en-US'});
29-
//=> 'loremIpsum'
30-
camelCase('lorem-ipsum', {locale: 'tr-TR'});
31-
//=> 'loremİpsum'
32-
camelCase('lorem-ipsum', {locale: ['en-US', 'en-GB']});
33-
//=> 'loremIpsum'
34-
camelCase('lorem-ipsum', {locale: ['tr', 'TR', 'tr-TR']});
35-
//=> 'loremİpsum'
36-
```
37-
*/
38-
readonly locale?: false | string | readonly string[];
39-
}
40-
}
1+
export type Options = {
2+
/**
3+
Uppercase the first character: `foo-bar` → `FooBar`.
4+
5+
@default false
6+
*/
7+
readonly pascalCase?: boolean;
8+
9+
/**
10+
Preserve consecutive uppercase characters: `foo-BAR` → `FooBAR`.
11+
12+
@default false
13+
*/
14+
readonly preserveConsecutiveUppercase?: boolean;
15+
16+
/**
17+
The 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.
18+
19+
Setting `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.
20+
21+
Default: The host environment’s current locale.
22+
23+
@example
24+
```
25+
import camelCase from 'camelcase';
26+
27+
camelCase('lorem-ipsum', {locale: 'en-US'});
28+
//=> 'loremIpsum'
29+
30+
camelCase('lorem-ipsum', {locale: 'tr-TR'});
31+
//=> 'loremİpsum'
32+
33+
camelCase('lorem-ipsum', {locale: ['en-US', 'en-GB']});
34+
//=> 'loremIpsum'
35+
36+
camelCase('lorem-ipsum', {locale: ['tr', 'TR', 'tr-TR']});
37+
//=> 'loremİpsum'
38+
```
39+
*/
40+
readonly locale?: false | string | readonly string[];
41+
};
4142

4243
/**
4344
Convert a dash/dot/underscore/space separated string to camelCase or PascalCase: `foo-bar` → `fooBar`.
@@ -48,7 +49,7 @@ Correctly handles Unicode strings.
4849
4950
@example
5051
```
51-
import camelCase = require('camelcase');
52+
import camelCase from 'camelcase';
5253
5354
camelCase('foo-bar');
5455
//=> 'fooBar'
@@ -95,9 +96,7 @@ camelCase('lorem-ipsum', {locale: 'en-US'});
9596
//=> 'loremIpsum'
9697
```
9798
*/
98-
declare function camelcase(
99+
export default function camelcase(
99100
input: string | readonly string[],
100-
options?: camelcase.Options
101+
options?: Options
101102
): string;
102-
103-
export = camelcase;

‎index.js

+16-26
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
'use strict';
2-
31
const UPPERCASE = /[\p{Lu}]/u;
42
const LOWERCASE = /[\p{Ll}]/u;
53
const LEADING_CAPITAL = /^[\p{Lu}](?![\p{Lu}])/gu;
@@ -15,17 +13,17 @@ const preserveCamelCase = (string, toLowerCase, toUpperCase) => {
1513
let isLastCharUpper = false;
1614
let isLastLastCharUpper = false;
1715

18-
for (let i = 0; i < string.length; i++) {
19-
const character = string[i];
16+
for (let index = 0; index < string.length; index++) {
17+
const character = string[index];
2018

2119
if (isLastCharLower && UPPERCASE.test(character)) {
22-
string = string.slice(0, i) + '-' + string.slice(i);
20+
string = string.slice(0, index) + '-' + string.slice(index);
2321
isLastCharLower = false;
2422
isLastLastCharUpper = isLastCharUpper;
2523
isLastCharUpper = true;
26-
i++;
24+
index++;
2725
} else if (isLastCharUpper && isLastLastCharUpper && LOWERCASE.test(character)) {
28-
string = string.slice(0, i - 1) + '-' + string.slice(i - 1);
26+
string = string.slice(0, index - 1) + '-' + string.slice(index - 1);
2927
isLastLastCharUpper = isLastCharUpper;
3028
isLastCharUpper = false;
3129
isLastCharLower = true;
@@ -53,15 +51,15 @@ const postProcess = (input, toUpperCase) => {
5351
.replace(NUMBERS_AND_IDENTIFIER, m => toUpperCase(m));
5452
};
5553

56-
const camelCase = (input, options) => {
54+
export default function camelCase(input, options) {
5755
if (!(typeof input === 'string' || Array.isArray(input))) {
5856
throw new TypeError('Expected the input to be `string | string[]`');
5957
}
6058

6159
options = {
6260
pascalCase: false,
6361
preserveConsecutiveUppercase: false,
64-
...options
62+
...options,
6563
};
6664

6765
if (Array.isArray(input)) {
@@ -76,12 +74,13 @@ const camelCase = (input, options) => {
7674
return '';
7775
}
7876

79-
const toLowerCase = options.locale === false ?
80-
string => string.toLowerCase() :
81-
string => string.toLocaleLowerCase(options.locale);
82-
const toUpperCase = options.locale === false ?
83-
string => string.toUpperCase() :
84-
string => string.toLocaleUpperCase(options.locale);
77+
const toLowerCase = options.locale === false
78+
? string => string.toLowerCase()
79+
: string => string.toLocaleLowerCase(options.locale);
80+
81+
const toUpperCase = options.locale === false
82+
? string => string.toUpperCase()
83+
: string => string.toLocaleUpperCase(options.locale);
8584

8685
if (input.length === 1) {
8786
if (SEPARATORS.test(input)) {
@@ -98,20 +97,11 @@ const camelCase = (input, options) => {
9897
}
9998

10099
input = input.replace(LEADING_SEPARATORS, '');
101-
102-
if (options.preserveConsecutiveUppercase) {
103-
input = preserveConsecutiveUppercase(input, toLowerCase);
104-
} else {
105-
input = toLowerCase(input);
106-
}
100+
input = options.preserveConsecutiveUppercase ? preserveConsecutiveUppercase(input, toLowerCase) : toLowerCase(input);
107101

108102
if (options.pascalCase) {
109103
input = toUpperCase(input.charAt(0)) + input.slice(1);
110104
}
111105

112106
return postProcess(input, toUpperCase);
113-
};
114-
115-
module.exports = camelCase;
116-
// TODO: Remove this for the next major release
117-
module.exports.default = camelCase;
107+
}

‎index.test-d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {expectType} from 'tsd';
2-
import camelCase = require('.');
2+
import camelCase from './index.js';
33

44
expectType<string>(camelCase('foo-bar'));
55
expectType<string>(camelCase('розовый_пушистый_единороги'));

‎package.json

+7-4
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,11 @@
1010
"email": "sindresorhus@gmail.com",
1111
"url": "https://sindresorhus.com"
1212
},
13+
"type": "module",
14+
"exports": "./index.js",
15+
"types": "./index.d.ts",
1316
"engines": {
14-
"node": ">=10"
17+
"node": ">=14.16"
1518
},
1619
"scripts": {
1720
"test": "xo && ava && tsd"
@@ -37,8 +40,8 @@
3740
"pascal-case"
3841
],
3942
"devDependencies": {
40-
"ava": "^1.4.1",
41-
"tsd": "^0.11.0",
42-
"xo": "^0.28.3"
43+
"ava": "^4.3.0",
44+
"tsd": "^0.20.0",
45+
"xo": "^0.49.0"
4346
}
4447
}

‎readme.md

+6-8
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,14 @@ If you use this on untrusted user input, don't forget to limit the length to som
88

99
## Install
1010

11+
```sh
12+
npm install camelcase
1113
```
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.*
1614

1715
## Usage
1816

1917
```js
20-
const camelCase = require('camelcase');
18+
import camelCase from 'camelcase';
2119

2220
camelCase('foo-bar');
2321
//=> 'fooBar'
@@ -90,7 +88,7 @@ Uppercase the first character: `foo-bar` → `FooBar`
9088
Type: `boolean`\
9189
Default: `false`
9290

93-
Preserve the consecutive uppercase characters: `foo-BAR``FooBAR`.
91+
Preserve consecutive uppercase characters: `foo-BAR``FooBAR`.
9492

9593
##### locale
9694

@@ -100,7 +98,7 @@ Default: The host environment’s current locale.
10098
The 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.
10199

102100
```js
103-
const camelCase = require('camelcase');
101+
import camelCase from 'camelcase';
104102

105103
camelCase('lorem-ipsum', {locale: 'en-US'});
106104
//=> 'loremIpsum'
@@ -118,7 +116,7 @@ camelCase('lorem-ipsum', {locale: ['tr', 'TR', 'tr-TR']});
118116
Setting `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:
119117

120118
```js
121-
const camelCase = require('camelcase');
119+
import camelCase from 'camelcase';
122120

123121
// On a platform with 'tr-TR'
124122

‎test.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import test from 'ava';
2-
import camelCase from '.';
2+
import camelCase from './index.js';
33

44
test('camelCase', t => {
55
t.is(camelCase('foo'), 'foo');
@@ -228,7 +228,9 @@ test('camelCase with disabled locale', t => {
228228
test('invalid input', t => {
229229
t.throws(() => {
230230
camelCase(1);
231-
}, /Expected the input to be/);
231+
}, {
232+
message: /Expected the input to be/,
233+
});
232234
});
233235

234236
/* eslint-disable no-extend-native */
@@ -242,11 +244,11 @@ const withLocaleCaseFunctionsMocked = fn => {
242244

243245
Object.defineProperty(String.prototype, 'toLocaleUpperCase', {
244246
...toLocaleUpperCase,
245-
value: throwWhenBeingCalled
247+
value: throwWhenBeingCalled,
246248
});
247249
Object.defineProperty(String.prototype, 'toLocaleLowerCase', {
248250
...toLocaleLowerCase,
249-
value: throwWhenBeingCalled
251+
value: throwWhenBeingCalled,
250252
});
251253

252254
try {

0 commit comments

Comments
 (0)
Please sign in to comment.