stringzy 1.1.1 → 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 +0 -0
- 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
|
Binary file
|
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
|
+
}
|