stringzy 1.1.2 → 2.0.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 CHANGED
@@ -5,15 +5,16 @@
5
5
  ![License](https://img.shields.io/npm/l/stringzy)
6
6
  ![Bundle Size](https://img.shields.io/bundlephobia/min/stringzy)
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 * as stringzy from 'stringzy';
36
+ import stringzy from 'stringzy';
36
37
 
37
38
  // Or import specific functions
38
- import { truncateText, toSlug } from 'stringzy';
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
- ### `truncateText(text, maxLength, suffix = '...')`
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
- ### `toSlug(text)`
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
- ### `capitalizeWords(text)`
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
- ### `removeSpecialChars(text, replacement = '')`
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 { truncateText, capitalizeWords } from 'stringzy';
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>{capitalizeWords(title)}</h2>
145
- <p>{truncateText(content, 150)}</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
- ### In a Node.js application
457
+ ### Form Validation
152
458
 
153
459
  ```javascript
154
- const { toSlug } = require('stringzy');
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
- function createPermalink(title) {
157
- const timestamp = Date.now();
158
- const slug = toSlug(title);
159
- return `${timestamp}-${slug}`;
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
- const permalink = createPermalink('New Blog Post Title!');
163
- // Returns something like: '1620000000000-new-blog-post-title'
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 { truncateText } from 'stringzy';
515
+ import { validate, analyze, format } from 'stringzy';
172
516
 
173
517
  // TypeScript will provide proper type checking
174
- const truncated: string = truncateText('Hello, world!', 5);
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 simple yet powerful string manipulation utilities.
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,83 @@
1
+ CHANGELOG
2
+
3
+ All notable changes to the `stringzy` package will be documented in this file.
4
+
5
+ =============================================================================
6
+ Version 2.0.1 - 2025-05-23
7
+ -----------------------------------------------------------------------------
8
+
9
+ FIXED:
10
+ - Fixed README.MD table of contents for transformations.
11
+
12
+
13
+
14
+ =============================================================================
15
+ Version 2.0.0 - 2025-05-22
16
+ -----------------------------------------------------------------------------
17
+
18
+ ADDED:
19
+ - Added a dynamic list of APIs in README
20
+ - Divided the package into 4 modules-transformations, validations, formatting and analysis
21
+ - Added many new APIs
22
+ - `camelCase`: Converts a string to camel case
23
+ - `kebabCase`: Converts a string to kebab case
24
+ - `snakeCase`: Converts a string to snake case
25
+ - `titleCase`: Converts a string to title case
26
+ - `constantCase`: Converts a string to constant case
27
+ - `pascalCase`: Converts a string to pascal case
28
+ - `isURL`: Validates if a string is a valid URL
29
+ - `isEmail`: Validates if a string is a valid email address
30
+ - `isEmpty`: Checks if a string is empty or contains only whitespace
31
+ - `wordCount`: Counts the number of words in a string
32
+ - `characterCount`: Counts the number of characters in a string
33
+ - `characterFrequency`: Calculates the frequency of each character in a string
34
+ - `capitalize`: Capitalizes the entire string
35
+ - `formatNumber`: Formats a number with commas as thousands separators
36
+ - `formatPhone`: Formats a phone number to a standard format
37
+
38
+ =============================================================================
39
+
40
+ Version 1.1.2 - 2025-05-15
41
+ -----------------------------------------------------------------------------
42
+
43
+ ADDED:
44
+ - Improved documentation with comprehensive README
45
+ - Enhanced examples for all utility functions
46
+
47
+ FIXED:
48
+ - Resolved documentation rendering issues on npm website
49
+
50
+ =============================================================================
51
+
52
+ Version 1.1.1 - 2025-05-15
53
+ -----------------------------------------------------------------------------
54
+
55
+ CHANGED:
56
+ - Updated package metadata
57
+
58
+ FIXED:
59
+ - Minor bug fixes in string handling edge cases
60
+
61
+ =============================================================================
62
+
63
+ Version 1.1.0 - 2025-05-15
64
+ -----------------------------------------------------------------------------
65
+
66
+ ADDED:
67
+ - Added extensive API documentation
68
+
69
+ CHANGED:
70
+ - Improved performance for `toSlug` function
71
+ - Enhanced error handling across all utility functions
72
+
73
+ =============================================================================
74
+
75
+ Version 1.0.0 - 2025-05-14
76
+ -----------------------------------------------------------------------------
77
+
78
+ ADDED:
79
+ - Initial release with four core utility functions:
80
+ - `truncateText`: Truncates text to a specified length with optional suffix
81
+ - `toSlug`: Converts text to URL-friendly slug format
82
+ - `capitalizeWords`: Capitalizes the first letter of each word
83
+ - `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
- 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
-
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
- 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
-
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
- 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
- }
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
- 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
-
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": "1.1.2",
3
+ "version": "2.0.1",
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
- "cli",
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
+ }