@asaidimu/utils-strings 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.md ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Saidimu
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/README.md ADDED
@@ -0,0 +1,195 @@
1
+ # `@asaidimu/utils-strings`
2
+
3
+ ![TypeScript](https://img.shields.io/badge/typescript-%23007ACC.svg?style=for-the-badge&logo=typescript&logoColor=white)
4
+ ![MIT License](https://img.shields.io/badge/License-MIT-yellow.svg)
5
+ ![npm version](https://img.shields.io/npm/v/@asaidimu/utils-strings?style=for-the-badge)
6
+
7
+ A comprehensive utility library providing static methods for robust string manipulation, validation, formatting, and UI-specific transformations.
8
+
9
+ ## Table of Contents
10
+
11
+ - [Features](#features)
12
+ - [Technology Stack](#technology-stack)
13
+ - [Installation](#installation)
14
+ - [Quick Start](#quick-start)
15
+ - [Usage](#usage)
16
+ - [Case Conversion](#case-conversion)
17
+ - [Validation & Checks](#validation--checks)
18
+ - [Manipulation](#manipulation)
19
+ - [UI & Localization Formatting](#ui--localization-formatting)
20
+ - [Hashing](#hashing)
21
+ - [Configuration](#configuration)
22
+ - [Development](#development)
23
+ - [Testing](#testing)
24
+ - [Contributing](#contributing)
25
+ - [License](#license)
26
+
27
+ `@asaidimu/utils-strings` is a TypeScript-first utility library designed to be a foundational toolkit for applications requiring robust string handling, from data processing to user interface rendering. It centralizes common string operations, ensuring consistency and reducing boilerplate across projects.
28
+
29
+ This library provides a single, well-tested, and comprehensive `String` class with static methods to address a wide array of common string-related tasks. By offering a consistent API for these operations, it aims to improve code quality, enhance developer efficiency, and ensure predictable string behavior across applications.
30
+
31
+ ## Features
32
+
33
+ `@asaidimu/utils-strings` offers a rich set of features, including:
34
+
35
+ - **Case Conversion:** Convert strings to uppercase, lowercase, capitalize, camelCase, kebab-case, snake_case, PascalCase, sentence case, and title case.
36
+ - **Validation & Checks:** Check for null, undefined, empty, or whitespace-only strings; verify if a string starts with, ends with, or contains a substring; and validate if a string consists purely of numeric or alphabetic characters.
37
+ - **Manipulation:** Trim whitespace, truncate strings with an ellipsis, reverse strings, replace all occurrences of a substring, remove diacritical marks (accents), and slugify strings for URL-friendly formats.
38
+ - **UI & Localization Formatting:** Handle basic pluralization logic, escape HTML special characters to prevent XSS, and format numbers as currency for localized display.
39
+ - **Hashing:** Generate short, deterministic hashes for strings.
40
+
41
+ ## Technology Stack
42
+
43
+ This library is built with:
44
+
45
+ - **TypeScript**: Ensures type safety and improves developer experience.
46
+ - **JavaScript**: The compiled output is fully compatible with JavaScript environments.
47
+
48
+ ## Installation
49
+
50
+ You can install `@asaidimu/utils-strings` using npm or yarn:
51
+
52
+ ```bash
53
+ npm install @asaidimu/utils-strings
54
+ # or
55
+ yarn add @asaidimu/utils-strings
56
+ ```
57
+
58
+ ## Quick Start
59
+
60
+ Once installed, you can import and use the `String` class directly:
61
+
62
+ ```typescript
63
+ import { String } from "@asaidimu/utils-strings";
64
+
65
+ // Basic Usage
66
+ console.log(String.capitalize("hello world")); // Output: "Hello world"
67
+ console.log(String.kebabCase("HelloWorld")); // Output: "hello-world"
68
+ console.log(String.isEmpty("")); // Output: true
69
+ console.log(String.formatCurrency(123.45, "en-US", "USD")); // Output: "$123.45"
70
+
71
+ // More examples
72
+ const text = " Crème Brûlée Dessert ";
73
+ console.log(String.slugify(text)); // Output: "creme-brulee-dessert"
74
+ console.log(String.escapeHtml(`<script>alert('xss')</script>`)); // Output: "&lt;script&gt;alert(&#39;xss&#39;)&lt;/script&gt;"
75
+ ```
76
+
77
+ ## Usage
78
+
79
+ Here are detailed examples of the various utility methods available in `String`.
80
+
81
+ ### Case Conversion
82
+
83
+ ```typescript
84
+ import { String } from "@asaidimu/utils-strings";
85
+
86
+ String.uppercase("hello"); // Returns "HELLO"
87
+ String.lowercase("HELLO"); // Returns "hello"
88
+ String.capitalize("hello world"); // Returns "Hello world"
89
+ String.camelCase("foo bar baz"); // Returns "fooBarBaz"
90
+ String.kebabCase("fooBarBaz"); // Returns "foo-bar-baz"
91
+ String.snakeCase("fooBarBaz"); // Returns "foo_bar_baz"
92
+ String.pascalCase("hello world"); // Returns "HelloWorld"
93
+ String.toSentenceCase("hello WORLD"); // Returns "Hello world"
94
+ String.toTitleCase("the quick brown fox"); // Returns "The Quick Brown Fox"
95
+ ```
96
+
97
+ ### Validation & Checks
98
+
99
+ ```typescript
100
+ import { String } from "@asaidimu/utils-strings";
101
+
102
+ String.isEmpty(null); // Returns true
103
+ String.isWhitespace(" "); // Returns true
104
+ String.startsWith("hello world", "hello"); // Returns true
105
+ String.endsWith("hello world", "world"); // Returns true
106
+ String.contains("hello world", "lo w"); // Returns true
107
+ String.isNumeric("123"); // Returns true
108
+ String.isAlpha("abc"); // Returns true
109
+ ```
110
+
111
+ ### Manipulation
112
+
113
+ ```typescript
114
+ import { String } from "@asaidimu/utils-strings";
115
+
116
+ String.trim(" hello "); // Returns "hello"
117
+ String.truncate("Long text example", 10); // Returns "Long text..."
118
+ String.reverse("hello"); // Returns "olleh"
119
+ String.replaceAll("banana", "a", "o"); // Returns "bonono"
120
+ String.removeAccents("crème brûlée"); // Returns "creme brulee"
121
+ String.slugify("Hello World! This is a test."); // Returns "hello-world-this-is-a-test"
122
+ ```
123
+
124
+ ### UI & Localization Formatting
125
+
126
+ ```typescript
127
+ import { String } from "@asaidimu/utils-strings";
128
+
129
+ String.pluralize(1, "item"); // Returns "item"
130
+ String.pluralize(2, "item"); // Returns "items"
131
+ String.escapeHtml(`<div>content</div>`); // Returns "&lt;div&gt;content&lt;/div&gt;"
132
+ String.formatCurrency(1234.5, "de-DE", "EUR"); // Returns "1.234,50 €"
133
+ ```
134
+
135
+ ### Hashing
136
+
137
+ ```typescript
138
+ import { String } from "@asaidimu/utils-strings";
139
+
140
+ String.hash("my-secret-string"); // Returns a short, deterministic hash like "03a7jk"
141
+ String.hash("another-string", 8); // Returns a hash of length 8
142
+ ```
143
+
144
+ ## Configuration
145
+
146
+ This library currently does not require any external configuration.
147
+
148
+ ## Development
149
+
150
+ To set up the project for local development, follow these steps:
151
+
152
+ 1. **Clone the repository:**
153
+
154
+ ```bash
155
+ git clone https://github.com/asaidimu/erp-utils.git
156
+ cd erp-utils/src/strings
157
+ ```
158
+
159
+ 2. **Install dependencies:**
160
+
161
+ ```bash
162
+ bun install
163
+ # or
164
+ yarn install
165
+ ```
166
+
167
+ 3. **Build the project (if needed):**
168
+ _(No specific build script provided, typically handled by consumer or pre-publish)_
169
+
170
+ ## Testing
171
+
172
+ Tests are run using [Vitest](https://vitest.dev/).
173
+
174
+ - **Run all tests:**
175
+ ```bash
176
+ bun test
177
+ ```
178
+ - **Run tests in watch mode:**
179
+ ```bash
180
+ bun run test:watch
181
+ ```
182
+ - **Run tests in a browser environment:**
183
+ ```bash
184
+ bun run test:browser
185
+ ```
186
+
187
+ ## Contributing
188
+
189
+ Contributions are welcome! Please refer to the main repository for guidelines on how to contribute:
190
+
191
+ [https://github.com/asaidimu/erp-utils](https://github.com/asaidimu/erp-utils)
192
+
193
+ ## License
194
+
195
+ This project is licensed under the MIT License - see the `LICENSE` file for details.
package/index.d.mts ADDED
@@ -0,0 +1,272 @@
1
+ /**
2
+ * @class StringUtils
3
+ * @description A comprehensive utility class providing static methods for string manipulation, validation,
4
+ * formatting, and UI-specific transformations. Designed to be a foundational toolkit for applications
5
+ * requiring robust string handling, from data processing to user interface rendering.
6
+ */
7
+ declare class StringUtils {
8
+ /**
9
+ * Converts a string to uppercase.
10
+ * @param s The input string.
11
+ * @returns The uppercase version of the string.
12
+ * @example
13
+ * StringUtils.uppercase("hello"); // Returns "HELLO"
14
+ */
15
+ static uppercase(s: string): string;
16
+ /**
17
+ * Converts a string to lowercase.
18
+ * @param s The input string.
19
+ * @returns The lowercase version of the string.
20
+ * @example
21
+ * StringUtils.lowercase("HELLO"); // Returns "hello"
22
+ */
23
+ static lowercase(s: string): string;
24
+ /**
25
+ * Capitalizes the first letter of a string. If a separator is provided,
26
+ * it splits the string by the separator, capitalizes each word, and then joins them back.
27
+ * @param s The input string.
28
+ * @param separator Optional. The string to use as a word separator for multi-word capitalization.
29
+ * @returns The capitalized string.
30
+ * @example
31
+ * StringUtils.capitalize("hello world"); // Returns "Hello world"
32
+ * StringUtils.capitalize("hello world", " "); // Returns "Hello World"
33
+ * StringUtils.capitalize("foo-bar", "-"); // Returns "Foo-Bar"
34
+ */
35
+ static capitalize(s: string, separator?: string): string;
36
+ /**
37
+ * Generates a short, deterministic hash of a string.
38
+ */
39
+ static hash(s: string, length?: number): string;
40
+ /**
41
+ * Checks if a string is null, undefined, or an empty string.
42
+ * @param s The input string, which can be null or undefined.
43
+ * @returns True if the string is null, undefined, or empty; otherwise, false.
44
+ * @example
45
+ * StringUtils.isEmpty(null); // Returns true
46
+ * StringUtils.isEmpty(""); // Returns true
47
+ * StringUtils.isEmpty("abc"); // Returns false
48
+ */
49
+ static isEmpty(s: string | null | undefined): boolean;
50
+ /**
51
+ * Checks if a string is null, undefined, empty, or consists only of whitespace characters.
52
+ * @param s The input string, which can be null or undefined.
53
+ * @returns True if the string is null, undefined, empty, or only whitespace; otherwise, false.
54
+ * @example
55
+ * StringUtils.isWhitespace(null); // Returns true
56
+ * StringUtils.isWhitespace(" "); // Returns true
57
+ * StringUtils.isWhitespace(" abc "); // Returns false
58
+ */
59
+ static isWhitespace(s: string | null | undefined): boolean;
60
+ /**
61
+ * Removes whitespace from both ends of a string.
62
+ * @param s The input string.
63
+ * @returns The string with leading and trailing whitespace removed.
64
+ * @example
65
+ * StringUtils.trim(" hello world "); // Returns "hello world"
66
+ */
67
+ static trim(s: string): string;
68
+ /**
69
+ * Truncates a string to a specified maximum length. If the string exceeds the length,
70
+ * it appends an ellipsis (or custom string) at the end.
71
+ * @param s The input string.
72
+ * @param maxLength The maximum length of the output string (including ellipsis).
73
+ * @param ellipsis Optional. The string to append if truncation occurs. Defaults to "...".
74
+ * @returns The truncated string.
75
+ * @example
76
+ * StringUtils.truncate("Long text example", 10); // Returns "Long text..."
77
+ * StringUtils.truncate("Short text", 10); // Returns "Short text"
78
+ * StringUtils.truncate("Very long string", 8, "..."); // Returns "Very lo..."
79
+ */
80
+ static truncate(s: string, maxLength: number, ellipsis?: string): string;
81
+ /**
82
+ * Checks if a string begins with the specified prefix.
83
+ * @param s The input string.
84
+ * @param prefix The prefix to search for.
85
+ * @returns True if the string starts with the prefix; otherwise, false.
86
+ * @example
87
+ * StringUtils.startsWith("hello world", "hello"); // Returns true
88
+ * StringUtils.startsWith("hello world", "world"); // Returns false
89
+ */
90
+ static startsWith(s: string, prefix: string): boolean;
91
+ /**
92
+ * Checks if a string ends with the specified suffix.
93
+ * @param s The input string.
94
+ * @param suffix The suffix to search for.
95
+ * @returns True if the string ends with the suffix; otherwise, false.
96
+ * @example
97
+ * StringUtils.endsWith("hello world", "world"); // Returns true
98
+ * StringUtils.endsWith("hello world", "hello"); // Returns false
99
+ */
100
+ static endsWith(s: string, suffix: string): boolean;
101
+ /**
102
+ * Checks if a string contains the specified substring.
103
+ * @param s The input string.
104
+ * @param sub The substring to search for.
105
+ * @returns True if the string contains the substring; otherwise, false.
106
+ * @example
107
+ * StringUtils.contains("hello world", "lo w"); // Returns true
108
+ * StringUtils.contains("hello world", "xyz"); // Returns false
109
+ */
110
+ static contains(s: string, sub: string): boolean;
111
+ /**
112
+ * Checks if a string consists only of numeric digits.
113
+ * @param s The input string.
114
+ * @returns True if the string contains only digits; otherwise, false.
115
+ * @example
116
+ * StringUtils.isNumeric("123"); // Returns true
117
+ * StringUtils.isNumeric("123a"); // Returns false
118
+ * StringUtils.isNumeric(""); // Returns false
119
+ */
120
+ static isNumeric(s: string): boolean;
121
+ /**
122
+ * Checks if a string consists only of alphabetic characters (a-z, A-Z).
123
+ * @param s The input string.
124
+ * @returns True if the string contains only alphabetic characters; otherwise, false.
125
+ * @example
126
+ * StringUtils.isAlpha("abc"); // Returns true
127
+ * StringUtils.isAlpha("abc1"); // Returns false
128
+ * StringUtils.isAlpha(""); // Returns false
129
+ */
130
+ static isAlpha(s: string): boolean;
131
+ /**
132
+ * Converts a string to camelCase.
133
+ * Handles spaces, hyphens, and underscores.
134
+ * @param s The input string.
135
+ * @returns The camelCase version of the string.
136
+ * @example
137
+ * StringUtils.camelCase("hello world"); // Returns "helloWorld"
138
+ * StringUtils.camelCase("foo-bar-baz"); // Returns "fooBarBaz"
139
+ * StringUtils.camelCase("foo_bar_baz"); // Returns "fooBarBaz"
140
+ */
141
+ static camelCase(s: string): string;
142
+ /**
143
+ * Converts a string to kebab-case.
144
+ * Handles spaces, camelCase, and underscores.
145
+ * @param s The input string.
146
+ * @returns The kebab-case version of the string.
147
+ * @example
148
+ * StringUtils.kebabCase("hello world"); // Returns "hello-world"
149
+ * StringUtils.kebabCase("fooBarBaz"); // Returns "foo-bar-baz"
150
+ * StringUtils.kebabCase("Foo_Bar-Baz"); // Returns "foo-bar-baz"
151
+ */
152
+ static kebabCase(s: string): string;
153
+ /**
154
+ * Converts a string to snake_case.
155
+ * Handles spaces, camelCase, and hyphens.
156
+ * @param s The input string.
157
+ * @returns The snake_case version of the string.
158
+ * @example
159
+ * StringUtils.snakeCase("hello world"); // Returns "hello_world"
160
+ * StringUtils.snakeCase("fooBarBaz"); // Returns "foo_bar_baz"
161
+ * StringUtils.snakeCase("Foo-Bar_Baz"); // Returns "foo_bar_baz"
162
+ */
163
+ static snakeCase(s: string): string;
164
+ /**
165
+ * Converts a string to PascalCase.
166
+ * Handles spaces, hyphens, and underscores.
167
+ * @param s The input string.
168
+ * @returns The PascalCase version of the string.
169
+ * @example
170
+ * StringUtils.pascalCase("hello world"); // Returns "HelloWorld"
171
+ * StringUtils.pascalCase("foo-bar-baz"); // Returns "FooBarBaz"
172
+ * StringUtils.pascalCase("foo_bar_baz"); // Returns "FooBarBaz"
173
+ */
174
+ static pascalCase(s: string): string;
175
+ /**
176
+ * Reverses the order of characters in a string.
177
+ * @param s The input string.
178
+ * @returns The reversed string.
179
+ * @example
180
+ * StringUtils.reverse("hello"); // Returns "olleh"
181
+ */
182
+ static reverse(s: string): string;
183
+ /**
184
+ * Replaces all occurrences of a specified substring with another string.
185
+ * @param s The input string.
186
+ * @param search The substring to search for.
187
+ * @param replacement The string to replace with.
188
+ * @returns The string with all occurrences replaced.
189
+ * @example
190
+ * StringUtils.replaceAll("banana", "a", "o"); // Returns "bonono"
191
+ */
192
+ static replaceAll(s: string, search: string, replacement: string): string;
193
+ /**
194
+ * Removes diacritical marks (accents) from a string.
195
+ * @param s The input string.
196
+ * @returns The string with accents removed.
197
+ * @example
198
+ * StringUtils.removeAccents("crème brûlée"); // Returns "creme brulee"
199
+ */
200
+ static removeAccents(s: string): string;
201
+ /**
202
+ * Converts a string to a URL-friendly slug.
203
+ * Removes accents, converts to lowercase, trims, replaces non-alphanumeric characters with hyphens,
204
+ * and consolidates multiple hyphens.
205
+ * @param s The input string.
206
+ * @returns The slugified string.
207
+ * @example
208
+ * StringUtils.slugify("Hello World! This is a test."); // Returns "hello-world-this-is-a-test"
209
+ * StringUtils.slugify("Crème brûlée"); // Returns "creme-brulee"
210
+ */
211
+ static slugify(s: string): string;
212
+ /**
213
+ * Capitalizes only the first letter of the string and converts the rest to lowercase.
214
+ * Useful for UI elements like headers and labels where proper sentence casing is required.
215
+ * @param s The input string.
216
+ * @returns The string in sentence case.
217
+ * @example
218
+ * StringUtils.toSentenceCase("hello WORLD"); // Returns "Hello world"
219
+ * StringUtils.toSentenceCase("a quick BROWN fox"); // Returns "A quick brown fox"
220
+ */
221
+ static toSentenceCase(s: string): string;
222
+ /**
223
+ * Capitalizes every word in a string, handling camelCase and special characters gracefully.
224
+ * Ensures consistent capitalization for UI components like card titles, buttons, or navigation items.
225
+ * @param s The input string.
226
+ * @returns The string in title case.
227
+ * @example
228
+ * StringUtils.toTitleCase("hello world"); // Returns "Hello World"
229
+ * StringUtils.toTitleCase("some_variable_name"); // Returns "Some Variable Name"
230
+ * StringUtils.toTitleCase("yetAnotherString"); // Returns "Yet Another String"
231
+ * StringUtils.toTitleCase("the quick brown fox"); // Returns "The Quick Brown Fox"
232
+ */
233
+ static toTitleCase(s: string): string;
234
+ /**
235
+ * Handles basic pluralization logic for displaying counts, e.g., "1 item" vs "2 items".
236
+ * @param count The number of items.
237
+ * @param singular The singular form of the word (e.g., "item", "person").
238
+ * @param plural Optional. The plural form of the word. If not provided, it defaults to appending 's'.
239
+ * @returns The appropriate singular or plural form of the word based on the count.
240
+ * @example
241
+ * StringUtils.pluralize(1, "item"); // Returns "item"
242
+ * StringUtils.pluralize(2, "item"); // Returns "items"
243
+ * StringUtils.pluralize(0, "item"); // Returns "items"
244
+ * StringUtils.pluralize(1, "sheep", "sheep"); // Returns "sheep"
245
+ * StringUtils.pluralize(2, "sheep", "sheep"); // Returns "sheep"
246
+ */
247
+ static pluralize(count: number, singular: string, plural?: string): string;
248
+ /**
249
+ * Prevents Cross-Site Scripting (XSS) by escaping HTML special characters in a string.
250
+ * Converts characters like '<', '>', '&', '"', "'" into their corresponding HTML entities.
251
+ * Essential for safely rendering user-generated content or dynamic text directly into the DOM.
252
+ * @param s The input string.
253
+ * @returns The HTML-escaped string.
254
+ * @example
255
+ * StringUtils.escapeHtml(`"<script>alert('xss')</script>"`); // Returns "&quot;&lt;script&gt;alert(&#39;xss&#39;)&lt;/script&gt;&quot;`
256
+ */
257
+ static escapeHtml(s: string): string;
258
+ /**
259
+ * Formats a number as currency for localized display, using `Intl.NumberFormat`.
260
+ * @param amount The number to format.
261
+ * @param locale Optional. The locale string (e.g., "en-US", "de-DE"). Defaults to "en-US".
262
+ * @param currency Optional. The currency code (e.g., "USD", "EUR"). Defaults to "USD".
263
+ * @returns The formatted currency string.
264
+ * @example
265
+ * StringUtils.formatCurrency(1234.50, "en-US", "USD"); // Returns "$1,234.50"
266
+ * StringUtils.formatCurrency(1234.50, "de-DE", "EUR"); // Returns "1.234,50 €"
267
+ * StringUtils.formatCurrency(5678, "ja-JP", "JPY"); // Returns "¥5,678"
268
+ */
269
+ static formatCurrency(amount: number, locale?: string, currency?: string): string;
270
+ }
271
+
272
+ export { StringUtils };
package/index.d.ts ADDED
@@ -0,0 +1,272 @@
1
+ /**
2
+ * @class StringUtils
3
+ * @description A comprehensive utility class providing static methods for string manipulation, validation,
4
+ * formatting, and UI-specific transformations. Designed to be a foundational toolkit for applications
5
+ * requiring robust string handling, from data processing to user interface rendering.
6
+ */
7
+ declare class StringUtils {
8
+ /**
9
+ * Converts a string to uppercase.
10
+ * @param s The input string.
11
+ * @returns The uppercase version of the string.
12
+ * @example
13
+ * StringUtils.uppercase("hello"); // Returns "HELLO"
14
+ */
15
+ static uppercase(s: string): string;
16
+ /**
17
+ * Converts a string to lowercase.
18
+ * @param s The input string.
19
+ * @returns The lowercase version of the string.
20
+ * @example
21
+ * StringUtils.lowercase("HELLO"); // Returns "hello"
22
+ */
23
+ static lowercase(s: string): string;
24
+ /**
25
+ * Capitalizes the first letter of a string. If a separator is provided,
26
+ * it splits the string by the separator, capitalizes each word, and then joins them back.
27
+ * @param s The input string.
28
+ * @param separator Optional. The string to use as a word separator for multi-word capitalization.
29
+ * @returns The capitalized string.
30
+ * @example
31
+ * StringUtils.capitalize("hello world"); // Returns "Hello world"
32
+ * StringUtils.capitalize("hello world", " "); // Returns "Hello World"
33
+ * StringUtils.capitalize("foo-bar", "-"); // Returns "Foo-Bar"
34
+ */
35
+ static capitalize(s: string, separator?: string): string;
36
+ /**
37
+ * Generates a short, deterministic hash of a string.
38
+ */
39
+ static hash(s: string, length?: number): string;
40
+ /**
41
+ * Checks if a string is null, undefined, or an empty string.
42
+ * @param s The input string, which can be null or undefined.
43
+ * @returns True if the string is null, undefined, or empty; otherwise, false.
44
+ * @example
45
+ * StringUtils.isEmpty(null); // Returns true
46
+ * StringUtils.isEmpty(""); // Returns true
47
+ * StringUtils.isEmpty("abc"); // Returns false
48
+ */
49
+ static isEmpty(s: string | null | undefined): boolean;
50
+ /**
51
+ * Checks if a string is null, undefined, empty, or consists only of whitespace characters.
52
+ * @param s The input string, which can be null or undefined.
53
+ * @returns True if the string is null, undefined, empty, or only whitespace; otherwise, false.
54
+ * @example
55
+ * StringUtils.isWhitespace(null); // Returns true
56
+ * StringUtils.isWhitespace(" "); // Returns true
57
+ * StringUtils.isWhitespace(" abc "); // Returns false
58
+ */
59
+ static isWhitespace(s: string | null | undefined): boolean;
60
+ /**
61
+ * Removes whitespace from both ends of a string.
62
+ * @param s The input string.
63
+ * @returns The string with leading and trailing whitespace removed.
64
+ * @example
65
+ * StringUtils.trim(" hello world "); // Returns "hello world"
66
+ */
67
+ static trim(s: string): string;
68
+ /**
69
+ * Truncates a string to a specified maximum length. If the string exceeds the length,
70
+ * it appends an ellipsis (or custom string) at the end.
71
+ * @param s The input string.
72
+ * @param maxLength The maximum length of the output string (including ellipsis).
73
+ * @param ellipsis Optional. The string to append if truncation occurs. Defaults to "...".
74
+ * @returns The truncated string.
75
+ * @example
76
+ * StringUtils.truncate("Long text example", 10); // Returns "Long text..."
77
+ * StringUtils.truncate("Short text", 10); // Returns "Short text"
78
+ * StringUtils.truncate("Very long string", 8, "..."); // Returns "Very lo..."
79
+ */
80
+ static truncate(s: string, maxLength: number, ellipsis?: string): string;
81
+ /**
82
+ * Checks if a string begins with the specified prefix.
83
+ * @param s The input string.
84
+ * @param prefix The prefix to search for.
85
+ * @returns True if the string starts with the prefix; otherwise, false.
86
+ * @example
87
+ * StringUtils.startsWith("hello world", "hello"); // Returns true
88
+ * StringUtils.startsWith("hello world", "world"); // Returns false
89
+ */
90
+ static startsWith(s: string, prefix: string): boolean;
91
+ /**
92
+ * Checks if a string ends with the specified suffix.
93
+ * @param s The input string.
94
+ * @param suffix The suffix to search for.
95
+ * @returns True if the string ends with the suffix; otherwise, false.
96
+ * @example
97
+ * StringUtils.endsWith("hello world", "world"); // Returns true
98
+ * StringUtils.endsWith("hello world", "hello"); // Returns false
99
+ */
100
+ static endsWith(s: string, suffix: string): boolean;
101
+ /**
102
+ * Checks if a string contains the specified substring.
103
+ * @param s The input string.
104
+ * @param sub The substring to search for.
105
+ * @returns True if the string contains the substring; otherwise, false.
106
+ * @example
107
+ * StringUtils.contains("hello world", "lo w"); // Returns true
108
+ * StringUtils.contains("hello world", "xyz"); // Returns false
109
+ */
110
+ static contains(s: string, sub: string): boolean;
111
+ /**
112
+ * Checks if a string consists only of numeric digits.
113
+ * @param s The input string.
114
+ * @returns True if the string contains only digits; otherwise, false.
115
+ * @example
116
+ * StringUtils.isNumeric("123"); // Returns true
117
+ * StringUtils.isNumeric("123a"); // Returns false
118
+ * StringUtils.isNumeric(""); // Returns false
119
+ */
120
+ static isNumeric(s: string): boolean;
121
+ /**
122
+ * Checks if a string consists only of alphabetic characters (a-z, A-Z).
123
+ * @param s The input string.
124
+ * @returns True if the string contains only alphabetic characters; otherwise, false.
125
+ * @example
126
+ * StringUtils.isAlpha("abc"); // Returns true
127
+ * StringUtils.isAlpha("abc1"); // Returns false
128
+ * StringUtils.isAlpha(""); // Returns false
129
+ */
130
+ static isAlpha(s: string): boolean;
131
+ /**
132
+ * Converts a string to camelCase.
133
+ * Handles spaces, hyphens, and underscores.
134
+ * @param s The input string.
135
+ * @returns The camelCase version of the string.
136
+ * @example
137
+ * StringUtils.camelCase("hello world"); // Returns "helloWorld"
138
+ * StringUtils.camelCase("foo-bar-baz"); // Returns "fooBarBaz"
139
+ * StringUtils.camelCase("foo_bar_baz"); // Returns "fooBarBaz"
140
+ */
141
+ static camelCase(s: string): string;
142
+ /**
143
+ * Converts a string to kebab-case.
144
+ * Handles spaces, camelCase, and underscores.
145
+ * @param s The input string.
146
+ * @returns The kebab-case version of the string.
147
+ * @example
148
+ * StringUtils.kebabCase("hello world"); // Returns "hello-world"
149
+ * StringUtils.kebabCase("fooBarBaz"); // Returns "foo-bar-baz"
150
+ * StringUtils.kebabCase("Foo_Bar-Baz"); // Returns "foo-bar-baz"
151
+ */
152
+ static kebabCase(s: string): string;
153
+ /**
154
+ * Converts a string to snake_case.
155
+ * Handles spaces, camelCase, and hyphens.
156
+ * @param s The input string.
157
+ * @returns The snake_case version of the string.
158
+ * @example
159
+ * StringUtils.snakeCase("hello world"); // Returns "hello_world"
160
+ * StringUtils.snakeCase("fooBarBaz"); // Returns "foo_bar_baz"
161
+ * StringUtils.snakeCase("Foo-Bar_Baz"); // Returns "foo_bar_baz"
162
+ */
163
+ static snakeCase(s: string): string;
164
+ /**
165
+ * Converts a string to PascalCase.
166
+ * Handles spaces, hyphens, and underscores.
167
+ * @param s The input string.
168
+ * @returns The PascalCase version of the string.
169
+ * @example
170
+ * StringUtils.pascalCase("hello world"); // Returns "HelloWorld"
171
+ * StringUtils.pascalCase("foo-bar-baz"); // Returns "FooBarBaz"
172
+ * StringUtils.pascalCase("foo_bar_baz"); // Returns "FooBarBaz"
173
+ */
174
+ static pascalCase(s: string): string;
175
+ /**
176
+ * Reverses the order of characters in a string.
177
+ * @param s The input string.
178
+ * @returns The reversed string.
179
+ * @example
180
+ * StringUtils.reverse("hello"); // Returns "olleh"
181
+ */
182
+ static reverse(s: string): string;
183
+ /**
184
+ * Replaces all occurrences of a specified substring with another string.
185
+ * @param s The input string.
186
+ * @param search The substring to search for.
187
+ * @param replacement The string to replace with.
188
+ * @returns The string with all occurrences replaced.
189
+ * @example
190
+ * StringUtils.replaceAll("banana", "a", "o"); // Returns "bonono"
191
+ */
192
+ static replaceAll(s: string, search: string, replacement: string): string;
193
+ /**
194
+ * Removes diacritical marks (accents) from a string.
195
+ * @param s The input string.
196
+ * @returns The string with accents removed.
197
+ * @example
198
+ * StringUtils.removeAccents("crème brûlée"); // Returns "creme brulee"
199
+ */
200
+ static removeAccents(s: string): string;
201
+ /**
202
+ * Converts a string to a URL-friendly slug.
203
+ * Removes accents, converts to lowercase, trims, replaces non-alphanumeric characters with hyphens,
204
+ * and consolidates multiple hyphens.
205
+ * @param s The input string.
206
+ * @returns The slugified string.
207
+ * @example
208
+ * StringUtils.slugify("Hello World! This is a test."); // Returns "hello-world-this-is-a-test"
209
+ * StringUtils.slugify("Crème brûlée"); // Returns "creme-brulee"
210
+ */
211
+ static slugify(s: string): string;
212
+ /**
213
+ * Capitalizes only the first letter of the string and converts the rest to lowercase.
214
+ * Useful for UI elements like headers and labels where proper sentence casing is required.
215
+ * @param s The input string.
216
+ * @returns The string in sentence case.
217
+ * @example
218
+ * StringUtils.toSentenceCase("hello WORLD"); // Returns "Hello world"
219
+ * StringUtils.toSentenceCase("a quick BROWN fox"); // Returns "A quick brown fox"
220
+ */
221
+ static toSentenceCase(s: string): string;
222
+ /**
223
+ * Capitalizes every word in a string, handling camelCase and special characters gracefully.
224
+ * Ensures consistent capitalization for UI components like card titles, buttons, or navigation items.
225
+ * @param s The input string.
226
+ * @returns The string in title case.
227
+ * @example
228
+ * StringUtils.toTitleCase("hello world"); // Returns "Hello World"
229
+ * StringUtils.toTitleCase("some_variable_name"); // Returns "Some Variable Name"
230
+ * StringUtils.toTitleCase("yetAnotherString"); // Returns "Yet Another String"
231
+ * StringUtils.toTitleCase("the quick brown fox"); // Returns "The Quick Brown Fox"
232
+ */
233
+ static toTitleCase(s: string): string;
234
+ /**
235
+ * Handles basic pluralization logic for displaying counts, e.g., "1 item" vs "2 items".
236
+ * @param count The number of items.
237
+ * @param singular The singular form of the word (e.g., "item", "person").
238
+ * @param plural Optional. The plural form of the word. If not provided, it defaults to appending 's'.
239
+ * @returns The appropriate singular or plural form of the word based on the count.
240
+ * @example
241
+ * StringUtils.pluralize(1, "item"); // Returns "item"
242
+ * StringUtils.pluralize(2, "item"); // Returns "items"
243
+ * StringUtils.pluralize(0, "item"); // Returns "items"
244
+ * StringUtils.pluralize(1, "sheep", "sheep"); // Returns "sheep"
245
+ * StringUtils.pluralize(2, "sheep", "sheep"); // Returns "sheep"
246
+ */
247
+ static pluralize(count: number, singular: string, plural?: string): string;
248
+ /**
249
+ * Prevents Cross-Site Scripting (XSS) by escaping HTML special characters in a string.
250
+ * Converts characters like '<', '>', '&', '"', "'" into their corresponding HTML entities.
251
+ * Essential for safely rendering user-generated content or dynamic text directly into the DOM.
252
+ * @param s The input string.
253
+ * @returns The HTML-escaped string.
254
+ * @example
255
+ * StringUtils.escapeHtml(`"<script>alert('xss')</script>"`); // Returns "&quot;&lt;script&gt;alert(&#39;xss&#39;)&lt;/script&gt;&quot;`
256
+ */
257
+ static escapeHtml(s: string): string;
258
+ /**
259
+ * Formats a number as currency for localized display, using `Intl.NumberFormat`.
260
+ * @param amount The number to format.
261
+ * @param locale Optional. The locale string (e.g., "en-US", "de-DE"). Defaults to "en-US".
262
+ * @param currency Optional. The currency code (e.g., "USD", "EUR"). Defaults to "USD".
263
+ * @returns The formatted currency string.
264
+ * @example
265
+ * StringUtils.formatCurrency(1234.50, "en-US", "USD"); // Returns "$1,234.50"
266
+ * StringUtils.formatCurrency(1234.50, "de-DE", "EUR"); // Returns "1.234,50 €"
267
+ * StringUtils.formatCurrency(5678, "ja-JP", "JPY"); // Returns "¥5,678"
268
+ */
269
+ static formatCurrency(amount: number, locale?: string, currency?: string): string;
270
+ }
271
+
272
+ export { StringUtils };
package/index.js ADDED
@@ -0,0 +1 @@
1
+ "use strict";exports.StringUtils=class t{static uppercase(t){return t.toUpperCase()}static lowercase(t){return t.toLowerCase()}static capitalize(e,r){return e?r?e.split(r).map((e=>t.capitalize(e))).join(r):e.charAt(0).toUpperCase()+e.slice(1).toLowerCase():""}static hash(t,e=6){let r=0;for(let e=0;e<t.length;e++)r=(r<<5)-r+t.charCodeAt(e);r|=0;return((r>>>0)%Math.pow(36,e)).toString(36).padStart(e,"0")}static isEmpty(t){return null==t||0===t.length}static isWhitespace(e){return t.isEmpty(e)||0===e.trim().length}static trim(t){return t.trim()}static truncate(t,e,r="..."){return t.length<=e?t:t.substring(0,e-r.length)+r}static startsWith(t,e){return t.startsWith(e)}static endsWith(t,e){return t.endsWith(e)}static contains(t,e){return t.includes(e)}static isNumeric(t){return/^[0-9]+$/.test(t)}static isAlpha(t){return/^[a-zA-Z]+$/.test(t)}static camelCase(t){return t?t.replace(/[^a-zA-Z0-9]+(.)?/g,((t,e)=>e?e.toUpperCase():"")).replace(/^./,(t=>t.toLowerCase())):""}static kebabCase(t){return t?t.replace(/([a-z0-9]|(?=[A-Z]))([A-Z])/g,"$1-$2").toLowerCase().replace(/[^a-zA-Z0-9-]+/g,"-").replace(/^-+|-+$/g,""):""}static snakeCase(t){return t?t.replace(/([a-z0-9]|(?=[A-Z]))([A-Z])/g,"$1_$2").toLowerCase().replace(/[^a-zA-Z0-9_]+/g,"_").replace(/^_|_$/g,""):""}static pascalCase(t){return t?t.replace(/[^a-zA-Z0-9]+(.)?/g,((t,e)=>e?e.toUpperCase():"")).replace(/^./,(t=>t.toUpperCase())):""}static reverse(t){return t.split("").reverse().join("")}static replaceAll(t,e,r){return t.split(e).join(r)}static removeAccents(t){return t.normalize("NFD").replace(/[\u0300-\u036f]/g,"")}static slugify(e){return e?t.removeAccents(e).toLowerCase().trim().replace(/[^a-z0-9\s-]/g,"").replace(/\s+/g,"-").replace(/-+/g,"-"):""}static toSentenceCase(t){return t?t.charAt(0).toUpperCase()+t.slice(1).toLowerCase():""}static toTitleCase(e){return e?e.replace(/([a-z])([A-Z])/g,"$1 $2").replace(/[^a-zA-Z0-9\s]/g," ").split(/\s+/).map((e=>t.capitalize(e))).join(" "):""}static pluralize(t,e,r){return 1===t?e:void 0!==r?r:`${e}s`}static escapeHtml(t){if(!t)return"";const e={"&":"&amp;","<":"&lt;",">":"&gt;",'"':"&quot;","'":"&#39;"};return t.replace(/[&<>"']/g,(t=>e[t]))}static formatCurrency(t,e="en-US",r="USD"){return new Intl.NumberFormat(e,{style:"currency",currency:r}).format(t)}};
package/index.mjs ADDED
@@ -0,0 +1 @@
1
+ var t=class t{static uppercase(t){return t.toUpperCase()}static lowercase(t){return t.toLowerCase()}static capitalize(e,r){return e?r?e.split(r).map((e=>t.capitalize(e))).join(r):e.charAt(0).toUpperCase()+e.slice(1).toLowerCase():""}static hash(t,e=6){let r=0;for(let e=0;e<t.length;e++)r=(r<<5)-r+t.charCodeAt(e);r|=0;return((r>>>0)%Math.pow(36,e)).toString(36).padStart(e,"0")}static isEmpty(t){return null==t||0===t.length}static isWhitespace(e){return t.isEmpty(e)||0===e.trim().length}static trim(t){return t.trim()}static truncate(t,e,r="..."){return t.length<=e?t:t.substring(0,e-r.length)+r}static startsWith(t,e){return t.startsWith(e)}static endsWith(t,e){return t.endsWith(e)}static contains(t,e){return t.includes(e)}static isNumeric(t){return/^[0-9]+$/.test(t)}static isAlpha(t){return/^[a-zA-Z]+$/.test(t)}static camelCase(t){return t?t.replace(/[^a-zA-Z0-9]+(.)?/g,((t,e)=>e?e.toUpperCase():"")).replace(/^./,(t=>t.toLowerCase())):""}static kebabCase(t){return t?t.replace(/([a-z0-9]|(?=[A-Z]))([A-Z])/g,"$1-$2").toLowerCase().replace(/[^a-zA-Z0-9-]+/g,"-").replace(/^-+|-+$/g,""):""}static snakeCase(t){return t?t.replace(/([a-z0-9]|(?=[A-Z]))([A-Z])/g,"$1_$2").toLowerCase().replace(/[^a-zA-Z0-9_]+/g,"_").replace(/^_|_$/g,""):""}static pascalCase(t){return t?t.replace(/[^a-zA-Z0-9]+(.)?/g,((t,e)=>e?e.toUpperCase():"")).replace(/^./,(t=>t.toUpperCase())):""}static reverse(t){return t.split("").reverse().join("")}static replaceAll(t,e,r){return t.split(e).join(r)}static removeAccents(t){return t.normalize("NFD").replace(/[\u0300-\u036f]/g,"")}static slugify(e){return e?t.removeAccents(e).toLowerCase().trim().replace(/[^a-z0-9\s-]/g,"").replace(/\s+/g,"-").replace(/-+/g,"-"):""}static toSentenceCase(t){return t?t.charAt(0).toUpperCase()+t.slice(1).toLowerCase():""}static toTitleCase(e){return e?e.replace(/([a-z])([A-Z])/g,"$1 $2").replace(/[^a-zA-Z0-9\s]/g," ").split(/\s+/).map((e=>t.capitalize(e))).join(" "):""}static pluralize(t,e,r){return 1===t?e:void 0!==r?r:`${e}s`}static escapeHtml(t){if(!t)return"";const e={"&":"&amp;","<":"&lt;",">":"&gt;",'"':"&quot;","'":"&#39;"};return t.replace(/[&<>"']/g,(t=>e[t]))}static formatCurrency(t,e="en-US",r="USD"){return new Intl.NumberFormat(e,{style:"currency",currency:r}).format(t)}};export{t as StringUtils};
package/package.json ADDED
@@ -0,0 +1,63 @@
1
+ {
2
+ "name": "@asaidimu/utils-strings",
3
+ "version": "1.0.0",
4
+ "description": "A collection of strings utilities.",
5
+ "main": "index.js",
6
+ "module": "index.mjs",
7
+ "types": "index.d.ts",
8
+ "keywords": [
9
+ "typescript",
10
+ "utility"
11
+ ],
12
+ "author": "Saidimu <47994458+asaidimu@users.noreply.github.com>",
13
+ "license": "MIT",
14
+ "repository": {
15
+ "type": "git",
16
+ "url": "git+https://github.com/asaidimu/erp-utils.git"
17
+ },
18
+ "bugs": {
19
+ "url": "https://github.com/asaidimu/erp-utils/issues"
20
+ },
21
+ "homepage": "https://github.com/asaidimu/erp-utils/tree/main/src/strings#readme",
22
+ "files": [
23
+ "./*"
24
+ ],
25
+ "exports": {
26
+ ".": {
27
+ "import": {
28
+ "types": "./index.d.ts",
29
+ "default": "./index.mjs"
30
+ },
31
+ "require": {
32
+ "types": "./index.d.ts",
33
+ "default": "./index.js"
34
+ }
35
+ }
36
+ },
37
+ "dependencies": {},
38
+ "publishConfig": {
39
+ "registry": "https://registry.npmjs.org/",
40
+ "tag": "latest",
41
+ "access": "public"
42
+ },
43
+ "release": {
44
+ "plugins": [
45
+ [
46
+ "@semantic-release/npm",
47
+ {
48
+ "pkgRoot": "./dist"
49
+ }
50
+ ],
51
+ [
52
+ "@semantic-release/git",
53
+ {
54
+ "assets": [
55
+ "CHANGELOG.md",
56
+ "package.json"
57
+ ],
58
+ "message": "chore(release): Release @asaidimu/utils-strings v${nextRelease.version} [skip ci]\n\n${nextRelease.notes}"
59
+ }
60
+ ]
61
+ ]
62
+ }
63
+ }