@rebasepro/utils 0.0.1-canary.09e5ec5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +6 -0
- package/dist/arrays.d.ts +1 -0
- package/dist/dates.d.ts +1 -0
- package/dist/fields.d.ts +1 -0
- package/dist/flatten_object.d.ts +5 -0
- package/dist/hash.d.ts +1 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.es.js +588 -0
- package/dist/index.es.js.map +1 -0
- package/dist/index.umd.js +590 -0
- package/dist/index.umd.js.map +1 -0
- package/dist/names.d.ts +22 -0
- package/dist/objects.d.ts +26 -0
- package/dist/os.d.ts +2 -0
- package/dist/plurals.d.ts +16 -0
- package/dist/regexp.d.ts +7 -0
- package/dist/strings.d.ts +8 -0
- package/package.json +108 -0
- package/src/arrays.ts +3 -0
- package/src/dates.ts +1 -0
- package/src/fields.ts +28 -0
- package/src/flatten_object.ts +45 -0
- package/src/hash.ts +12 -0
- package/src/index.ts +11 -0
- package/src/names.ts +30 -0
- package/src/objects.ts +377 -0
- package/src/os.ts +13 -0
- package/src/plurals.ts +188 -0
- package/src/regexp.ts +32 -0
- package/src/strings.ts +100 -0
package/src/strings.ts
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
const kebabCaseRegex = /[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|\b)|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+/g;
|
|
2
|
+
|
|
3
|
+
export const toKebabCase = (str: string) => {
|
|
4
|
+
const regExpMatchArray = str.match(kebabCaseRegex);
|
|
5
|
+
if (!regExpMatchArray) return "";
|
|
6
|
+
return regExpMatchArray
|
|
7
|
+
.map(x => x.toLowerCase())
|
|
8
|
+
.join("-");
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
const snakeCaseRegex = /[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|\b)|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+/g;
|
|
12
|
+
|
|
13
|
+
export const toSnakeCase = (str: string) => {
|
|
14
|
+
const regExpMatchArray = str.match(snakeCaseRegex);
|
|
15
|
+
if (!regExpMatchArray) return "";
|
|
16
|
+
return regExpMatchArray
|
|
17
|
+
.map(x => x.toLowerCase())
|
|
18
|
+
.join("_");
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
export function camelCase(str: string): string {
|
|
22
|
+
if (!str) return "";
|
|
23
|
+
if (str.length === 1) return str.toLowerCase();
|
|
24
|
+
|
|
25
|
+
// Split by hyphens, underscores, or spaces and filter out empty strings
|
|
26
|
+
const parts = str.split(/[-_ ]+/).filter(Boolean);
|
|
27
|
+
|
|
28
|
+
if (parts.length === 0) return "";
|
|
29
|
+
|
|
30
|
+
// Start with first part in lowercase
|
|
31
|
+
return parts[0].toLowerCase() +
|
|
32
|
+
// Transform remaining parts to have first letter uppercase
|
|
33
|
+
parts.slice(1)
|
|
34
|
+
.map(part => part.charAt(0).toUpperCase() + part.substring(1).toLowerCase())
|
|
35
|
+
.join("");
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export function randomString(strLength = 5) {
|
|
39
|
+
return Math.random().toString(36).slice(2, 2 + strLength);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export function randomColor() {
|
|
43
|
+
return Math.floor(Math.random() * 16777215).toString(16);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export function slugify(text?: string, separator = "_", lowercase = true) {
|
|
47
|
+
if (!text) return "";
|
|
48
|
+
const from = "ãàáäâẽèéëêìíïîõòóöôùúüûñç·/_,:;-"
|
|
49
|
+
const to = `aaaaaeeeeeiiiiooooouuuunc${separator}${separator}${separator}${separator}${separator}${separator}${separator}`;
|
|
50
|
+
|
|
51
|
+
for (let i = 0, l = from.length; i < l; i++) {
|
|
52
|
+
text = text.replace(new RegExp(from.charAt(i), "g"), to.charAt(i));
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
text = text
|
|
56
|
+
.toString() // Cast to string
|
|
57
|
+
.trim() // Remove whitespace from both sides of a string
|
|
58
|
+
.replace(/^\s+|\s+$/g, "")
|
|
59
|
+
.replace(/\s+/g, separator) // Replace spaces with separator
|
|
60
|
+
.replace(/&/g, separator) // Replace & with separator
|
|
61
|
+
.replace(/[^\w\\-]+/g, "") // Remove all non-word chars
|
|
62
|
+
.replace(new RegExp("\\" + separator + "\\" + separator + "+", "g"),
|
|
63
|
+
separator); // Replace multiple separators with single one
|
|
64
|
+
|
|
65
|
+
return lowercase
|
|
66
|
+
? text.toLowerCase() // Convert the string to lowercase letters
|
|
67
|
+
: text;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export function unslugify(slug?: string): string {
|
|
71
|
+
if (!slug) return "";
|
|
72
|
+
if (slug.includes("-") || slug.includes("_") || !slug.includes(" ")) {
|
|
73
|
+
const result = slug.replace(/[-_]/g, " ");
|
|
74
|
+
return result.replace(/\w\S*/g, function (txt) {
|
|
75
|
+
return txt.charAt(0).toUpperCase() + txt.substring(1);
|
|
76
|
+
}).trim();
|
|
77
|
+
} else {
|
|
78
|
+
return slug.trim();
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export function prettifyIdentifier(input: string) {
|
|
83
|
+
if (!input) return "";
|
|
84
|
+
|
|
85
|
+
let text = input;
|
|
86
|
+
|
|
87
|
+
// 1. Handle camelCase and Acronyms
|
|
88
|
+
// Group 1 ($1 $2): Lowercase followed by Uppercase (e.g., imageURL -> image URL)
|
|
89
|
+
// Group 2 ($3 $4): Uppercase followed by Uppercase+lowercase (e.g., XMLParser -> XML Parser)
|
|
90
|
+
text = text.replace(/([a-z])([A-Z])|([A-Z])([A-Z][a-z])/g, "$1$3 $2$4");
|
|
91
|
+
|
|
92
|
+
// 2. Replace hyphens/underscores with spaces
|
|
93
|
+
text = text.replace(/[_-]+/g, " ");
|
|
94
|
+
|
|
95
|
+
// 3. Capitalize first letter of each word (Title Case)
|
|
96
|
+
const s = text
|
|
97
|
+
.trim()
|
|
98
|
+
.replace(/\b\w/g, (char) => char.toUpperCase());
|
|
99
|
+
return s;
|
|
100
|
+
}
|