@js-utils-kit/string 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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Sriman
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/dist/index.cjs ADDED
@@ -0,0 +1 @@
1
+ function e(e){return e.replace(/^\w/,e=>e.toUpperCase())}function t(e,t=/\s+/){return e.split(t)}function n(e,n){return n?[...e].filter(e=>e===n).length:t(e).length}function r(e){return[...e].reduce((e,t)=>(e[t]=(e[t]||0)+1,e),{})}function i(e){return e?t(e,/\r\n|\r|\n/).length:0}function a(e,n){return n.length===0?0:t(e,n).length-1}function o(e){return t(e.trim()).length}function s(e,t=``){return e.replace(/[^\p{L}\p{N}\s]/gu,t)}function c(e){let n=t(e.trim()).map(e=>s(e)).filter(Boolean);if(n.length===0)return``;let r=Math.max(...n.map(e=>e.length)),i=n.filter(e=>e.length===r),a=[...new Set(i)];return a.length===1?a[0]:a}function l(e){let n=t(e).map(e=>s(e)).filter(Boolean);return n.length===0?0:n.reduce((e,t)=>Math.max(e,t.length),0)}function u(e,t,n=` `){return e.padStart(t,n)}function d(e,t,n=` `){return e.padEnd(t,n)}function f(e,t){return e.repeat(t)}function p(e){let n=t(e.trim()).map(e=>s(e)).filter(Boolean);if(n.length===0)return``;let r=Math.min(...n.map(e=>e.length)),i=n.filter(e=>e.length===r),a=[...new Set(i)];return a.length===1?a[0]:a}function m(e){let n=t(e.trim()).map(e=>s(e)).filter(Boolean);return n.length===0?0:n.reduce((e,t)=>Math.min(e,t.length),1/0)}const h=Object.assign(e=>e.trim(),{start:e=>e.trimStart(),end:e=>e.trimEnd(),normalizeWhitespace:e=>e.trim().replace(/\s+/g,` `)});function g(e,t,n=`...`){return t<=0?``:e.length<=t?e:n.length>=t?n.slice(0,t):e.trim().slice(0,t)+n}function _(e){return[...new Set(e)]}exports.capitalize=e,exports.countChars=n,exports.countFrequencies=r,exports.countLines=i,exports.countSubstring=a,exports.countWords=o,exports.longestWord=c,exports.longestWordLength=l,exports.padLeft=u,exports.padRight=d,exports.repeatString=f,exports.shortestWord=p,exports.shortestWordLength=m,exports.splitString=t,exports.stripSymbols=s,exports.trim=h,exports.truncate=g,exports.uniqueChars=_;var v=require(`@js-utils-kit/types`);Object.keys(v).forEach(function(e){e!==`default`&&!Object.prototype.hasOwnProperty.call(exports,e)&&Object.defineProperty(exports,e,{enumerable:!0,get:function(){return v[e]}})});
@@ -0,0 +1,413 @@
1
+ import { Trim } from "@js-utils-kit/types";
2
+ export * from "@js-utils-kit/types";
3
+
4
+ //#region src/capitalize.d.ts
5
+
6
+ /**
7
+ * Capitalizes the first character of a string using a regular expression.
8
+ *
9
+ * @returns The input string with its first character capitalized, or the original string if empty or not a string.
10
+ *
11
+ * @example
12
+ * ```ts
13
+ * capitalize("hello"); // "Hello"
14
+ * capitalize("world"); // "World"
15
+ * capitalize(""); // ""
16
+ * capitalize("a"); // "A"
17
+ * ```
18
+ */
19
+ declare function capitalize(/** The string to capitalize */
20
+ value: string): string;
21
+ //#endregion
22
+ //#region src/countChars.d.ts
23
+ /**
24
+ * Counts characters in a string.
25
+ *
26
+ * @remarks
27
+ * - If `char` is provided, only that character’s occurrences are counted.
28
+ * - If `char` is omitted, the function counts the number of substrings returned by {@link splitString} (similar to a word count).
29
+ *
30
+ * @returns Number of characters or character occurrences.
31
+ *
32
+ * @example
33
+ * ```ts
34
+ * countChars("banana"); // 6
35
+ * countChars("banana", "a"); // 3
36
+ * countChars("banana", "n"); // 2
37
+ * ```
38
+ */
39
+ declare function countChars(/** The input string. */
40
+ str: string, /** Optional character to count. */
41
+ char?: string): number;
42
+ //#endregion
43
+ //#region src/countFrequencies.d.ts
44
+ /**
45
+ * Builds a frequency map of characters in a string.
46
+ *
47
+ * @remarks
48
+ * - Returns an object where keys are characters and values are counts.
49
+ * - Case-sensitive by default.
50
+ *
51
+ * @returns A mapping of each character to its frequency.
52
+ *
53
+ * @example
54
+ * ```ts
55
+ * countFrequencies("banana");
56
+ * // { b: 1, a: 3, n: 2 }
57
+ *
58
+ * countFrequencies("");
59
+ * // {}
60
+ * ```
61
+ */
62
+ declare function countFrequencies(/** The input string. */
63
+ str: string): Record<string, number>;
64
+ //#endregion
65
+ //#region src/countLines.d.ts
66
+ /**
67
+ * Counts the number of lines in a string.
68
+ *
69
+ * @remarks
70
+ * - Handles Unix (`\n`), Windows (`\r\n`), and old Mac (`\r`) line endings.
71
+ *
72
+ * @returns Number of lines in the string.
73
+ *
74
+ * @example
75
+ * ```ts
76
+ * countLines("a\nb\nc"); // 3
77
+ * countLines("a\r\nb\r\nc"); // 3
78
+ * countLines("hello"); // 1
79
+ * ```
80
+ */
81
+ declare function countLines(/** The input string. */
82
+ str: string): number;
83
+ //#endregion
84
+ //#region src/countSubstring.d.ts
85
+ /**
86
+ * Counts how many times a substring occurs within a string.
87
+ *
88
+ * @remarks
89
+ * - Overlapping substrings are not double-counted.
90
+ * - Returns `0` if the substring is not found.
91
+ *
92
+ * @returns Number of times `sub` occurs in `str`.
93
+ *
94
+ * @example
95
+ * ```ts
96
+ * countSubstring("lololol", "lo"); // 3
97
+ * countSubstring("aaaaa", "a"); // 5
98
+ * countSubstring("hello", "z"); // 0
99
+ * ```
100
+ */
101
+ declare function countSubstring(/** The input string. */
102
+ str: string, /** The substring to count. */
103
+ sub: string): number;
104
+ //#endregion
105
+ //#region src/countWords.d.ts
106
+ /**
107
+ * Counts the number of words in a string.
108
+ *
109
+ * @returns Number of words in the string.
110
+ *
111
+ * @example
112
+ * ```ts
113
+ * countWords("js utils kit"); // 3
114
+ * countWords(" js utils kit"); // 3
115
+ * countWords(""); // 0
116
+ * ```
117
+ */
118
+ declare function countWords(/** The input string */
119
+ str: string): number;
120
+ //#endregion
121
+ //#region src/longestWord.d.ts
122
+ /**
123
+ * Finds the longest word(s) in a string.
124
+ *
125
+ * @remarks
126
+ * - If only one longest word exists, returns it as a string.
127
+ * - If multiple longest words have the same length, returns them as an array.
128
+ * - Returns an empty string if there are no words.
129
+ *
130
+ * @returns The longest word as a string, or an array of tied words.
131
+ *
132
+ * @example
133
+ * ```ts
134
+ * longestWord("js utils kit"); // "utils"
135
+ * longestWord("short longerword mid"); // "longerword"
136
+ * longestWord("hello"); // "hello"
137
+ * longestWord(""); // ""
138
+ * ```
139
+ */
140
+ declare function longestWord(/** The input string. */
141
+ str: string): string | string[];
142
+ //#endregion
143
+ //#region src/longestWordLength.d.ts
144
+ /**
145
+ * Finds the length of the longest word in a string.
146
+ *
147
+ * @remarks
148
+ * - Words are split by whitespace (`/\s+/`).
149
+ * - Symbols (punctuation, special chars) are removed using {@link stripSymbols}.
150
+ * - Returns `0` if the string is empty or contains no words.
151
+ *
152
+ * @returns Length of the longest word.
153
+ *
154
+ * @example
155
+ * ```ts
156
+ * longestWordLength("js utils kit"); // 5
157
+ * longestWordLength("short longerword mid"); // 10
158
+ * longestWordLength("hello"); // 5
159
+ * longestWordLength(""); // 0
160
+ * ```
161
+ */
162
+ declare function longestWordLength(/** The input string. */
163
+ str: string): number;
164
+ //#endregion
165
+ //#region src/padLeft.d.ts
166
+ /**
167
+ * Pads a string on the left with a specified character until it reaches the desired length.
168
+ *
169
+ * @returns The padded string, or the original string if its length is already greater than or equal to the target length.
170
+ *
171
+ * @example
172
+ * ```ts
173
+ * console.log(padLeft("hello", 8)); // " hello"
174
+ * console.log(padLeft("hi", 5, "*")); // "***hi"
175
+ * console.log(padLeft("hello", 3)); // "hello"
176
+ * console.log(padLeft("", 3, "0")); // "000"
177
+ * ```
178
+ */
179
+ declare function padLeft(/** The string to pad */
180
+ str: string, /** The target length of the padded string */
181
+ length: number,
182
+ /**
183
+ * The character to use for padding (defaults to a space)
184
+ *
185
+ * @default ' '
186
+ */
187
+ char?: string): string;
188
+ //#endregion
189
+ //#region src/padRight.d.ts
190
+ /**
191
+ * Pads a string on the right with a specified character until it reaches the desired length.
192
+ *
193
+ * @returns The padded string, or the original string if its length is already greater than or equal to the target length.
194
+ *
195
+ * @example
196
+ * ```ts
197
+ * console.log(padRight("hello", 8)); // "hello "
198
+ * console.log(padRight("hi", 5, "*")); // "hi***"
199
+ * console.log(padRight("hello", 3)); // "hello"
200
+ * console.log(padRight("", 3, "0")); // "000"
201
+ * ```
202
+ */
203
+ declare function padRight(/** The string to pad */
204
+ str: string, /** The target length of the padded string */
205
+ length: number,
206
+ /**
207
+ * The character to use for padding (defaults to a space)
208
+ *
209
+ * @default ' '
210
+ */
211
+ char?: string): string;
212
+ //#endregion
213
+ //#region src/repeatString.d.ts
214
+ /**
215
+ * Repeats a string a specified number of times.
216
+ *
217
+ * @returns The string repeated the specified number of times.
218
+ *
219
+ * @example
220
+ * ```ts
221
+ * console.log(repeatString("hi", 3)); // "hihihi"
222
+ * console.log(repeatString("a", 2)); // "aa"
223
+ * console.log(repeatString("test", 0)); // ""
224
+ * console.log(repeatString("", 5)); // ""
225
+ * ```
226
+ */
227
+ declare function repeatString(/** The string to repeat */
228
+ str: string, /** The number of times to repeat the string (must be non-negative) */
229
+ count: number): string;
230
+ //#endregion
231
+ //#region src/shortestWord.d.ts
232
+ /**
233
+ * Finds the shortest word(s) in a string.
234
+ *
235
+ * @remarks
236
+ * - If only one shortest word exists, returns it as a string.
237
+ * - If multiple shortest words have the same length, returns them as an array.
238
+ * - Returns an empty string if there are no words.
239
+ *
240
+ * @returns The shortest word as a string, or an array of tied words.
241
+ *
242
+ * @example
243
+ * ```ts
244
+ * shortestWord("js utils kit"); // "js"
245
+ * shortestWord("one three five"); // "one"
246
+ * shortestWord("a ab abc abcd"); // "a"
247
+ * shortestWord(""); // ""
248
+ * ```
249
+ */
250
+ declare function shortestWord(/** The input string. */
251
+ str: string): string | string[];
252
+ //#endregion
253
+ //#region src/shortestWordLength.d.ts
254
+ /**
255
+ * Finds the length of the shortest word in a string.
256
+ *
257
+ * @remarks
258
+ * - Words are split by whitespace (`/\s+/`).
259
+ * - Symbols (punctuation, special chars) are removed using {@link stripSymbols}.
260
+ * - Returns `0` if the string is empty or contains no words.
261
+ *
262
+ * @returns Length of the shortest word.
263
+ *
264
+ * @example
265
+ * ```ts
266
+ * shortestWordLength("js utils kit"); // 2
267
+ * shortestWordLength("one three five"); // 3
268
+ * shortestWordLength(""); // 0
269
+ * ```
270
+ */
271
+ declare function shortestWordLength(/** The input string. */
272
+ str: string): number;
273
+ //#endregion
274
+ //#region src/splitString.d.ts
275
+ /**
276
+ * Splits a string into an array of substrings using a given delimiter.
277
+ *
278
+ * @remarks
279
+ * - Defaults to splitting by whitespace using `/\s+/`.
280
+ * - Does not trim leading/trailing spaces unless explicitly passed.
281
+ * - Leading/trailing delimiters may produce empty strings.
282
+ * - With `/\s+/` specifically, consecutive whitespace is collapsed (no interior empty tokens).
283
+ *
284
+ * @returns An array of substrings.
285
+ *
286
+ * @example
287
+ * ```ts
288
+ * splitString("a b c"); // ["a", "b", "c"]
289
+ * splitString("a,b,c", ","); // ["a", "b", "c"]
290
+ * splitString("a1b2c3", /\d/); // ["a", "b", "c", ""]
291
+ * ```
292
+ */
293
+ declare function splitString(/** The input string to split. */
294
+ str: string,
295
+ /**
296
+ * The delimiter (string or RegExp)
297
+ *
298
+ * @default whitespace `/\s+/`
299
+ */
300
+ val?: string | RegExp): string[];
301
+ //#endregion
302
+ //#region src/stripSymbols.d.ts
303
+ /**
304
+ * Removes or replaces common symbol characters from a string.
305
+ *
306
+ * @remarks
307
+ * - Strips symbols like `- _ @ ! $ % ^ & # * ( ) + = , . ; : ' " < > ? / \ | [ ] { }`.
308
+ * - Keeps letters, numbers, and spaces intact.
309
+ * - By default, removes symbols (replaces with `""`).
310
+ *
311
+ * @returns A new string with symbols removed or replaced.
312
+ *
313
+ * @example
314
+ * ```ts
315
+ * stripSymbols("hello-world!"); // "helloworld"
316
+ * stripSymbols("hello-world!", " "); // "hello world "
317
+ * stripSymbols("user_name@test", "_"); // "user_nametest"
318
+ * stripSymbols("symbols-only!!!", "*"); // "symbols-only***"
319
+ * ```
320
+ */
321
+ declare function stripSymbols(/** The input string */
322
+ str: string,
323
+ /**
324
+ * Optional replacement string for removed symbols.
325
+ *
326
+ * @default ""
327
+ */
328
+ replacement?: string): string;
329
+ //#endregion
330
+ //#region src/trim.d.ts
331
+ /**
332
+ * Trims whitespace from a string with methods for leading and trailing and normalizing whitespace.
333
+ *
334
+ * @param str - The string to trim.
335
+ *
336
+ * @returns The string with leading and trailing whitespace removed.
337
+ *
338
+ * @remarks
339
+ * - The main callable form behaves like {@link String.prototype.trim}, removing whitespace from both ends.
340
+ * - `.start` removes only leading whitespace (like {@link String.prototype.trimStart}).
341
+ * - `.end` removes only trailing whitespace (like {@link String.prototype.trimEnd}).
342
+ * - `.normalizeWhitespace` trims the string and replaces any sequence of whitespace characters with a single space.
343
+ *
344
+ * @default 'trims both sides'
345
+ *
346
+ * @example
347
+ * ```ts
348
+ * // Trim both sides
349
+ * console.log(trim(" hello ")); // "hello"
350
+ * console.log(trim("")); // ""
351
+ * console.log(trim("hello")); // "hello"
352
+ *
353
+ * // Trim leading whitespace
354
+ * console.log(trim.start(" hello ")); // "hello "
355
+ * console.log(trim.start("")); // ""
356
+ * console.log(trim.start("hello")); // "hello"
357
+ *
358
+ * // Trim trailing whitespace
359
+ * console.log(trim.end(" hello ")); // " hello"
360
+ * console.log(trim.end("")); // ""
361
+ * console.log(trim.end("hello")); // "hello"
362
+ *
363
+ * // Normalize whitespace
364
+ * console.log(trim.normalizeWhitespace(" hello world ")); // "hello world"
365
+ * console.log(trim.normalizeWhitespace("hello\t world")); // "hello world"
366
+ * console.log(trim.normalizeWhitespace("")); // ""
367
+ * ```
368
+ */
369
+ declare const trim: Trim;
370
+ //#endregion
371
+ //#region src/truncate.d.ts
372
+ /**
373
+ * Truncates a string to a specified length, appending a suffix if the string is too long.
374
+ *
375
+ * @returns The truncated string with the suffix if the original string exceeds the length, otherwise the original string.
376
+ *
377
+ * @example
378
+ * ```ts
379
+ * console.log(truncate("hello world", 8)); // "hello..."
380
+ * console.log(truncate("hi", 5)); // "hi"
381
+ * console.log(truncate("hello", 5, "...")); // "hello"
382
+ * console.log(truncate("", 3)); // ""
383
+ * ```
384
+ */
385
+ declare function truncate(/** The string to truncate */
386
+ str: string, /** The maximum length of the resulting string */
387
+ length: number,
388
+ /**
389
+ * The suffix to append if truncation occurs
390
+ *
391
+ * @default "..."
392
+ */
393
+ suffix?: string): string;
394
+ //#endregion
395
+ //#region src/uniqueChars.d.ts
396
+ /**
397
+ * Returns an array of unique characters from a string.
398
+ *
399
+ * @remarks
400
+ * - Preserves order of first appearance.
401
+ *
402
+ * @returns Array of unique characters.
403
+ *
404
+ * @example
405
+ * ```ts
406
+ * uniqueChars("banana"); // ["b", "a", "n"]
407
+ * uniqueChars(""); // []
408
+ * ```
409
+ */
410
+ declare function uniqueChars(/** The input string. */
411
+ str: string): string[];
412
+ //#endregion
413
+ export { capitalize, countChars, countFrequencies, countLines, countSubstring, countWords, longestWord, longestWordLength, padLeft, padRight, repeatString, shortestWord, shortestWordLength, splitString, stripSymbols, trim, truncate, uniqueChars };
@@ -0,0 +1,413 @@
1
+ import { Trim } from "@js-utils-kit/types";
2
+ export * from "@js-utils-kit/types";
3
+
4
+ //#region src/capitalize.d.ts
5
+
6
+ /**
7
+ * Capitalizes the first character of a string using a regular expression.
8
+ *
9
+ * @returns The input string with its first character capitalized, or the original string if empty or not a string.
10
+ *
11
+ * @example
12
+ * ```ts
13
+ * capitalize("hello"); // "Hello"
14
+ * capitalize("world"); // "World"
15
+ * capitalize(""); // ""
16
+ * capitalize("a"); // "A"
17
+ * ```
18
+ */
19
+ declare function capitalize(/** The string to capitalize */
20
+ value: string): string;
21
+ //#endregion
22
+ //#region src/countChars.d.ts
23
+ /**
24
+ * Counts characters in a string.
25
+ *
26
+ * @remarks
27
+ * - If `char` is provided, only that character’s occurrences are counted.
28
+ * - If `char` is omitted, the function counts the number of substrings returned by {@link splitString} (similar to a word count).
29
+ *
30
+ * @returns Number of characters or character occurrences.
31
+ *
32
+ * @example
33
+ * ```ts
34
+ * countChars("banana"); // 6
35
+ * countChars("banana", "a"); // 3
36
+ * countChars("banana", "n"); // 2
37
+ * ```
38
+ */
39
+ declare function countChars(/** The input string. */
40
+ str: string, /** Optional character to count. */
41
+ char?: string): number;
42
+ //#endregion
43
+ //#region src/countFrequencies.d.ts
44
+ /**
45
+ * Builds a frequency map of characters in a string.
46
+ *
47
+ * @remarks
48
+ * - Returns an object where keys are characters and values are counts.
49
+ * - Case-sensitive by default.
50
+ *
51
+ * @returns A mapping of each character to its frequency.
52
+ *
53
+ * @example
54
+ * ```ts
55
+ * countFrequencies("banana");
56
+ * // { b: 1, a: 3, n: 2 }
57
+ *
58
+ * countFrequencies("");
59
+ * // {}
60
+ * ```
61
+ */
62
+ declare function countFrequencies(/** The input string. */
63
+ str: string): Record<string, number>;
64
+ //#endregion
65
+ //#region src/countLines.d.ts
66
+ /**
67
+ * Counts the number of lines in a string.
68
+ *
69
+ * @remarks
70
+ * - Handles Unix (`\n`), Windows (`\r\n`), and old Mac (`\r`) line endings.
71
+ *
72
+ * @returns Number of lines in the string.
73
+ *
74
+ * @example
75
+ * ```ts
76
+ * countLines("a\nb\nc"); // 3
77
+ * countLines("a\r\nb\r\nc"); // 3
78
+ * countLines("hello"); // 1
79
+ * ```
80
+ */
81
+ declare function countLines(/** The input string. */
82
+ str: string): number;
83
+ //#endregion
84
+ //#region src/countSubstring.d.ts
85
+ /**
86
+ * Counts how many times a substring occurs within a string.
87
+ *
88
+ * @remarks
89
+ * - Overlapping substrings are not double-counted.
90
+ * - Returns `0` if the substring is not found.
91
+ *
92
+ * @returns Number of times `sub` occurs in `str`.
93
+ *
94
+ * @example
95
+ * ```ts
96
+ * countSubstring("lololol", "lo"); // 3
97
+ * countSubstring("aaaaa", "a"); // 5
98
+ * countSubstring("hello", "z"); // 0
99
+ * ```
100
+ */
101
+ declare function countSubstring(/** The input string. */
102
+ str: string, /** The substring to count. */
103
+ sub: string): number;
104
+ //#endregion
105
+ //#region src/countWords.d.ts
106
+ /**
107
+ * Counts the number of words in a string.
108
+ *
109
+ * @returns Number of words in the string.
110
+ *
111
+ * @example
112
+ * ```ts
113
+ * countWords("js utils kit"); // 3
114
+ * countWords(" js utils kit"); // 3
115
+ * countWords(""); // 0
116
+ * ```
117
+ */
118
+ declare function countWords(/** The input string */
119
+ str: string): number;
120
+ //#endregion
121
+ //#region src/longestWord.d.ts
122
+ /**
123
+ * Finds the longest word(s) in a string.
124
+ *
125
+ * @remarks
126
+ * - If only one longest word exists, returns it as a string.
127
+ * - If multiple longest words have the same length, returns them as an array.
128
+ * - Returns an empty string if there are no words.
129
+ *
130
+ * @returns The longest word as a string, or an array of tied words.
131
+ *
132
+ * @example
133
+ * ```ts
134
+ * longestWord("js utils kit"); // "utils"
135
+ * longestWord("short longerword mid"); // "longerword"
136
+ * longestWord("hello"); // "hello"
137
+ * longestWord(""); // ""
138
+ * ```
139
+ */
140
+ declare function longestWord(/** The input string. */
141
+ str: string): string | string[];
142
+ //#endregion
143
+ //#region src/longestWordLength.d.ts
144
+ /**
145
+ * Finds the length of the longest word in a string.
146
+ *
147
+ * @remarks
148
+ * - Words are split by whitespace (`/\s+/`).
149
+ * - Symbols (punctuation, special chars) are removed using {@link stripSymbols}.
150
+ * - Returns `0` if the string is empty or contains no words.
151
+ *
152
+ * @returns Length of the longest word.
153
+ *
154
+ * @example
155
+ * ```ts
156
+ * longestWordLength("js utils kit"); // 5
157
+ * longestWordLength("short longerword mid"); // 10
158
+ * longestWordLength("hello"); // 5
159
+ * longestWordLength(""); // 0
160
+ * ```
161
+ */
162
+ declare function longestWordLength(/** The input string. */
163
+ str: string): number;
164
+ //#endregion
165
+ //#region src/padLeft.d.ts
166
+ /**
167
+ * Pads a string on the left with a specified character until it reaches the desired length.
168
+ *
169
+ * @returns The padded string, or the original string if its length is already greater than or equal to the target length.
170
+ *
171
+ * @example
172
+ * ```ts
173
+ * console.log(padLeft("hello", 8)); // " hello"
174
+ * console.log(padLeft("hi", 5, "*")); // "***hi"
175
+ * console.log(padLeft("hello", 3)); // "hello"
176
+ * console.log(padLeft("", 3, "0")); // "000"
177
+ * ```
178
+ */
179
+ declare function padLeft(/** The string to pad */
180
+ str: string, /** The target length of the padded string */
181
+ length: number,
182
+ /**
183
+ * The character to use for padding (defaults to a space)
184
+ *
185
+ * @default ' '
186
+ */
187
+ char?: string): string;
188
+ //#endregion
189
+ //#region src/padRight.d.ts
190
+ /**
191
+ * Pads a string on the right with a specified character until it reaches the desired length.
192
+ *
193
+ * @returns The padded string, or the original string if its length is already greater than or equal to the target length.
194
+ *
195
+ * @example
196
+ * ```ts
197
+ * console.log(padRight("hello", 8)); // "hello "
198
+ * console.log(padRight("hi", 5, "*")); // "hi***"
199
+ * console.log(padRight("hello", 3)); // "hello"
200
+ * console.log(padRight("", 3, "0")); // "000"
201
+ * ```
202
+ */
203
+ declare function padRight(/** The string to pad */
204
+ str: string, /** The target length of the padded string */
205
+ length: number,
206
+ /**
207
+ * The character to use for padding (defaults to a space)
208
+ *
209
+ * @default ' '
210
+ */
211
+ char?: string): string;
212
+ //#endregion
213
+ //#region src/repeatString.d.ts
214
+ /**
215
+ * Repeats a string a specified number of times.
216
+ *
217
+ * @returns The string repeated the specified number of times.
218
+ *
219
+ * @example
220
+ * ```ts
221
+ * console.log(repeatString("hi", 3)); // "hihihi"
222
+ * console.log(repeatString("a", 2)); // "aa"
223
+ * console.log(repeatString("test", 0)); // ""
224
+ * console.log(repeatString("", 5)); // ""
225
+ * ```
226
+ */
227
+ declare function repeatString(/** The string to repeat */
228
+ str: string, /** The number of times to repeat the string (must be non-negative) */
229
+ count: number): string;
230
+ //#endregion
231
+ //#region src/shortestWord.d.ts
232
+ /**
233
+ * Finds the shortest word(s) in a string.
234
+ *
235
+ * @remarks
236
+ * - If only one shortest word exists, returns it as a string.
237
+ * - If multiple shortest words have the same length, returns them as an array.
238
+ * - Returns an empty string if there are no words.
239
+ *
240
+ * @returns The shortest word as a string, or an array of tied words.
241
+ *
242
+ * @example
243
+ * ```ts
244
+ * shortestWord("js utils kit"); // "js"
245
+ * shortestWord("one three five"); // "one"
246
+ * shortestWord("a ab abc abcd"); // "a"
247
+ * shortestWord(""); // ""
248
+ * ```
249
+ */
250
+ declare function shortestWord(/** The input string. */
251
+ str: string): string | string[];
252
+ //#endregion
253
+ //#region src/shortestWordLength.d.ts
254
+ /**
255
+ * Finds the length of the shortest word in a string.
256
+ *
257
+ * @remarks
258
+ * - Words are split by whitespace (`/\s+/`).
259
+ * - Symbols (punctuation, special chars) are removed using {@link stripSymbols}.
260
+ * - Returns `0` if the string is empty or contains no words.
261
+ *
262
+ * @returns Length of the shortest word.
263
+ *
264
+ * @example
265
+ * ```ts
266
+ * shortestWordLength("js utils kit"); // 2
267
+ * shortestWordLength("one three five"); // 3
268
+ * shortestWordLength(""); // 0
269
+ * ```
270
+ */
271
+ declare function shortestWordLength(/** The input string. */
272
+ str: string): number;
273
+ //#endregion
274
+ //#region src/splitString.d.ts
275
+ /**
276
+ * Splits a string into an array of substrings using a given delimiter.
277
+ *
278
+ * @remarks
279
+ * - Defaults to splitting by whitespace using `/\s+/`.
280
+ * - Does not trim leading/trailing spaces unless explicitly passed.
281
+ * - Leading/trailing delimiters may produce empty strings.
282
+ * - With `/\s+/` specifically, consecutive whitespace is collapsed (no interior empty tokens).
283
+ *
284
+ * @returns An array of substrings.
285
+ *
286
+ * @example
287
+ * ```ts
288
+ * splitString("a b c"); // ["a", "b", "c"]
289
+ * splitString("a,b,c", ","); // ["a", "b", "c"]
290
+ * splitString("a1b2c3", /\d/); // ["a", "b", "c", ""]
291
+ * ```
292
+ */
293
+ declare function splitString(/** The input string to split. */
294
+ str: string,
295
+ /**
296
+ * The delimiter (string or RegExp)
297
+ *
298
+ * @default whitespace `/\s+/`
299
+ */
300
+ val?: string | RegExp): string[];
301
+ //#endregion
302
+ //#region src/stripSymbols.d.ts
303
+ /**
304
+ * Removes or replaces common symbol characters from a string.
305
+ *
306
+ * @remarks
307
+ * - Strips symbols like `- _ @ ! $ % ^ & # * ( ) + = , . ; : ' " < > ? / \ | [ ] { }`.
308
+ * - Keeps letters, numbers, and spaces intact.
309
+ * - By default, removes symbols (replaces with `""`).
310
+ *
311
+ * @returns A new string with symbols removed or replaced.
312
+ *
313
+ * @example
314
+ * ```ts
315
+ * stripSymbols("hello-world!"); // "helloworld"
316
+ * stripSymbols("hello-world!", " "); // "hello world "
317
+ * stripSymbols("user_name@test", "_"); // "user_nametest"
318
+ * stripSymbols("symbols-only!!!", "*"); // "symbols-only***"
319
+ * ```
320
+ */
321
+ declare function stripSymbols(/** The input string */
322
+ str: string,
323
+ /**
324
+ * Optional replacement string for removed symbols.
325
+ *
326
+ * @default ""
327
+ */
328
+ replacement?: string): string;
329
+ //#endregion
330
+ //#region src/trim.d.ts
331
+ /**
332
+ * Trims whitespace from a string with methods for leading and trailing and normalizing whitespace.
333
+ *
334
+ * @param str - The string to trim.
335
+ *
336
+ * @returns The string with leading and trailing whitespace removed.
337
+ *
338
+ * @remarks
339
+ * - The main callable form behaves like {@link String.prototype.trim}, removing whitespace from both ends.
340
+ * - `.start` removes only leading whitespace (like {@link String.prototype.trimStart}).
341
+ * - `.end` removes only trailing whitespace (like {@link String.prototype.trimEnd}).
342
+ * - `.normalizeWhitespace` trims the string and replaces any sequence of whitespace characters with a single space.
343
+ *
344
+ * @default 'trims both sides'
345
+ *
346
+ * @example
347
+ * ```ts
348
+ * // Trim both sides
349
+ * console.log(trim(" hello ")); // "hello"
350
+ * console.log(trim("")); // ""
351
+ * console.log(trim("hello")); // "hello"
352
+ *
353
+ * // Trim leading whitespace
354
+ * console.log(trim.start(" hello ")); // "hello "
355
+ * console.log(trim.start("")); // ""
356
+ * console.log(trim.start("hello")); // "hello"
357
+ *
358
+ * // Trim trailing whitespace
359
+ * console.log(trim.end(" hello ")); // " hello"
360
+ * console.log(trim.end("")); // ""
361
+ * console.log(trim.end("hello")); // "hello"
362
+ *
363
+ * // Normalize whitespace
364
+ * console.log(trim.normalizeWhitespace(" hello world ")); // "hello world"
365
+ * console.log(trim.normalizeWhitespace("hello\t world")); // "hello world"
366
+ * console.log(trim.normalizeWhitespace("")); // ""
367
+ * ```
368
+ */
369
+ declare const trim: Trim;
370
+ //#endregion
371
+ //#region src/truncate.d.ts
372
+ /**
373
+ * Truncates a string to a specified length, appending a suffix if the string is too long.
374
+ *
375
+ * @returns The truncated string with the suffix if the original string exceeds the length, otherwise the original string.
376
+ *
377
+ * @example
378
+ * ```ts
379
+ * console.log(truncate("hello world", 8)); // "hello..."
380
+ * console.log(truncate("hi", 5)); // "hi"
381
+ * console.log(truncate("hello", 5, "...")); // "hello"
382
+ * console.log(truncate("", 3)); // ""
383
+ * ```
384
+ */
385
+ declare function truncate(/** The string to truncate */
386
+ str: string, /** The maximum length of the resulting string */
387
+ length: number,
388
+ /**
389
+ * The suffix to append if truncation occurs
390
+ *
391
+ * @default "..."
392
+ */
393
+ suffix?: string): string;
394
+ //#endregion
395
+ //#region src/uniqueChars.d.ts
396
+ /**
397
+ * Returns an array of unique characters from a string.
398
+ *
399
+ * @remarks
400
+ * - Preserves order of first appearance.
401
+ *
402
+ * @returns Array of unique characters.
403
+ *
404
+ * @example
405
+ * ```ts
406
+ * uniqueChars("banana"); // ["b", "a", "n"]
407
+ * uniqueChars(""); // []
408
+ * ```
409
+ */
410
+ declare function uniqueChars(/** The input string. */
411
+ str: string): string[];
412
+ //#endregion
413
+ export { capitalize, countChars, countFrequencies, countLines, countSubstring, countWords, longestWord, longestWordLength, padLeft, padRight, repeatString, shortestWord, shortestWordLength, splitString, stripSymbols, trim, truncate, uniqueChars };
package/dist/index.mjs ADDED
@@ -0,0 +1 @@
1
+ export*from"@js-utils-kit/types";function e(e){return e.replace(/^\w/,e=>e.toUpperCase())}function t(e,t=/\s+/){return e.split(t)}function n(e,n){return n?[...e].filter(e=>e===n).length:t(e).length}function r(e){return[...e].reduce((e,t)=>(e[t]=(e[t]||0)+1,e),{})}function i(e){return e?t(e,/\r\n|\r|\n/).length:0}function a(e,n){return n.length===0?0:t(e,n).length-1}function o(e){return t(e.trim()).length}function s(e,t=``){return e.replace(/[^\p{L}\p{N}\s]/gu,t)}function c(e){let n=t(e.trim()).map(e=>s(e)).filter(Boolean);if(n.length===0)return``;let r=Math.max(...n.map(e=>e.length)),i=n.filter(e=>e.length===r),a=[...new Set(i)];return a.length===1?a[0]:a}function l(e){let n=t(e).map(e=>s(e)).filter(Boolean);return n.length===0?0:n.reduce((e,t)=>Math.max(e,t.length),0)}function u(e,t,n=` `){return e.padStart(t,n)}function d(e,t,n=` `){return e.padEnd(t,n)}function f(e,t){return e.repeat(t)}function p(e){let n=t(e.trim()).map(e=>s(e)).filter(Boolean);if(n.length===0)return``;let r=Math.min(...n.map(e=>e.length)),i=n.filter(e=>e.length===r),a=[...new Set(i)];return a.length===1?a[0]:a}function m(e){let n=t(e.trim()).map(e=>s(e)).filter(Boolean);return n.length===0?0:n.reduce((e,t)=>Math.min(e,t.length),1/0)}const h=Object.assign(e=>e.trim(),{start:e=>e.trimStart(),end:e=>e.trimEnd(),normalizeWhitespace:e=>e.trim().replace(/\s+/g,` `)});function g(e,t,n=`...`){return t<=0?``:e.length<=t?e:n.length>=t?n.slice(0,t):e.trim().slice(0,t)+n}function _(e){return[...new Set(e)]}export{e as capitalize,n as countChars,r as countFrequencies,i as countLines,a as countSubstring,o as countWords,c as longestWord,l as longestWordLength,u as padLeft,d as padRight,f as repeatString,p as shortestWord,m as shortestWordLength,t as splitString,s as stripSymbols,h as trim,g as truncate,_ as uniqueChars};
package/package.json ADDED
@@ -0,0 +1,56 @@
1
+ {
2
+ "name": "@js-utils-kit/string",
3
+ "version": "1.0.0",
4
+ "description": "String utilities",
5
+ "license": "MIT",
6
+ "private": false,
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "https://github.com/teneplaysofficial/js-utils-kit",
10
+ "directory": "packages/string"
11
+ },
12
+ "bugs": {
13
+ "url": "https://github.com/teneplaysofficial/js-utils-kit/issues"
14
+ },
15
+ "author": {
16
+ "name": "Sriman",
17
+ "email": "136729116+TenEplaysOfficial@users.noreply.github.com",
18
+ "url": "https://tene.vercel.app"
19
+ },
20
+ "keywords": [
21
+ "string",
22
+ "string-utils",
23
+ "stringify",
24
+ "text",
25
+ "case",
26
+ "trim",
27
+ "format",
28
+ "sanitize",
29
+ "camelcase",
30
+ "typescript",
31
+ "utilities"
32
+ ],
33
+ "files": [
34
+ "dist"
35
+ ],
36
+ "engines": {
37
+ "node": ">=22"
38
+ },
39
+ "type": "module",
40
+ "main": "./dist/index.cjs",
41
+ "module": "./dist/index.mjs",
42
+ "types": "./dist/index.d.cts",
43
+ "exports": {
44
+ ".": {
45
+ "import": "./dist/index.mjs",
46
+ "require": "./dist/index.cjs"
47
+ }
48
+ },
49
+ "dependencies": {
50
+ "@js-utils-kit/types": "1.0.0"
51
+ },
52
+ "scripts": {
53
+ "build": "tsdown",
54
+ "test": "vitest run"
55
+ }
56
+ }