@zairakai/js-utils 1.0.0
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 +21 -0
- package/README.md +270 -0
- package/dist/arrays.cjs +210 -0
- package/dist/arrays.d.cts +119 -0
- package/dist/arrays.d.ts +119 -0
- package/dist/arrays.js +32 -0
- package/dist/chunk-27YHP2CK.js +407 -0
- package/dist/chunk-3WNRYKPG.js +37 -0
- package/dist/chunk-42CHLXT7.js +214 -0
- package/dist/chunk-6F4PWJZI.js +0 -0
- package/dist/chunk-7SXRFZBB.js +173 -0
- package/dist/chunk-F6RSTW65.js +156 -0
- package/dist/chunk-G7ZJ23DW.js +253 -0
- package/dist/chunk-IPP7PA6H.js +136 -0
- package/dist/chunk-LDSWHSRX.js +96 -0
- package/dist/chunk-TY75OOIQ.js +700 -0
- package/dist/chunk-W6JEMFAF.js +54 -0
- package/dist/chunk-XEJLBAXE.js +164 -0
- package/dist/chunk-Z7G3SIQH.js +270 -0
- package/dist/chunk-ZJPKS2MQ.js +101 -0
- package/dist/collections.cjs +797 -0
- package/dist/collections.d.cts +353 -0
- package/dist/collections.d.ts +353 -0
- package/dist/collections.js +17 -0
- package/dist/datetime.cjs +80 -0
- package/dist/datetime.d.cts +75 -0
- package/dist/datetime.d.ts +75 -0
- package/dist/datetime.js +24 -0
- package/dist/equals.cjs +121 -0
- package/dist/equals.d.cts +24 -0
- package/dist/equals.d.ts +24 -0
- package/dist/equals.js +8 -0
- package/dist/formatters.cjs +201 -0
- package/dist/formatters.d.cts +180 -0
- package/dist/formatters.d.ts +180 -0
- package/dist/formatters.js +48 -0
- package/dist/index.cjs +2906 -0
- package/dist/index.d.cts +120 -0
- package/dist/index.d.ts +120 -0
- package/dist/index.js +348 -0
- package/dist/number.cjs +279 -0
- package/dist/number.d.cts +177 -0
- package/dist/number.d.ts +177 -0
- package/dist/number.js +10 -0
- package/dist/obj.cjs +427 -0
- package/dist/obj.d.cts +177 -0
- package/dist/obj.d.ts +177 -0
- package/dist/obj.js +12 -0
- package/dist/php-arrays.cjs +954 -0
- package/dist/php-arrays.d.cts +256 -0
- package/dist/php-arrays.d.ts +256 -0
- package/dist/php-arrays.js +70 -0
- package/dist/runtime.cjs +134 -0
- package/dist/runtime.d.cts +90 -0
- package/dist/runtime.d.ts +90 -0
- package/dist/runtime.js +24 -0
- package/dist/schemas.cjs +86 -0
- package/dist/schemas.d.cts +108 -0
- package/dist/schemas.d.ts +108 -0
- package/dist/schemas.js +22 -0
- package/dist/str.cjs +499 -0
- package/dist/str.d.cts +282 -0
- package/dist/str.d.ts +282 -0
- package/dist/str.js +11 -0
- package/dist/types.cjs +18 -0
- package/dist/types.d.cts +13 -0
- package/dist/types.d.ts +13 -0
- package/dist/types.js +1 -0
- package/dist/validator.cjs +251 -0
- package/dist/validator.d.cts +99 -0
- package/dist/validator.d.ts +99 -0
- package/dist/validator.js +11 -0
- package/dist/validators.cjs +217 -0
- package/dist/validators.d.cts +216 -0
- package/dist/validators.d.ts +216 -0
- package/dist/validators.js +64 -0
- package/package.json +180 -0
- package/src/arrays.ts +316 -0
- package/src/collections.ts +866 -0
- package/src/datetime.ts +103 -0
- package/src/equals.ts +134 -0
- package/src/formatters.ts +342 -0
- package/src/index.ts +36 -0
- package/src/number.ts +281 -0
- package/src/obj.ts +303 -0
- package/src/php-arrays.ts +445 -0
- package/src/pipe.ts +29 -0
- package/src/runtime.ts +194 -0
- package/src/schemas.ts +136 -0
- package/src/str.ts +438 -0
- package/src/types.ts +13 -0
- package/src/validator.ts +157 -0
- package/src/validators.ts +359 -0
package/dist/arrays.d.ts
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Array manipulation and utility functions
|
|
3
|
+
* Inspired by Laravel's array helpers
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* Add an element to an array or record at a specific key.
|
|
7
|
+
*
|
|
8
|
+
* @param {T[] | Record<string, T>} array The source array or record
|
|
9
|
+
* @param {string} key The key to add the value at
|
|
10
|
+
* @param {T} value The value to add
|
|
11
|
+
* @returns {Record<string, unknown>} The resulting record
|
|
12
|
+
*/
|
|
13
|
+
declare const arrayAdd: <T>(array: T[] | Record<string, T>, key: string, value: T) => Record<string, unknown>;
|
|
14
|
+
/**
|
|
15
|
+
* Collapse an array of arrays into a single array.
|
|
16
|
+
*
|
|
17
|
+
* @param {T[][]} array The array of arrays to collapse
|
|
18
|
+
* @returns {T[]} The collapsed array
|
|
19
|
+
*/
|
|
20
|
+
declare const arrayCollapse: <T>(array: T[][]) => T[];
|
|
21
|
+
/**
|
|
22
|
+
* Divide an array into two arrays: one with keys (indices) and one with values.
|
|
23
|
+
*
|
|
24
|
+
* @param {T[]} array The array to divide
|
|
25
|
+
* @returns {[string[], T[]]} A tuple containing keys and values
|
|
26
|
+
*/
|
|
27
|
+
declare const arrayDivide: <T>(array: T[]) => [string[], T[]];
|
|
28
|
+
/**
|
|
29
|
+
* Flatten a multi-dimensional record into a single-level record using dot notation.
|
|
30
|
+
*
|
|
31
|
+
* @param {Record<string, unknown>} array The record to flatten
|
|
32
|
+
* @param {string} prepend The string to prepend to keys
|
|
33
|
+
* @returns {Record<string, unknown>} The flattened record
|
|
34
|
+
*/
|
|
35
|
+
declare const arrayDot: (array: Record<string, unknown>, prepend?: string) => Record<string, unknown>;
|
|
36
|
+
/**
|
|
37
|
+
* Get all of the given array except for a specified array of keys (indices).
|
|
38
|
+
*
|
|
39
|
+
* @param {T[]} array The source array
|
|
40
|
+
* @param {string[]} keys The keys to exclude
|
|
41
|
+
* @returns {T[]} The filtered array
|
|
42
|
+
*/
|
|
43
|
+
declare const arrayExcept: <T>(array: T[], keys: string[]) => T[];
|
|
44
|
+
/**
|
|
45
|
+
* Return the first element in an array passing a given truth test.
|
|
46
|
+
*
|
|
47
|
+
* @param {T[]} array The source array
|
|
48
|
+
* @param {Function} [callback] Optional callback for truth test
|
|
49
|
+
* @param {T} [defaultValue] Optional default value if no element found
|
|
50
|
+
* @returns {T | undefined} The first matching element or default value
|
|
51
|
+
*/
|
|
52
|
+
declare const arrayFirst: <T>(array: T[], callback?: (item: T, index: number) => boolean, defaultValue?: T) => T | undefined;
|
|
53
|
+
/**
|
|
54
|
+
* Flatten a multi-dimensional array into a single level.
|
|
55
|
+
*
|
|
56
|
+
* @param {unknown[]} array The array to flatten
|
|
57
|
+
* @param {number} [depth=Infinity] The depth to flatten to
|
|
58
|
+
* @returns {T[]} The flattened array
|
|
59
|
+
*/
|
|
60
|
+
declare const arrayFlatten: <T>(array: unknown[], depth?: number) => T[];
|
|
61
|
+
/**
|
|
62
|
+
* Get an item from a record using dot notation.
|
|
63
|
+
*
|
|
64
|
+
* @param {Record<string, unknown>} array The source record
|
|
65
|
+
* @param {string} key The key to retrieve
|
|
66
|
+
* @param {unknown} [defaultValue] The default value if key not found
|
|
67
|
+
* @returns {unknown} The value at the key or default value
|
|
68
|
+
*/
|
|
69
|
+
declare const arrayGet: (array: Record<string, unknown>, key: string, defaultValue?: unknown) => unknown;
|
|
70
|
+
/**
|
|
71
|
+
* Check if an item or items exist in a record using "dot" notation.
|
|
72
|
+
*
|
|
73
|
+
* @param {Record<string, unknown>} array The source record
|
|
74
|
+
* @param {string | string[]} keys The key or keys to check
|
|
75
|
+
* @returns {boolean} True if all keys exist, false otherwise
|
|
76
|
+
*/
|
|
77
|
+
declare const arrayHas: (array: Record<string, unknown>, keys: string | string[]) => boolean;
|
|
78
|
+
/**
|
|
79
|
+
* Get a subset of the array's elements specified by keys (indices).
|
|
80
|
+
*
|
|
81
|
+
* @param {T[]} array The source array
|
|
82
|
+
* @param {string[]} keys The keys to include
|
|
83
|
+
* @returns {T[]} The filtered array
|
|
84
|
+
*/
|
|
85
|
+
declare const arrayOnly: <T>(array: T[], keys: string[]) => T[];
|
|
86
|
+
/**
|
|
87
|
+
* Filter the array using the given callback.
|
|
88
|
+
*
|
|
89
|
+
* @param {T[]} array The source array
|
|
90
|
+
* @param {Function} callback The callback to use for filtering
|
|
91
|
+
* @returns {T[]} The filtered array
|
|
92
|
+
*/
|
|
93
|
+
declare const arrayWhere: <T>(array: T[], callback: (item: T, index: number) => boolean) => T[];
|
|
94
|
+
/**
|
|
95
|
+
* Pluck an array of values from an array of objects.
|
|
96
|
+
*
|
|
97
|
+
* @param {T[]} array The source array
|
|
98
|
+
* @param {K} key The key to pluck
|
|
99
|
+
* @returns {T[K][]} The plucked values
|
|
100
|
+
*/
|
|
101
|
+
declare const arrayPluck: <T, K extends keyof T>(array: T[], key: K) => T[K][];
|
|
102
|
+
/**
|
|
103
|
+
* Group an array's items by a given key or callback.
|
|
104
|
+
*
|
|
105
|
+
* @param {T[]} array The source array
|
|
106
|
+
* @param {keyof T | Function} keyOrFn The key or callback to group by
|
|
107
|
+
* @returns {Record<string, T[]>} The grouped items
|
|
108
|
+
*/
|
|
109
|
+
declare const arrayGroupBy: <T>(array: T[], keyOrFn: keyof T | ((item: T) => string | number)) => Record<string, T[]>;
|
|
110
|
+
/**
|
|
111
|
+
* Filter the array to only unique elements.
|
|
112
|
+
*
|
|
113
|
+
* @param {T[]} array The source array
|
|
114
|
+
* @param {keyof T} [key] Optional key to use for uniqueness
|
|
115
|
+
* @returns {T[]} The array with unique elements
|
|
116
|
+
*/
|
|
117
|
+
declare const arrayUnique: <T>(array: T[], key?: keyof T) => T[];
|
|
118
|
+
|
|
119
|
+
export { arrayAdd, arrayCollapse, arrayDivide, arrayDot, arrayExcept, arrayFirst, arrayFlatten, arrayGet, arrayGroupBy, arrayHas, arrayOnly, arrayPluck, arrayUnique, arrayWhere };
|
package/dist/arrays.js
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import {
|
|
2
|
+
arrayAdd,
|
|
3
|
+
arrayCollapse,
|
|
4
|
+
arrayDivide,
|
|
5
|
+
arrayDot,
|
|
6
|
+
arrayExcept,
|
|
7
|
+
arrayFirst,
|
|
8
|
+
arrayFlatten,
|
|
9
|
+
arrayGet,
|
|
10
|
+
arrayGroupBy,
|
|
11
|
+
arrayHas,
|
|
12
|
+
arrayOnly,
|
|
13
|
+
arrayPluck,
|
|
14
|
+
arrayUnique,
|
|
15
|
+
arrayWhere
|
|
16
|
+
} from "./chunk-7SXRFZBB.js";
|
|
17
|
+
export {
|
|
18
|
+
arrayAdd,
|
|
19
|
+
arrayCollapse,
|
|
20
|
+
arrayDivide,
|
|
21
|
+
arrayDot,
|
|
22
|
+
arrayExcept,
|
|
23
|
+
arrayFirst,
|
|
24
|
+
arrayFlatten,
|
|
25
|
+
arrayGet,
|
|
26
|
+
arrayGroupBy,
|
|
27
|
+
arrayHas,
|
|
28
|
+
arrayOnly,
|
|
29
|
+
arrayPluck,
|
|
30
|
+
arrayUnique,
|
|
31
|
+
arrayWhere
|
|
32
|
+
};
|
|
@@ -0,0 +1,407 @@
|
|
|
1
|
+
import {
|
|
2
|
+
camelCase,
|
|
3
|
+
capitalize,
|
|
4
|
+
kebabCase,
|
|
5
|
+
slugify,
|
|
6
|
+
snakeCase,
|
|
7
|
+
strContainsAll,
|
|
8
|
+
strContainsAny,
|
|
9
|
+
strFinish,
|
|
10
|
+
strLimit,
|
|
11
|
+
strMask,
|
|
12
|
+
strReverse,
|
|
13
|
+
strStart,
|
|
14
|
+
studlyCase,
|
|
15
|
+
titleCase
|
|
16
|
+
} from "./chunk-F6RSTW65.js";
|
|
17
|
+
|
|
18
|
+
// src/str.ts
|
|
19
|
+
var Stringable = class {
|
|
20
|
+
constructor(value) {
|
|
21
|
+
this.value = String(value ?? "");
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Get the raw string value
|
|
25
|
+
*
|
|
26
|
+
* @returns {string} The raw string
|
|
27
|
+
*/
|
|
28
|
+
toString() {
|
|
29
|
+
return this.value;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Alias for toString()
|
|
33
|
+
*
|
|
34
|
+
* @returns {string} The raw string
|
|
35
|
+
*/
|
|
36
|
+
get() {
|
|
37
|
+
return this.value;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Convert to title case
|
|
41
|
+
*
|
|
42
|
+
* @returns {this} The Stringable instance
|
|
43
|
+
*/
|
|
44
|
+
title() {
|
|
45
|
+
this.value = titleCase(this.value);
|
|
46
|
+
return this;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Convert to slug
|
|
50
|
+
*
|
|
51
|
+
* @returns {this} The Stringable instance
|
|
52
|
+
*/
|
|
53
|
+
slug() {
|
|
54
|
+
this.value = slugify(this.value);
|
|
55
|
+
return this;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Convert to snake_case
|
|
59
|
+
*
|
|
60
|
+
* @returns {this} The Stringable instance
|
|
61
|
+
*/
|
|
62
|
+
snake() {
|
|
63
|
+
this.value = snakeCase(this.value);
|
|
64
|
+
return this;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Convert to kebab-case
|
|
68
|
+
*
|
|
69
|
+
* @returns {this} The Stringable instance
|
|
70
|
+
*/
|
|
71
|
+
kebab() {
|
|
72
|
+
this.value = kebabCase(this.value);
|
|
73
|
+
return this;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Convert to camelCase
|
|
77
|
+
*
|
|
78
|
+
* @returns {this} The Stringable instance
|
|
79
|
+
*/
|
|
80
|
+
camel() {
|
|
81
|
+
this.value = camelCase(this.value);
|
|
82
|
+
return this;
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Convert to StudlyCase
|
|
86
|
+
*
|
|
87
|
+
* @returns {this} The Stringable instance
|
|
88
|
+
*/
|
|
89
|
+
studly() {
|
|
90
|
+
this.value = studlyCase(this.value);
|
|
91
|
+
return this;
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Limit the string length
|
|
95
|
+
*
|
|
96
|
+
* @param {number} size The maximum length
|
|
97
|
+
* @param {string} [end='…'] The string to append if limited
|
|
98
|
+
* @returns {this} The Stringable instance
|
|
99
|
+
*/
|
|
100
|
+
limit(size, end = "\u2026") {
|
|
101
|
+
this.value = strLimit(this.value, size);
|
|
102
|
+
if (this.value.endsWith("\u2026") && "\u2026" !== end) {
|
|
103
|
+
this.value = this.value.slice(0, -1) + end;
|
|
104
|
+
}
|
|
105
|
+
return this;
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Append a value to the string
|
|
109
|
+
*
|
|
110
|
+
* @param {...unknown[]} values The values to append
|
|
111
|
+
* @returns {this} The Stringable instance
|
|
112
|
+
*/
|
|
113
|
+
append(...values) {
|
|
114
|
+
this.value += values.join("");
|
|
115
|
+
return this;
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Prepend a value to the string
|
|
119
|
+
*
|
|
120
|
+
* @param {...unknown[]} values The values to prepend
|
|
121
|
+
* @returns {this} The Stringable instance
|
|
122
|
+
*/
|
|
123
|
+
prepend(...values) {
|
|
124
|
+
this.value = values.join("") + this.value;
|
|
125
|
+
return this;
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Cap the string with a value if it doesn't already end with it
|
|
129
|
+
*
|
|
130
|
+
* @param {string} cap The string to end with
|
|
131
|
+
* @returns {this} The Stringable instance
|
|
132
|
+
*/
|
|
133
|
+
finish(cap) {
|
|
134
|
+
this.value = strFinish(this.value, cap);
|
|
135
|
+
return this;
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Start the string with a value if it doesn't already start with it
|
|
139
|
+
*
|
|
140
|
+
* @param {string} prefix The string to start with
|
|
141
|
+
* @returns {this} The Stringable instance
|
|
142
|
+
*/
|
|
143
|
+
start(prefix) {
|
|
144
|
+
this.value = strStart(this.value, prefix);
|
|
145
|
+
return this;
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* Replace the first occurrence of a value
|
|
149
|
+
*
|
|
150
|
+
* @param {string | RegExp} search The value to search for
|
|
151
|
+
* @param {string} replace The value to replace with
|
|
152
|
+
* @returns {this} The Stringable instance
|
|
153
|
+
*/
|
|
154
|
+
replace(search, replace) {
|
|
155
|
+
this.value = this.value.replace(search, replace);
|
|
156
|
+
return this;
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* Replace all occurrences of a value
|
|
160
|
+
*
|
|
161
|
+
* @param {string | RegExp} search The value to search for
|
|
162
|
+
* @param {string} replace The value to replace with
|
|
163
|
+
* @returns {this} The Stringable instance
|
|
164
|
+
*/
|
|
165
|
+
replaceAll(search, replace) {
|
|
166
|
+
if ("string" === typeof search) {
|
|
167
|
+
this.value = this.value.split(search).join(replace);
|
|
168
|
+
} else {
|
|
169
|
+
const flags = search.flags.includes("g") ? search.flags : `${search.flags}g`;
|
|
170
|
+
this.value = this.value.replace(new RegExp(search.source, flags), replace);
|
|
171
|
+
}
|
|
172
|
+
return this;
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* Reverse the string
|
|
176
|
+
*
|
|
177
|
+
* @returns {this} The Stringable instance
|
|
178
|
+
*/
|
|
179
|
+
reverse() {
|
|
180
|
+
this.value = strReverse(this.value);
|
|
181
|
+
return this;
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* Mask a portion of the string
|
|
185
|
+
*
|
|
186
|
+
* @param {string} character The masking character
|
|
187
|
+
* @param {number} index The starting index
|
|
188
|
+
* @param {number} [length] The number of characters to mask
|
|
189
|
+
* @returns {this} The Stringable instance
|
|
190
|
+
*/
|
|
191
|
+
mask(character, index, length) {
|
|
192
|
+
this.value = strMask(this.value, character, index, length);
|
|
193
|
+
return this;
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* Trim the string
|
|
197
|
+
*
|
|
198
|
+
* @param {string} [chars] The characters to trim (defaults to whitespace)
|
|
199
|
+
* @returns {this} The Stringable instance
|
|
200
|
+
*/
|
|
201
|
+
trim(chars) {
|
|
202
|
+
if (!chars) {
|
|
203
|
+
this.value = this.value.trim();
|
|
204
|
+
} else {
|
|
205
|
+
const pattern = new RegExp(`^[${chars}]+|[${chars}]+$`, "g");
|
|
206
|
+
this.value = this.value.replace(pattern, "");
|
|
207
|
+
}
|
|
208
|
+
return this;
|
|
209
|
+
}
|
|
210
|
+
/**
|
|
211
|
+
* Convert to lower case
|
|
212
|
+
*
|
|
213
|
+
* @returns {this} The Stringable instance
|
|
214
|
+
*/
|
|
215
|
+
lower() {
|
|
216
|
+
this.value = this.value.toLowerCase();
|
|
217
|
+
return this;
|
|
218
|
+
}
|
|
219
|
+
/**
|
|
220
|
+
* Convert to upper case
|
|
221
|
+
*
|
|
222
|
+
* @returns {this} The Stringable instance
|
|
223
|
+
*/
|
|
224
|
+
upper() {
|
|
225
|
+
this.value = this.value.toUpperCase();
|
|
226
|
+
return this;
|
|
227
|
+
}
|
|
228
|
+
/**
|
|
229
|
+
* Capitalize the first letter
|
|
230
|
+
*
|
|
231
|
+
* @returns {this} The Stringable instance
|
|
232
|
+
*/
|
|
233
|
+
capitalize() {
|
|
234
|
+
this.value = capitalize(this.value);
|
|
235
|
+
return this;
|
|
236
|
+
}
|
|
237
|
+
/**
|
|
238
|
+
* Check if string contains a value
|
|
239
|
+
*
|
|
240
|
+
* @param {string | string[]} needles The values to search for
|
|
241
|
+
* @returns {boolean} True if the string contains any of the values
|
|
242
|
+
*/
|
|
243
|
+
contains(needles) {
|
|
244
|
+
if (Array.isArray(needles)) {
|
|
245
|
+
return strContainsAny(this.value, needles);
|
|
246
|
+
}
|
|
247
|
+
return this.value.includes(needles);
|
|
248
|
+
}
|
|
249
|
+
/**
|
|
250
|
+
* Check if string contains all values
|
|
251
|
+
*
|
|
252
|
+
* @param {string[]} needles The values to search for
|
|
253
|
+
* @returns {boolean} True if the string contains all values
|
|
254
|
+
*/
|
|
255
|
+
containsAll(needles) {
|
|
256
|
+
return strContainsAll(this.value, needles);
|
|
257
|
+
}
|
|
258
|
+
/**
|
|
259
|
+
* Check if string starts with a value
|
|
260
|
+
*
|
|
261
|
+
* @param {string | string[]} needles The values to check
|
|
262
|
+
* @returns {boolean} True if the string starts with any of the values
|
|
263
|
+
*/
|
|
264
|
+
startsWith(needles) {
|
|
265
|
+
if (Array.isArray(needles)) {
|
|
266
|
+
return needles.some((needle) => this.value.startsWith(needle));
|
|
267
|
+
}
|
|
268
|
+
return this.value.startsWith(needles);
|
|
269
|
+
}
|
|
270
|
+
/**
|
|
271
|
+
* Check if string ends with a value
|
|
272
|
+
*
|
|
273
|
+
* @param {string | string[]} needles The values to check
|
|
274
|
+
* @returns {boolean} True if the string ends with any of the values
|
|
275
|
+
*/
|
|
276
|
+
endsWith(needles) {
|
|
277
|
+
if (Array.isArray(needles)) {
|
|
278
|
+
return needles.some((needle) => this.value.endsWith(needle));
|
|
279
|
+
}
|
|
280
|
+
return this.value.endsWith(needles);
|
|
281
|
+
}
|
|
282
|
+
/**
|
|
283
|
+
* Execute a callback with the stringable and return the result
|
|
284
|
+
*
|
|
285
|
+
* @param {(str: this) => U} callback The callback to execute
|
|
286
|
+
* @returns {U} The result of the callback
|
|
287
|
+
*/
|
|
288
|
+
pipe(callback) {
|
|
289
|
+
return callback(this);
|
|
290
|
+
}
|
|
291
|
+
/**
|
|
292
|
+
* Conditionally execute a callback
|
|
293
|
+
*
|
|
294
|
+
* @param {boolean | (() => boolean)} condition The condition to check
|
|
295
|
+
* @param {(str: this) => void} callback The callback to execute
|
|
296
|
+
* @returns {this} The Stringable instance
|
|
297
|
+
*/
|
|
298
|
+
when(condition, callback) {
|
|
299
|
+
const shouldExecute = "function" === typeof condition ? condition() : condition;
|
|
300
|
+
if (shouldExecute) {
|
|
301
|
+
callback(this);
|
|
302
|
+
}
|
|
303
|
+
return this;
|
|
304
|
+
}
|
|
305
|
+
/**
|
|
306
|
+
* Execute a callback and return the stringable (for side effects)
|
|
307
|
+
*
|
|
308
|
+
* @param {(str: this) => void} callback The callback to execute
|
|
309
|
+
* @returns {this} The Stringable instance
|
|
310
|
+
*/
|
|
311
|
+
tap(callback) {
|
|
312
|
+
callback(this);
|
|
313
|
+
return this;
|
|
314
|
+
}
|
|
315
|
+
};
|
|
316
|
+
var str = (value) => {
|
|
317
|
+
return new Stringable(value);
|
|
318
|
+
};
|
|
319
|
+
var Str = {
|
|
320
|
+
/**
|
|
321
|
+
* Create a new fluent stringable instance
|
|
322
|
+
*
|
|
323
|
+
* @param {unknown} value The initial string value
|
|
324
|
+
* @returns {Stringable} A new Stringable instance
|
|
325
|
+
*/
|
|
326
|
+
of: (value) => new Stringable(value),
|
|
327
|
+
/**
|
|
328
|
+
* Convert a string to a slug
|
|
329
|
+
*
|
|
330
|
+
* @param {string} value The string to slugify
|
|
331
|
+
* @returns {string} The slugified string
|
|
332
|
+
*/
|
|
333
|
+
slug: (value) => slugify(value),
|
|
334
|
+
/**
|
|
335
|
+
* Convert a string to snake_case
|
|
336
|
+
*
|
|
337
|
+
* @param {string} value The string to convert
|
|
338
|
+
* @returns {string} The snake_case string
|
|
339
|
+
*/
|
|
340
|
+
snake: (value) => snakeCase(value),
|
|
341
|
+
/**
|
|
342
|
+
* Convert a string to kebab-case
|
|
343
|
+
*
|
|
344
|
+
* @param {string} value The string to convert
|
|
345
|
+
* @returns {string} The kebab-case string
|
|
346
|
+
*/
|
|
347
|
+
kebab: (value) => kebabCase(value),
|
|
348
|
+
/**
|
|
349
|
+
* Convert a string to camelCase
|
|
350
|
+
*
|
|
351
|
+
* @param {string} value The string to convert
|
|
352
|
+
* @returns {string} The camelCase string
|
|
353
|
+
*/
|
|
354
|
+
camel: (value) => camelCase(value),
|
|
355
|
+
/**
|
|
356
|
+
* Convert a string to StudlyCase
|
|
357
|
+
*
|
|
358
|
+
* @param {string} value The string to convert
|
|
359
|
+
* @returns {string} The StudlyCase string
|
|
360
|
+
*/
|
|
361
|
+
studly: (value) => studlyCase(value),
|
|
362
|
+
/**
|
|
363
|
+
* Convert a string to title case
|
|
364
|
+
*
|
|
365
|
+
* @param {string} value The string to convert
|
|
366
|
+
* @returns {string} The title case string
|
|
367
|
+
*/
|
|
368
|
+
title: (value) => titleCase(value),
|
|
369
|
+
/**
|
|
370
|
+
* Limit the length of a string
|
|
371
|
+
*
|
|
372
|
+
* @param {string} value The string to limit
|
|
373
|
+
* @param {number} size The maximum length
|
|
374
|
+
* @param {string} [end='…'] The string to append if limited
|
|
375
|
+
* @returns {string} The limited string
|
|
376
|
+
*/
|
|
377
|
+
limit: (value, size, end = "\u2026") => {
|
|
378
|
+
const result = strLimit(value, size);
|
|
379
|
+
return result.endsWith("\u2026") && "\u2026" !== end ? result.slice(0, -1) + end : result;
|
|
380
|
+
},
|
|
381
|
+
/**
|
|
382
|
+
* Generate a random alphanumeric string
|
|
383
|
+
*
|
|
384
|
+
* @param {number} [length=16] The length of the random string
|
|
385
|
+
* @returns {string} The random string
|
|
386
|
+
*/
|
|
387
|
+
random: (length = 16) => {
|
|
388
|
+
const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
|
|
389
|
+
let result = "";
|
|
390
|
+
for (let i = 0; i < length; i++) {
|
|
391
|
+
result += chars.charAt(Math.floor(Math.random() * chars.length));
|
|
392
|
+
}
|
|
393
|
+
return result;
|
|
394
|
+
},
|
|
395
|
+
/**
|
|
396
|
+
* Generate a UUID (version 4)
|
|
397
|
+
*
|
|
398
|
+
* @returns {string} The generated UUID
|
|
399
|
+
*/
|
|
400
|
+
uuid: () => crypto.randomUUID()
|
|
401
|
+
};
|
|
402
|
+
|
|
403
|
+
export {
|
|
404
|
+
Stringable,
|
|
405
|
+
str,
|
|
406
|
+
Str
|
|
407
|
+
};
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
// src/datetime.ts
|
|
2
|
+
import dayjsModule from "dayjs";
|
|
3
|
+
import isBetween from "dayjs/plugin/isBetween";
|
|
4
|
+
import isSameOrAfter from "dayjs/plugin/isSameOrAfter";
|
|
5
|
+
import isSameOrBefore from "dayjs/plugin/isSameOrBefore";
|
|
6
|
+
import relativeTime from "dayjs/plugin/relativeTime";
|
|
7
|
+
import utc from "dayjs/plugin/utc";
|
|
8
|
+
dayjsModule.extend(isBetween);
|
|
9
|
+
dayjsModule.extend(isSameOrAfter);
|
|
10
|
+
dayjsModule.extend(isSameOrBefore);
|
|
11
|
+
dayjsModule.extend(relativeTime);
|
|
12
|
+
dayjsModule.extend(utc);
|
|
13
|
+
var dayjs = dayjsModule;
|
|
14
|
+
var now = () => dayjs();
|
|
15
|
+
var today = () => dayjs().startOf("day");
|
|
16
|
+
var tomorrow = () => dayjs().add(1, "day").startOf("day");
|
|
17
|
+
var yesterday = () => dayjs().subtract(1, "day").startOf("day");
|
|
18
|
+
var isBetweenDates = (date, start, end, unit = "day", inclusivity = "[]") => {
|
|
19
|
+
return dayjs(date).isBetween(dayjs(start), dayjs(end), unit, inclusivity);
|
|
20
|
+
};
|
|
21
|
+
var fromNow = (date) => dayjs(date).fromNow();
|
|
22
|
+
var isToday = (date) => dayjs(date).isSame(dayjs(), "day");
|
|
23
|
+
var isPast = (date) => dayjs(date).isBefore(dayjs());
|
|
24
|
+
var isFuture = (date) => dayjs(date).isAfter(dayjs());
|
|
25
|
+
|
|
26
|
+
export {
|
|
27
|
+
dayjs,
|
|
28
|
+
now,
|
|
29
|
+
today,
|
|
30
|
+
tomorrow,
|
|
31
|
+
yesterday,
|
|
32
|
+
isBetweenDates,
|
|
33
|
+
fromNow,
|
|
34
|
+
isToday,
|
|
35
|
+
isPast,
|
|
36
|
+
isFuture
|
|
37
|
+
};
|