js-utils-kit 0.4.0 → 0.5.1
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/README.md +9 -9
- package/dist/index.cjs +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/string/index.cjs +1 -1
- package/dist/string/index.d.ts +278 -6
- package/dist/string/index.js +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -31,6 +31,14 @@ _Modular JavaScript utilities with type support for strings, objects, arrays and
|
|
|
31
31
|
|
|
32
32
|
This package includes utilities designed for both browser and Node.js environments, clearly organized and optimized for cross-platform compatibility.
|
|
33
33
|
|
|
34
|
+
## Features
|
|
35
|
+
|
|
36
|
+
- Organized by category (e.g., `number`, `string`, `file`, etc.)
|
|
37
|
+
- Fully typed in TypeScript
|
|
38
|
+
- Thoroughly tested with Jest
|
|
39
|
+
- Tree‑shakeable and supports both ESM and CJS
|
|
40
|
+
- CLI-ready tools for file and system operations
|
|
41
|
+
|
|
34
42
|
## Installation
|
|
35
43
|
|
|
36
44
|
```sh
|
|
@@ -126,17 +134,9 @@ console.log(object.isEmpty({})); // true
|
|
|
126
134
|
console.log(env.isBrowser()); // true/false
|
|
127
135
|
```
|
|
128
136
|
|
|
129
|
-
## Features
|
|
130
|
-
|
|
131
|
-
- Organized by category (e.g., `number`, `string`, `file`, etc.)
|
|
132
|
-
- Fully typed with TypeScript
|
|
133
|
-
- Thoroughly tested with Jest
|
|
134
|
-
- Tree-shakable and supports both ESM and CJS
|
|
135
|
-
- CLI-ready tools for file and system operations
|
|
136
|
-
|
|
137
137
|
## Documentation
|
|
138
138
|
|
|
139
|
-
Full documentation available at [GitHub Pages](https://
|
|
139
|
+
Full documentation available at [GitHub Pages](https://js-utils.js.org/modules.html)
|
|
140
140
|
|
|
141
141
|
## Requirements
|
|
142
142
|
|
package/dist/index.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var e=require("./array/index.cjs"),r=require("./char/index.cjs"),s=require("./env/index.cjs"),t=require("./file/index.cjs"),i=require("./number/index.cjs"),o=require("./object/index.cjs"),
|
|
1
|
+
"use strict";var e=require("./array/index.cjs"),r=require("./char/index.cjs"),s=require("./env/index.cjs"),t=require("./file/index.cjs"),i=require("./number/index.cjs"),o=require("./object/index.cjs"),n=require("./string/index.cjs"),p=require("./validate-BKQ-HFTd.js");require("fs"),require("path"),require("events"),require("constants"),require("stream"),require("util"),require("assert"),require("buffer"),require("node:url"),require("node:path"),require("node:fs"),require("node:fs/promises"),require("node:events"),require("node:stream"),require("node:string_decoder"),require("zlib"),exports.array=e.default,exports.all=r.all,exports.char=r.default,exports.digits=r.digits,exports.lowercase=r.lowercase,exports.uppercase=r.uppercase,Object.defineProperty(exports,"Environment",{enumerable:!0,get:function(){return s.Environment}}),exports.env=s.default,exports.getRunTimeEnvironment=s.getRunTimeEnvironment,exports.isBrowser=s.isBrowser,exports.isDev=s.isDev,exports.isNode=s.isNode,exports.isProd=s.isProd,exports.isTest=s.isTest,exports.createArchive=t.createArchive,exports.file=t.default,exports.clamp=i.clamp,exports.getMilliseconds=i.getMilliseconds,exports.isEven=i.isEven,exports.isOdd=i.isOdd,exports.number=i.default,exports.randomFloat=i.randomFloat,exports.randomInt=i.randomInt,exports.isEmptyObject=o.isEmptyObject,exports.isNonEmptyObject=o.isNonEmptyObject,exports.isObject=o.isObject,exports.mergeObj=o.mergeObj,exports.object=o.default,exports.capitalize=n.capitalize,exports.containsWhitespace=n.containsWhitespace,exports.countChars=n.countChars,exports.countFrequencies=n.countFrequencies,exports.countLines=n.countLines,exports.countSubstring=n.countSubstring,exports.countWords=n.countWords,exports.endsWithPunctuation=n.endsWithPunctuation,exports.isAlphabetic=n.isAlphabetic,exports.isCamelCase=n.isCamelCase,exports.isEmail=n.isEmail,exports.isKebabCase=n.isKebabCase,exports.isLowerCase=n.isLowerCase,exports.isNonEmptyString=n.isNonEmptyString,exports.isNumericString=n.isNumericString,exports.isPascalCase=n.isPascalCase,exports.isSnakeCase=n.isSnakeCase,exports.isString=n.isString,exports.isURL=n.isURL,exports.isUpperCase=n.isUpperCase,exports.longestWord=n.longestWord,exports.longestWordLength=n.longestWordLength,exports.padLeft=n.padLeft,exports.padRight=n.padRight,exports.repeatString=n.repeatString,exports.shortestWord=n.shortestWord,exports.shortestWordLength=n.shortestWordLength,exports.splitString=n.splitString,exports.startsWithUppercase=n.startsWithUppercase,exports.string=n.default,exports.stripSymbols=n.stripSymbols,exports.trim=n.trim,exports.truncate=n.truncate,exports.uniqueChars=n.uniqueChars,exports.isArray=p.isArray,exports.isDefined=p.isDefined,exports.isUndefinedOrNull=p.isUndefinedOrNull;
|
package/dist/index.d.ts
CHANGED
|
@@ -4,7 +4,7 @@ export { Environment, default as env, getRunTimeEnvironment, isBrowser, isDev, i
|
|
|
4
4
|
export { ArchiveFormat, CreateArchiveOptions, createArchive, default as file } from './file/index.js';
|
|
5
5
|
export { Hour, MinuteOrSecond, clamp, getMilliseconds, isEven, isOdd, default as number, randomFloat, randomInt } from './number/index.js';
|
|
6
6
|
export { isEmptyObject, isNonEmptyObject, isObject, mergeObj, default as object } from './object/index.js';
|
|
7
|
-
export { Trim, capitalize, containsWhitespace, endsWithPunctuation, isAlphabetic, isCamelCase, isEmail, isKebabCase, isLowerCase, isNonEmptyString, isNumericString, isPascalCase, isSnakeCase, isString, isURL, isUpperCase, padLeft, padRight, repeatString, startsWithUppercase, default as string, trim, truncate } from './string/index.js';
|
|
7
|
+
export { Trim, capitalize, containsWhitespace, countChars, countFrequencies, countLines, countSubstring, countWords, endsWithPunctuation, isAlphabetic, isCamelCase, isEmail, isKebabCase, isLowerCase, isNonEmptyString, isNumericString, isPascalCase, isSnakeCase, isString, isURL, isUpperCase, longestWord, longestWordLength, padLeft, padRight, repeatString, shortestWord, shortestWordLength, splitString, startsWithUppercase, default as string, stripSymbols, trim, truncate, uniqueChars } from './string/index.js';
|
|
8
8
|
export { ArchiverOptions } from 'archiver';
|
|
9
9
|
|
|
10
10
|
/**
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export{default as array}from"./array/index.js";export{all,default as char,digits,lowercase,uppercase}from"./char/index.js";export{Environment,default as env,getRunTimeEnvironment,isBrowser,isDev,isNode,isProd,isTest}from"./env/index.js";export{createArchive,default as file}from"./file/index.js";export{clamp,getMilliseconds,isEven,isOdd,default as number,randomFloat,randomInt}from"./number/index.js";export{isEmptyObject,isNonEmptyObject,isObject,mergeObj,default as object}from"./object/index.js";export{capitalize,containsWhitespace,endsWithPunctuation,isAlphabetic,isCamelCase,isEmail,isKebabCase,isLowerCase,isNonEmptyString,isNumericString,isPascalCase,isSnakeCase,isString,isURL,isUpperCase,padLeft,padRight,repeatString,startsWithUppercase,default as string,trim,truncate}from"./string/index.js";export{i as isArray,a as isDefined,b as isUndefinedOrNull}from"./validate-D9J7otjc.js";import"fs";import"path";import"events";import"constants";import"stream";import"util";import"assert";import"buffer";import"node:url";import"node:path";import"node:fs";import"node:fs/promises";import"node:events";import"node:stream";import"node:string_decoder";import"zlib";
|
|
1
|
+
export{default as array}from"./array/index.js";export{all,default as char,digits,lowercase,uppercase}from"./char/index.js";export{Environment,default as env,getRunTimeEnvironment,isBrowser,isDev,isNode,isProd,isTest}from"./env/index.js";export{createArchive,default as file}from"./file/index.js";export{clamp,getMilliseconds,isEven,isOdd,default as number,randomFloat,randomInt}from"./number/index.js";export{isEmptyObject,isNonEmptyObject,isObject,mergeObj,default as object}from"./object/index.js";export{capitalize,containsWhitespace,countChars,countFrequencies,countLines,countSubstring,countWords,endsWithPunctuation,isAlphabetic,isCamelCase,isEmail,isKebabCase,isLowerCase,isNonEmptyString,isNumericString,isPascalCase,isSnakeCase,isString,isURL,isUpperCase,longestWord,longestWordLength,padLeft,padRight,repeatString,shortestWord,shortestWordLength,splitString,startsWithUppercase,default as string,stripSymbols,trim,truncate,uniqueChars}from"./string/index.js";export{i as isArray,a as isDefined,b as isUndefinedOrNull}from"./validate-D9J7otjc.js";import"fs";import"path";import"events";import"constants";import"stream";import"util";import"assert";import"buffer";import"node:url";import"node:path";import"node:fs";import"node:fs/promises";import"node:events";import"node:stream";import"node:string_decoder";import"zlib";
|
package/dist/string/index.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";function t(t){return t.replace(/^\w/,t=>t.toUpperCase())}function e(t,e,
|
|
1
|
+
"use strict";function t(t){return t.replace(/^\w/,t=>t.toUpperCase())}function e(t,e,n=" "){return t.padStart(e,n)}function n(t,e,n=" "){return t.padEnd(e,n)}function r(t,e,n="..."){return e<=0?"":t.length<=e?t:n.length>=e?n.slice(0,e):t.trim().slice(0,e)+n}function s(t,e){return t.repeat(e)}function i(t,e=""){return t.replace(/[^\p{L}\p{N}\s]/gu,e)}function o(t,e=/\s+/){return t.split(e)}function u(t,e){return e?[...t].filter(t=>t===e).length:o(t).length}function a(t){return o(t.trim()).length}function p(t,e){return 0===e.length?0:o(t,e).length-1}function c(t){return[...t].reduce((t,e)=>(t[e]=(t[e]||0)+1,t),{})}function l(t){return t?o(t,/\r\n|\r|\n/).length:0}function h(t){return[...new Set(t)]}function f(t){const e=o(t).map(t=>i(t)).filter(Boolean);return 0===e.length?0:e.reduce((t,e)=>Math.max(t,e.length),0)}function g(t){const e=o(t.trim()).map(t=>i(t)).filter(Boolean);return 0===e.length?0:e.reduce((t,e)=>Math.min(t,e.length),1/0)}function x(t){const e=o(t.trim()).map(t=>i(t)).filter(Boolean);if(0===e.length)return"";const n=Math.max(...e.map(t=>t.length)),r=e.filter(t=>t.length===n),s=[...new Set(r)];return 1===s.length?s[0]:s}function m(t){const e=o(t.trim()).map(t=>i(t)).filter(Boolean);if(0===e.length)return"";const n=Math.min(...e.map(t=>t.length)),r=e.filter(t=>t.length===n),s=[...new Set(r)];return 1===s.length?s[0]:s}Object.defineProperty(exports,"__esModule",{value:!0});const d={function:t=>t.trim(),start:t=>t.trimStart(),end:t=>t.trimEnd(),normalizeWhitespace:t=>t.trim().replace(/\s+/g," ")};function S(t){return null!=t&&"string"==typeof t}function C(t,e=!0){return!!S(t)&&("string"==typeof t&&(e?t.trim().length>0:t.length>0))}function W(t){try{return new URL(t),!0}catch{return!1}}function L(t){return/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(t)}function b(t){return/^[a-zA-Z]+$/.test(t)}function z(t){return!isNaN(Number(t))&&!isNaN(parseFloat(t))}function N(t){return/^[A-Z]/.test(t)}function $(t){return/[\p{P}]$/u.test(t)}function y(t){return/\s/.test(t)}function A(t){return/^[a-z][a-zA-Z0-9]*$/.test(t)}function U(t){return/^[A-Z][a-zA-Z0-9]*$/.test(t)}function w(t){return/^[a-z0-9]+(-[a-z0-9]+)*$/.test(t)}function E(t){return/^[a-z0-9]+(_[a-z0-9]+)*$/.test(t)}function P(t){return/^[A-Z]+$/.test(t)}function Z(t){return/^[a-z]+$/.test(t)}var M={capitalize:t,splitString:o,countChars:u,countWords:a,countSubstring:p,countFrequencies:c,countLines:l,uniqueChars:h,longestWordLength:f,shortestWordLength:g,longestWord:x,shortestWord:m,padLeft:e,padRight:n,truncate:r,repeatString:s,stripSymbols:i,trim:d,isString:S,isNonEmptyString:C,isURL:W,isEmail:L,isAlphabetic:b,isNumericString:z,startsWithUppercase:N,endsWithPunctuation:$,containsWhitespace:y,isCamelCase:A,isPascalCase:U,isKebabCase:w,isSnakeCase:E,isUpperCase:P,isLowerCase:Z};exports.capitalize=t,exports.containsWhitespace=y,exports.countChars=u,exports.countFrequencies=c,exports.countLines=l,exports.countSubstring=p,exports.countWords=a,exports.default=M,exports.endsWithPunctuation=$,exports.isAlphabetic=b,exports.isCamelCase=A,exports.isEmail=L,exports.isKebabCase=w,exports.isLowerCase=Z,exports.isNonEmptyString=C,exports.isNumericString=z,exports.isPascalCase=U,exports.isSnakeCase=E,exports.isString=S,exports.isURL=W,exports.isUpperCase=P,exports.longestWord=x,exports.longestWordLength=f,exports.padLeft=e,exports.padRight=n,exports.repeatString=s,exports.shortestWord=m,exports.shortestWordLength=g,exports.splitString=o,exports.startsWithUppercase=N,exports.stripSymbols=i,exports.trim=d,exports.truncate=r,exports.uniqueChars=h;
|
package/dist/string/index.d.ts
CHANGED
|
@@ -88,6 +88,230 @@ declare const trim: Trim;
|
|
|
88
88
|
*/
|
|
89
89
|
declare function capitalize(value: string): string;
|
|
90
90
|
|
|
91
|
+
/**
|
|
92
|
+
* Splits a string into an array of substrings using a given delimiter.
|
|
93
|
+
*
|
|
94
|
+
* @remarks
|
|
95
|
+
* - Defaults to splitting by whitespace using `/\s+/`.
|
|
96
|
+
* - Does not trim leading/trailing spaces unless explicitly passed.
|
|
97
|
+
* - Leading/trailing delimiters may produce empty strings.
|
|
98
|
+
* - With `/\s+/` specifically, consecutive whitespace is collapsed (no interior empty tokens).
|
|
99
|
+
*
|
|
100
|
+
* @returns An array of substrings.
|
|
101
|
+
*
|
|
102
|
+
* @example
|
|
103
|
+
* ```ts
|
|
104
|
+
* splitString("a b c"); // ["a", "b", "c"]
|
|
105
|
+
* splitString("a,b,c", ","); // ["a", "b", "c"]
|
|
106
|
+
* splitString("a1b2c3", /\d/); // ["a", "b", "c", ""]
|
|
107
|
+
* ```
|
|
108
|
+
*/
|
|
109
|
+
declare function splitString(
|
|
110
|
+
/** The input string to split. */
|
|
111
|
+
str: string,
|
|
112
|
+
/**
|
|
113
|
+
* The delimiter (string or RegExp)
|
|
114
|
+
*
|
|
115
|
+
* @default whitespace `/\s+/`
|
|
116
|
+
*/
|
|
117
|
+
val?: string | RegExp): string[];
|
|
118
|
+
/**
|
|
119
|
+
* Counts characters in a string.
|
|
120
|
+
*
|
|
121
|
+
* @remarks
|
|
122
|
+
* - If `char` is provided, only that character’s occurrences are counted.
|
|
123
|
+
* - If `char` is omitted, the function counts the number of substrings returned by {@link splitString} (similar to a word count).
|
|
124
|
+
*
|
|
125
|
+
* @returns Number of characters or character occurrences.
|
|
126
|
+
*
|
|
127
|
+
* @example
|
|
128
|
+
* ```ts
|
|
129
|
+
* countChars("banana"); // 6
|
|
130
|
+
* countChars("banana", "a"); // 3
|
|
131
|
+
* countChars("banana", "n"); // 2
|
|
132
|
+
* ```
|
|
133
|
+
*/
|
|
134
|
+
declare function countChars(
|
|
135
|
+
/** The input string. */
|
|
136
|
+
str: string,
|
|
137
|
+
/** Optional character to count. */
|
|
138
|
+
char?: string): number;
|
|
139
|
+
/**
|
|
140
|
+
* Counts the number of words in a string.
|
|
141
|
+
*
|
|
142
|
+
* @returns Number of words in the string.
|
|
143
|
+
*
|
|
144
|
+
* @example
|
|
145
|
+
* ```ts
|
|
146
|
+
* countWords("js utils kit"); // 3
|
|
147
|
+
* countWords(" js utils kit"); // 3
|
|
148
|
+
* countWords(""); // 0
|
|
149
|
+
* ```
|
|
150
|
+
*/
|
|
151
|
+
declare function countWords(
|
|
152
|
+
/** The input string */
|
|
153
|
+
str: string): number;
|
|
154
|
+
/**
|
|
155
|
+
* Counts how many times a substring occurs within a string.
|
|
156
|
+
*
|
|
157
|
+
* @remarks
|
|
158
|
+
* - Overlapping substrings are not double-counted.
|
|
159
|
+
* - Returns `0` if the substring is not found.
|
|
160
|
+
*
|
|
161
|
+
* @returns Number of times `sub` occurs in `str`.
|
|
162
|
+
*
|
|
163
|
+
* @example
|
|
164
|
+
* ```ts
|
|
165
|
+
* countSubstring("lololol", "lo"); // 3
|
|
166
|
+
* countSubstring("aaaaa", "a"); // 5
|
|
167
|
+
* countSubstring("hello", "z"); // 0
|
|
168
|
+
* ```
|
|
169
|
+
*/
|
|
170
|
+
declare function countSubstring(
|
|
171
|
+
/** The input string. */
|
|
172
|
+
str: string,
|
|
173
|
+
/** The substring to count. */
|
|
174
|
+
sub: string): number;
|
|
175
|
+
/**
|
|
176
|
+
* Builds a frequency map of characters in a string.
|
|
177
|
+
*
|
|
178
|
+
* @remarks
|
|
179
|
+
* - Returns an object where keys are characters and values are counts.
|
|
180
|
+
* - Case-sensitive by default.
|
|
181
|
+
*
|
|
182
|
+
* @returns A mapping of each character to its frequency.
|
|
183
|
+
*
|
|
184
|
+
* @example
|
|
185
|
+
* ```ts
|
|
186
|
+
* countFrequencies("banana");
|
|
187
|
+
* // { b: 1, a: 3, n: 2 }
|
|
188
|
+
*
|
|
189
|
+
* countFrequencies("");
|
|
190
|
+
* // {}
|
|
191
|
+
* ```
|
|
192
|
+
*/
|
|
193
|
+
declare function countFrequencies(
|
|
194
|
+
/** The input string. */
|
|
195
|
+
str: string): Record<string, number>;
|
|
196
|
+
/**
|
|
197
|
+
* Counts the number of lines in a string.
|
|
198
|
+
*
|
|
199
|
+
* @remarks
|
|
200
|
+
* - Handles Unix (`\n`), Windows (`\r\n`), and old Mac (`\r`) line endings.
|
|
201
|
+
*
|
|
202
|
+
* @returns Number of lines in the string.
|
|
203
|
+
*
|
|
204
|
+
* @example
|
|
205
|
+
* ```ts
|
|
206
|
+
* countLines("a\nb\nc"); // 3
|
|
207
|
+
* countLines("a\r\nb\r\nc"); // 3
|
|
208
|
+
* countLines("hello"); // 1
|
|
209
|
+
* ```
|
|
210
|
+
*/
|
|
211
|
+
declare function countLines(
|
|
212
|
+
/** The input string. */
|
|
213
|
+
str: string): number;
|
|
214
|
+
/**
|
|
215
|
+
* Returns an array of unique characters from a string.
|
|
216
|
+
*
|
|
217
|
+
* @remarks
|
|
218
|
+
* - Preserves order of first appearance.
|
|
219
|
+
*
|
|
220
|
+
* @returns Array of unique characters.
|
|
221
|
+
*
|
|
222
|
+
* @example
|
|
223
|
+
* ```ts
|
|
224
|
+
* uniqueChars("banana"); // ["b", "a", "n"]
|
|
225
|
+
* uniqueChars(""); // []
|
|
226
|
+
* ```
|
|
227
|
+
*/
|
|
228
|
+
declare function uniqueChars(
|
|
229
|
+
/** The input string. */
|
|
230
|
+
str: string): string[];
|
|
231
|
+
/**
|
|
232
|
+
* Finds the length of the longest word in a string.
|
|
233
|
+
*
|
|
234
|
+
* @remarks
|
|
235
|
+
* - Words are split by whitespace (`/\s+/`).
|
|
236
|
+
* - Symbols (punctuation, special chars) are removed using {@link stripSymbols}.
|
|
237
|
+
* - Returns `0` if the string is empty or contains no words.
|
|
238
|
+
*
|
|
239
|
+
* @returns Length of the longest word.
|
|
240
|
+
*
|
|
241
|
+
* @example
|
|
242
|
+
* ```ts
|
|
243
|
+
* longestWordLength("js utils kit"); // 5
|
|
244
|
+
* longestWordLength("short longerword mid"); // 10
|
|
245
|
+
* longestWordLength("hello"); // 5
|
|
246
|
+
* longestWordLength(""); // 0
|
|
247
|
+
* ```
|
|
248
|
+
*/
|
|
249
|
+
declare function longestWordLength(
|
|
250
|
+
/** The input string. */
|
|
251
|
+
str: string): number;
|
|
252
|
+
/**
|
|
253
|
+
* Finds the length of the shortest word in a string.
|
|
254
|
+
*
|
|
255
|
+
* @remarks
|
|
256
|
+
* - Words are split by whitespace (`/\s+/`).
|
|
257
|
+
* - Symbols (punctuation, special chars) are removed using {@link stripSymbols}.
|
|
258
|
+
* - Returns `0` if the string is empty or contains no words.
|
|
259
|
+
*
|
|
260
|
+
* @returns Length of the shortest word.
|
|
261
|
+
*
|
|
262
|
+
* @example
|
|
263
|
+
* ```ts
|
|
264
|
+
* shortestWordLength("js utils kit"); // 2
|
|
265
|
+
* shortestWordLength("one three five"); // 3
|
|
266
|
+
* shortestWordLength(""); // 0
|
|
267
|
+
* ```
|
|
268
|
+
*/
|
|
269
|
+
declare function shortestWordLength(
|
|
270
|
+
/** The input string. */
|
|
271
|
+
str: string): number;
|
|
272
|
+
/**
|
|
273
|
+
* Finds the longest word(s) in a string.
|
|
274
|
+
*
|
|
275
|
+
* @remarks
|
|
276
|
+
* - If only one longest word exists, returns it as a string.
|
|
277
|
+
* - If multiple longest words have the same length, returns them as an array.
|
|
278
|
+
* - Returns an empty string if there are no words.
|
|
279
|
+
*
|
|
280
|
+
* @returns The longest word as a string, or an array of tied words.
|
|
281
|
+
*
|
|
282
|
+
* @example
|
|
283
|
+
* ```ts
|
|
284
|
+
* longestWord("js utils kit"); // "utils"
|
|
285
|
+
* longestWord("short longerword mid"); // "longerword"
|
|
286
|
+
* longestWord("hello"); // "hello"
|
|
287
|
+
* longestWord(""); // ""
|
|
288
|
+
* ```
|
|
289
|
+
*/
|
|
290
|
+
declare function longestWord(
|
|
291
|
+
/** The input string. */
|
|
292
|
+
str: string): string | string[];
|
|
293
|
+
/**
|
|
294
|
+
* Finds the shortest word(s) in a string.
|
|
295
|
+
*
|
|
296
|
+
* @remarks
|
|
297
|
+
* - If only one shortest word exists, returns it as a string.
|
|
298
|
+
* - If multiple shortest words have the same length, returns them as an array.
|
|
299
|
+
* - Returns an empty string if there are no words.
|
|
300
|
+
*
|
|
301
|
+
* @returns The shortest word as a string, or an array of tied words.
|
|
302
|
+
*
|
|
303
|
+
* @example
|
|
304
|
+
* ```ts
|
|
305
|
+
* shortestWord("js utils kit"); // "js"
|
|
306
|
+
* shortestWord("one three five"); // "one"
|
|
307
|
+
* shortestWord("a ab abc abcd"); // "a"
|
|
308
|
+
* shortestWord(""); // ""
|
|
309
|
+
* ```
|
|
310
|
+
*/
|
|
311
|
+
declare function shortestWord(
|
|
312
|
+
/** The input string. */
|
|
313
|
+
str: string): string | string[];
|
|
314
|
+
|
|
91
315
|
/**
|
|
92
316
|
* Pads a string on the left with a specified character until it reaches the desired length.
|
|
93
317
|
* @param str - The string to pad.
|
|
@@ -120,19 +344,28 @@ declare function padLeft(str: string, length: number, char?: string): string;
|
|
|
120
344
|
declare function padRight(str: string, length: number, char?: string): string;
|
|
121
345
|
/**
|
|
122
346
|
* Truncates a string to a specified length, appending a suffix if the string is too long.
|
|
123
|
-
*
|
|
124
|
-
* @param length - The maximum length of the resulting string (including the suffix).
|
|
125
|
-
* @param suffix - The suffix to append if truncation occurs (defaults to "...").
|
|
347
|
+
*
|
|
126
348
|
* @returns The truncated string with the suffix if the original string exceeds the length, otherwise the original string.
|
|
349
|
+
*
|
|
127
350
|
* @example
|
|
128
351
|
* ```ts
|
|
129
352
|
* console.log(truncate("hello world", 8)); // "hello..."
|
|
130
353
|
* console.log(truncate("hi", 5)); // "hi"
|
|
131
|
-
* console.log(truncate("hello", 5, "...")); // "
|
|
354
|
+
* console.log(truncate("hello", 5, "...")); // "hello"
|
|
132
355
|
* console.log(truncate("", 3)); // ""
|
|
133
356
|
* ```
|
|
134
357
|
*/
|
|
135
|
-
declare function truncate(
|
|
358
|
+
declare function truncate(
|
|
359
|
+
/** The string to truncate */
|
|
360
|
+
str: string,
|
|
361
|
+
/** The maximum length of the resulting string */
|
|
362
|
+
length: number,
|
|
363
|
+
/**
|
|
364
|
+
* The suffix to append if truncation occurs
|
|
365
|
+
*
|
|
366
|
+
* @default "..."
|
|
367
|
+
*/
|
|
368
|
+
suffix?: string): string;
|
|
136
369
|
/**
|
|
137
370
|
* Repeats a string a specified number of times.
|
|
138
371
|
* @param str - The string to repeat.
|
|
@@ -147,6 +380,33 @@ declare function truncate(str: string, length: number, suffix?: string): string;
|
|
|
147
380
|
* ```
|
|
148
381
|
*/
|
|
149
382
|
declare function repeatString(str: string, count: number): string;
|
|
383
|
+
/**
|
|
384
|
+
* Removes or replaces common symbol characters from a string.
|
|
385
|
+
*
|
|
386
|
+
* @remarks
|
|
387
|
+
* - Strips symbols like `- _ @ ! $ % ^ & # * ( ) + = , . ; : ' " < > ? / \ | [ ] { }`.
|
|
388
|
+
* - Keeps letters, numbers, and spaces intact.
|
|
389
|
+
* - By default, removes symbols (replaces with `""`).
|
|
390
|
+
*
|
|
391
|
+
* @returns A new string with symbols removed or replaced.
|
|
392
|
+
*
|
|
393
|
+
* @example
|
|
394
|
+
* ```ts
|
|
395
|
+
* stripSymbols("hello-world!"); // "helloworld"
|
|
396
|
+
* stripSymbols("hello-world!", " "); // "hello world "
|
|
397
|
+
* stripSymbols("user_name@test", "_"); // "user_nametest"
|
|
398
|
+
* stripSymbols("symbols-only!!!", "*"); // "symbols-only***"
|
|
399
|
+
* ```
|
|
400
|
+
*/
|
|
401
|
+
declare function stripSymbols(
|
|
402
|
+
/** The input string */
|
|
403
|
+
str: string,
|
|
404
|
+
/**
|
|
405
|
+
* Optional replacement string for removed symbols.
|
|
406
|
+
*
|
|
407
|
+
* @default ""
|
|
408
|
+
*/
|
|
409
|
+
replacement?: string): string;
|
|
150
410
|
|
|
151
411
|
/**
|
|
152
412
|
* Determines whether the provided value is a string.
|
|
@@ -387,10 +647,22 @@ declare function isLowerCase(value: string): boolean;
|
|
|
387
647
|
|
|
388
648
|
declare const _default: {
|
|
389
649
|
capitalize: typeof capitalize;
|
|
650
|
+
splitString: typeof splitString;
|
|
651
|
+
countChars: typeof countChars;
|
|
652
|
+
countWords: typeof countWords;
|
|
653
|
+
countSubstring: typeof countSubstring;
|
|
654
|
+
countFrequencies: typeof countFrequencies;
|
|
655
|
+
countLines: typeof countLines;
|
|
656
|
+
uniqueChars: typeof uniqueChars;
|
|
657
|
+
longestWordLength: typeof longestWordLength;
|
|
658
|
+
shortestWordLength: typeof shortestWordLength;
|
|
659
|
+
longestWord: typeof longestWord;
|
|
660
|
+
shortestWord: typeof shortestWord;
|
|
390
661
|
padLeft: typeof padLeft;
|
|
391
662
|
padRight: typeof padRight;
|
|
392
663
|
truncate: typeof truncate;
|
|
393
664
|
repeatString: typeof repeatString;
|
|
665
|
+
stripSymbols: typeof stripSymbols;
|
|
394
666
|
trim: Trim;
|
|
395
667
|
isString: typeof isString;
|
|
396
668
|
isNonEmptyString: typeof isNonEmptyString;
|
|
@@ -409,5 +681,5 @@ declare const _default: {
|
|
|
409
681
|
isLowerCase: typeof isLowerCase;
|
|
410
682
|
};
|
|
411
683
|
|
|
412
|
-
export { capitalize, containsWhitespace, _default as default, endsWithPunctuation, isAlphabetic, isCamelCase, isEmail, isKebabCase, isLowerCase, isNonEmptyString, isNumericString, isPascalCase, isSnakeCase, isString, isURL, isUpperCase, padLeft, padRight, repeatString, startsWithUppercase, trim, truncate };
|
|
684
|
+
export { capitalize, containsWhitespace, countChars, countFrequencies, countLines, countSubstring, countWords, _default as default, endsWithPunctuation, isAlphabetic, isCamelCase, isEmail, isKebabCase, isLowerCase, isNonEmptyString, isNumericString, isPascalCase, isSnakeCase, isString, isURL, isUpperCase, longestWord, longestWordLength, padLeft, padRight, repeatString, shortestWord, shortestWordLength, splitString, startsWithUppercase, stripSymbols, trim, truncate, uniqueChars };
|
|
413
685
|
export type { Trim };
|
package/dist/string/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
function t(t){return t.replace(/^\w/,t=>t.toUpperCase())}function n(t,n,e=" "){return t.padStart(n,e)}function e(t,n,e=" "){return t.padEnd(n,e)}function r(t,n,e="..."){return t.length
|
|
1
|
+
function t(t){return t.replace(/^\w/,t=>t.toUpperCase())}function n(t,n,e=" "){return t.padStart(n,e)}function e(t,n,e=" "){return t.padEnd(n,e)}function r(t,n,e="..."){return n<=0?"":t.length<=n?t:e.length>=n?e.slice(0,n):t.trim().slice(0,n)+e}function i(t,n){return t.repeat(n)}function u(t,n=""){return t.replace(/[^\p{L}\p{N}\s]/gu,n)}function s(t,n=/\s+/){return t.split(n)}function o(t,n){return n?[...t].filter(t=>t===n).length:s(t).length}function a(t){return s(t.trim()).length}function c(t,n){return 0===n.length?0:s(t,n).length-1}function l(t){return[...t].reduce((t,n)=>(t[n]=(t[n]||0)+1,t),{})}function f(t){return t?s(t,/\r\n|\r|\n/).length:0}function h(t){return[...new Set(t)]}function g(t){const n=s(t).map(t=>u(t)).filter(Boolean);return 0===n.length?0:n.reduce((t,n)=>Math.max(t,n.length),0)}function p(t){const n=s(t.trim()).map(t=>u(t)).filter(Boolean);return 0===n.length?0:n.reduce((t,n)=>Math.min(t,n.length),1/0)}function m(t){const n=s(t.trim()).map(t=>u(t)).filter(Boolean);if(0===n.length)return"";const e=Math.max(...n.map(t=>t.length)),r=n.filter(t=>t.length===e),i=[...new Set(r)];return 1===i.length?i[0]:i}function d(t){const n=s(t.trim()).map(t=>u(t)).filter(Boolean);if(0===n.length)return"";const e=Math.min(...n.map(t=>t.length)),r=n.filter(t=>t.length===e),i=[...new Set(r)];return 1===i.length?i[0]:i}const S={function:t=>t.trim(),start:t=>t.trimStart(),end:t=>t.trimEnd(),normalizeWhitespace:t=>t.trim().replace(/\s+/g," ")};function z(t){return null!=t&&"string"==typeof t}function C(t,n=!0){return!!z(t)&&("string"==typeof t&&(n?t.trim().length>0:t.length>0))}function W(t){try{return new URL(t),!0}catch{return!1}}function $(t){return/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(t)}function L(t){return/^[a-zA-Z]+$/.test(t)}function N(t){return!isNaN(Number(t))&&!isNaN(parseFloat(t))}function A(t){return/^[A-Z]/.test(t)}function b(t){return/[\p{P}]$/u.test(t)}function w(t){return/\s/.test(t)}function Z(t){return/^[a-z][a-zA-Z0-9]*$/.test(t)}function y(t){return/^[A-Z][a-zA-Z0-9]*$/.test(t)}function U(t){return/^[a-z0-9]+(-[a-z0-9]+)*$/.test(t)}function B(t){return/^[a-z0-9]+(_[a-z0-9]+)*$/.test(t)}function E(t){return/^[A-Z]+$/.test(t)}function M(t){return/^[a-z]+$/.test(t)}var x={capitalize:t,splitString:s,countChars:o,countWords:a,countSubstring:c,countFrequencies:l,countLines:f,uniqueChars:h,longestWordLength:g,shortestWordLength:p,longestWord:m,shortestWord:d,padLeft:n,padRight:e,truncate:r,repeatString:i,stripSymbols:u,trim:S,isString:z,isNonEmptyString:C,isURL:W,isEmail:$,isAlphabetic:L,isNumericString:N,startsWithUppercase:A,endsWithPunctuation:b,containsWhitespace:w,isCamelCase:Z,isPascalCase:y,isKebabCase:U,isSnakeCase:B,isUpperCase:E,isLowerCase:M};export{t as capitalize,w as containsWhitespace,o as countChars,l as countFrequencies,f as countLines,c as countSubstring,a as countWords,x as default,b as endsWithPunctuation,L as isAlphabetic,Z as isCamelCase,$ as isEmail,U as isKebabCase,M as isLowerCase,C as isNonEmptyString,N as isNumericString,y as isPascalCase,B as isSnakeCase,z as isString,W as isURL,E as isUpperCase,m as longestWord,g as longestWordLength,n as padLeft,e as padRight,i as repeatString,d as shortestWord,p as shortestWordLength,s as splitString,A as startsWithUppercase,u as stripSymbols,S as trim,r as truncate,h as uniqueChars};
|