stringzy 2.2.1 → 4.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.
Files changed (190) hide show
  1. package/.github/workflows/publish.yml +29 -0
  2. package/.github/workflows/test-on-pr.yml +23 -0
  3. package/.prettierignore +4 -0
  4. package/.prettierrc +7 -0
  5. package/CONTRIBUTING.md +99 -0
  6. package/LICENSE +21 -21
  7. package/README.md +1189 -723
  8. package/changelog.txt +110 -110
  9. package/dist/analyzing/characterCount.d.ts +20 -0
  10. package/dist/analyzing/characterCount.js +28 -0
  11. package/dist/analyzing/characterFrequency.d.ts +20 -0
  12. package/dist/analyzing/characterFrequency.js +35 -0
  13. package/dist/analyzing/complexity.d.ts +39 -0
  14. package/dist/analyzing/complexity.js +63 -0
  15. package/dist/analyzing/index.d.ts +26 -0
  16. package/dist/analyzing/index.js +37 -0
  17. package/dist/analyzing/patternCount.d.ts +10 -0
  18. package/dist/analyzing/patternCount.js +52 -0
  19. package/dist/analyzing/readingDuration.d.ts +10 -0
  20. package/dist/analyzing/readingDuration.js +17 -0
  21. package/dist/analyzing/stringSimilarity.d.ts +8 -0
  22. package/dist/analyzing/stringSimilarity.js +145 -0
  23. package/dist/analyzing/vowelConsonantCount.d.ts +22 -0
  24. package/dist/analyzing/vowelConsonantCount.js +38 -0
  25. package/dist/analyzing/wordCount.d.ts +23 -0
  26. package/dist/analyzing/wordCount.js +33 -0
  27. package/dist/formatting/capitalize.d.ts +22 -0
  28. package/dist/formatting/capitalize.js +33 -0
  29. package/dist/formatting/index.d.ts +11 -0
  30. package/dist/formatting/index.js +17 -0
  31. package/dist/formatting/number.d.ts +24 -0
  32. package/dist/formatting/number.js +30 -0
  33. package/dist/formatting/phone.d.ts +25 -0
  34. package/dist/formatting/phone.js +48 -0
  35. package/dist/index.d.ts +52 -0
  36. package/dist/index.js +30 -0
  37. package/dist/tests/analyzing/characterCount.test.d.ts +1 -0
  38. package/dist/tests/analyzing/characterCount.test.js +19 -0
  39. package/dist/tests/analyzing/characterFrequency.test.d.ts +1 -0
  40. package/dist/tests/analyzing/characterFrequency.test.js +22 -0
  41. package/dist/tests/analyzing/complexity.test.d.ts +1 -0
  42. package/dist/tests/analyzing/complexity.test.js +28 -0
  43. package/dist/tests/analyzing/patternCount.test.d.ts +1 -0
  44. package/dist/tests/analyzing/patternCount.test.js +34 -0
  45. package/dist/tests/analyzing/readingDuration.test.d.ts +1 -0
  46. package/dist/tests/analyzing/readingDuration.test.js +45 -0
  47. package/dist/tests/analyzing/stringSimilarity.test.d.ts +1 -0
  48. package/dist/tests/analyzing/stringSimilarity.test.js +61 -0
  49. package/dist/tests/analyzing/vowelConsonantCount.test.d.ts +1 -0
  50. package/dist/tests/analyzing/vowelConsonantCount.test.js +25 -0
  51. package/dist/tests/analyzing/wordCount.test.d.ts +1 -0
  52. package/dist/tests/analyzing/wordCount.test.js +22 -0
  53. package/dist/tests/formatting/capitalize.test.d.ts +1 -0
  54. package/dist/tests/formatting/capitalize.test.js +22 -0
  55. package/dist/tests/formatting/number.test.d.ts +1 -0
  56. package/dist/tests/formatting/number.test.js +19 -0
  57. package/dist/tests/formatting/phone.test.d.ts +1 -0
  58. package/dist/tests/formatting/phone.test.js +25 -0
  59. package/dist/tests/transformations/camelCase.test.d.ts +1 -0
  60. package/dist/tests/transformations/camelCase.test.js +19 -0
  61. package/dist/tests/transformations/capitalizeWords.test.d.ts +1 -0
  62. package/dist/tests/transformations/capitalizeWords.test.js +19 -0
  63. package/dist/tests/transformations/constantCase.test.d.ts +1 -0
  64. package/dist/tests/transformations/constantCase.test.js +19 -0
  65. package/dist/tests/transformations/deburr.test.d.ts +1 -0
  66. package/dist/tests/transformations/deburr.test.js +27 -0
  67. package/dist/tests/transformations/escapeHTML.test.d.ts +1 -0
  68. package/dist/tests/transformations/escapeHTML.test.js +46 -0
  69. package/dist/tests/transformations/initials.test.d.ts +1 -0
  70. package/dist/tests/transformations/initials.test.js +25 -0
  71. package/dist/tests/transformations/kebabCase.test.d.ts +1 -0
  72. package/dist/tests/transformations/kebabCase.test.js +19 -0
  73. package/dist/tests/transformations/maskSegment.test.d.ts +1 -0
  74. package/dist/tests/transformations/maskSegment.test.js +30 -0
  75. package/dist/tests/transformations/numberToText.test.d.ts +1 -0
  76. package/dist/tests/transformations/numberToText.test.js +60 -0
  77. package/dist/tests/transformations/pascalCase.test.d.ts +1 -0
  78. package/dist/tests/transformations/pascalCase.test.js +19 -0
  79. package/dist/tests/transformations/removeDuplicates.test.d.ts +1 -0
  80. package/dist/tests/transformations/removeDuplicates.test.js +19 -0
  81. package/dist/tests/transformations/removeSpecialChars.test.d.ts +1 -0
  82. package/dist/tests/transformations/removeSpecialChars.test.js +22 -0
  83. package/dist/tests/transformations/removeWords.test.d.ts +1 -0
  84. package/dist/tests/transformations/removeWords.test.js +25 -0
  85. package/dist/tests/transformations/snakeCase.test.d.ts +1 -0
  86. package/dist/tests/transformations/snakeCase.test.js +19 -0
  87. package/dist/tests/transformations/splitChunks.test.d.ts +1 -0
  88. package/dist/tests/transformations/splitChunks.test.js +31 -0
  89. package/dist/tests/transformations/titleCase.test.d.ts +1 -0
  90. package/dist/tests/transformations/titleCase.test.js +19 -0
  91. package/dist/tests/transformations/toSlug.test.d.ts +1 -0
  92. package/dist/tests/transformations/toSlug.test.js +22 -0
  93. package/dist/tests/transformations/truncateText.test.d.ts +1 -0
  94. package/dist/tests/transformations/truncateText.test.js +31 -0
  95. package/dist/tests/validations/isCoordinates.test.d.ts +1 -0
  96. package/dist/tests/validations/isCoordinates.test.js +18 -0
  97. package/dist/tests/validations/isDate.test.d.ts +1 -0
  98. package/dist/tests/validations/isDate.test.js +55 -0
  99. package/dist/tests/validations/isEmail.smtpUTF8.test.d.ts +1 -0
  100. package/dist/tests/validations/isEmail.smtpUTF8.test.js +16 -0
  101. package/dist/tests/validations/isEmail.test.d.ts +1 -0
  102. package/dist/tests/validations/isEmail.test.js +67 -0
  103. package/dist/tests/validations/isEmpty.test.d.ts +1 -0
  104. package/dist/tests/validations/isEmpty.test.js +19 -0
  105. package/dist/tests/validations/isHexColor.test.d.ts +1 -0
  106. package/dist/tests/validations/isHexColor.test.js +32 -0
  107. package/dist/tests/validations/isIPv4.test.d.ts +1 -0
  108. package/dist/tests/validations/isIPv4.test.js +59 -0
  109. package/dist/tests/validations/isPalindrome.test.d.ts +1 -0
  110. package/dist/tests/validations/isPalindrome.test.js +39 -0
  111. package/dist/tests/validations/isSlug.test.d.ts +1 -0
  112. package/dist/tests/validations/isSlug.test.js +22 -0
  113. package/dist/tests/validations/isTypeOf.test.d.ts +1 -0
  114. package/dist/tests/validations/isTypeOf.test.js +28 -0
  115. package/dist/tests/validations/isURL.test.d.ts +1 -0
  116. package/dist/tests/validations/isURL.test.js +18 -0
  117. package/dist/transformations/camelCase.d.ts +25 -0
  118. package/dist/transformations/camelCase.js +39 -0
  119. package/dist/transformations/capitalizeWords.d.ts +22 -0
  120. package/dist/transformations/capitalizeWords.js +30 -0
  121. package/dist/transformations/constantCase.d.ts +27 -0
  122. package/dist/transformations/constantCase.js +41 -0
  123. package/dist/transformations/deburr.d.ts +7 -0
  124. package/dist/transformations/deburr.js +16 -0
  125. package/dist/transformations/escapeHTML.d.ts +24 -0
  126. package/dist/transformations/escapeHTML.js +36 -0
  127. package/dist/transformations/index.d.ts +52 -0
  128. package/dist/transformations/index.js +71 -0
  129. package/dist/transformations/initials.d.ts +28 -0
  130. package/dist/transformations/initials.js +49 -0
  131. package/dist/transformations/kebabCase.d.ts +27 -0
  132. package/dist/transformations/kebabCase.js +41 -0
  133. package/dist/transformations/maskSegment.d.ts +9 -0
  134. package/dist/transformations/maskSegment.js +23 -0
  135. package/dist/transformations/numberToText/helpers.d.ts +10 -0
  136. package/dist/transformations/numberToText/helpers.js +31 -0
  137. package/dist/transformations/numberToText/implementation_EN.d.ts +10 -0
  138. package/dist/transformations/numberToText/implementation_EN.js +45 -0
  139. package/dist/transformations/numberToText/implementation_PL.d.ts +10 -0
  140. package/dist/transformations/numberToText/implementation_PL.js +79 -0
  141. package/dist/transformations/numberToText/main.d.ts +19 -0
  142. package/dist/transformations/numberToText/main.js +67 -0
  143. package/dist/transformations/numberToText/types.d.ts +3 -0
  144. package/dist/transformations/numberToText/types.js +82 -0
  145. package/dist/transformations/pascalCase.d.ts +26 -0
  146. package/dist/transformations/pascalCase.js +40 -0
  147. package/dist/transformations/removeDuplicates.d.ts +22 -0
  148. package/dist/transformations/removeDuplicates.js +34 -0
  149. package/dist/transformations/removeSpecialChars.d.ts +23 -0
  150. package/dist/transformations/removeSpecialChars.js +34 -0
  151. package/dist/transformations/removeWords.d.ts +28 -0
  152. package/dist/transformations/removeWords.js +50 -0
  153. package/dist/transformations/snakeCase.d.ts +27 -0
  154. package/dist/transformations/snakeCase.js +41 -0
  155. package/dist/transformations/splitChunks.d.ts +8 -0
  156. package/dist/transformations/splitChunks.js +24 -0
  157. package/dist/transformations/titleCase.d.ts +26 -0
  158. package/dist/transformations/titleCase.js +39 -0
  159. package/dist/transformations/toSlug.d.ts +25 -0
  160. package/dist/transformations/toSlug.js +37 -0
  161. package/dist/transformations/truncateText.d.ts +26 -0
  162. package/dist/transformations/truncateText.js +44 -0
  163. package/dist/validations/index.d.ts +29 -0
  164. package/dist/validations/index.js +41 -0
  165. package/dist/validations/isCoordinates.d.ts +8 -0
  166. package/dist/validations/isCoordinates.js +19 -0
  167. package/dist/validations/isDate.d.ts +18 -0
  168. package/dist/validations/isDate.js +61 -0
  169. package/dist/validations/isEmail.d.ts +13 -0
  170. package/dist/validations/isEmail.js +180 -0
  171. package/dist/validations/isEmpty.d.ts +10 -0
  172. package/dist/validations/isEmpty.js +15 -0
  173. package/dist/validations/isHexColor.d.ts +7 -0
  174. package/dist/validations/isHexColor.js +15 -0
  175. package/dist/validations/isIPv4.d.ts +22 -0
  176. package/dist/validations/isIPv4.js +33 -0
  177. package/dist/validations/isPalindrome.d.ts +10 -0
  178. package/dist/validations/isPalindrome.js +21 -0
  179. package/dist/validations/isSlug.d.ts +28 -0
  180. package/dist/validations/isSlug.js +37 -0
  181. package/dist/validations/isTypeOf.d.ts +9 -0
  182. package/dist/validations/isTypeOf.js +30 -0
  183. package/dist/validations/isURL.d.ts +22 -0
  184. package/dist/validations/isURL.js +33 -0
  185. package/package.json +12 -4
  186. package/analysis.js +0 -21
  187. package/formatting.js +0 -37
  188. package/index.js +0 -75
  189. package/transformations.js +0 -190
  190. package/validations.js +0 -80
@@ -0,0 +1,22 @@
1
+ /**
2
+ * Checks if a given string is a valid IPv4 address.
3
+ *
4
+ * Valid IPv4 addresses consist of four decimal numbers (0-255) separated by dots.
5
+ * Leading zeros are not allowed (e.g., "01" is invalid).
6
+ *
7
+ * @param {string} str - The string to validate as an IPv4 address.
8
+ * @returns {boolean} `true` if the string is a valid IPv4 address, otherwise `false`.
9
+ *
10
+ * @example
11
+ * isIPv4("192.168.1.1"); // true
12
+ *
13
+ * @example
14
+ * isIPv4("255.255.255.255"); // true
15
+ *
16
+ * @example
17
+ * isIPv4("256.100.50.0"); // false (256 is out of range)
18
+ *
19
+ * @example
20
+ * isIPv4("192.168.1"); // false (not enough parts)
21
+ */
22
+ export declare function isIPv4(str: string): boolean;
@@ -0,0 +1,33 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isIPv4 = isIPv4;
4
+ /**
5
+ * Checks if a given string is a valid IPv4 address.
6
+ *
7
+ * Valid IPv4 addresses consist of four decimal numbers (0-255) separated by dots.
8
+ * Leading zeros are not allowed (e.g., "01" is invalid).
9
+ *
10
+ * @param {string} str - The string to validate as an IPv4 address.
11
+ * @returns {boolean} `true` if the string is a valid IPv4 address, otherwise `false`.
12
+ *
13
+ * @example
14
+ * isIPv4("192.168.1.1"); // true
15
+ *
16
+ * @example
17
+ * isIPv4("255.255.255.255"); // true
18
+ *
19
+ * @example
20
+ * isIPv4("256.100.50.0"); // false (256 is out of range)
21
+ *
22
+ * @example
23
+ * isIPv4("192.168.1"); // false (not enough parts)
24
+ */
25
+ function isIPv4(str) {
26
+ const parts = str.split('.');
27
+ if (parts.length !== 4)
28
+ return false;
29
+ return parts.every((part) => {
30
+ const num = parseInt(part, 10);
31
+ return !isNaN(num) && num >= 0 && num <= 255 && part === num.toString();
32
+ });
33
+ }
@@ -0,0 +1,10 @@
1
+ /**
2
+ * Checks whether the given string is a palindrome.
3
+ *
4
+ * The check is case-insensitive and ignores all non-alphanumeric characters.
5
+ *
6
+ * @param {string} str - The input string to check.
7
+ * @returns {boolean} True if the input is a palindrome, false otherwise.
8
+ * @throws {TypeError} If the input is not a string.
9
+ */
10
+ export declare function isPalindrome(str: string): boolean;
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isPalindrome = isPalindrome;
4
+ /**
5
+ * Checks whether the given string is a palindrome.
6
+ *
7
+ * The check is case-insensitive and ignores all non-alphanumeric characters.
8
+ *
9
+ * @param {string} str - The input string to check.
10
+ * @returns {boolean} True if the input is a palindrome, false otherwise.
11
+ * @throws {TypeError} If the input is not a string.
12
+ */
13
+ function isPalindrome(str) {
14
+ if (typeof str !== 'string') {
15
+ throw new TypeError('Input must be a string');
16
+ }
17
+ const sanitized = str
18
+ .toLowerCase()
19
+ .replace(/[^a-z0-9]/gi, '');
20
+ return sanitized === sanitized.split('').reverse().join('');
21
+ }
@@ -0,0 +1,28 @@
1
+ /**
2
+ * Checks if a given string is a valid slug.
3
+ *
4
+ * A valid slug:
5
+ * - Contains only lowercase letters (`a-z`), numbers (`0-9`), and hyphens (`-`).
6
+ * - Does not start or end with a hyphen.
7
+ * - Does not contain consecutive hyphens.
8
+ * - Is not an empty string.
9
+ *
10
+ * @param {string} str - The string to validate as a slug.
11
+ * @returns {boolean} `true` if the string is a valid slug, otherwise `false`.
12
+ *
13
+ * @example
14
+ * isSlug("valid-slug-123"); // true
15
+ *
16
+ * @example
17
+ * isSlug("Invalid-Slug"); // false (uppercase letters)
18
+ *
19
+ * @example
20
+ * isSlug("-starts-with-hyphen"); // false
21
+ *
22
+ * @example
23
+ * isSlug("ends-with-hyphen-"); // false
24
+ *
25
+ * @example
26
+ * isSlug("double--hyphen"); // false
27
+ */
28
+ export declare function isSlug(str: string): boolean;
@@ -0,0 +1,37 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isSlug = isSlug;
4
+ /**
5
+ * Checks if a given string is a valid slug.
6
+ *
7
+ * A valid slug:
8
+ * - Contains only lowercase letters (`a-z`), numbers (`0-9`), and hyphens (`-`).
9
+ * - Does not start or end with a hyphen.
10
+ * - Does not contain consecutive hyphens.
11
+ * - Is not an empty string.
12
+ *
13
+ * @param {string} str - The string to validate as a slug.
14
+ * @returns {boolean} `true` if the string is a valid slug, otherwise `false`.
15
+ *
16
+ * @example
17
+ * isSlug("valid-slug-123"); // true
18
+ *
19
+ * @example
20
+ * isSlug("Invalid-Slug"); // false (uppercase letters)
21
+ *
22
+ * @example
23
+ * isSlug("-starts-with-hyphen"); // false
24
+ *
25
+ * @example
26
+ * isSlug("ends-with-hyphen-"); // false
27
+ *
28
+ * @example
29
+ * isSlug("double--hyphen"); // false
30
+ */
31
+ function isSlug(str) {
32
+ if (typeof str !== 'string' || str.length === 0) {
33
+ return false;
34
+ }
35
+ const slugRegex = /^[a-z0-9]+(-[a-z0-9]+)*$/;
36
+ return slugRegex.test(str);
37
+ }
@@ -0,0 +1,9 @@
1
+ /**
2
+ * Checks if a file name or URL string has a valid extension for a given type.
3
+ *
4
+ * @param {string} input - File name or URL.
5
+ * @param {string} type - The type category to validate.
6
+ * @returns {boolean}
7
+ * @throws error when input is not string or type is not defined
8
+ */
9
+ export declare function isTypeOf(input: string, type: string): boolean;
@@ -0,0 +1,30 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isTypeOf = isTypeOf;
4
+ /**
5
+ * Checks if a file name or URL string has a valid extension for a given type.
6
+ *
7
+ * @param {string} input - File name or URL.
8
+ * @param {string} type - The type category to validate.
9
+ * @returns {boolean}
10
+ * @throws error when input is not string or type is not defined
11
+ */
12
+ function isTypeOf(input, type) {
13
+ var _a;
14
+ if (typeof input !== "string" || typeof type !== "string")
15
+ return false;
16
+ // Gets the substring after the last (.), then converts to lowercase, removes (?) and (#)
17
+ // Example: "image.png?version=2#top", returns "png"
18
+ const extension = (_a = input.split(".").pop()) === null || _a === void 0 ? void 0 : _a.toLowerCase().split("?")[0].split("#")[0];
19
+ if (!extension)
20
+ return false;
21
+ const typeMap = {
22
+ image: ["png", "jpg", "jpeg", "gif", "bmp", "svg", "webp", "ico"],
23
+ video: ["mp4", "webm", "avi", "mkv", "mov", "flv"],
24
+ audio: ["mp3", "wav", "ogg", "flac", "aac"],
25
+ document: ["pdf", "doc", "docx", "xls", "xlsx", "ppt", "pptx", "txt", "md"],
26
+ archive: ["zip", "rar", "tar", "gz", "7z"],
27
+ };
28
+ const allowed = typeMap[type.toLowerCase()];
29
+ return allowed ? allowed.includes(extension) : false;
30
+ }
@@ -0,0 +1,22 @@
1
+ /**
2
+ * Checks if a given string is a valid URL.
3
+ *
4
+ * Uses the built-in `URL` constructor to attempt parsing the input string.
5
+ * Returns `true` if the string is a valid URL, otherwise `false`.
6
+ *
7
+ * @param {string} str - The string to validate as a URL.
8
+ * @returns {boolean} `true` if the string is a valid URL, otherwise `false`.
9
+ *
10
+ * @example
11
+ * isURL("https://www.example.com"); // true
12
+ *
13
+ * @example
14
+ * isURL("ftp://ftp.example.com/resource.txt"); // true
15
+ *
16
+ * @example
17
+ * isURL("not a url"); // false
18
+ *
19
+ * @example
20
+ * isURL("http:/invalid-url"); // false
21
+ */
22
+ export declare function isURL(str: string): boolean;
@@ -0,0 +1,33 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isURL = isURL;
4
+ /**
5
+ * Checks if a given string is a valid URL.
6
+ *
7
+ * Uses the built-in `URL` constructor to attempt parsing the input string.
8
+ * Returns `true` if the string is a valid URL, otherwise `false`.
9
+ *
10
+ * @param {string} str - The string to validate as a URL.
11
+ * @returns {boolean} `true` if the string is a valid URL, otherwise `false`.
12
+ *
13
+ * @example
14
+ * isURL("https://www.example.com"); // true
15
+ *
16
+ * @example
17
+ * isURL("ftp://ftp.example.com/resource.txt"); // true
18
+ *
19
+ * @example
20
+ * isURL("not a url"); // false
21
+ *
22
+ * @example
23
+ * isURL("http:/invalid-url"); // false
24
+ */
25
+ function isURL(str) {
26
+ try {
27
+ new URL(str);
28
+ return true;
29
+ }
30
+ catch (_a) {
31
+ return false;
32
+ }
33
+ }
package/package.json CHANGED
@@ -1,10 +1,13 @@
1
1
  {
2
2
  "name": "stringzy",
3
- "version": "2.2.1",
4
- "main": "index.js",
5
- "type": "module",
3
+ "version": "4.0.0",
4
+ "main": "dist/index.js",
5
+ "types": "dist/index.d.ts",
6
6
  "scripts": {
7
- "test": "echo \"Error: no test specified\" && exit 1"
7
+ "build": "tsc",
8
+ "test": "npm run build && node --test",
9
+ "prepublishOnly": "npm run build",
10
+ "format": "prettier --write ."
8
11
  },
9
12
  "author": "Samarth Ruia",
10
13
  "readmeFilename": "README.md",
@@ -40,5 +43,10 @@
40
43
  "repository": {
41
44
  "type": "git",
42
45
  "url": "git+https://github.com/Samarth2190/stringzy.git"
46
+ },
47
+ "devDependencies": {
48
+ "@types/node": "^24.0.4",
49
+ "prettier": "^3.6.2",
50
+ "typescript": "^5.8.3"
43
51
  }
44
52
  }
package/analysis.js DELETED
@@ -1,21 +0,0 @@
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/formatting.js DELETED
@@ -1,37 +0,0 @@
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 DELETED
@@ -1,75 +0,0 @@
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.2.1
11
- */
12
-
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
- removeWords,
31
- initials,
32
- removeDuplicates
33
- } = transformations;
34
-
35
- export const {
36
- isURL,
37
- isEmail,
38
- isEmpty,
39
- isDate,
40
- } = validations;
41
-
42
- export const {
43
- wordCount,
44
- characterCount,
45
- characterFrequency
46
- } = analysis;
47
-
48
- export const {
49
- capitalize,
50
- formatNumber,
51
- formatPhone
52
- } = formatting;
53
-
54
- export const transform = transformations;
55
- export const validate = validations;
56
- export const analyze = analysis;
57
- export const format = formatting;
58
-
59
-
60
- export default {
61
- ...transformations,
62
- ...validations,
63
- ...analysis,
64
- ...formatting,
65
-
66
- transform: transformations,
67
- validate: validations,
68
- analyze: analysis,
69
- format: formatting,
70
-
71
- transform,
72
- validate,
73
- analyze,
74
- format,
75
- };
@@ -1,190 +0,0 @@
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
- }
129
-
130
- export function removeWords(str, wordsToRemove) {
131
- if (str === null || str === undefined) {
132
- throw new Error('Input string cannot be null or undefined');
133
- }
134
- if (typeof str !== 'string') {
135
- throw new Error('First parameter must be a string');
136
- }
137
- if (wordsToRemove === null || wordsToRemove === undefined) {
138
- throw new Error('Words to remove cannot be null or undefined');
139
- }
140
- if (typeof wordsToRemove !== 'string' && !Array.isArray(wordsToRemove)) {
141
- throw new Error('Second parameter must be a string or an array of strings');
142
- }
143
- if (str === '') {
144
- return '';
145
- }
146
- const wordsArray = Array.isArray(wordsToRemove) ? wordsToRemove : [wordsToRemove];
147
- const regex = new RegExp(`\\b(${wordsArray.join('|')})\\b`, 'gi');
148
- return str.replace(regex, '').replace(/\s+/g, ' ').trim();
149
- }
150
-
151
- export function initials(text, limit) {
152
- if (typeof text !== "string") {
153
- throw new Error("Input must be a string");
154
- }
155
-
156
- if (limit !== undefined && (typeof limit !== "number" || isNaN(limit))) {
157
- throw new Error("Limit must be a valid number");
158
- }
159
-
160
- if (limit < 0) {
161
- throw new Error("Limit must be a non-negative number");
162
- }
163
-
164
- const words = text
165
- .trim()
166
- .split(/\s+/)
167
- .filter((word) => word.length > 0);
168
-
169
- if (words.length === 0) return "";
170
-
171
- const initialsArray = words
172
- .map((word) => word.charAt(0))
173
- .slice(0, limit ?? words.length);
174
-
175
- return initialsArray.join("");
176
- }
177
-
178
- export function removeDuplicates(text) {
179
- if (typeof text !== "string") {
180
- throw new Error("Input must be a string");
181
- }
182
-
183
- const wordSet = new Set();
184
-
185
- text.split(" ").forEach((word) => {
186
- wordSet.add(word);
187
- });
188
-
189
- return Array.from(wordSet).join(" ");
190
- }
package/validations.js DELETED
@@ -1,80 +0,0 @@
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
- }
20
-
21
-
22
-
23
- export function isDate(input) {
24
-
25
- //console.log("True")
26
-
27
-
28
-
29
- if (typeof input !== "string") return false;
30
-
31
- const formats = [
32
- {
33
- // Format: YYYY-MM-DD
34
- regex: /^\d{4}-\d{2}-\d{2}$/,
35
- split: (s) => s.split("-").map(Number),
36
- order: ["Y", "M", "D"],
37
- },
38
- {
39
- // Format: MM-DD-YYYY
40
- regex: /^\d{2}-\d{2}-\d{4}$/,
41
- split: (s) => s.split("-").map(Number),
42
- order: ["M", "D", "Y"],
43
- },
44
- {
45
- // Format: DD-MM-YYYY
46
- regex: /^\d{2}-\d{2}-\d{4}$/,
47
- split: (s) => s.split("-").map(Number),
48
- order: ["D", "M", "Y"],
49
- },
50
- {
51
- // Format: DD/MM/YYYY
52
- regex: /^\d{2}\/\d{2}\/\d{4}$/,
53
- split: (s) => s.split("/").map(Number),
54
- order: ["D", "M", "Y"],
55
- },
56
- {
57
- // Format: YYYY/MM/DD
58
- regex: /^\d{4}\/\d{2}\/\d{2}$/,
59
- split: (s) => s.split("/").map(Number),
60
- order: ["Y", "M", "D"],
61
- },
62
- ];
63
-
64
- for (const format of formats) {
65
- if (format.regex.test(input)) {
66
- const parts = format.split(input);
67
- const dateParts = {};
68
- format.order.forEach((key, i) => {
69
- dateParts[key] = parts[i];
70
- });
71
-
72
- const { Y, M, D } = dateParts;
73
- const date = new Date(Y, M - 1, D); // JS months are 0-indexed
74
- return date.getFullYear() === Y && date.getMonth() === M -1 && date.getDate() === D;
75
- }
76
- }
77
-
78
- return false;
79
- }
80
-