toolbox-x 2.2.5 → 2.3.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/CHANGELOG.md +10 -0
- package/dist/{guards-Dc9MB9on.mjs → basics-B7Sqf33u.mjs} +233 -233
- package/dist/{guards-BSwFQX1M.cjs → basics-Cif013VV.cjs} +233 -233
- package/dist/{colors.cjs → colors/index.cjs} +6 -6
- package/dist/{colors.d.mts → colors/index.d.cts} +2 -2
- package/dist/{colors.d.cts → colors/index.d.mts} +2 -2
- package/dist/{colors.mjs → colors/index.mjs} +6 -6
- package/dist/constants.cjs +13 -13
- package/dist/constants.mjs +3 -3
- package/dist/{converter.cjs → converter/index.cjs} +1 -1
- package/dist/{converter.d.cts → converter/index.d.cts} +2 -2
- package/dist/{converter.d.mts → converter/index.d.mts} +2 -2
- package/dist/{converter.mjs → converter/index.mjs} +1 -1
- package/dist/{date.cjs → date/index.cjs} +7 -7
- package/dist/{date.d.cts → date/index.d.cts} +1 -1
- package/dist/{date.d.mts → date/index.d.mts} +1 -1
- package/dist/{date.mjs → date/index.mjs} +7 -7
- package/dist/{dom.cjs → dom/index.cjs} +8 -8
- package/dist/{dom.d.cts → dom/index.d.cts} +3 -3
- package/dist/{dom.d.mts → dom/index.d.mts} +3 -3
- package/dist/{dom.mjs → dom/index.mjs} +5 -5
- package/dist/{guards.cjs → guards/index.cjs} +38 -38
- package/dist/{guards.d.mts → guards/index.d.cts} +4 -4
- package/dist/{guards.d.cts → guards/index.d.mts} +4 -4
- package/dist/{guards.mjs → guards/index.mjs} +6 -6
- package/dist/{guards-CqbVT4L5.cjs → guards-BD2Fkugj.cjs} +2 -2
- package/dist/{guards-DKGBsd6x.mjs → guards-DBunDnng.mjs} +2 -2
- package/dist/{hash.cjs → hash/index.cjs} +69 -66
- package/dist/{hash.d.cts → hash/index.d.cts} +77 -5
- package/dist/{hash.d.mts → hash/index.d.mts} +77 -5
- package/dist/{hash.mjs → hash/index.mjs} +5 -5
- package/dist/{http-status.cjs → http-status/index.cjs} +1 -1
- package/dist/{http-status.d.mts → http-status/index.d.cts} +2 -2
- package/dist/{http-status.d.cts → http-status/index.d.mts} +2 -2
- package/dist/{http-status.mjs → http-status/index.mjs} +1 -1
- package/dist/index.cjs +84 -84
- package/dist/index.mjs +5 -5
- package/dist/{parse-TuFyLeVH.mjs → parse-CILDG7TG.mjs} +3 -3
- package/dist/{parse-jh637S25.cjs → parse-CffWYCuy.cjs} +3 -3
- package/dist/{query-sWSi-d7u.cjs → query-Blm7AiEZ.cjs} +9 -9
- package/dist/{query-BomnyWh3.mjs → query-PGFEup3p.mjs} +2 -2
- package/dist/{stylog.cjs → stylog/index.cjs} +5 -5
- package/dist/{stylog.d.cts → stylog/index.d.cts} +2 -2
- package/dist/{stylog.d.mts → stylog/index.d.mts} +2 -2
- package/dist/{stylog.mjs → stylog/index.mjs} +5 -5
- package/dist/types/pluralizer.d.cts +25 -1
- package/dist/types/pluralizer.d.mts +25 -1
- package/dist/{utilities-BSv8VbnM.mjs → utilities-CmPwOkdy.mjs} +1 -1
- package/dist/{utilities-BVpk3LKy.cjs → utilities-_COSGq1U.cjs} +1 -1
- package/dist/{basics-20lm69yy.mjs → utils-BO5CcsjK.mjs} +631 -548
- package/dist/{basics-byj0VH1c.cjs → utils-DR9g0Sef.cjs} +580 -479
- package/dist/{verbalizer.cjs → verbalizer/index.cjs} +1 -1
- package/dist/{verbalizer.mjs → verbalizer/index.mjs} +1 -1
- package/package.json +19 -23
- package/dist/pluralizer-DXMuPUAK.d.mts +0 -42
- package/dist/pluralizer-DYsDqq9c.d.cts +0 -42
- package/dist/pluralizer.cjs +0 -678
- package/dist/pluralizer.d.cts +0 -152
- package/dist/pluralizer.d.mts +0 -152
- package/dist/pluralizer.mjs +0 -676
- /package/dist/{verbalizer.d.cts → verbalizer/index.d.cts} +0 -0
- /package/dist/{verbalizer.d.mts → verbalizer/index.d.mts} +0 -0
package/CHANGELOG.md
CHANGED
|
@@ -4,6 +4,16 @@ All notable changes to **toolbox-x** will be documented in this file.
|
|
|
4
4
|
|
|
5
5
|
> Auto-generated from git history using [changelog-maker](https://github.com/nodejs/changelog-maker).
|
|
6
6
|
|
|
7
|
+
## [v2.3.0](https://github.com/nazmul-nhb/toolbox-x/releases/tag/v2.3.0) — 2026-06-16
|
|
8
|
+
|
|
9
|
+
[Compare changes](https://github.com/nazmul-nhb/toolbox-x/compare/v2.2.5...v2.3.0)
|
|
10
|
+
|
|
11
|
+
* \[[`70ffd18605`](https://github.com/nazmul-nhb/toolbox-x/commit/70ffd18605)] - ✨ feat(hash): added 3 new utils: `randomBytes`, `randomNumeric`, `randomAlphaNumeric` (Nazmul Hassan)
|
|
12
|
+
* \[[`278c9392f0`](https://github.com/nazmul-nhb/toolbox-x/commit/278c9392f0)] - 🧼 refactor(src): moved package subpaths in the respective directories (Nazmul Hassan)
|
|
13
|
+
* \[[`a67db2ae2d`](https://github.com/nazmul-nhb/toolbox-x/commit/a67db2ae2d)] - 🛠️ chore(scripts): updated config file for nhb-scripts (Nazmul Hassan)
|
|
14
|
+
* \[[`f8478c1085`](https://github.com/nazmul-nhb/toolbox-x/commit/f8478c1085)] - 🧪 test(commit): testing commit (Nazmul Hassan)
|
|
15
|
+
* \[[`4e8b29c382`](https://github.com/nazmul-nhb/toolbox-x/commit/4e8b29c382)] - 📚 docs: updated CHANGELOG.md for v2.2.5 (github-actions\[bot])
|
|
16
|
+
|
|
7
17
|
## [v2.2.5](https://github.com/nazmul-nhb/toolbox-x/releases/tag/v2.2.5) — 2026-06-16
|
|
8
18
|
|
|
9
19
|
[Compare changes](https://github.com/nazmul-nhb/toolbox-x/compare/v2.2.4...v2.2.5)
|
|
@@ -15,10 +15,240 @@
|
|
|
15
15
|
*/
|
|
16
16
|
|
|
17
17
|
import { d as isString, m as isUndefined } from "./primitives-Djsevc69.mjs";
|
|
18
|
+
import { $ as trimString, m as stableStringify } from "./utils-BO5CcsjK.mjs";
|
|
18
19
|
import { b as isFunction, w as isNotEmptyObject } from "./specials-krf7zsqI.mjs";
|
|
19
|
-
import { a as normalizeNumber } from "./utilities-
|
|
20
|
-
import { G as stableStringify, n as trimString } from "./basics-20lm69yy.mjs";
|
|
20
|
+
import { a as normalizeNumber } from "./utilities-CmPwOkdy.mjs";
|
|
21
21
|
|
|
22
|
+
//#region src/string/convert.ts
|
|
23
|
+
/**
|
|
24
|
+
* * Replaces all occurrences of a string or pattern in the given input string.
|
|
25
|
+
*
|
|
26
|
+
* - If `find` is a string, it is converted into a global regular expression (`/find/g`).
|
|
27
|
+
* - If `find` is a `RegExp`, the global (`g`) flag is ensured.
|
|
28
|
+
* - Trims the input before performing replacements.
|
|
29
|
+
*
|
|
30
|
+
* @param input - The string in which replacements should be performed.
|
|
31
|
+
* @param find - The substring or regex pattern to search for.
|
|
32
|
+
* @param replace - The string to replace matches with.
|
|
33
|
+
* @returns The modified/refined string with replacements applied.
|
|
34
|
+
*/
|
|
35
|
+
const replaceAllInString = (input, find, replace) => {
|
|
36
|
+
const trimmedString = trimString(input);
|
|
37
|
+
const regex = isString(find) ? new RegExp(find, "g") : new RegExp(find, find?.flags.includes("g") ? find?.flags : find?.flags + "g");
|
|
38
|
+
return trimmedString?.replace(regex, replace);
|
|
39
|
+
};
|
|
40
|
+
/**
|
|
41
|
+
* * Converts a string into a URL-friendly slug.
|
|
42
|
+
* @param input - The string to be converted.
|
|
43
|
+
* @returns The slugified string.
|
|
44
|
+
*/
|
|
45
|
+
const slugifyString = (input) => {
|
|
46
|
+
return trimString(normalizeString(input))?.toLowerCase()?.replace(/[^a-z0-9]+/g, "-")?.replace(/^-+|-+$/g, "");
|
|
47
|
+
};
|
|
48
|
+
/**
|
|
49
|
+
* * Masks part of a string for privacy.
|
|
50
|
+
* @param input - The string to mask.
|
|
51
|
+
* @param options - Options for masking a string.
|
|
52
|
+
* @returns The masked string.
|
|
53
|
+
*/
|
|
54
|
+
const maskString = (input, options) => {
|
|
55
|
+
const { start = 1, end = 1, maskCharacter: maskChar = "*" } = options || {};
|
|
56
|
+
const trimmedString = trimString(input);
|
|
57
|
+
if (trimmedString?.length <= start + end) return maskChar?.repeat(trimmedString?.length);
|
|
58
|
+
return trimmedString.slice(0, start) + maskChar?.repeat(trimmedString?.length - start - end) + (end > 0 ? trimmedString.slice(-end) : "");
|
|
59
|
+
};
|
|
60
|
+
/**
|
|
61
|
+
* * Reverses a given string.
|
|
62
|
+
* @param input - The string to reverse.
|
|
63
|
+
* @returns The reversed string.
|
|
64
|
+
*
|
|
65
|
+
* @remarks It {@link trimString trims} the whitespace of the input string before reversing it.
|
|
66
|
+
*/
|
|
67
|
+
const reverseString = (input) => {
|
|
68
|
+
return trimString(input)?.split("")?.reverse()?.join("");
|
|
69
|
+
};
|
|
70
|
+
/**
|
|
71
|
+
* * Normalizes a string by removing diacritics (accents).
|
|
72
|
+
* @param str The input string.
|
|
73
|
+
* @returns The normalized string.
|
|
74
|
+
*/
|
|
75
|
+
function normalizeString(str) {
|
|
76
|
+
return str?.normalize("NFD")?.replace(/[\u0300-\u036f]/g, "");
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* * Extracts all email addresses from a string.
|
|
80
|
+
* @param str The input string.
|
|
81
|
+
* @returns An array of extracted email addresses.
|
|
82
|
+
*/
|
|
83
|
+
function extractEmails(str) {
|
|
84
|
+
return str?.match(/[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}/g) || [];
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* * Extracts all URLs from a string.
|
|
88
|
+
* @param str The input string.
|
|
89
|
+
* @returns An array of extracted URLs.
|
|
90
|
+
*/
|
|
91
|
+
function extractURLs(str) {
|
|
92
|
+
return str?.match(/https?:\/\/[^\s/$.?#].[^\s]*/g) || [];
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* * Returns a grammatically correct unit string, optionally prefixed with the number.
|
|
96
|
+
*
|
|
97
|
+
* @remarks For complex and versatile pluralization, please refer to {@link https://toolbox-x.nazmul-nhb.dev/docs/utils/string/pluralizer pluralizer} or {@link https://toolbox-x.nazmul-nhb.dev/docs/classes/pluralizer Pluralizer Class} instead.
|
|
98
|
+
*
|
|
99
|
+
* @param count The numeric value to determine singular or plural.
|
|
100
|
+
* @param unit The unit name (e.g., "day", "hour").
|
|
101
|
+
* @param withNumber Whether to prefix the count before the unit. Defaults to `true`.
|
|
102
|
+
* @returns Formatted unit string like `"1 day"`, `"2 months"`, or `"hour"`.
|
|
103
|
+
*/
|
|
104
|
+
function formatUnitWithPlural(count, unit, withNumber = true) {
|
|
105
|
+
const pluralized = Math.abs(count) === 1 ? unit : `${unit}s`;
|
|
106
|
+
return withNumber ? `${count} ${pluralized}` : pluralized;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
//#endregion
|
|
110
|
+
//#region src/number/guards.ts
|
|
111
|
+
/**
|
|
112
|
+
* * Check if a number is even or not.
|
|
113
|
+
*
|
|
114
|
+
* @param input The number or numeric string to check.
|
|
115
|
+
* @returns Boolean: `true` if even and `false` if not even.
|
|
116
|
+
*/
|
|
117
|
+
const isEven = (input) => {
|
|
118
|
+
const parsed = normalizeNumber(input);
|
|
119
|
+
if (isUndefined(parsed)) return false;
|
|
120
|
+
return parsed % 2 === 0;
|
|
121
|
+
};
|
|
122
|
+
/**
|
|
123
|
+
* * Checks if a number is odd or not.
|
|
124
|
+
*
|
|
125
|
+
* @param input The number or numeric string to check.
|
|
126
|
+
* @returns Boolean: `true` if odd and `false` if not odd.
|
|
127
|
+
*/
|
|
128
|
+
const isOdd = (input) => {
|
|
129
|
+
const parsed = normalizeNumber(input);
|
|
130
|
+
if (isUndefined(parsed)) return false;
|
|
131
|
+
return parsed % 2 !== 0;
|
|
132
|
+
};
|
|
133
|
+
/**
|
|
134
|
+
* * Checks if a number is a multiple of another number.
|
|
135
|
+
*
|
|
136
|
+
* @param input - The number to check.
|
|
137
|
+
* @param multipleOf - The number to check against.
|
|
138
|
+
* @returns `true` if `input` is a multiple of `multipleOf`, otherwise `false`.
|
|
139
|
+
*/
|
|
140
|
+
const isMultiple = (input, multipleOf) => {
|
|
141
|
+
return input % multipleOf === 0;
|
|
142
|
+
};
|
|
143
|
+
/**
|
|
144
|
+
* * Checks if a number is a perfect square.
|
|
145
|
+
*
|
|
146
|
+
* @param num The number to check.
|
|
147
|
+
* @returns `true` if the number is a perfect square, otherwise `false`.
|
|
148
|
+
*/
|
|
149
|
+
function isPerfectSquare(num) {
|
|
150
|
+
return Number.isInteger(Math.sqrt(num));
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* * Checks if a number is part of the Fibonacci sequence.
|
|
154
|
+
*
|
|
155
|
+
* @param num The number to check.
|
|
156
|
+
* @returns `true` if the number is a Fibonacci number, otherwise `false`.
|
|
157
|
+
*/
|
|
158
|
+
function isFibonacci(num) {
|
|
159
|
+
return isPerfectSquare(5 * num * num + 4) || isPerfectSquare(5 * num * num - 4);
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* * Checks whether any input is not a finite number.
|
|
163
|
+
*
|
|
164
|
+
* @param numbers - The list of numbers to validate.
|
|
165
|
+
* @returns `true` if any input is not finite.
|
|
166
|
+
*/
|
|
167
|
+
function areInvalidNumbers(...numbers) {
|
|
168
|
+
return numbers?.some((n) => !Number.isFinite(n));
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
//#endregion
|
|
172
|
+
//#region src/number/prime.ts
|
|
173
|
+
/**
|
|
174
|
+
* * Checks if a number is prime.
|
|
175
|
+
*
|
|
176
|
+
* @param number The number to check.
|
|
177
|
+
* @returns Boolean: `true` if the number is prime, otherwise `false`.
|
|
178
|
+
*/
|
|
179
|
+
const isPrime = (number) => {
|
|
180
|
+
if (number < 2) return false;
|
|
181
|
+
if (number === 2 || number === 3) return true;
|
|
182
|
+
if (number % 2 === 0 || number % 3 === 0) return false;
|
|
183
|
+
for (let i = 5; i * i <= number; i += 6) if (number % i === 0 || number % (i + 2) === 0) return false;
|
|
184
|
+
return true;
|
|
185
|
+
};
|
|
186
|
+
/**
|
|
187
|
+
* * Find prime numbers in a given range.
|
|
188
|
+
*
|
|
189
|
+
* @param start The starting number of the range. Default is `1`.
|
|
190
|
+
* @param end The ending number of the range. Default is `1000`.
|
|
191
|
+
* @returns An array of prime numbers within the range (inclusive).
|
|
192
|
+
*/
|
|
193
|
+
const findPrimeNumbers = (start = 1, end = 1e3) => {
|
|
194
|
+
let startNumber = start, endNumber = end;
|
|
195
|
+
if (start > end) [startNumber, endNumber] = [end, start];
|
|
196
|
+
return Array.from({ length: endNumber - startNumber + 1 }, (_, i) => startNumber + i).filter(isPrime);
|
|
197
|
+
};
|
|
198
|
+
|
|
199
|
+
//#endregion
|
|
200
|
+
//#region src/string/guards.ts
|
|
201
|
+
/**
|
|
202
|
+
* * Checks if a string is a palindrome.
|
|
203
|
+
* @param input - The string to check.
|
|
204
|
+
* @returns True if the string is a palindrome, otherwise false.
|
|
205
|
+
*/
|
|
206
|
+
const isPalindrome = (input) => {
|
|
207
|
+
const normalized = input.toLowerCase().replace(/[^a-z0-9]/g, "");
|
|
208
|
+
return normalized === reverseString(normalized);
|
|
209
|
+
};
|
|
210
|
+
/**
|
|
211
|
+
* * Checks if a string is in camelCase format.
|
|
212
|
+
* @param str The string to check.
|
|
213
|
+
* @returns `true` if the string is in camelCase, otherwise `false`.
|
|
214
|
+
*/
|
|
215
|
+
function isCamelCase(str) {
|
|
216
|
+
return /^[a-z]+([A-Z][a-z]*)*$/.test(str);
|
|
217
|
+
}
|
|
218
|
+
/**
|
|
219
|
+
* * Checks if a string is in PascalCase format.
|
|
220
|
+
* @param str The string to check.
|
|
221
|
+
* @returns `true` if the string is in PascalCase, otherwise `false`.
|
|
222
|
+
*/
|
|
223
|
+
function isPascalCase(str) {
|
|
224
|
+
return /^[A-Z][a-zA-Z]*$/.test(str);
|
|
225
|
+
}
|
|
226
|
+
/**
|
|
227
|
+
* * Checks if a string is in snake_case format.
|
|
228
|
+
* @param str The string to check.
|
|
229
|
+
* @returns `true` if the string is in snake_case, otherwise `false`.
|
|
230
|
+
*/
|
|
231
|
+
function isSnakeCase(str) {
|
|
232
|
+
return /^[a-z]+(_[a-z]+)*$/.test(str);
|
|
233
|
+
}
|
|
234
|
+
/**
|
|
235
|
+
* * Checks if a string is in kebab-case format.
|
|
236
|
+
* @param str The string to check.
|
|
237
|
+
* @returns `true` if the string is in kebab-case, otherwise `false`.
|
|
238
|
+
*/
|
|
239
|
+
function isKebabCase(str) {
|
|
240
|
+
return /^[a-z]+(-[a-z]+)*$/.test(str);
|
|
241
|
+
}
|
|
242
|
+
/**
|
|
243
|
+
* * Checks if a string contains only emojis.
|
|
244
|
+
* @param str The string to check.
|
|
245
|
+
* @returns `true` if the string contains only emojis, otherwise `false`.
|
|
246
|
+
*/
|
|
247
|
+
function isEmojiOnly(str) {
|
|
248
|
+
return /^[\p{Emoji}]+$/u.test(str);
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
//#endregion
|
|
22
252
|
//#region src/object/basics.ts
|
|
23
253
|
/**
|
|
24
254
|
* * Deep clone an object using `structuredClone` or deterministic *JSON serialization*.
|
|
@@ -192,234 +422,4 @@ const getLastArrayElement = (array) => {
|
|
|
192
422
|
};
|
|
193
423
|
|
|
194
424
|
//#endregion
|
|
195
|
-
|
|
196
|
-
/**
|
|
197
|
-
* * Check if a number is even or not.
|
|
198
|
-
*
|
|
199
|
-
* @param input The number or numeric string to check.
|
|
200
|
-
* @returns Boolean: `true` if even and `false` if not even.
|
|
201
|
-
*/
|
|
202
|
-
const isEven = (input) => {
|
|
203
|
-
const parsed = normalizeNumber(input);
|
|
204
|
-
if (isUndefined(parsed)) return false;
|
|
205
|
-
return parsed % 2 === 0;
|
|
206
|
-
};
|
|
207
|
-
/**
|
|
208
|
-
* * Checks if a number is odd or not.
|
|
209
|
-
*
|
|
210
|
-
* @param input The number or numeric string to check.
|
|
211
|
-
* @returns Boolean: `true` if odd and `false` if not odd.
|
|
212
|
-
*/
|
|
213
|
-
const isOdd = (input) => {
|
|
214
|
-
const parsed = normalizeNumber(input);
|
|
215
|
-
if (isUndefined(parsed)) return false;
|
|
216
|
-
return parsed % 2 !== 0;
|
|
217
|
-
};
|
|
218
|
-
/**
|
|
219
|
-
* * Checks if a number is a multiple of another number.
|
|
220
|
-
*
|
|
221
|
-
* @param input - The number to check.
|
|
222
|
-
* @param multipleOf - The number to check against.
|
|
223
|
-
* @returns `true` if `input` is a multiple of `multipleOf`, otherwise `false`.
|
|
224
|
-
*/
|
|
225
|
-
const isMultiple = (input, multipleOf) => {
|
|
226
|
-
return input % multipleOf === 0;
|
|
227
|
-
};
|
|
228
|
-
/**
|
|
229
|
-
* * Checks if a number is a perfect square.
|
|
230
|
-
*
|
|
231
|
-
* @param num The number to check.
|
|
232
|
-
* @returns `true` if the number is a perfect square, otherwise `false`.
|
|
233
|
-
*/
|
|
234
|
-
function isPerfectSquare(num) {
|
|
235
|
-
return Number.isInteger(Math.sqrt(num));
|
|
236
|
-
}
|
|
237
|
-
/**
|
|
238
|
-
* * Checks if a number is part of the Fibonacci sequence.
|
|
239
|
-
*
|
|
240
|
-
* @param num The number to check.
|
|
241
|
-
* @returns `true` if the number is a Fibonacci number, otherwise `false`.
|
|
242
|
-
*/
|
|
243
|
-
function isFibonacci(num) {
|
|
244
|
-
return isPerfectSquare(5 * num * num + 4) || isPerfectSquare(5 * num * num - 4);
|
|
245
|
-
}
|
|
246
|
-
/**
|
|
247
|
-
* * Checks whether any input is not a finite number.
|
|
248
|
-
*
|
|
249
|
-
* @param numbers - The list of numbers to validate.
|
|
250
|
-
* @returns `true` if any input is not finite.
|
|
251
|
-
*/
|
|
252
|
-
function areInvalidNumbers(...numbers) {
|
|
253
|
-
return numbers?.some((n) => !Number.isFinite(n));
|
|
254
|
-
}
|
|
255
|
-
|
|
256
|
-
//#endregion
|
|
257
|
-
//#region src/number/prime.ts
|
|
258
|
-
/**
|
|
259
|
-
* * Checks if a number is prime.
|
|
260
|
-
*
|
|
261
|
-
* @param number The number to check.
|
|
262
|
-
* @returns Boolean: `true` if the number is prime, otherwise `false`.
|
|
263
|
-
*/
|
|
264
|
-
const isPrime = (number) => {
|
|
265
|
-
if (number < 2) return false;
|
|
266
|
-
if (number === 2 || number === 3) return true;
|
|
267
|
-
if (number % 2 === 0 || number % 3 === 0) return false;
|
|
268
|
-
for (let i = 5; i * i <= number; i += 6) if (number % i === 0 || number % (i + 2) === 0) return false;
|
|
269
|
-
return true;
|
|
270
|
-
};
|
|
271
|
-
/**
|
|
272
|
-
* * Find prime numbers in a given range.
|
|
273
|
-
*
|
|
274
|
-
* @param start The starting number of the range. Default is `1`.
|
|
275
|
-
* @param end The ending number of the range. Default is `1000`.
|
|
276
|
-
* @returns An array of prime numbers within the range (inclusive).
|
|
277
|
-
*/
|
|
278
|
-
const findPrimeNumbers = (start = 1, end = 1e3) => {
|
|
279
|
-
let startNumber = start, endNumber = end;
|
|
280
|
-
if (start > end) [startNumber, endNumber] = [end, start];
|
|
281
|
-
return Array.from({ length: endNumber - startNumber + 1 }, (_, i) => startNumber + i).filter(isPrime);
|
|
282
|
-
};
|
|
283
|
-
|
|
284
|
-
//#endregion
|
|
285
|
-
//#region src/string/convert.ts
|
|
286
|
-
/**
|
|
287
|
-
* * Replaces all occurrences of a string or pattern in the given input string.
|
|
288
|
-
*
|
|
289
|
-
* - If `find` is a string, it is converted into a global regular expression (`/find/g`).
|
|
290
|
-
* - If `find` is a `RegExp`, the global (`g`) flag is ensured.
|
|
291
|
-
* - Trims the input before performing replacements.
|
|
292
|
-
*
|
|
293
|
-
* @param input - The string in which replacements should be performed.
|
|
294
|
-
* @param find - The substring or regex pattern to search for.
|
|
295
|
-
* @param replace - The string to replace matches with.
|
|
296
|
-
* @returns The modified/refined string with replacements applied.
|
|
297
|
-
*/
|
|
298
|
-
const replaceAllInString = (input, find, replace) => {
|
|
299
|
-
const trimmedString = trimString(input);
|
|
300
|
-
const regex = isString(find) ? new RegExp(find, "g") : new RegExp(find, find?.flags.includes("g") ? find?.flags : find?.flags + "g");
|
|
301
|
-
return trimmedString?.replace(regex, replace);
|
|
302
|
-
};
|
|
303
|
-
/**
|
|
304
|
-
* * Converts a string into a URL-friendly slug.
|
|
305
|
-
* @param input - The string to be converted.
|
|
306
|
-
* @returns The slugified string.
|
|
307
|
-
*/
|
|
308
|
-
const slugifyString = (input) => {
|
|
309
|
-
return trimString(normalizeString(input))?.toLowerCase()?.replace(/[^a-z0-9]+/g, "-")?.replace(/^-+|-+$/g, "");
|
|
310
|
-
};
|
|
311
|
-
/**
|
|
312
|
-
* * Masks part of a string for privacy.
|
|
313
|
-
* @param input - The string to mask.
|
|
314
|
-
* @param options - Options for masking a string.
|
|
315
|
-
* @returns The masked string.
|
|
316
|
-
*/
|
|
317
|
-
const maskString = (input, options) => {
|
|
318
|
-
const { start = 1, end = 1, maskCharacter: maskChar = "*" } = options || {};
|
|
319
|
-
const trimmedString = trimString(input);
|
|
320
|
-
if (trimmedString?.length <= start + end) return maskChar?.repeat(trimmedString?.length);
|
|
321
|
-
return trimmedString.slice(0, start) + maskChar?.repeat(trimmedString?.length - start - end) + (end > 0 ? trimmedString.slice(-end) : "");
|
|
322
|
-
};
|
|
323
|
-
/**
|
|
324
|
-
* * Reverses a given string.
|
|
325
|
-
* @param input - The string to reverse.
|
|
326
|
-
* @returns The reversed string.
|
|
327
|
-
*
|
|
328
|
-
* @remarks It {@link trimString trims} the whitespace of the input string before reversing it.
|
|
329
|
-
*/
|
|
330
|
-
const reverseString = (input) => {
|
|
331
|
-
return trimString(input)?.split("")?.reverse()?.join("");
|
|
332
|
-
};
|
|
333
|
-
/**
|
|
334
|
-
* * Normalizes a string by removing diacritics (accents).
|
|
335
|
-
* @param str The input string.
|
|
336
|
-
* @returns The normalized string.
|
|
337
|
-
*/
|
|
338
|
-
function normalizeString(str) {
|
|
339
|
-
return str?.normalize("NFD")?.replace(/[\u0300-\u036f]/g, "");
|
|
340
|
-
}
|
|
341
|
-
/**
|
|
342
|
-
* * Extracts all email addresses from a string.
|
|
343
|
-
* @param str The input string.
|
|
344
|
-
* @returns An array of extracted email addresses.
|
|
345
|
-
*/
|
|
346
|
-
function extractEmails(str) {
|
|
347
|
-
return str?.match(/[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}/g) || [];
|
|
348
|
-
}
|
|
349
|
-
/**
|
|
350
|
-
* * Extracts all URLs from a string.
|
|
351
|
-
* @param str The input string.
|
|
352
|
-
* @returns An array of extracted URLs.
|
|
353
|
-
*/
|
|
354
|
-
function extractURLs(str) {
|
|
355
|
-
return str?.match(/https?:\/\/[^\s/$.?#].[^\s]*/g) || [];
|
|
356
|
-
}
|
|
357
|
-
/**
|
|
358
|
-
* * Returns a grammatically correct unit string, optionally prefixed with the number.
|
|
359
|
-
*
|
|
360
|
-
* @remarks For complex and versatile pluralization, please refer to {@link https://toolbox-x.nazmul-nhb.dev/docs/utils/string/pluralizer pluralizer} or {@link https://toolbox-x.nazmul-nhb.dev/docs/classes/pluralizer Pluralizer Class} instead.
|
|
361
|
-
*
|
|
362
|
-
* @param count The numeric value to determine singular or plural.
|
|
363
|
-
* @param unit The unit name (e.g., "day", "hour").
|
|
364
|
-
* @param withNumber Whether to prefix the count before the unit. Defaults to `true`.
|
|
365
|
-
* @returns Formatted unit string like `"1 day"`, `"2 months"`, or `"hour"`.
|
|
366
|
-
*/
|
|
367
|
-
function formatUnitWithPlural(count, unit, withNumber = true) {
|
|
368
|
-
const pluralized = Math.abs(count) === 1 ? unit : `${unit}s`;
|
|
369
|
-
return withNumber ? `${count} ${pluralized}` : pluralized;
|
|
370
|
-
}
|
|
371
|
-
|
|
372
|
-
//#endregion
|
|
373
|
-
//#region src/string/guards.ts
|
|
374
|
-
/**
|
|
375
|
-
* * Checks if a string is a palindrome.
|
|
376
|
-
* @param input - The string to check.
|
|
377
|
-
* @returns True if the string is a palindrome, otherwise false.
|
|
378
|
-
*/
|
|
379
|
-
const isPalindrome = (input) => {
|
|
380
|
-
const normalized = input.toLowerCase().replace(/[^a-z0-9]/g, "");
|
|
381
|
-
return normalized === reverseString(normalized);
|
|
382
|
-
};
|
|
383
|
-
/**
|
|
384
|
-
* * Checks if a string is in camelCase format.
|
|
385
|
-
* @param str The string to check.
|
|
386
|
-
* @returns `true` if the string is in camelCase, otherwise `false`.
|
|
387
|
-
*/
|
|
388
|
-
function isCamelCase(str) {
|
|
389
|
-
return /^[a-z]+([A-Z][a-z]*)*$/.test(str);
|
|
390
|
-
}
|
|
391
|
-
/**
|
|
392
|
-
* * Checks if a string is in PascalCase format.
|
|
393
|
-
* @param str The string to check.
|
|
394
|
-
* @returns `true` if the string is in PascalCase, otherwise `false`.
|
|
395
|
-
*/
|
|
396
|
-
function isPascalCase(str) {
|
|
397
|
-
return /^[A-Z][a-zA-Z]*$/.test(str);
|
|
398
|
-
}
|
|
399
|
-
/**
|
|
400
|
-
* * Checks if a string is in snake_case format.
|
|
401
|
-
* @param str The string to check.
|
|
402
|
-
* @returns `true` if the string is in snake_case, otherwise `false`.
|
|
403
|
-
*/
|
|
404
|
-
function isSnakeCase(str) {
|
|
405
|
-
return /^[a-z]+(_[a-z]+)*$/.test(str);
|
|
406
|
-
}
|
|
407
|
-
/**
|
|
408
|
-
* * Checks if a string is in kebab-case format.
|
|
409
|
-
* @param str The string to check.
|
|
410
|
-
* @returns `true` if the string is in kebab-case, otherwise `false`.
|
|
411
|
-
*/
|
|
412
|
-
function isKebabCase(str) {
|
|
413
|
-
return /^[a-z]+(-[a-z]+)*$/.test(str);
|
|
414
|
-
}
|
|
415
|
-
/**
|
|
416
|
-
* * Checks if a string contains only emojis.
|
|
417
|
-
* @param str The string to check.
|
|
418
|
-
* @returns `true` if the string contains only emojis, otherwise `false`.
|
|
419
|
-
*/
|
|
420
|
-
function isEmojiOnly(str) {
|
|
421
|
-
return /^[\p{Emoji}]+$/u.test(str);
|
|
422
|
-
}
|
|
423
|
-
|
|
424
|
-
//#endregion
|
|
425
|
-
export { extractObjectEntries as A, filterArrayOfObjects as C, shuffleArray as D, isInvalidOrEmptyArray as E, extractObjectKeysDeep as M, cloneObject as O, isPerfectSquare as S, getLastArrayElement as T, areInvalidNumbers as _, isPascalCase as a, isMultiple as b, extractURLs as c, normalizeString as d, replaceAllInString as f, isPrime as g, findPrimeNumbers as h, isPalindrome as i, extractObjectKeys as j, countObjectFields as k, formatUnitWithPlural as l, slugifyString as m, isEmojiOnly as n, isSnakeCase as o, reverseString as p, isKebabCase as r, extractEmails as s, isCamelCase as t, maskString as u, isEven as v, flattenArray as w, isOdd as x, isFibonacci as y };
|
|
425
|
+
export { replaceAllInString as A, isOdd as C, formatUnitWithPlural as D, extractURLs as E, slugifyString as M, maskString as O, isMultiple as S, extractEmails as T, findPrimeNumbers as _, shuffleArray as a, isEven as b, extractObjectEntries as c, isCamelCase as d, isEmojiOnly as f, isSnakeCase as g, isPascalCase as h, isInvalidOrEmptyArray as i, reverseString as j, normalizeString as k, extractObjectKeys as l, isPalindrome as m, flattenArray as n, cloneObject as o, isKebabCase as p, getLastArrayElement as r, countObjectFields as s, filterArrayOfObjects as t, extractObjectKeysDeep as u, isPrime as v, isPerfectSquare as w, isFibonacci as x, areInvalidNumbers as y };
|