@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.
Files changed (93) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +270 -0
  3. package/dist/arrays.cjs +210 -0
  4. package/dist/arrays.d.cts +119 -0
  5. package/dist/arrays.d.ts +119 -0
  6. package/dist/arrays.js +32 -0
  7. package/dist/chunk-27YHP2CK.js +407 -0
  8. package/dist/chunk-3WNRYKPG.js +37 -0
  9. package/dist/chunk-42CHLXT7.js +214 -0
  10. package/dist/chunk-6F4PWJZI.js +0 -0
  11. package/dist/chunk-7SXRFZBB.js +173 -0
  12. package/dist/chunk-F6RSTW65.js +156 -0
  13. package/dist/chunk-G7ZJ23DW.js +253 -0
  14. package/dist/chunk-IPP7PA6H.js +136 -0
  15. package/dist/chunk-LDSWHSRX.js +96 -0
  16. package/dist/chunk-TY75OOIQ.js +700 -0
  17. package/dist/chunk-W6JEMFAF.js +54 -0
  18. package/dist/chunk-XEJLBAXE.js +164 -0
  19. package/dist/chunk-Z7G3SIQH.js +270 -0
  20. package/dist/chunk-ZJPKS2MQ.js +101 -0
  21. package/dist/collections.cjs +797 -0
  22. package/dist/collections.d.cts +353 -0
  23. package/dist/collections.d.ts +353 -0
  24. package/dist/collections.js +17 -0
  25. package/dist/datetime.cjs +80 -0
  26. package/dist/datetime.d.cts +75 -0
  27. package/dist/datetime.d.ts +75 -0
  28. package/dist/datetime.js +24 -0
  29. package/dist/equals.cjs +121 -0
  30. package/dist/equals.d.cts +24 -0
  31. package/dist/equals.d.ts +24 -0
  32. package/dist/equals.js +8 -0
  33. package/dist/formatters.cjs +201 -0
  34. package/dist/formatters.d.cts +180 -0
  35. package/dist/formatters.d.ts +180 -0
  36. package/dist/formatters.js +48 -0
  37. package/dist/index.cjs +2906 -0
  38. package/dist/index.d.cts +120 -0
  39. package/dist/index.d.ts +120 -0
  40. package/dist/index.js +348 -0
  41. package/dist/number.cjs +279 -0
  42. package/dist/number.d.cts +177 -0
  43. package/dist/number.d.ts +177 -0
  44. package/dist/number.js +10 -0
  45. package/dist/obj.cjs +427 -0
  46. package/dist/obj.d.cts +177 -0
  47. package/dist/obj.d.ts +177 -0
  48. package/dist/obj.js +12 -0
  49. package/dist/php-arrays.cjs +954 -0
  50. package/dist/php-arrays.d.cts +256 -0
  51. package/dist/php-arrays.d.ts +256 -0
  52. package/dist/php-arrays.js +70 -0
  53. package/dist/runtime.cjs +134 -0
  54. package/dist/runtime.d.cts +90 -0
  55. package/dist/runtime.d.ts +90 -0
  56. package/dist/runtime.js +24 -0
  57. package/dist/schemas.cjs +86 -0
  58. package/dist/schemas.d.cts +108 -0
  59. package/dist/schemas.d.ts +108 -0
  60. package/dist/schemas.js +22 -0
  61. package/dist/str.cjs +499 -0
  62. package/dist/str.d.cts +282 -0
  63. package/dist/str.d.ts +282 -0
  64. package/dist/str.js +11 -0
  65. package/dist/types.cjs +18 -0
  66. package/dist/types.d.cts +13 -0
  67. package/dist/types.d.ts +13 -0
  68. package/dist/types.js +1 -0
  69. package/dist/validator.cjs +251 -0
  70. package/dist/validator.d.cts +99 -0
  71. package/dist/validator.d.ts +99 -0
  72. package/dist/validator.js +11 -0
  73. package/dist/validators.cjs +217 -0
  74. package/dist/validators.d.cts +216 -0
  75. package/dist/validators.d.ts +216 -0
  76. package/dist/validators.js +64 -0
  77. package/package.json +180 -0
  78. package/src/arrays.ts +316 -0
  79. package/src/collections.ts +866 -0
  80. package/src/datetime.ts +103 -0
  81. package/src/equals.ts +134 -0
  82. package/src/formatters.ts +342 -0
  83. package/src/index.ts +36 -0
  84. package/src/number.ts +281 -0
  85. package/src/obj.ts +303 -0
  86. package/src/php-arrays.ts +445 -0
  87. package/src/pipe.ts +29 -0
  88. package/src/runtime.ts +194 -0
  89. package/src/schemas.ts +136 -0
  90. package/src/str.ts +438 -0
  91. package/src/types.ts +13 -0
  92. package/src/validator.ts +157 -0
  93. package/src/validators.ts +359 -0
package/dist/str.cjs ADDED
@@ -0,0 +1,499 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/str.ts
21
+ var str_exports = {};
22
+ __export(str_exports, {
23
+ Str: () => Str,
24
+ Stringable: () => Stringable,
25
+ str: () => str
26
+ });
27
+ module.exports = __toCommonJS(str_exports);
28
+
29
+ // src/formatters.ts
30
+ var capitalize = (value) => {
31
+ if (value == null) {
32
+ return "";
33
+ }
34
+ const stringValue = String(value);
35
+ return stringValue.charAt(0).toUpperCase() + stringValue.slice(1).toLowerCase();
36
+ };
37
+ var slugify = (text) => {
38
+ if (!text) {
39
+ return "";
40
+ }
41
+ return text.toString().normalize("NFD").replace(/[\u0300-\u036f]/g, "").toLowerCase().trim().replace(/\s+/g, "-").replace(/[^\w-]+/g, "").replace(/--+/g, "-");
42
+ };
43
+ var strLimit = (value, size) => {
44
+ if (!value) {
45
+ return "";
46
+ }
47
+ const stringValue = String(value);
48
+ return stringValue.length <= size ? stringValue : `${stringValue.slice(0, size)}\u2026`;
49
+ };
50
+ var strContainsAll = (haystack, needles) => {
51
+ if (!haystack || !Array.isArray(needles)) {
52
+ return false;
53
+ }
54
+ return needles.every((needle) => haystack.includes(needle));
55
+ };
56
+ var strContainsAny = (haystack, needles) => {
57
+ if (!haystack || !Array.isArray(needles)) {
58
+ return false;
59
+ }
60
+ return needles.some((needle) => haystack.includes(needle));
61
+ };
62
+ var strFinish = (value, cap) => {
63
+ if (!value) {
64
+ return cap;
65
+ }
66
+ if (!cap) {
67
+ return value;
68
+ }
69
+ return value.endsWith(cap) ? value : value + cap;
70
+ };
71
+ var strStart = (value, prefix) => {
72
+ if (!value) {
73
+ return prefix;
74
+ }
75
+ if (!prefix) {
76
+ return value;
77
+ }
78
+ return value.startsWith(prefix) ? value : prefix + value;
79
+ };
80
+ var strReverse = (value) => {
81
+ return value.split("").reverse().join("");
82
+ };
83
+ var strMask = (str2, character, index, length) => {
84
+ if (!str2) {
85
+ return str2;
86
+ }
87
+ const start = Math.max(0, index);
88
+ const end = length !== void 0 ? start + length : str2.length;
89
+ return str2.substring(0, start) + character.repeat(end - start) + str2.substring(end);
90
+ };
91
+ var camelCase = (value) => {
92
+ const studly = studlyCase(value);
93
+ return studly.charAt(0).toLowerCase() + studly.slice(1);
94
+ };
95
+ var snakeCase = (value) => {
96
+ return value.replace(/([a-z])([A-Z])/g, "$1_$2").replace(/[\s-]+/g, "_").toLowerCase();
97
+ };
98
+ var kebabCase = (value) => {
99
+ return value.replace(/([a-z])([A-Z])/g, "$1-$2").replace(/[\s_]+/g, "-").toLowerCase();
100
+ };
101
+ var studlyCase = (value) => {
102
+ return value.replace(/[_-]/g, " ").replace(/\s+(.)/g, (_, c) => c.toUpperCase()).replace(/^(.)/, (_, c) => c.toUpperCase()).replace(/\s+/g, "");
103
+ };
104
+ var titleCase = (value) => {
105
+ return value.replace(/\w\S*/g, (txt) => {
106
+ return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
107
+ });
108
+ };
109
+
110
+ // src/str.ts
111
+ var Stringable = class {
112
+ constructor(value) {
113
+ this.value = String(value ?? "");
114
+ }
115
+ /**
116
+ * Get the raw string value
117
+ *
118
+ * @returns {string} The raw string
119
+ */
120
+ toString() {
121
+ return this.value;
122
+ }
123
+ /**
124
+ * Alias for toString()
125
+ *
126
+ * @returns {string} The raw string
127
+ */
128
+ get() {
129
+ return this.value;
130
+ }
131
+ /**
132
+ * Convert to title case
133
+ *
134
+ * @returns {this} The Stringable instance
135
+ */
136
+ title() {
137
+ this.value = titleCase(this.value);
138
+ return this;
139
+ }
140
+ /**
141
+ * Convert to slug
142
+ *
143
+ * @returns {this} The Stringable instance
144
+ */
145
+ slug() {
146
+ this.value = slugify(this.value);
147
+ return this;
148
+ }
149
+ /**
150
+ * Convert to snake_case
151
+ *
152
+ * @returns {this} The Stringable instance
153
+ */
154
+ snake() {
155
+ this.value = snakeCase(this.value);
156
+ return this;
157
+ }
158
+ /**
159
+ * Convert to kebab-case
160
+ *
161
+ * @returns {this} The Stringable instance
162
+ */
163
+ kebab() {
164
+ this.value = kebabCase(this.value);
165
+ return this;
166
+ }
167
+ /**
168
+ * Convert to camelCase
169
+ *
170
+ * @returns {this} The Stringable instance
171
+ */
172
+ camel() {
173
+ this.value = camelCase(this.value);
174
+ return this;
175
+ }
176
+ /**
177
+ * Convert to StudlyCase
178
+ *
179
+ * @returns {this} The Stringable instance
180
+ */
181
+ studly() {
182
+ this.value = studlyCase(this.value);
183
+ return this;
184
+ }
185
+ /**
186
+ * Limit the string length
187
+ *
188
+ * @param {number} size The maximum length
189
+ * @param {string} [end='…'] The string to append if limited
190
+ * @returns {this} The Stringable instance
191
+ */
192
+ limit(size, end = "\u2026") {
193
+ this.value = strLimit(this.value, size);
194
+ if (this.value.endsWith("\u2026") && "\u2026" !== end) {
195
+ this.value = this.value.slice(0, -1) + end;
196
+ }
197
+ return this;
198
+ }
199
+ /**
200
+ * Append a value to the string
201
+ *
202
+ * @param {...unknown[]} values The values to append
203
+ * @returns {this} The Stringable instance
204
+ */
205
+ append(...values) {
206
+ this.value += values.join("");
207
+ return this;
208
+ }
209
+ /**
210
+ * Prepend a value to the string
211
+ *
212
+ * @param {...unknown[]} values The values to prepend
213
+ * @returns {this} The Stringable instance
214
+ */
215
+ prepend(...values) {
216
+ this.value = values.join("") + this.value;
217
+ return this;
218
+ }
219
+ /**
220
+ * Cap the string with a value if it doesn't already end with it
221
+ *
222
+ * @param {string} cap The string to end with
223
+ * @returns {this} The Stringable instance
224
+ */
225
+ finish(cap) {
226
+ this.value = strFinish(this.value, cap);
227
+ return this;
228
+ }
229
+ /**
230
+ * Start the string with a value if it doesn't already start with it
231
+ *
232
+ * @param {string} prefix The string to start with
233
+ * @returns {this} The Stringable instance
234
+ */
235
+ start(prefix) {
236
+ this.value = strStart(this.value, prefix);
237
+ return this;
238
+ }
239
+ /**
240
+ * Replace the first occurrence of a value
241
+ *
242
+ * @param {string | RegExp} search The value to search for
243
+ * @param {string} replace The value to replace with
244
+ * @returns {this} The Stringable instance
245
+ */
246
+ replace(search, replace) {
247
+ this.value = this.value.replace(search, replace);
248
+ return this;
249
+ }
250
+ /**
251
+ * Replace all occurrences of a value
252
+ *
253
+ * @param {string | RegExp} search The value to search for
254
+ * @param {string} replace The value to replace with
255
+ * @returns {this} The Stringable instance
256
+ */
257
+ replaceAll(search, replace) {
258
+ if ("string" === typeof search) {
259
+ this.value = this.value.split(search).join(replace);
260
+ } else {
261
+ const flags = search.flags.includes("g") ? search.flags : `${search.flags}g`;
262
+ this.value = this.value.replace(new RegExp(search.source, flags), replace);
263
+ }
264
+ return this;
265
+ }
266
+ /**
267
+ * Reverse the string
268
+ *
269
+ * @returns {this} The Stringable instance
270
+ */
271
+ reverse() {
272
+ this.value = strReverse(this.value);
273
+ return this;
274
+ }
275
+ /**
276
+ * Mask a portion of the string
277
+ *
278
+ * @param {string} character The masking character
279
+ * @param {number} index The starting index
280
+ * @param {number} [length] The number of characters to mask
281
+ * @returns {this} The Stringable instance
282
+ */
283
+ mask(character, index, length) {
284
+ this.value = strMask(this.value, character, index, length);
285
+ return this;
286
+ }
287
+ /**
288
+ * Trim the string
289
+ *
290
+ * @param {string} [chars] The characters to trim (defaults to whitespace)
291
+ * @returns {this} The Stringable instance
292
+ */
293
+ trim(chars) {
294
+ if (!chars) {
295
+ this.value = this.value.trim();
296
+ } else {
297
+ const pattern = new RegExp(`^[${chars}]+|[${chars}]+$`, "g");
298
+ this.value = this.value.replace(pattern, "");
299
+ }
300
+ return this;
301
+ }
302
+ /**
303
+ * Convert to lower case
304
+ *
305
+ * @returns {this} The Stringable instance
306
+ */
307
+ lower() {
308
+ this.value = this.value.toLowerCase();
309
+ return this;
310
+ }
311
+ /**
312
+ * Convert to upper case
313
+ *
314
+ * @returns {this} The Stringable instance
315
+ */
316
+ upper() {
317
+ this.value = this.value.toUpperCase();
318
+ return this;
319
+ }
320
+ /**
321
+ * Capitalize the first letter
322
+ *
323
+ * @returns {this} The Stringable instance
324
+ */
325
+ capitalize() {
326
+ this.value = capitalize(this.value);
327
+ return this;
328
+ }
329
+ /**
330
+ * Check if string contains a value
331
+ *
332
+ * @param {string | string[]} needles The values to search for
333
+ * @returns {boolean} True if the string contains any of the values
334
+ */
335
+ contains(needles) {
336
+ if (Array.isArray(needles)) {
337
+ return strContainsAny(this.value, needles);
338
+ }
339
+ return this.value.includes(needles);
340
+ }
341
+ /**
342
+ * Check if string contains all values
343
+ *
344
+ * @param {string[]} needles The values to search for
345
+ * @returns {boolean} True if the string contains all values
346
+ */
347
+ containsAll(needles) {
348
+ return strContainsAll(this.value, needles);
349
+ }
350
+ /**
351
+ * Check if string starts with a value
352
+ *
353
+ * @param {string | string[]} needles The values to check
354
+ * @returns {boolean} True if the string starts with any of the values
355
+ */
356
+ startsWith(needles) {
357
+ if (Array.isArray(needles)) {
358
+ return needles.some((needle) => this.value.startsWith(needle));
359
+ }
360
+ return this.value.startsWith(needles);
361
+ }
362
+ /**
363
+ * Check if string ends with a value
364
+ *
365
+ * @param {string | string[]} needles The values to check
366
+ * @returns {boolean} True if the string ends with any of the values
367
+ */
368
+ endsWith(needles) {
369
+ if (Array.isArray(needles)) {
370
+ return needles.some((needle) => this.value.endsWith(needle));
371
+ }
372
+ return this.value.endsWith(needles);
373
+ }
374
+ /**
375
+ * Execute a callback with the stringable and return the result
376
+ *
377
+ * @param {(str: this) => U} callback The callback to execute
378
+ * @returns {U} The result of the callback
379
+ */
380
+ pipe(callback) {
381
+ return callback(this);
382
+ }
383
+ /**
384
+ * Conditionally execute a callback
385
+ *
386
+ * @param {boolean | (() => boolean)} condition The condition to check
387
+ * @param {(str: this) => void} callback The callback to execute
388
+ * @returns {this} The Stringable instance
389
+ */
390
+ when(condition, callback) {
391
+ const shouldExecute = "function" === typeof condition ? condition() : condition;
392
+ if (shouldExecute) {
393
+ callback(this);
394
+ }
395
+ return this;
396
+ }
397
+ /**
398
+ * Execute a callback and return the stringable (for side effects)
399
+ *
400
+ * @param {(str: this) => void} callback The callback to execute
401
+ * @returns {this} The Stringable instance
402
+ */
403
+ tap(callback) {
404
+ callback(this);
405
+ return this;
406
+ }
407
+ };
408
+ var str = (value) => {
409
+ return new Stringable(value);
410
+ };
411
+ var Str = {
412
+ /**
413
+ * Create a new fluent stringable instance
414
+ *
415
+ * @param {unknown} value The initial string value
416
+ * @returns {Stringable} A new Stringable instance
417
+ */
418
+ of: (value) => new Stringable(value),
419
+ /**
420
+ * Convert a string to a slug
421
+ *
422
+ * @param {string} value The string to slugify
423
+ * @returns {string} The slugified string
424
+ */
425
+ slug: (value) => slugify(value),
426
+ /**
427
+ * Convert a string to snake_case
428
+ *
429
+ * @param {string} value The string to convert
430
+ * @returns {string} The snake_case string
431
+ */
432
+ snake: (value) => snakeCase(value),
433
+ /**
434
+ * Convert a string to kebab-case
435
+ *
436
+ * @param {string} value The string to convert
437
+ * @returns {string} The kebab-case string
438
+ */
439
+ kebab: (value) => kebabCase(value),
440
+ /**
441
+ * Convert a string to camelCase
442
+ *
443
+ * @param {string} value The string to convert
444
+ * @returns {string} The camelCase string
445
+ */
446
+ camel: (value) => camelCase(value),
447
+ /**
448
+ * Convert a string to StudlyCase
449
+ *
450
+ * @param {string} value The string to convert
451
+ * @returns {string} The StudlyCase string
452
+ */
453
+ studly: (value) => studlyCase(value),
454
+ /**
455
+ * Convert a string to title case
456
+ *
457
+ * @param {string} value The string to convert
458
+ * @returns {string} The title case string
459
+ */
460
+ title: (value) => titleCase(value),
461
+ /**
462
+ * Limit the length of a string
463
+ *
464
+ * @param {string} value The string to limit
465
+ * @param {number} size The maximum length
466
+ * @param {string} [end='…'] The string to append if limited
467
+ * @returns {string} The limited string
468
+ */
469
+ limit: (value, size, end = "\u2026") => {
470
+ const result = strLimit(value, size);
471
+ return result.endsWith("\u2026") && "\u2026" !== end ? result.slice(0, -1) + end : result;
472
+ },
473
+ /**
474
+ * Generate a random alphanumeric string
475
+ *
476
+ * @param {number} [length=16] The length of the random string
477
+ * @returns {string} The random string
478
+ */
479
+ random: (length = 16) => {
480
+ const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
481
+ let result = "";
482
+ for (let i = 0; i < length; i++) {
483
+ result += chars.charAt(Math.floor(Math.random() * chars.length));
484
+ }
485
+ return result;
486
+ },
487
+ /**
488
+ * Generate a UUID (version 4)
489
+ *
490
+ * @returns {string} The generated UUID
491
+ */
492
+ uuid: () => crypto.randomUUID()
493
+ };
494
+ // Annotate the CommonJS export names for ESM import in node:
495
+ 0 && (module.exports = {
496
+ Str,
497
+ Stringable,
498
+ str
499
+ });