Skip to content

Commit bd6eebb

Browse files
committed
Added same implementation of kebab-case function as vscode extension
1 parent 05a313b commit bd6eebb

File tree

3 files changed

+31
-7
lines changed

3 files changed

+31
-7
lines changed

package-lock.json

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

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "generator-bitloops",
3-
"version": "0.3.10",
3+
"version": "0.3.11",
44
"description": "Next.js with TypeScript, Tailwind, Storybook and Cypress generator by Bitloops",
55
"license": "MIT",
66
"author": "Bitloops S.A.",

setup/index.js

+28-4
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,35 @@ import { fileURLToPath } from 'url';
88
const __filename = fileURLToPath(import.meta.url);
99
const __dirname = path.dirname(__filename);
1010

11+
function isKebabCase(str) {
12+
// Check if the string is empty
13+
if (!str || str.trim().length === 0) {
14+
return false;
15+
}
16+
17+
// Regular expression to check if a string is kebab-case,
18+
// ensuring it starts with a lowercase letter or digit, allowing for lowercase letters and digits in the middle or end,
19+
// and ensuring each new word starts with a lowercase letter or digit
20+
const kebabCaseRegex = /^[a-z0-9]+(?:-[a-z0-9]+)*$/;
21+
22+
return kebabCaseRegex.test(str);
23+
}
24+
1125
function toKebabCase(str) {
12-
return str
13-
.replace(/([a-z0-9])([A-Z])/g, '$1-$2')
14-
.toLowerCase()
15-
.replace(/\s+/g, '-');
26+
if (isKebabCase(str)) {
27+
return str;
28+
}
29+
30+
const words = str
31+
.trim()
32+
// Split by non-alphanumeric characters and the transition from lowercase to uppercase
33+
.split(/(?=[A-Z])|[^a-zA-Z0-9]+/)
34+
.filter((word) => word.length > 0);
35+
36+
return words
37+
.map((word) => word.toLowerCase())
38+
.filter((word) => word.length > 0) // Remove empty words
39+
.join('-');
1640
}
1741

1842
export default class extends Generator {

0 commit comments

Comments
 (0)