Skip to content

Commit ab92c82

Browse files
chore: removed camelCase package from dependencies (#1311)
1 parent a3ca8c0 commit ab92c82

File tree

5 files changed

+172
-4
lines changed

5 files changed

+172
-4
lines changed

package-lock.json

+3-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242
"webpack": "^4.27.0 || ^5.0.0"
4343
},
4444
"dependencies": {
45-
"camelcase": "^6.2.0",
4645
"icss-utils": "^5.1.0",
4746
"loader-utils": "^2.0.0",
4847
"postcss": "^8.2.15",

src/utils.js

+63-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import modulesValues from "postcss-modules-values";
1010
import localByDefault from "postcss-modules-local-by-default";
1111
import extractImports from "postcss-modules-extract-imports";
1212
import modulesScope from "postcss-modules-scope";
13-
import camelCase from "camelcase";
1413

1514
const WEBPACK_IGNORE_COMMENT_REGEXP = /webpackIgnore:(\s+)?(true|false)/;
1615

@@ -19,6 +18,68 @@ const regexSingleEscape = /[ -,.\/:-@[\]\^`{-~]/;
1918
const regexExcessiveSpaces =
2019
/(^|\\+)?(\\[A-F0-9]{1,6})\x20(?![a-fA-F0-9\x20])/g;
2120

21+
const preserveCamelCase = (string) => {
22+
let result = string;
23+
let isLastCharLower = false;
24+
let isLastCharUpper = false;
25+
let isLastLastCharUpper = false;
26+
27+
for (let i = 0; i < result.length; i++) {
28+
const character = result[i];
29+
30+
if (isLastCharLower && /[\p{Lu}]/u.test(character)) {
31+
result = `${result.slice(0, i)}-${result.slice(i)}`;
32+
isLastCharLower = false;
33+
isLastLastCharUpper = isLastCharUpper;
34+
isLastCharUpper = true;
35+
i += 1;
36+
} else if (
37+
isLastCharUpper &&
38+
isLastLastCharUpper &&
39+
/[\p{Ll}]/u.test(character)
40+
) {
41+
result = `${result.slice(0, i - 1)}-${result.slice(i - 1)}`;
42+
isLastLastCharUpper = isLastCharUpper;
43+
isLastCharUpper = false;
44+
isLastCharLower = true;
45+
} else {
46+
isLastCharLower =
47+
character.toLowerCase() === character &&
48+
character.toUpperCase() !== character;
49+
isLastLastCharUpper = isLastCharUpper;
50+
isLastCharUpper =
51+
character.toUpperCase() === character &&
52+
character.toLowerCase() !== character;
53+
}
54+
}
55+
56+
return result;
57+
};
58+
59+
function camelCase(input) {
60+
let result = input.trim();
61+
62+
if (result.length === 0) {
63+
return "";
64+
}
65+
66+
if (result.length === 1) {
67+
return result.toLowerCase();
68+
}
69+
70+
const hasUpperCase = result !== result.toLowerCase();
71+
72+
if (hasUpperCase) {
73+
result = preserveCamelCase(result);
74+
}
75+
76+
return result
77+
.replace(/^[_.\- ]+/, "")
78+
.toLowerCase()
79+
.replace(/[_.\- ]+([\p{Alpha}\p{N}_]|$)/gu, (_, p1) => p1.toUpperCase())
80+
.replace(/\d+([\p{Alpha}\p{N}_]|$)/gu, (m) => m.toUpperCase());
81+
}
82+
2283
function escape(string) {
2384
let output = "";
2485
let counter = 0;
@@ -881,4 +942,5 @@ export {
881942
sort,
882943
WEBPACK_IGNORE_COMMENT_REGEXP,
883944
combineRequests,
945+
camelCase,
884946
};
+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`camelCase should transform 1`] = `""`;
4+
5+
exports[`camelCase should transform: foo bar 1`] = `"fooBar"`;
6+
7+
exports[`camelCase should transform: __foo__bar__ 1`] = `"fooBar"`;
8+
9+
exports[`camelCase should transform: - 1`] = `"-"`;
10+
11+
exports[`camelCase should transform: --__--_--_ 1`] = `""`;
12+
13+
exports[`camelCase should transform: --foo..bar 1`] = `"fooBar"`;
14+
15+
exports[`camelCase should transform: --foo---bar 1`] = `"fooBar"`;
16+
17+
exports[`camelCase should transform: --foo---bar-- 1`] = `"fooBar"`;
18+
19+
exports[`camelCase should transform: --foo--1 1`] = `"foo1"`;
20+
21+
exports[`camelCase should transform: --foo-bar 1`] = `"fooBar"`;
22+
23+
exports[`camelCase should transform: 1Hello 1`] = `"1Hello"`;
24+
25+
exports[`camelCase should transform: A::a 1`] = `"a::a"`;
26+
27+
exports[`camelCase should transform: F 1`] = `"f"`;
28+
29+
exports[`camelCase should transform: FOO-BAR 1`] = `"fooBar"`;
30+
31+
exports[`camelCase should transform: FOÈ-BAR 1`] = `"foèBar"`;
32+
33+
exports[`camelCase should transform: FOÈ-BAr 1`] = `"foèBAr"`;
34+
35+
exports[`camelCase should transform: Hello1World11foo 1`] = `"hello1World11Foo"`;
36+
37+
exports[`camelCase should transform: foo 1`] = `"foo"`;
38+
39+
exports[`camelCase should transform: foo bar 1`] = `"fooBar"`;
40+
41+
exports[`camelCase should transform: foo bar! 1`] = `"fooBar!"`;
42+
43+
exports[`camelCase should transform: foo bar# 1`] = `"fooBar#"`;
44+
45+
exports[`camelCase should transform: foo bar? 1`] = `"fooBar?"`;
46+
47+
exports[`camelCase should transform: foo_bar 1`] = `"fooBar"`;
48+
49+
exports[`camelCase should transform: foo--bar 1`] = `"fooBar"`;
50+
51+
exports[`camelCase should transform: foo-bar 1`] = `"fooBar"`;
52+
53+
exports[`camelCase should transform: foo-bar-baz 1`] = `"fooBarBaz"`;
54+
55+
exports[`camelCase should transform: fooBar 1`] = `"fooBar"`;
56+
57+
exports[`camelCase should transform: fooBar-baz 1`] = `"fooBarBaz"`;
58+
59+
exports[`camelCase should transform: fooBarBaz-bazzy 1`] = `"fooBarBazBazzy"`;
60+
61+
exports[`camelCase should transform: h2w 1`] = `"h2W"`;
62+
63+
exports[`camelCase should transform: mGridCol6@md 1`] = `"mGridCol6@md"`;

test/camelCase.test.js

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { camelCase } from "../src/utils";
2+
3+
describe("camelCase", () => {
4+
const data = [
5+
"foo",
6+
"foo-bar",
7+
"foo-bar-baz",
8+
"foo--bar",
9+
"--foo-bar",
10+
"--foo---bar",
11+
"FOO-BAR",
12+
"FOÈ-BAR",
13+
"FOÈ-BAr",
14+
"--foo---bar--",
15+
"--foo--1",
16+
"--foo..bar",
17+
"foo_bar",
18+
"__foo__bar__",
19+
"foo bar",
20+
" foo bar ",
21+
"-",
22+
"fooBar",
23+
"fooBar-baz",
24+
"fooBarBaz-bazzy",
25+
"",
26+
"--__--_--_",
27+
"A::a",
28+
"1Hello",
29+
"h2w",
30+
"F",
31+
"foo bar?",
32+
"foo bar!",
33+
"foo bar#",
34+
"mGridCol6@md",
35+
"Hello1World11foo",
36+
];
37+
38+
for (const entry of data) {
39+
it(`should transform`, () => {
40+
expect(camelCase(entry)).toMatchSnapshot(`${entry}`);
41+
});
42+
}
43+
});

0 commit comments

Comments
 (0)