-
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathtoKebabCase.ts
More file actions
66 lines (60 loc) · 1.81 KB
/
toKebabCase.ts
File metadata and controls
66 lines (60 loc) · 1.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import { requireNonEmptyString } from "./requireNonEmptyString";
interface ToKebabCaseOptions {
clean?: boolean;
trim?: boolean;
}
/**
* Converts a string to `kebab-case` format.
*
* `kebab-case` is a naming convention where words are separated by hyphens (`-`),
* and all letters are in lowercase.
*
* This function handles spaces, underscores, and camelCase transitions by inserting hyphens.
* It also cleans up multiple or leading/trailing hyphens.
*
* @example (Default Behavior)
* ```javascript
* const text = "Hello world! How are you?";
* const result = toKebabCase(text);
*
* console.log(result); // hello-world!-how-are-you
* ```
*
* @example (Strict Cleaning)
* ```javascript
* const text = "Hello world! How are you?";
* const result = toKebabCase(text, { clean: true });
*
* console.log(result); // hello-world-how-are-you
* ```
*
* @param {string} value - The input string to convert.
* @param {ToKebabCaseOptions} [options] - Optional configuration options.
* @returns {string} The string converted to kebab-case.
* @throws {@link EmptyStringException}
* @see {@link toCamelCase}
* @see {@link toLowerCase}
* @see {@link toProperCase}
* @see {@link toSnakeCase}
* @see {@link toUpperCase}
* @since 1.0.0
* @version 1.0.0
*/
export function toKebabCase(value: string, options: ToKebabCaseOptions = {}): string {
const effectiveOptions: Required<ToKebabCaseOptions> = {
clean: false,
trim: false,
...options
};
let result = requireNonEmptyString(value)
.replace(/[\s_]/g, "-")
.replace(/([a-z])([A-Z])/g, "$1-$2")
.toLowerCase();
if (effectiveOptions.clean !== false) {
result = result.replace(/[^a-zA-Z0-9-]/g, "");
}
if (effectiveOptions.trim === true) {
result = result.trim().replace(/-+/g, "-");
}
return result;
}