@rebasepro/utils 0.0.1-canary.4d4fb3e

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/src/objects.ts ADDED
@@ -0,0 +1,376 @@
1
+ import hash from "object-hash";
2
+ import { GeoPoint } from "@rebasepro/types";
3
+
4
+ /** @private is the value an empty array? */
5
+ export const isEmptyArray = (value?: unknown) =>
6
+ Array.isArray(value) && value.length === 0;
7
+
8
+ /** @private is the given object a Function? */
9
+ export const isFunction = (obj: unknown): obj is Function =>
10
+ typeof obj === "function";
11
+
12
+ /** @private is the given object an integer? */
13
+ export const isInteger = (obj: unknown): boolean =>
14
+ String(Math.floor(Number(obj))) === String(obj);
15
+
16
+ /** @private is the given object a NaN? */
17
+ // eslint-disable-next-line no-self-compare
18
+ export const isNaN = (obj: unknown): boolean => obj !== obj;
19
+
20
+ /**
21
+ * Deeply get a value from an object via its path.
22
+ */
23
+ export function getIn(
24
+ obj: Record<string, unknown> | unknown[] | unknown,
25
+ key: string | string[],
26
+ def?: unknown,
27
+ p = 0
28
+ ) {
29
+ const path = toPath(key);
30
+ while (obj && p < path.length) {
31
+ obj = (obj as Record<string, unknown>)[path[p++]];
32
+ }
33
+
34
+ // check if path is not in the end
35
+ if (p !== path.length && !obj) {
36
+ return def;
37
+ }
38
+
39
+ return obj === undefined ? def : obj;
40
+ }
41
+
42
+ export function setIn<T>(obj: T, path: string, value: unknown): T {
43
+ const res = clone(obj) as Record<string, unknown>;
44
+ let resVal: Record<string, unknown> = res;
45
+ let i = 0;
46
+ const pathArray = toPath(path);
47
+
48
+ for (; i < pathArray.length - 1; i++) {
49
+ const currentPath: string = pathArray[i];
50
+ const currentObj = getIn(obj as Record<string, unknown>, pathArray.slice(0, i + 1));
51
+
52
+ if (currentObj && (isObject(currentObj) || Array.isArray(currentObj))) {
53
+ resVal = resVal[currentPath] = clone(currentObj) as Record<string, unknown>;
54
+ } else {
55
+ const nextPath: string = pathArray[i + 1];
56
+ resVal = resVal[currentPath] =
57
+ (isInteger(nextPath) && Number(nextPath) >= 0 ? [] : {}) as Record<string, unknown>;
58
+ }
59
+ }
60
+
61
+ // Return original object if new value is the same as current
62
+ if ((i === 0 ? obj as Record<string, unknown> : resVal)[pathArray[i]] === value) {
63
+ return obj;
64
+ }
65
+
66
+ if (value === undefined) {
67
+ delete resVal[pathArray[i]];
68
+ } else {
69
+ resVal[pathArray[i]] = value;
70
+ }
71
+
72
+ // If the path array has a single element, the loop did not run.
73
+ // Deleting on `resVal` had no effect in this scenario, so we delete on the result instead.
74
+ if (i === 0 && value === undefined) {
75
+ delete res[pathArray[i]];
76
+ }
77
+
78
+ return res as T;
79
+ }
80
+
81
+ export function clone<T>(value: T): T {
82
+ if (Array.isArray(value)) {
83
+ return [...value] as unknown as T;
84
+ } else if (typeof value === "object" && value !== null) {
85
+ return { ...value } as T;
86
+ } else {
87
+ return value; // This is for primitive types which do not need cloning.
88
+ }
89
+ }
90
+
91
+ function toPath(value: string | string[]) {
92
+ if (Array.isArray(value)) return value; // Already in path array form.
93
+ // Replace brackets with dots, remove leading/trailing dots, then split by dot.
94
+ return value.replace(/\[(\d+)]/g, ".$1").replace(/^\./, "").replace(/\.$/, "").split(".");
95
+ }
96
+
97
+
98
+ export const pick: <T extends Record<string, unknown>>(obj: T, ...args: (keyof T)[]) => Partial<T> = <T extends Record<string, unknown>>(obj: T, ...args: (keyof T)[]) => ({
99
+ ...args.reduce<Record<string, unknown>>((res, key) => ({
100
+ ...res,
101
+ [key as string]: obj[key as string]
102
+ }), {})
103
+ }) as Partial<T>;
104
+
105
+ export function isObject(item: unknown): item is Record<string, unknown> {
106
+ return !!item && typeof item === "object" && !Array.isArray(item);
107
+ }
108
+
109
+ export function isPlainObject(obj: unknown): obj is Record<string, unknown> {
110
+ // 1. Rule out non-objects, null, and arrays
111
+ if (typeof obj !== "object" || obj === null || Array.isArray(obj)) {
112
+ return false;
113
+ }
114
+
115
+ // 2. Get the object's direct prototype
116
+ const proto = Object.getPrototypeOf(obj);
117
+
118
+ // 3. A plain object's direct prototype is Object.prototype
119
+ return proto === Object.prototype;
120
+ }
121
+
122
+ export function mergeDeep<T extends object, U extends object>(
123
+ target: T,
124
+ source: U,
125
+ ignoreUndefined: boolean = false
126
+ ): T & U {
127
+ // If target is not a true object (e.g., null, array, primitive), return target itself.
128
+ if (!isObject(target)) {
129
+ return target as T & U;
130
+ }
131
+
132
+ // Create a shallow copy of the target to avoid modifying the original object.
133
+ const output = { ...target };
134
+
135
+ // If source is not a true object, there's nothing to merge from it.
136
+ // Return the shallow copy of target.
137
+ if (!isObject(source)) {
138
+ return output as T & U;
139
+ }
140
+
141
+ // Iterate over keys in the source object.
142
+ for (const key in source) {
143
+ if (Object.prototype.hasOwnProperty.call(source, key)) {
144
+ const sourceValue = source[key];
145
+ const outputValue = (output as Record<string, unknown>)[key]; // Current value in our merged object (originating from target)
146
+
147
+ // Skip if source value is undefined and ignoreUndefined is true.
148
+ // This handles both not adding new undefined properties and not overwriting existing properties with undefined.
149
+ if (ignoreUndefined && sourceValue === undefined) {
150
+ continue;
151
+ }
152
+
153
+ if ((sourceValue as unknown) instanceof Date) {
154
+ // If source value is a Date, create a new Date instance.
155
+ (output as Record<string, unknown>)[key] = new Date((sourceValue as unknown as Date).getTime());
156
+ } else if (Array.isArray(sourceValue)) {
157
+ if (Array.isArray(outputValue)) {
158
+ const newArray = [];
159
+ const maxLength = Math.max(outputValue.length, sourceValue.length);
160
+ for (let i = 0; i < maxLength; i++) {
161
+ const sourceItem = sourceValue[i];
162
+ const targetItem = outputValue[i];
163
+
164
+ if (i >= sourceValue.length) { // source is shorter
165
+ newArray[i] = targetItem;
166
+ } else if (i >= outputValue.length) { // target is shorter
167
+ newArray[i] = sourceItem;
168
+ } else if (sourceItem === null) {
169
+ newArray[i] = targetItem;
170
+ } else if (isPlainObject(sourceItem) && isPlainObject(targetItem)) {
171
+ // Only recursively merge plain objects, preserve class instances
172
+ newArray[i] = mergeDeep(targetItem, sourceItem, ignoreUndefined);
173
+ } else {
174
+ // For class instances and primitives, use source directly
175
+ newArray[i] = sourceItem;
176
+ }
177
+ }
178
+ (output as Record<string, unknown>)[key] = newArray;
179
+ } else {
180
+ // If output's value (from target) is not an array,
181
+ // overwrite with a shallow copy of the source array.
182
+ (output as Record<string, unknown>)[key] = [...sourceValue];
183
+ }
184
+ } else if (isPlainObject(sourceValue)) {
185
+ // If source value is a plain object (not a class instance like EntityReference, GeoPoint, etc.):
186
+ if (isPlainObject(outputValue)) {
187
+ // If the corresponding value in output (from target) is also a plain object, recurse.
188
+ // Ensure the ignoreUndefined flag is passed down.
189
+ (output as Record<string, unknown>)[key] = mergeDeep(outputValue as Record<string, unknown>, sourceValue, ignoreUndefined);
190
+ } else {
191
+ // If output's value (from target) is not a plain object (e.g., null, primitive, class instance, or key didn't exist in original target),
192
+ // overwrite with the source object.
193
+ (output as Record<string, unknown>)[key] = sourceValue;
194
+ }
195
+ } else if (isObject(sourceValue)) {
196
+ // If source value is a class instance (not a plain object), use it directly to preserve prototype
197
+ (output as Record<string, unknown>)[key] = sourceValue;
198
+ } else {
199
+ // If source value is a primitive, null, or undefined (and not ignored).
200
+ (output as Record<string, unknown>)[key] = sourceValue;
201
+ }
202
+ }
203
+ }
204
+
205
+ return output as T & U;
206
+ }
207
+
208
+ export function getValueInPath(o: object | undefined, path: string): unknown {
209
+ if (!o) return undefined;
210
+ if (typeof o === "object") {
211
+ if (path in o) {
212
+ return (o as Record<string, unknown>)[path];
213
+ }
214
+ if (path.includes(".") || path.includes("[")) {
215
+ let pathSegments = path.split(/[.[]/);
216
+ if (path.includes("[")) {
217
+ pathSegments = pathSegments.map(segment => segment.replace("]", ""));
218
+ }
219
+ const firstSegment = pathSegments[0];
220
+ const isArrayAndIndexExists = Array.isArray((o as Record<string, unknown>)[firstSegment]) && !isNaN(parseInt(pathSegments[1]));
221
+ const nextObject = isArrayAndIndexExists
222
+ ? ((o as Record<string, unknown>)[firstSegment] as unknown[])[parseInt(pathSegments[1])]
223
+ : (o as Record<string, unknown>)[firstSegment];
224
+
225
+ const nextPath = pathSegments.slice(isArrayAndIndexExists ? 2 : 1).join(".");
226
+ if (nextPath === "")
227
+ return nextObject;
228
+ return getValueInPath(nextObject as object | undefined, nextPath);
229
+ }
230
+ }
231
+ return undefined;
232
+ }
233
+
234
+ export function removeInPath(o: object, path: string): object | undefined {
235
+ let currentObject = { ...o };
236
+ const parts = path.split(".");
237
+ const last = parts.pop();
238
+ for (const part of parts) {
239
+ currentObject = (currentObject as Record<string, unknown>)[part] as Record<string, unknown>;
240
+ }
241
+ if (last)
242
+ delete (currentObject as Record<string, unknown>)[last];
243
+ return currentObject;
244
+ }
245
+
246
+ export function removeFunctions(o: unknown): unknown {
247
+ if (o === undefined) return undefined;
248
+ if (o === null) return null;
249
+ if (typeof o === "object") {
250
+ // Handle arrays first - map over them recursively
251
+ if (Array.isArray(o)) {
252
+ return o.map(v => removeFunctions(v));
253
+ }
254
+ // Preserve class instances (EntityReference, GeoPoint, etc.) - don't recurse into them
255
+ if (!isPlainObject(o)) {
256
+ return o;
257
+ }
258
+ return Object.entries(o)
259
+ .filter(([_, value]) => typeof value !== "function")
260
+ .map(([key, value]) => {
261
+ if (Array.isArray(value)) {
262
+ return { [key]: value.map(v => removeFunctions(v)) };
263
+ } else if (typeof value === "object") {
264
+ return { [key]: removeFunctions(value) };
265
+ } else return { [key]: value };
266
+ })
267
+ .reduce((a, b) => ({ ...a, ...b }), {});
268
+ }
269
+ return o;
270
+ }
271
+
272
+ export function getHashValue<T>(v: T): string | null {
273
+ if (!v) return null;
274
+ if (typeof v === "object" && v !== null) {
275
+ if ("id" in v)
276
+ return String((v as Record<string, unknown>).id);
277
+ else if (v instanceof Date)
278
+ return v.toLocaleString();
279
+ else if (v instanceof GeoPoint)
280
+ return hash(v as unknown as Record<string, unknown>);
281
+ }
282
+ return hash(v as object, { ignoreUnknown: true });
283
+ }
284
+
285
+ export function removeUndefined(value: unknown, removeEmptyStrings?: boolean): unknown {
286
+ if (typeof value === "function") {
287
+ return value;
288
+ }
289
+ if (Array.isArray(value)) {
290
+ return value.map((v: unknown) => removeUndefined(v, removeEmptyStrings));
291
+ }
292
+ if (typeof value === "object") {
293
+ if (value === null)
294
+ return value;
295
+ // Preserve class instances (EntityReference, GeoPoint, etc.) - don't recurse into them
296
+ if (!isPlainObject(value)) {
297
+ return value;
298
+ }
299
+ const res: Record<string, unknown> = {};
300
+ Object.keys(value).forEach((key) => {
301
+ if (!isEmptyObject(value as object)) {
302
+ const childRes = removeUndefined((value as Record<string, unknown>)[key], removeEmptyStrings);
303
+ const isString = typeof childRes === "string";
304
+ const shouldKeepIfString = !removeEmptyStrings || (removeEmptyStrings && !isString) || (removeEmptyStrings && isString && childRes !== "");
305
+ if (childRes !== undefined && !isEmptyObject(childRes as object) && shouldKeepIfString)
306
+ res[key] = childRes;
307
+ }
308
+ });
309
+ return res;
310
+ }
311
+ return value;
312
+ }
313
+
314
+ export function removeNulls(value: unknown): unknown {
315
+ if (typeof value === "function") {
316
+ return value;
317
+ }
318
+ if (Array.isArray(value)) {
319
+ return value.map((v: unknown) => removeNulls(v));
320
+ }
321
+ if (typeof value === "object") {
322
+ if (value === null)
323
+ return value;
324
+ // Preserve class instances (EntityReference, GeoPoint, etc.) - don't recurse into them
325
+ if (!isPlainObject(value)) {
326
+ return value;
327
+ }
328
+ const res: Record<string, unknown> = {};
329
+ const obj = value as Record<string, unknown>;
330
+ Object.keys(obj).forEach((key) => {
331
+ if (obj[key] !== null)
332
+ res[key] = removeNulls(obj[key]);
333
+ });
334
+ return res;
335
+ }
336
+ return value;
337
+ }
338
+
339
+ export function isEmptyObject(obj: object) {
340
+ return obj &&
341
+ Object.getPrototypeOf(obj) === Object.prototype &&
342
+ Object.keys(obj).length === 0
343
+ }
344
+
345
+ export function removePropsIfExisting(source: Record<string, unknown> | unknown[], comparison: Record<string, unknown> | unknown[]) {
346
+ const isObject = (val: unknown): val is Record<string, unknown> => typeof val === "object" && val !== null;
347
+ const isArray = (val: unknown): val is unknown[] => Array.isArray(val);
348
+
349
+ if (!isObject(source) || !isObject(comparison)) {
350
+ return source;
351
+ }
352
+
353
+ const res = isArray(source) ? [...source] : { ...source };
354
+
355
+ if (isArray(res)) {
356
+ for (let i = res.length - 1; i >= 0; i--) {
357
+ if (res[i] === comparison[i]) {
358
+ res.splice(i, 1);
359
+ } else if (isObject(res[i]) && isObject(comparison[i])) {
360
+ res[i] = removePropsIfExisting(res[i] as Record<string, unknown>, (comparison as unknown as unknown[])[i] as Record<string, unknown>);
361
+ }
362
+ }
363
+ } else {
364
+ Object.keys(comparison).forEach(key => {
365
+ if (key in res) {
366
+ if (isObject(res[key]) && isObject(comparison[key])) {
367
+ res[key] = removePropsIfExisting(res[key], comparison[key]);
368
+ } else if (res[key] === comparison[key]) {
369
+ delete res[key];
370
+ }
371
+ }
372
+ });
373
+ }
374
+
375
+ return res;
376
+ }
package/src/os.ts ADDED
@@ -0,0 +1,13 @@
1
+ export function getOS() {
2
+ let OS = "Unknown";
3
+ if (navigator.userAgent.indexOf("Win") !== -1) OS = "Windows";
4
+ if (navigator.userAgent.indexOf("Mac") !== -1) OS = "MacOS";
5
+ if (navigator.userAgent.indexOf("X11") !== -1) OS = "UNIX";
6
+ if (navigator.userAgent.indexOf("Linux") !== -1) OS = "Linux";
7
+ return OS;
8
+ }
9
+
10
+ export function getAltSymbol() {
11
+ if (getOS() === "MacOS") return "⌥";
12
+ return "Alt";
13
+ }
package/src/plurals.ts ADDED
@@ -0,0 +1,188 @@
1
+ /**
2
+ * Returns the plural of an English word.
3
+ *
4
+ * @param {string} word
5
+ * @param {number} [amount]
6
+ * @returns {string}
7
+ */
8
+ export function plural(word: string, amount?: number): string {
9
+ if (amount !== undefined && amount === 1) {
10
+ return word
11
+ }
12
+ const plurals: { [key: string]: string } = {
13
+ "(quiz)$": "$1zes",
14
+ "^(ox)$": "$1en",
15
+ "([m|l])ouse$": "$1ice",
16
+ "(matr|vert|ind)ix|ex$": "$1ices",
17
+ "(x|ch|ss|sh)$": "$1es",
18
+ "([^aeiouy]|qu)y$": "$1ies",
19
+ "(hive)$": "$1s",
20
+ "(?:([^f])fe|([lr])f)$": "$1$2ves",
21
+ "(shea|lea|loa|thie)f$": "$1ves",
22
+ sis$: "ses",
23
+ "([ti])um$": "$1a",
24
+ "(tomat|potat|ech|her|vet)o$": "$1oes",
25
+ "(bu)s$": "$1ses",
26
+ "(alias)$": "$1es",
27
+ "(octop)us$": "$1i",
28
+ "(ax|test)is$": "$1es",
29
+ "(us)$": "$1es",
30
+ "([^s]+)$": "$1s"
31
+ }
32
+ const irregular: { [key: string]: string } = {
33
+ move: "moves",
34
+ foot: "feet",
35
+ goose: "geese",
36
+ sex: "sexes",
37
+ child: "children",
38
+ man: "men",
39
+ tooth: "teeth",
40
+ person: "people"
41
+ }
42
+ const uncountable: string[] = [
43
+ "sheep",
44
+ "fish",
45
+ "deer",
46
+ "moose",
47
+ "series",
48
+ "species",
49
+ "money",
50
+ "rice",
51
+ "information",
52
+ "equipment",
53
+ "bison",
54
+ "cod",
55
+ "offspring",
56
+ "pike",
57
+ "salmon",
58
+ "shrimp",
59
+ "swine",
60
+ "trout",
61
+ "aircraft",
62
+ "hovercraft",
63
+ "spacecraft",
64
+ "sugar",
65
+ "tuna",
66
+ "you",
67
+ "wood"
68
+ ]
69
+ // save some time in the case that singular and plural are the same
70
+ if (uncountable.indexOf(word.toLowerCase()) >= 0) {
71
+ return word;
72
+ }
73
+ // check for irregular forms
74
+ for (const w in irregular) {
75
+ const pattern = new RegExp(`${w}$`, "i")
76
+ const replace = irregular[w]
77
+ if (pattern.test(word)) {
78
+ return word.replace(pattern, replace);
79
+ }
80
+ }
81
+ // check for matches using regular expressions
82
+ for (const reg in plurals) {
83
+ const pattern = new RegExp(reg, "i")
84
+ if (pattern.test(word)) {
85
+ return word.replace(pattern, plurals[reg])
86
+ }
87
+ }
88
+ return word;
89
+ }
90
+
91
+ /**
92
+ * Returns the singular of an English word.
93
+ *
94
+ * @param {string} word
95
+ * @param {number} [amount]
96
+ * @returns {string}
97
+ */
98
+ export function singular(word: string, amount?: number): string {
99
+ if (amount !== undefined && amount !== 1) {
100
+ return word;
101
+ }
102
+ const singulars: { [key: string]: string } = {
103
+ "(quiz)zes$": "$1",
104
+ "(matr)ices$": "$1ix",
105
+ "(vert|ind)ices$": "$1ex",
106
+ "^(ox)en$": "$1",
107
+ "(alias)es$": "$1",
108
+ "(octop|vir)i$": "$1us",
109
+ "(cris|ax|test)es$": "$1is",
110
+ "(shoe)s$": "$1",
111
+ "(o)es$": "$1",
112
+ "(bus)es$": "$1",
113
+ "([m|l])ice$": "$1ouse",
114
+ "(x|ch|ss|sh)es$": "$1",
115
+ "(m)ovies$": "$1ovie",
116
+ "(s)eries$": "$1eries",
117
+ "([^aeiouy]|qu)ies$": "$1y",
118
+ "([lr])ves$": "$1f",
119
+ "(tive)s$": "$1",
120
+ "(hive)s$": "$1",
121
+ "(li|wi|kni)ves$": "$1fe",
122
+ "(shea|loa|lea|thie)ves$": "$1f",
123
+ "(^analy)ses$": "$1sis",
124
+ "((a)naly|(b)a|(d)iagno|(p)arenthe|(p)rogno|(s)ynop|(t)he)ses$": "$1$2sis",
125
+ "([ti])a$": "$1um",
126
+ "(n)ews$": "$1ews",
127
+ "(h|bl)ouses$": "$1ouse",
128
+ "(corpse)s$": "$1",
129
+ "(us)es$": "$1",
130
+ s$: ""
131
+ }
132
+ const irregular: { [key: string]: string } = {
133
+ move: "moves",
134
+ foot: "feet",
135
+ goose: "geese",
136
+ sex: "sexes",
137
+ child: "children",
138
+ man: "men",
139
+ tooth: "teeth",
140
+ person: "people"
141
+ }
142
+ const uncountable: string[] = [
143
+ "sheep",
144
+ "fish",
145
+ "deer",
146
+ "moose",
147
+ "series",
148
+ "species",
149
+ "money",
150
+ "rice",
151
+ "information",
152
+ "equipment",
153
+ "bison",
154
+ "cod",
155
+ "offspring",
156
+ "pike",
157
+ "salmon",
158
+ "shrimp",
159
+ "swine",
160
+ "trout",
161
+ "aircraft",
162
+ "hovercraft",
163
+ "spacecraft",
164
+ "sugar",
165
+ "tuna",
166
+ "you",
167
+ "wood"
168
+ ]
169
+ // save some time in the case that singular and plural are the same
170
+ if (uncountable.indexOf(word.toLowerCase()) >= 0) {
171
+ return word;
172
+ }
173
+ // check for irregular forms
174
+ for (const w in irregular) {
175
+ const pattern = new RegExp(`${irregular[w]}$`, "i");
176
+ if (pattern.test(word)) {
177
+ return word.replace(pattern, w);
178
+ }
179
+ }
180
+ // check for matches using regular expressions
181
+ for (const reg in singulars) {
182
+ const pattern = new RegExp(reg, "i");
183
+ if (pattern.test(word)) {
184
+ return word.replace(pattern, singulars[reg]);
185
+ }
186
+ }
187
+ return word;
188
+ }
package/src/regexp.ts ADDED
@@ -0,0 +1,32 @@
1
+ export function serializeRegExp(input: RegExp): string {
2
+ if (!input) return "";
3
+ // const fragments = input.toString().match(/\/(.*?)\/([a-z]*)?$/i);
4
+ // if (fragments) {
5
+ // if (fragments[2])
6
+ // return input.toString();
7
+ // return fragments[1];
8
+ // }
9
+ return input.toString();
10
+ }
11
+
12
+ /**
13
+ * Get a RegExp out of a serialized string
14
+ * @param input
15
+ */
16
+ export function hydrateRegExp(input?: string): RegExp | undefined {
17
+ if (!input) return undefined;
18
+ const fragments = input.match(/\/(.*?)\/([a-z]*)?$/i);
19
+ if (fragments) {
20
+ return new RegExp(fragments[1], fragments[2] || "");
21
+ } else {
22
+ return new RegExp(input, "");
23
+ }
24
+ }
25
+
26
+ export function isValidRegExp(input: string): boolean {
27
+ const fullRegexp = input.match(/\/((?![*+?])(?:[^\r\n[/\\]|\\.|\[(?:[^\r\n\]\\]|\\.)*])+)\/((?:g(?:im?|mi?)?|i(?:gm?|mg?)?|m(?:gi?|ig?)?)?)/);
28
+ if (fullRegexp)
29
+ return true;
30
+ const simpleRegexp = input.match(/((?![*+?])(?:[^\r\n[/\\]|\\.|\[(?:[^\r\n\]\\]|\\.)*])+)/);
31
+ return !!simpleRegexp;
32
+ }
package/src/strings.ts ADDED
@@ -0,0 +1,84 @@
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 randomString(strLength = 5) {
22
+ return Math.random().toString(36).slice(2, 2 + strLength);
23
+ }
24
+
25
+ export function randomColor() {
26
+ return Math.floor(Math.random() * 16777215).toString(16);
27
+ }
28
+
29
+ export function slugify(text?: string, separator = "_", lowercase = true) {
30
+ if (!text) return "";
31
+ const from = "ãàáäâẽèéëêìíïîõòóöôùúüûñç·/_,:;-"
32
+ const to = `aaaaaeeeeeiiiiooooouuuunc${separator}${separator}${separator}${separator}${separator}${separator}${separator}`;
33
+
34
+ for (let i = 0, l = from.length; i < l; i++) {
35
+ text = text.replace(new RegExp(from.charAt(i), "g"), to.charAt(i));
36
+ }
37
+
38
+ text = text
39
+ .toString() // Cast to string
40
+ .trim() // Remove whitespace from both sides of a string
41
+ .replace(/^\s+|\s+$/g, "")
42
+ .replace(/\s+/g, separator) // Replace spaces with separator
43
+ .replace(/&/g, separator) // Replace & with separator
44
+ .replace(/[^\w\\-]+/g, "") // Remove all non-word chars
45
+ .replace(new RegExp("\\" + separator + "\\" + separator + "+", "g"),
46
+ separator); // Replace multiple separators with single one
47
+
48
+ return lowercase
49
+ ? text.toLowerCase() // Convert the string to lowercase letters
50
+ : text;
51
+ }
52
+
53
+ export function unslugify(slug?: string): string {
54
+ if (!slug) return "";
55
+ if (slug.includes("-") || slug.includes("_") || !slug.includes(" ")) {
56
+ const result = slug.replace(/[-_]/g, " ");
57
+ return result.replace(/\w\S*/g, function (txt) {
58
+ return txt.charAt(0).toUpperCase() + txt.substring(1);
59
+ }).trim();
60
+ } else {
61
+ return slug.trim();
62
+ }
63
+ }
64
+
65
+ export function prettifyIdentifier(input: string) {
66
+ if (!input) return "";
67
+
68
+ let text = input;
69
+
70
+ // 1. Handle camelCase and Acronyms
71
+ // Group 1 ($1 $2): Lowercase followed by Uppercase (e.g., imageURL -> image URL)
72
+ // Group 2 ($3 $4): Uppercase followed by Uppercase+lowercase (e.g., XMLParser -> XML Parser)
73
+ text = text.replace(/([a-z])([A-Z])|([A-Z])([A-Z][a-z])/g, "$1$3 $2$4");
74
+
75
+ // 2. Replace hyphens/underscores with spaces
76
+ text = text.replace(/[_-]+/g, " ");
77
+
78
+ // 3. Capitalize first letter of each word (Title Case)
79
+ const s = text
80
+ .trim()
81
+ .replace(/\b\w/g, (char) => char.toUpperCase());
82
+ console.log("Prettified identifier:", { input,s });
83
+ return s;
84
+ }