stringzy 1.1.2 â 2.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/README.md +381 -24
- package/analysis.js +21 -0
- package/changelog.txt +74 -0
- package/formatting.js +37 -0
- package/index.js +64 -46
- package/package.json +7 -2
- package/transformations.js +128 -0
- package/validations.js +19 -0
package/README.md
CHANGED
|
@@ -5,15 +5,16 @@
|
|
|
5
5
|

|
|
6
6
|

|
|
7
7
|
|
|
8
|
-
**A lightweight, zero-dependency NPM Package for elegant string manipulations. It provides a range of text utilities for JavaScript and Node.js applications.**
|
|
8
|
+
**A lightweight, zero-dependency NPM Package for elegant string manipulations. It provides a comprehensive range of text utilities for JavaScript and Node.js applications including transformations, validations, analysis, and formatting.**
|
|
9
9
|
|
|
10
10
|
## âĻ Features
|
|
11
11
|
|
|
12
|
-
- ðŠ **Powerful** - Transform strings with minimal code
|
|
12
|
+
- ðŠ **Powerful** - Transform, validate, analyze, and format strings with minimal code
|
|
13
13
|
- ðŠķ **Lightweight** - Zero dependencies, tiny footprint
|
|
14
|
-
- ð§Đ **Modular** - Import only what you need
|
|
14
|
+
- ð§Đ **Modular** - Import only what you need with organized namespaces
|
|
15
15
|
- ð **Fast** - Optimized for performance
|
|
16
16
|
- â
**Tested** - Reliable and robust
|
|
17
|
+
- ðŊ **Comprehensive** - 4 specialized modules for all string needs
|
|
17
18
|
|
|
18
19
|
## ðĶ Installation
|
|
19
20
|
|
|
@@ -32,18 +33,56 @@ pnpm add stringzy
|
|
|
32
33
|
|
|
33
34
|
```javascript
|
|
34
35
|
// Import the entire library
|
|
35
|
-
import
|
|
36
|
+
import stringzy from 'stringzy';
|
|
36
37
|
|
|
37
38
|
// Or import specific functions
|
|
38
|
-
import {
|
|
39
|
+
import { toUpperCase, isEmail, wordCount, formatPhone } from 'stringzy';
|
|
40
|
+
|
|
41
|
+
// Or import by namespace
|
|
42
|
+
import { transform, validate, analyze, format } from 'stringzy';
|
|
39
43
|
|
|
40
44
|
// Transform your strings
|
|
41
45
|
const slug = stringzy.toSlug('Hello World!'); // 'hello-world'
|
|
46
|
+
const isValid = stringzy.validate.isEmail('user@example.com'); // true
|
|
47
|
+
const count = stringzy.analyze.wordCount('Hello world'); // 2
|
|
42
48
|
```
|
|
43
49
|
|
|
50
|
+
## ð Table of Contents
|
|
51
|
+
|
|
52
|
+
### Transformations
|
|
53
|
+
- [truncateText](#truncatetext) - Truncates text to a specified maximum length
|
|
54
|
+
- [toSlug](#toslug) - Converts a string to a URL-friendly slug
|
|
55
|
+
- [capitalizeWords](#capitalizewords) - Capitalizes the first letter of each word
|
|
56
|
+
- [removeSpecialChars](#removespecialchars) - Removes special characters from a string
|
|
57
|
+
- [camelCase](#camelCase) - Converts the given string to Camel Case
|
|
58
|
+
- [pascaslCase](#pascalCase) - Converts the given string to Pascal Case
|
|
59
|
+
- [snakeCase](#snakeCase) - Converts the given string to Snake Case
|
|
60
|
+
- [kebabCase](#kebabCase) - Converts the given string to Kebab Case
|
|
61
|
+
- [titleCase](#titleCase) - Converts the given string to Title Case
|
|
62
|
+
- [constantCase](#constantCase) - Converts the given string to Constant Case
|
|
63
|
+
|
|
64
|
+
### Validations
|
|
65
|
+
- [isURL](#isurl) - Checks if a string is a valid URL
|
|
66
|
+
- [isEmail](#isemail) - Checks if a string is a valid email address
|
|
67
|
+
- [isEmpty](#isempty) - Checks if a string is empty or contains only whitespace
|
|
68
|
+
|
|
69
|
+
### Analysis
|
|
70
|
+
- [wordCount](#wordcount) - Counts the number of words in a string
|
|
71
|
+
- [characterCount](#charactercount) - Counts the number of characters in a string
|
|
72
|
+
- [characterFrequency](#characterfrequency) - Analyzes character frequency in a string
|
|
73
|
+
|
|
74
|
+
### Formatting
|
|
75
|
+
- [capitalize](#capitalize) - Capitalizes the first letter of each word
|
|
76
|
+
- [formatNumber](#formatnumber) - Formats a number string with thousand separators
|
|
77
|
+
- [formatPhone](#formatphone) - Formats a phone number string to standard format
|
|
78
|
+
|
|
44
79
|
## ð API Reference
|
|
45
80
|
|
|
46
|
-
###
|
|
81
|
+
### ð Transformations
|
|
82
|
+
|
|
83
|
+
Functions for transforming and manipulating strings.
|
|
84
|
+
|
|
85
|
+
#### <a id="truncatetext"></a>`truncateText(text, maxLength, suffix = '...')`
|
|
47
86
|
|
|
48
87
|
Truncates text to a specified maximum length, adding a suffix if truncated.
|
|
49
88
|
|
|
@@ -66,7 +105,7 @@ truncateText('Short', 10);
|
|
|
66
105
|
| maxLength | number | required | Maximum length of the output string (excluding suffix) |
|
|
67
106
|
| suffix | string | '...' | String to append if truncation occurs |
|
|
68
107
|
|
|
69
|
-
|
|
108
|
+
#### <a id="toslug"></a>`toSlug(text)`
|
|
70
109
|
|
|
71
110
|
Converts a string to a URL-friendly slug.
|
|
72
111
|
|
|
@@ -87,7 +126,7 @@ toSlug('Special $#@! characters');
|
|
|
87
126
|
|-----------|------|---------|-------------|
|
|
88
127
|
| text | string | required | The input string to convert to a slug |
|
|
89
128
|
|
|
90
|
-
|
|
129
|
+
#### <a id="capitalizewords"></a>`capitalizeWords(text)`
|
|
91
130
|
|
|
92
131
|
Capitalizes the first letter of each word in a string.
|
|
93
132
|
|
|
@@ -108,7 +147,7 @@ capitalizeWords('already Capitalized');
|
|
|
108
147
|
|-----------|------|---------|-------------|
|
|
109
148
|
| text | string | required | The input string to capitalize |
|
|
110
149
|
|
|
111
|
-
|
|
150
|
+
#### <a id="removespecialchars"></a>`removeSpecialChars(text, replacement = '')`
|
|
112
151
|
|
|
113
152
|
Removes special characters from a string, optionally replacing them.
|
|
114
153
|
|
|
@@ -130,50 +169,368 @@ removeSpecialChars('Phone: (123) 456-7890', '-');
|
|
|
130
169
|
| text | string | required | The input string to process |
|
|
131
170
|
| replacement | string | '' | String to replace special characters with |
|
|
132
171
|
|
|
172
|
+
#### <a id="camelCase"></a>`camelCase(text)`
|
|
173
|
+
|
|
174
|
+
Converts the given string to Camel Case.
|
|
175
|
+
|
|
176
|
+
```javascript
|
|
177
|
+
import { camelCase } from 'stringzy';
|
|
178
|
+
|
|
179
|
+
camelCase('hello world'); // 'helloWorld'
|
|
180
|
+
camelCase('this is a test'); // 'thisIsATest'
|
|
181
|
+
```
|
|
182
|
+
| Parameter | Type | Default | Description |
|
|
183
|
+
|-----------|------|---------|-------------|
|
|
184
|
+
| text | string | required | The input string to convert to Camel Case |
|
|
185
|
+
|
|
186
|
+
|
|
187
|
+
#### <a id="pascalCase"></a>`pascalCase(text)`
|
|
188
|
+
Converts the given string to Pascal Case.
|
|
189
|
+
|
|
190
|
+
```javascript
|
|
191
|
+
import { pascalCase } from 'stringzy';
|
|
192
|
+
|
|
193
|
+
|
|
194
|
+
pascalCase('hello world'); // 'HelloWorld'
|
|
195
|
+
pascalCase('this is a test'); // 'ThisIsATest'
|
|
196
|
+
```
|
|
197
|
+
| Parameter | Type | Default | Description |
|
|
198
|
+
|-----------|------|---------|-------------|
|
|
199
|
+
| text | string | required | The input string to convert to Pascal Case |
|
|
200
|
+
|
|
201
|
+
#### <a id="snakeCase"></a>`snakeCase(text)`
|
|
202
|
+
|
|
203
|
+
Converts the given string to Snake Case.
|
|
204
|
+
|
|
205
|
+
```javascript
|
|
206
|
+
import { snakeCase } from 'stringzy';
|
|
207
|
+
snakeCase('hello world'); // 'hello_world'
|
|
208
|
+
snakeCase('this is a test'); // 'this_is_a_test'
|
|
209
|
+
```
|
|
210
|
+
|
|
211
|
+
| Parameter | Type | Default | Description |
|
|
212
|
+
|-----------|------|---------|-------------|
|
|
213
|
+
| text | string | required | The input string to convert to Snake Case |
|
|
214
|
+
|
|
215
|
+
|
|
216
|
+
|
|
217
|
+
|
|
218
|
+
#### <a id="kebabCase"></a>`kebabCase(text)`
|
|
219
|
+
|
|
220
|
+
Converts the given string to Kebab Case.
|
|
221
|
+
|
|
222
|
+
```javascript
|
|
223
|
+
import { kebabCase } from 'stringzy';
|
|
224
|
+
|
|
225
|
+
|
|
226
|
+
kebabCase('hello world'); // 'hello-world'
|
|
227
|
+
kebabCase('this is a test'); // 'this-is-a-test'
|
|
228
|
+
```
|
|
229
|
+
| Parameter | Type | Default | Description |
|
|
230
|
+
|-----------|------|---------|-------------|
|
|
231
|
+
| text | string | required | The input string to convert to Kebab Case |
|
|
232
|
+
|
|
233
|
+
|
|
234
|
+
|
|
235
|
+
#### <a id="titleCase"></a>`titleCase(text)`
|
|
236
|
+
|
|
237
|
+
Converts the given string to Title Case.
|
|
238
|
+
|
|
239
|
+
```javascript
|
|
240
|
+
import { titleCase } from 'stringzy';
|
|
241
|
+
|
|
242
|
+
|
|
243
|
+
titleCase('hello world'); // 'Hello World'
|
|
244
|
+
titleCase('this is a test'); // 'This Is A Test'
|
|
245
|
+
```
|
|
246
|
+
| Parameter | Type | Default | Description |
|
|
247
|
+
|-----------|------|---------|-------------|
|
|
248
|
+
| text | string | required | The input string to convert to Title Case |
|
|
249
|
+
|
|
250
|
+
#### <a id="constantCase"></a>`constantCase(text)`
|
|
251
|
+
Converts the given string to Constant Case.
|
|
252
|
+
|
|
253
|
+
```javascript
|
|
254
|
+
import { constantCase } from 'stringzy';
|
|
255
|
+
|
|
256
|
+
|
|
257
|
+
constantCase('hello world'); // 'HELLO_WORLD'
|
|
258
|
+
constantCase('this is a test'); // 'THIS_IS_A_TEST'
|
|
259
|
+
|
|
260
|
+
```
|
|
261
|
+
| Parameter | Type | Default | Description |
|
|
262
|
+
|-----------|------|---------|-------------|
|
|
263
|
+
| text | string | required | The input string to convert to Constant Case |
|
|
264
|
+
|
|
265
|
+
---
|
|
266
|
+
|
|
267
|
+
### â
Validations
|
|
268
|
+
|
|
269
|
+
Functions for validating string formats and content.
|
|
270
|
+
|
|
271
|
+
#### <a id="isurl"></a>`isURL(text)`
|
|
272
|
+
|
|
273
|
+
Checks if a string is a valid URL.
|
|
274
|
+
|
|
275
|
+
```javascript
|
|
276
|
+
isURL('https://example.com'); // true
|
|
277
|
+
isURL('not-a-url'); // false
|
|
278
|
+
```
|
|
279
|
+
|
|
280
|
+
| Parameter | Type | Default | Description |
|
|
281
|
+
|-----------|------|---------|-------------|
|
|
282
|
+
| text | string | required | The input string to validate as URL |
|
|
283
|
+
|
|
284
|
+
#### <a id="isemail"></a>`isEmail(text)`
|
|
285
|
+
|
|
286
|
+
Checks if a string is a valid email address.
|
|
287
|
+
|
|
288
|
+
```javascript
|
|
289
|
+
isEmail('user@example.com'); // true
|
|
290
|
+
isEmail('invalid-email'); // false
|
|
291
|
+
```
|
|
292
|
+
|
|
293
|
+
| Parameter | Type | Default | Description |
|
|
294
|
+
|-----------|------|---------|-------------|
|
|
295
|
+
| text | string | required | The input string to validate as email |
|
|
296
|
+
|
|
297
|
+
#### <a id="isempty"></a>`isEmpty(text)`
|
|
298
|
+
|
|
299
|
+
Checks if a string is empty or contains only whitespace.
|
|
300
|
+
|
|
301
|
+
```javascript
|
|
302
|
+
isEmpty(' '); // true
|
|
303
|
+
isEmpty('hello'); // false
|
|
304
|
+
```
|
|
305
|
+
|
|
306
|
+
| Parameter | Type | Default | Description |
|
|
307
|
+
|-----------|------|---------|-------------|
|
|
308
|
+
| text | string | required | The input string to check for emptiness |
|
|
309
|
+
|
|
310
|
+
---
|
|
311
|
+
|
|
312
|
+
|
|
313
|
+
### ð Analysis
|
|
314
|
+
|
|
315
|
+
Functions for analyzing string content and structure.
|
|
316
|
+
|
|
317
|
+
#### <a id="wordcount"></a>`wordCount(text)`
|
|
318
|
+
|
|
319
|
+
Counts the number of words in a string.
|
|
320
|
+
|
|
321
|
+
```javascript
|
|
322
|
+
wordCount('Hello world'); // 2
|
|
323
|
+
wordCount(''); // 0
|
|
324
|
+
```
|
|
325
|
+
|
|
326
|
+
| Parameter | Type | Default | Description |
|
|
327
|
+
|-----------|------|---------|-------------|
|
|
328
|
+
| text | string | required | The input string to count words in |
|
|
329
|
+
|
|
330
|
+
#### <a id="charactercount"></a>`characterCount(text)`
|
|
331
|
+
|
|
332
|
+
Counts the number of characters in a string.
|
|
333
|
+
|
|
334
|
+
```javascript
|
|
335
|
+
characterCount('Hello'); // 5
|
|
336
|
+
```
|
|
337
|
+
|
|
338
|
+
| Parameter | Type | Default | Description |
|
|
339
|
+
|-----------|------|---------|-------------|
|
|
340
|
+
| text | string | required | The input string to count characters in |
|
|
341
|
+
|
|
342
|
+
#### <a id="characterfrequency"></a>`characterFrequency(text)`
|
|
343
|
+
|
|
344
|
+
Analyzes character frequency in a string (excluding spaces).
|
|
345
|
+
|
|
346
|
+
```javascript
|
|
347
|
+
characterFrequency('hello'); // { h: 1, e: 1, l: 2, o: 1 }
|
|
348
|
+
```
|
|
349
|
+
|
|
350
|
+
| Parameter | Type | Default | Description |
|
|
351
|
+
|-----------|------|---------|-------------|
|
|
352
|
+
| text | string | required | The input string to analyze character frequency |
|
|
353
|
+
|
|
354
|
+
---
|
|
355
|
+
|
|
356
|
+
### ðĻ Formatting
|
|
357
|
+
|
|
358
|
+
Functions for formatting strings into specific patterns.
|
|
359
|
+
|
|
360
|
+
#### <a id="capitalize"></a>`capitalize(text)`
|
|
361
|
+
|
|
362
|
+
Capitalizes the first letter of each word.
|
|
363
|
+
|
|
364
|
+
```javascript
|
|
365
|
+
capitalize('hello world'); // 'Hello World'
|
|
366
|
+
capitalize('javaScript programming'); // 'Javascript Programming'
|
|
367
|
+
```
|
|
368
|
+
|
|
369
|
+
| Parameter | Type | Default | Description |
|
|
370
|
+
|-----------|------|---------|-------------|
|
|
371
|
+
| text | string | required | The input string to capitalize |
|
|
372
|
+
|
|
373
|
+
#### <a id="formatnumber"></a>`formatNumber(number, separator = ',')`
|
|
374
|
+
|
|
375
|
+
Formats a number string with thousand separators.
|
|
376
|
+
|
|
377
|
+
```javascript
|
|
378
|
+
formatNumber('1234567'); // '1,234,567'
|
|
379
|
+
formatNumber('1234567', '.'); // '1.234.567'
|
|
380
|
+
```
|
|
381
|
+
|
|
382
|
+
| Parameter | Type | Default | Description |
|
|
383
|
+
|-----------|------|---------|-------------|
|
|
384
|
+
| number | string\|number | required | The number to format |
|
|
385
|
+
| separator | string | ',' | The separator to use for thousands |
|
|
386
|
+
|
|
387
|
+
#### <a id="formatphone"></a>`formatPhone(phone, format = 'us')`
|
|
388
|
+
|
|
389
|
+
Formats a phone number string to standard format.
|
|
390
|
+
|
|
391
|
+
```javascript
|
|
392
|
+
formatPhone('1234567890'); // '(123) 456-7890'
|
|
393
|
+
formatPhone('11234567890', 'international'); // '+1 (123) 456-7890'
|
|
394
|
+
```
|
|
395
|
+
|
|
396
|
+
| Parameter | Type | Default | Description |
|
|
397
|
+
|-----------|------|---------|-------------|
|
|
398
|
+
| phone | string | required | The phone number string to format |
|
|
399
|
+
| format | string | 'us' | Format type: 'us' or 'international' |
|
|
400
|
+
|
|
401
|
+
## ð§ Usage Patterns
|
|
402
|
+
|
|
403
|
+
### Individual Function Imports
|
|
404
|
+
```javascript
|
|
405
|
+
import { isEmail, wordCount, capitalize } from 'stringzy';
|
|
406
|
+
|
|
407
|
+
const email = 'user@example.com';
|
|
408
|
+
if (isEmail(email)) {
|
|
409
|
+
console.log('Valid email!');
|
|
410
|
+
}
|
|
411
|
+
```
|
|
412
|
+
|
|
413
|
+
### Namespace Imports
|
|
414
|
+
```javascript
|
|
415
|
+
import { validate, analyze, format } from 'stringzy';
|
|
416
|
+
|
|
417
|
+
// Organized by functionality
|
|
418
|
+
const emailValid = validate.isEmail('test@example.com');
|
|
419
|
+
const words = analyze.wordCount('Hello world');
|
|
420
|
+
const formatted = format.capitalize('hello world');
|
|
421
|
+
```
|
|
422
|
+
|
|
423
|
+
### Default Import (All Functions)
|
|
424
|
+
```javascript
|
|
425
|
+
import stringzy from 'stringzy';
|
|
426
|
+
|
|
427
|
+
// Access any function
|
|
428
|
+
stringzy.toUpperCase('hello');
|
|
429
|
+
stringzy.validate.isEmail('test@example.com');
|
|
430
|
+
stringzy.analyze.wordCount('Hello world');
|
|
431
|
+
stringzy.format.capitalize('hello world');
|
|
432
|
+
```
|
|
433
|
+
|
|
133
434
|
## ð ïļ Usage Examples
|
|
134
435
|
|
|
135
436
|
### In a React component
|
|
136
437
|
|
|
137
438
|
```jsx
|
|
138
439
|
import React from 'react';
|
|
139
|
-
import {
|
|
440
|
+
import { truncate, capitalize, wordCount, isEmpty } from 'stringzy';
|
|
140
441
|
|
|
141
442
|
function ArticlePreview({ title, content }) {
|
|
443
|
+
const displayTitle = isEmpty(title) ? 'Untitled' : capitalize(title);
|
|
444
|
+
const previewText = truncate(content, 150);
|
|
445
|
+
const readingTime = Math.ceil(wordCount(content) / 200);
|
|
446
|
+
|
|
142
447
|
return (
|
|
143
448
|
<div className="article-preview">
|
|
144
|
-
<h2>{
|
|
145
|
-
<p>{
|
|
449
|
+
<h2>{displayTitle}</h2>
|
|
450
|
+
<p>{previewText}</p>
|
|
451
|
+
<small>{readingTime} min read</small>
|
|
146
452
|
</div>
|
|
147
453
|
);
|
|
148
454
|
}
|
|
149
455
|
```
|
|
150
456
|
|
|
151
|
-
###
|
|
457
|
+
### Form Validation
|
|
152
458
|
|
|
153
459
|
```javascript
|
|
154
|
-
|
|
460
|
+
import { validate } from 'stringzy';
|
|
461
|
+
|
|
462
|
+
function validateForm(formData) {
|
|
463
|
+
const errors = {};
|
|
464
|
+
|
|
465
|
+
if (!validate.isEmail(formData.email)) {
|
|
466
|
+
errors.email = 'Please enter a valid email address';
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
if (!validate.isURL(formData.website)) {
|
|
470
|
+
errors.website = 'Please enter a valid URL';
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
if (validate.isEmpty(formData.name)) {
|
|
474
|
+
errors.name = 'Name is required';
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
return errors;
|
|
478
|
+
}
|
|
479
|
+
```
|
|
480
|
+
|
|
481
|
+
### Content Analysis Dashboard
|
|
155
482
|
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
483
|
+
```javascript
|
|
484
|
+
import { analyze } from 'stringzy';
|
|
485
|
+
|
|
486
|
+
function getContentStats(text) {
|
|
487
|
+
return {
|
|
488
|
+
words: analyze.wordCount(text),
|
|
489
|
+
characters: analyze.characterCount(text),
|
|
490
|
+
frequency: analyze.characterFrequency(text),
|
|
491
|
+
readingTime: Math.ceil(analyze.wordCount(text) / 200)
|
|
492
|
+
};
|
|
160
493
|
}
|
|
494
|
+
```
|
|
495
|
+
|
|
496
|
+
### Data Formatting
|
|
161
497
|
|
|
162
|
-
|
|
163
|
-
|
|
498
|
+
```javascript
|
|
499
|
+
import { format } from 'stringzy';
|
|
500
|
+
|
|
501
|
+
function formatUserData(userData) {
|
|
502
|
+
return {
|
|
503
|
+
name: format.capitalize(userData.name),
|
|
504
|
+
phone: format.formatPhone(userData.phone),
|
|
505
|
+
revenue: format.formatNumber(userData.revenue)
|
|
506
|
+
};
|
|
507
|
+
}
|
|
164
508
|
```
|
|
165
509
|
|
|
166
510
|
## ð TypeScript Support
|
|
167
511
|
|
|
168
|
-
The package includes TypeScript type definitions.
|
|
512
|
+
The package includes TypeScript type definitions for all functions.
|
|
169
513
|
|
|
170
514
|
```typescript
|
|
171
|
-
import {
|
|
515
|
+
import { validate, analyze, format } from 'stringzy';
|
|
172
516
|
|
|
173
517
|
// TypeScript will provide proper type checking
|
|
174
|
-
const
|
|
518
|
+
const isValid: boolean = validate.isEmail('test@example.com');
|
|
519
|
+
const count: number = analyze.wordCount('Hello world');
|
|
520
|
+
const formatted: string = format.capitalize('hello world');
|
|
175
521
|
```
|
|
176
522
|
|
|
523
|
+
## ðïļ Architecture
|
|
524
|
+
|
|
525
|
+
stringzy is organized into four specialized modules:
|
|
526
|
+
|
|
527
|
+
- **`transformations.js`** - Core string transformations
|
|
528
|
+
- **`validations.js`** - String validation utilities
|
|
529
|
+
- **`analysis.js`** - String analysis and metrics
|
|
530
|
+
- **`formatting.js`** - String formatting functions
|
|
531
|
+
|
|
532
|
+
Each module can be imported individually or accessed through the main entry point.
|
|
533
|
+
|
|
177
534
|
## ðĪ Contributing
|
|
178
535
|
|
|
179
536
|
Contributions are welcome! Please feel free to submit a Pull Request.
|
|
@@ -191,7 +548,7 @@ This project is licensed under the MIT License - see the LICENSE file for detail
|
|
|
191
548
|
## ð Acknowledgments
|
|
192
549
|
|
|
193
550
|
- Thank you to all contributors and users of this package!
|
|
194
|
-
- Inspired by the need for
|
|
551
|
+
- Inspired by the need for comprehensive yet simple string manipulation utilities.
|
|
195
552
|
|
|
196
553
|
---
|
|
197
554
|
|
package/analysis.js
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
|
|
2
|
+
export function wordCount(str) {
|
|
3
|
+
if (!str.trim()) return 0;
|
|
4
|
+
return str.trim().split(/\s+/).length;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
export function characterCount(str) {
|
|
9
|
+
|
|
10
|
+
return str.length;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export function characterFrequency(str) {
|
|
14
|
+
const frequency = {};
|
|
15
|
+
for (const char of str.toLowerCase()) {
|
|
16
|
+
if (char !== ' ') { // Exclude spaces for cleaner analysis
|
|
17
|
+
frequency[char] = (frequency[char] || 0) + 1;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
return frequency;
|
|
21
|
+
}
|
package/changelog.txt
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
CHANGELOG
|
|
2
|
+
|
|
3
|
+
All notable changes to the `stringzy` package will be documented in this file.
|
|
4
|
+
|
|
5
|
+
=============================================================================
|
|
6
|
+
Version 2.0.0 - 2025-05-22
|
|
7
|
+
-----------------------------------------------------------------------------
|
|
8
|
+
|
|
9
|
+
ADDED:
|
|
10
|
+
- Added a dynamic list of APIs in README
|
|
11
|
+
- Divided the package into 4 modules-transformations, validations, formatting and analysis
|
|
12
|
+
- Added many new APIs
|
|
13
|
+
- `camelCase`: Converts a string to camel case
|
|
14
|
+
- `kebabCase`: Converts a string to kebab case
|
|
15
|
+
- `snakeCase`: Converts a string to snake case
|
|
16
|
+
- `titleCase`: Converts a string to title case
|
|
17
|
+
- `constantCase`: Converts a string to constant case
|
|
18
|
+
- `pascalCase`: Converts a string to pascal case
|
|
19
|
+
- `isURL`: Validates if a string is a valid URL
|
|
20
|
+
- `isEmail`: Validates if a string is a valid email address
|
|
21
|
+
- `isEmpty`: Checks if a string is empty or contains only whitespace
|
|
22
|
+
- `wordCount`: Counts the number of words in a string
|
|
23
|
+
- `characterCount`: Counts the number of characters in a string
|
|
24
|
+
- `characterFrequency`: Calculates the frequency of each character in a string
|
|
25
|
+
- `capitalize`: Capitalizes the entire string
|
|
26
|
+
- `formatNumber`: Formats a number with commas as thousands separators
|
|
27
|
+
- `formatPhone`: Formats a phone number to a standard format
|
|
28
|
+
|
|
29
|
+
=============================================================================
|
|
30
|
+
|
|
31
|
+
Version 1.1.2 - 2025-05-15
|
|
32
|
+
-----------------------------------------------------------------------------
|
|
33
|
+
|
|
34
|
+
ADDED:
|
|
35
|
+
- Improved documentation with comprehensive README
|
|
36
|
+
- Enhanced examples for all utility functions
|
|
37
|
+
|
|
38
|
+
FIXED:
|
|
39
|
+
- Resolved documentation rendering issues on npm website
|
|
40
|
+
|
|
41
|
+
=============================================================================
|
|
42
|
+
|
|
43
|
+
Version 1.1.1 - 2025-05-15
|
|
44
|
+
-----------------------------------------------------------------------------
|
|
45
|
+
|
|
46
|
+
CHANGED:
|
|
47
|
+
- Updated package metadata
|
|
48
|
+
|
|
49
|
+
FIXED:
|
|
50
|
+
- Minor bug fixes in string handling edge cases
|
|
51
|
+
|
|
52
|
+
=============================================================================
|
|
53
|
+
|
|
54
|
+
Version 1.1.0 - 2025-05-15
|
|
55
|
+
-----------------------------------------------------------------------------
|
|
56
|
+
|
|
57
|
+
ADDED:
|
|
58
|
+
- Added extensive API documentation
|
|
59
|
+
|
|
60
|
+
CHANGED:
|
|
61
|
+
- Improved performance for `toSlug` function
|
|
62
|
+
- Enhanced error handling across all utility functions
|
|
63
|
+
|
|
64
|
+
=============================================================================
|
|
65
|
+
|
|
66
|
+
Version 1.0.0 - 2025-05-14
|
|
67
|
+
-----------------------------------------------------------------------------
|
|
68
|
+
|
|
69
|
+
ADDED:
|
|
70
|
+
- Initial release with four core utility functions:
|
|
71
|
+
- `truncateText`: Truncates text to a specified length with optional suffix
|
|
72
|
+
- `toSlug`: Converts text to URL-friendly slug format
|
|
73
|
+
- `capitalizeWords`: Capitalizes the first letter of each word
|
|
74
|
+
- `removeSpecialChars`: Removes special characters from text with optional replacement
|
package/formatting.js
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
|
|
2
|
+
export function capitalize(str) {
|
|
3
|
+
return str
|
|
4
|
+
.split(' ')
|
|
5
|
+
.map(word => word.charAt(0).toUpperCase() + word.slice(1).toLowerCase())
|
|
6
|
+
.join(' ');
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
export function formatNumber(num, separator = ',') {
|
|
11
|
+
const numStr = num.toString();
|
|
12
|
+
return numStr.replace(/\B(?=(\d{3})+(?!\d))/g, separator);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
export function formatPhone(phone, format = 'us') {
|
|
17
|
+
const digits = phone.replace(/\D/g, '');
|
|
18
|
+
|
|
19
|
+
if (format === 'us' && digits.length === 10) {
|
|
20
|
+
return `(${digits.slice(0, 3)}) ${digits.slice(3, 6)}-${digits.slice(6)}`;
|
|
21
|
+
} else if (format === 'international' && digits.length >= 10) {
|
|
22
|
+
const countryCode = digits.slice(0, -10);
|
|
23
|
+
const areaCode = digits.slice(-10, -7);
|
|
24
|
+
const firstPart = digits.slice(-7, -4);
|
|
25
|
+
const lastPart = digits.slice(-4);
|
|
26
|
+
return `+${countryCode} (${areaCode}) ${firstPart}-${lastPart}`;
|
|
27
|
+
}
|
|
28
|
+
else if (format === 'indian') {
|
|
29
|
+
if (digits.length === 10) {
|
|
30
|
+
return `+91-${digits.slice(0, 5)}-${digits.slice(5)}`;
|
|
31
|
+
} else if (digits.length === 12 && digits.startsWith('91')) {
|
|
32
|
+
return `+91-${digits.slice(2, 7)}-${digits.slice(7)}`;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
return phone;
|
|
37
|
+
}
|
package/index.js
CHANGED
|
@@ -1,49 +1,67 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
if (text.length <= maxLength) {
|
|
13
|
-
return text;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
const adjustedLength = maxLength - suffix.length;
|
|
17
|
-
return text.slice(0, adjustedLength > 0 ? adjustedLength : 0) + suffix;
|
|
18
|
-
}
|
|
19
|
-
|
|
1
|
+
/**
|
|
2
|
+
* index.js
|
|
3
|
+
* Main entry point for the srtingzy package
|
|
4
|
+
*
|
|
5
|
+
* This file serves as the public API for the entire library,
|
|
6
|
+
* organizing and exporting all functionality in a structured way.
|
|
7
|
+
*
|
|
8
|
+
* @module stringzy
|
|
9
|
+
* @author Samarth Ruia
|
|
10
|
+
* @version 2.0.0
|
|
11
|
+
*/
|
|
20
12
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
13
|
+
import * as transformations from './transformations.js';
|
|
14
|
+
import * as validations from './validations.js';
|
|
15
|
+
import * as analysis from './analysis.js';
|
|
16
|
+
import * as formatting from './formatting.js';
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
export const {
|
|
20
|
+
truncateText,
|
|
21
|
+
toSlug,
|
|
22
|
+
capitalizeWords,
|
|
23
|
+
removeSpecialChars,
|
|
24
|
+
camelCase,
|
|
25
|
+
pascalCase,
|
|
26
|
+
snakeCase,
|
|
27
|
+
kebabCase,
|
|
28
|
+
titleCase,
|
|
29
|
+
constantCase,
|
|
30
|
+
} = transformations;
|
|
31
|
+
|
|
32
|
+
export const {
|
|
33
|
+
isURL,
|
|
34
|
+
isEmail,
|
|
35
|
+
isEmpty
|
|
36
|
+
} = validations;
|
|
32
37
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
38
|
+
export const {
|
|
39
|
+
wordCount,
|
|
40
|
+
characterCount,
|
|
41
|
+
characterFrequency
|
|
42
|
+
} = analysis;
|
|
43
|
+
|
|
44
|
+
export const {
|
|
45
|
+
capitalize,
|
|
46
|
+
formatNumber,
|
|
47
|
+
formatPhone
|
|
48
|
+
} = formatting;
|
|
49
|
+
|
|
50
|
+
export const transform = transformations;
|
|
51
|
+
export const validate = validations;
|
|
52
|
+
export const analyze = analysis;
|
|
53
|
+
export const format = formatting;
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
export default {
|
|
57
|
+
...transformations,
|
|
58
|
+
...validations,
|
|
59
|
+
...analysis,
|
|
60
|
+
...formatting,
|
|
39
61
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
}
|
|
47
|
-
return text.replace(/[^\w\s]/gi, replacement);
|
|
48
|
-
}
|
|
49
|
-
|
|
62
|
+
transform: transformations,
|
|
63
|
+
validate: validations,
|
|
64
|
+
analyze: analysis,
|
|
65
|
+
format: formatting,
|
|
66
|
+
|
|
67
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "stringzy",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"main": "index.js",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"scripts": {
|
|
@@ -14,12 +14,17 @@
|
|
|
14
14
|
"string",
|
|
15
15
|
"string-manipulation",
|
|
16
16
|
"text-formatting",
|
|
17
|
-
"
|
|
17
|
+
"dev-tools",
|
|
18
18
|
"text-utils",
|
|
19
19
|
"stringify",
|
|
20
|
+
"string-tools",
|
|
21
|
+
"string-manipulation-library",
|
|
22
|
+
"utility-library",
|
|
23
|
+
"ReactJS",
|
|
20
24
|
"stringer",
|
|
21
25
|
"string",
|
|
22
26
|
"text",
|
|
27
|
+
"best",
|
|
23
28
|
"manipulation",
|
|
24
29
|
"utilities",
|
|
25
30
|
"javascript",
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
export function truncateText(text, maxLength, suffix = '...') {
|
|
2
|
+
if (typeof text !== 'string') {
|
|
3
|
+
throw new Error("Input text must be a string.");
|
|
4
|
+
}
|
|
5
|
+
if (typeof maxLength !== 'number' || maxLength < 0) {
|
|
6
|
+
throw new Error("maxLength must be a non-negative number.");
|
|
7
|
+
}
|
|
8
|
+
if (typeof suffix !== 'string') {
|
|
9
|
+
throw new Error("Suffix must be a string.");
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
if (text.length <= maxLength) {
|
|
13
|
+
return text;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const adjustedLength = maxLength - suffix.length;
|
|
17
|
+
return text.slice(0, adjustedLength > 0 ? adjustedLength : 0) + suffix;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
export function toSlug(text) {
|
|
22
|
+
if (typeof text !== "string") {
|
|
23
|
+
throw new Error("Invalid argument. Expected a string.");
|
|
24
|
+
}
|
|
25
|
+
return text
|
|
26
|
+
.toLowerCase()
|
|
27
|
+
.trim()
|
|
28
|
+
.replace(/[\s]+/g, "-")
|
|
29
|
+
.replace(/[^\w-]+/g, "");
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
export function capitalizeWords(text) {
|
|
34
|
+
if (typeof text !== "string") {
|
|
35
|
+
throw new Error("Invalid argument. Expected a string.");
|
|
36
|
+
}
|
|
37
|
+
return text.replace(/\b\w/g, (char) => char.toUpperCase());
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function removeSpecialChars(text, replacement = '') {
|
|
41
|
+
if (typeof text !== "string") {
|
|
42
|
+
throw new Error("Invalid argument. Expected a string.");
|
|
43
|
+
}
|
|
44
|
+
if (typeof replacement !== "string") {
|
|
45
|
+
throw new Error("Replacement must be a string.");
|
|
46
|
+
}
|
|
47
|
+
return text.replace(/[^\w\s]/gi, replacement);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
export function camelCase(text) {
|
|
52
|
+
if (text == null) return '';
|
|
53
|
+
|
|
54
|
+
return text
|
|
55
|
+
.trim()
|
|
56
|
+
.toLowerCase()
|
|
57
|
+
.replace(/[^\w\s]/g, ' ')
|
|
58
|
+
.replace(/_/g, ' ')
|
|
59
|
+
.replace(/\s+/g, ' ')
|
|
60
|
+
.replace(/\s(.)/g, (_, character) => character.toUpperCase())
|
|
61
|
+
.replace(/^(.)/, (_, character) => character.toLowerCase());
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
export function pascalCase(text) {
|
|
66
|
+
if (text == null) return '';
|
|
67
|
+
|
|
68
|
+
return text
|
|
69
|
+
.trim()
|
|
70
|
+
.toLowerCase()
|
|
71
|
+
.replace(/[^\w\s]/g, ' ')
|
|
72
|
+
.replace(/_/g, ' ')
|
|
73
|
+
.replace(/\s+/g, ' ')
|
|
74
|
+
.replace(/(^|\s)(.)/g, (_, prefix, character) => character.toUpperCase())
|
|
75
|
+
.replace(/\s/g, '');
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
export function snakeCase(text) {
|
|
80
|
+
if (text == null) return '';
|
|
81
|
+
|
|
82
|
+
return text
|
|
83
|
+
.trim()
|
|
84
|
+
.replace(/[\s-]+/g, '_')
|
|
85
|
+
.replace(/([a-z])([A-Z])/g, '$1_$2')
|
|
86
|
+
.replace(/[^\w_]/g, '_')
|
|
87
|
+
.toLowerCase()
|
|
88
|
+
.replace(/_+/g, '_')
|
|
89
|
+
.replace(/^_+|_+$/g, '');
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
export function kebabCase(text) {
|
|
94
|
+
if (text == null) return '';
|
|
95
|
+
|
|
96
|
+
return text
|
|
97
|
+
.trim()
|
|
98
|
+
.replace(/[\s_]+/g, '-')
|
|
99
|
+
.replace(/([a-z])([A-Z])/g, '$1-$2')
|
|
100
|
+
.replace(/[^\w-]/g, '-')
|
|
101
|
+
.toLowerCase()
|
|
102
|
+
.replace(/-+/g, '-')
|
|
103
|
+
.replace(/^-+|-+$/g, '');
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export function titleCase(text) {
|
|
107
|
+
if (text == null) return '';
|
|
108
|
+
|
|
109
|
+
return text
|
|
110
|
+
.trim()
|
|
111
|
+
.toLowerCase()
|
|
112
|
+
.replace(/([^\w\s]|_)/g, ' ')
|
|
113
|
+
.replace(/\s+/g, ' ')
|
|
114
|
+
.replace(/(^|\s)(.)/g, (_, prefix, character) => prefix + character.toUpperCase())
|
|
115
|
+
.replace(/\s/g, ' ');
|
|
116
|
+
}
|
|
117
|
+
export function constantCase(text) {
|
|
118
|
+
if (text == null) return '';
|
|
119
|
+
|
|
120
|
+
return text
|
|
121
|
+
.trim()
|
|
122
|
+
.replace(/[\s-]+/g, '_')
|
|
123
|
+
.replace(/([a-z])([A-Z])/g, '$1_$2')
|
|
124
|
+
.replace(/[^\w_]/g, '_')
|
|
125
|
+
.toUpperCase()
|
|
126
|
+
.replace(/_+/g, '_')
|
|
127
|
+
.replace(/^_+|_+$/g, '');
|
|
128
|
+
}
|
package/validations.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export function isURL(str) {
|
|
2
|
+
try {
|
|
3
|
+
new URL(str);
|
|
4
|
+
return true;
|
|
5
|
+
} catch {
|
|
6
|
+
return false;
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
export function isEmail(str) {
|
|
12
|
+
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
|
13
|
+
return emailRegex.test(str);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
export function isEmpty(str) {
|
|
18
|
+
return str.trim().length === 0;
|
|
19
|
+
}
|