Skip to content

Commit 3f7d4cd

Browse files
refactor: removed camelCase package from dependencies (#1301)
1 parent 853b074 commit 3f7d4cd

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": "^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.10",

src/utils.js

+63-1
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,75 @@ 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

1716
// eslint-disable-next-line no-useless-escape
1817
const regexSingleEscape = /[ -,.\/:-@[\]\^`{-~]/;
1918
const regexExcessiveSpaces = /(^|\\+)?(\\[A-F0-9]{1,6})\x20(?![a-fA-F0-9\x20])/g;
2019

20+
const preserveCamelCase = (string) => {
21+
let result = string;
22+
let isLastCharLower = false;
23+
let isLastCharUpper = false;
24+
let isLastLastCharUpper = false;
25+
26+
for (let i = 0; i < result.length; i++) {
27+
const character = result[i];
28+
29+
if (isLastCharLower && /[\p{Lu}]/u.test(character)) {
30+
result = `${result.slice(0, i)}-${result.slice(i)}`;
31+
isLastCharLower = false;
32+
isLastLastCharUpper = isLastCharUpper;
33+
isLastCharUpper = true;
34+
i += 1;
35+
} else if (
36+
isLastCharUpper &&
37+
isLastLastCharUpper &&
38+
/[\p{Ll}]/u.test(character)
39+
) {
40+
result = `${result.slice(0, i - 1)}-${result.slice(i - 1)}`;
41+
isLastLastCharUpper = isLastCharUpper;
42+
isLastCharUpper = false;
43+
isLastCharLower = true;
44+
} else {
45+
isLastCharLower =
46+
character.toLowerCase() === character &&
47+
character.toUpperCase() !== character;
48+
isLastLastCharUpper = isLastCharUpper;
49+
isLastCharUpper =
50+
character.toUpperCase() === character &&
51+
character.toLowerCase() !== character;
52+
}
53+
}
54+
55+
return result;
56+
};
57+
58+
function camelCase(input) {
59+
let result = input.trim();
60+
61+
if (result.length === 0) {
62+
return "";
63+
}
64+
65+
if (result.length === 1) {
66+
return result.toLowerCase();
67+
}
68+
69+
const hasUpperCase = result !== result.toLowerCase();
70+
71+
if (hasUpperCase) {
72+
result = preserveCamelCase(result);
73+
}
74+
75+
return result
76+
.replace(/^[_.\- ]+/, "")
77+
.toLowerCase()
78+
.replace(/[_.\- ]+([\p{Alpha}\p{N}_]|$)/gu, (_, p1) => p1.toUpperCase())
79+
.replace(/\d+([\p{Alpha}\p{N}_]|$)/gu, (m) => m.toUpperCase());
80+
}
81+
2182
function escape(string) {
2283
let output = "";
2384
let counter = 0;
@@ -884,4 +945,5 @@ export {
884945
sort,
885946
WEBPACK_IGNORE_COMMENT_REGEXP,
886947
combineRequests,
948+
camelCase,
887949
};
+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)