@arkyn/shared 3.0.1-beta.20 → 3.0.1-beta.201

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 (174) hide show
  1. package/README.md +344 -69
  2. package/dist/formats/formatDate.d.ts +18 -26
  3. package/dist/formats/formatDate.d.ts.map +1 -1
  4. package/dist/formats/formatJsonObject.d.ts +5 -7
  5. package/dist/formats/formatJsonObject.d.ts.map +1 -1
  6. package/dist/formats/formatJsonString.d.ts +5 -21
  7. package/dist/formats/formatJsonString.d.ts.map +1 -1
  8. package/dist/formats/formatToCapitalizeFirstWordLetter.d.ts +5 -24
  9. package/dist/formats/formatToCapitalizeFirstWordLetter.d.ts.map +1 -1
  10. package/dist/formats/formatToCep.d.ts +3 -11
  11. package/dist/formats/formatToCep.d.ts.map +1 -1
  12. package/dist/formats/formatToCnpj.d.ts +3 -14
  13. package/dist/formats/formatToCnpj.d.ts.map +1 -1
  14. package/dist/formats/formatToCpf.d.ts +4 -13
  15. package/dist/formats/formatToCpf.d.ts.map +1 -1
  16. package/dist/formats/formatToCurrency.d.ts +10 -23
  17. package/dist/formats/formatToCurrency.d.ts.map +1 -1
  18. package/dist/formats/formatToEllipsis.d.ts +5 -5
  19. package/dist/formats/formatToEllipsis.d.ts.map +1 -1
  20. package/dist/formats/formatToHiddenDigits.d.ts +15 -26
  21. package/dist/formats/formatToHiddenDigits.d.ts.map +1 -1
  22. package/dist/formats/formatToPhone.d.ts +11 -23
  23. package/dist/formats/formatToPhone.d.ts.map +1 -1
  24. package/dist/generators/generateColorByString.d.ts +6 -4
  25. package/dist/generators/generateColorByString.d.ts.map +1 -1
  26. package/dist/generators/generateId.d.ts +8 -19
  27. package/dist/generators/generateId.d.ts.map +1 -1
  28. package/dist/generators/generateSlug.d.ts +8 -2
  29. package/dist/generators/generateSlug.d.ts.map +1 -1
  30. package/dist/index.d.ts +11 -16
  31. package/dist/index.d.ts.map +1 -1
  32. package/dist/index.js +371 -34
  33. package/dist/modules/formats/formatDate.js +35 -0
  34. package/dist/modules/formats/formatJsonObject.js +23 -0
  35. package/dist/modules/formats/formatJsonString.js +11 -0
  36. package/dist/modules/formats/formatToCapitalizeFirstWordLetter.js +6 -0
  37. package/dist/modules/formats/formatToCep.js +9 -0
  38. package/dist/modules/formats/formatToCnpj.js +9 -0
  39. package/dist/modules/formats/formatToCpf.js +9 -0
  40. package/dist/modules/formats/formatToCurrency.js +13 -0
  41. package/dist/modules/formats/formatToEllipsis.js +10 -0
  42. package/dist/modules/formats/formatToHiddenDigits.js +22 -0
  43. package/dist/modules/formats/formatToPhone.js +14 -0
  44. package/dist/modules/generators/generateColorByString.js +9 -0
  45. package/dist/modules/generators/generateId.js +32 -0
  46. package/dist/modules/generators/generateSlug.js +7 -0
  47. package/dist/modules/index.js +26 -0
  48. package/dist/modules/parsers/parseLargeFields.js +17 -0
  49. package/dist/modules/parsers/parseSensitiveData.js +28 -0
  50. package/dist/modules/parsers/parseToDate.js +23 -0
  51. package/dist/modules/services/validateDateService.js +61 -0
  52. package/dist/modules/utilities/calculateCardInstallment.js +17 -0
  53. package/dist/modules/utilities/ensureQuotes.js +7 -0
  54. package/dist/modules/utilities/findCountryMask.js +23 -0
  55. package/dist/modules/utilities/isHtml.js +6 -0
  56. package/dist/modules/utilities/removeCurrencySymbols.js +6 -0
  57. package/dist/modules/utilities/removeNonNumeric.js +6 -0
  58. package/dist/modules/utilities/stripHtmlTags.js +6 -0
  59. package/dist/{services/truncateLargeFields.d.ts → parsers/parseLargeFields.d.ts} +5 -8
  60. package/dist/parsers/parseLargeFields.d.ts.map +1 -0
  61. package/dist/parsers/parseSensitiveData.d.ts +22 -0
  62. package/dist/parsers/parseSensitiveData.d.ts.map +1 -0
  63. package/dist/parsers/parseToDate.d.ts +25 -0
  64. package/dist/parsers/parseToDate.d.ts.map +1 -0
  65. package/dist/services/validateDateService.d.ts +34 -0
  66. package/dist/services/validateDateService.d.ts.map +1 -0
  67. package/dist/utilities/calculateCardInstallment.d.ts +25 -0
  68. package/dist/utilities/calculateCardInstallment.d.ts.map +1 -0
  69. package/dist/utilities/ensureQuotes.d.ts +20 -0
  70. package/dist/utilities/ensureQuotes.d.ts.map +1 -0
  71. package/dist/utilities/findCountryMask.d.ts +21 -0
  72. package/dist/utilities/findCountryMask.d.ts.map +1 -0
  73. package/dist/utilities/isHtml.d.ts +21 -0
  74. package/dist/utilities/isHtml.d.ts.map +1 -0
  75. package/dist/{services/removeCurrencySymbols.js → utilities/removeCurrencySymbols.d.ts} +6 -7
  76. package/dist/utilities/removeCurrencySymbols.d.ts.map +1 -0
  77. package/dist/utilities/removeNonNumeric.d.ts +15 -0
  78. package/dist/utilities/removeNonNumeric.d.ts.map +1 -0
  79. package/dist/utilities/stripHtmlTags.d.ts +18 -0
  80. package/dist/utilities/stripHtmlTags.d.ts.map +1 -0
  81. package/package.json +183 -13
  82. package/dist/formats/formatDate.js +0 -66
  83. package/dist/formats/formatJsonObject.js +0 -90
  84. package/dist/formats/formatJsonString.js +0 -46
  85. package/dist/formats/formatToCapitalizeFirstWordLetter.js +0 -42
  86. package/dist/formats/formatToCep.js +0 -34
  87. package/dist/formats/formatToCnpj.js +0 -35
  88. package/dist/formats/formatToCpf.js +0 -35
  89. package/dist/formats/formatToCpfCnpj.d.ts +0 -24
  90. package/dist/formats/formatToCpfCnpj.d.ts.map +0 -1
  91. package/dist/formats/formatToCpfCnpj.js +0 -33
  92. package/dist/formats/formatToCurrency.js +0 -41
  93. package/dist/formats/formatToDate.d.ts +0 -32
  94. package/dist/formats/formatToDate.d.ts.map +0 -1
  95. package/dist/formats/formatToDate.js +0 -50
  96. package/dist/formats/formatToEllipsis.js +0 -21
  97. package/dist/formats/formatToHiddenDigits.js +0 -60
  98. package/dist/formats/formatToPhone.js +0 -131
  99. package/dist/generators/generateColorByString.js +0 -25
  100. package/dist/generators/generateId.js +0 -29
  101. package/dist/generators/generateSlug.js +0 -25
  102. package/dist/services/calculateCardInstallment.d.ts +0 -40
  103. package/dist/services/calculateCardInstallment.d.ts.map +0 -1
  104. package/dist/services/calculateCardInstallment.js +0 -55
  105. package/dist/services/ensureQuotes.d.ts +0 -14
  106. package/dist/services/ensureQuotes.d.ts.map +0 -1
  107. package/dist/services/ensureQuotes.js +0 -19
  108. package/dist/services/maskSensitiveData.d.ts +0 -26
  109. package/dist/services/maskSensitiveData.d.ts.map +0 -1
  110. package/dist/services/maskSensitiveData.js +0 -60
  111. package/dist/services/removeCurrencySymbols.d.ts +0 -21
  112. package/dist/services/removeCurrencySymbols.d.ts.map +0 -1
  113. package/dist/services/removeNonNumeric.d.ts +0 -16
  114. package/dist/services/removeNonNumeric.d.ts.map +0 -1
  115. package/dist/services/removeNonNumeric.js +0 -16
  116. package/dist/services/stripHtmlTags.d.ts +0 -16
  117. package/dist/services/stripHtmlTags.d.ts.map +0 -1
  118. package/dist/services/stripHtmlTags.js +0 -16
  119. package/dist/services/truncateLargeFields.d.ts.map +0 -1
  120. package/dist/services/truncateLargeFields.js +0 -57
  121. package/dist/validations/validateCep.d.ts +0 -24
  122. package/dist/validations/validateCep.d.ts.map +0 -1
  123. package/dist/validations/validateCep.js +0 -33
  124. package/dist/validations/validateCnpj.d.ts +0 -22
  125. package/dist/validations/validateCnpj.d.ts.map +0 -1
  126. package/dist/validations/validateCnpj.js +0 -52
  127. package/dist/validations/validateCpf.d.ts +0 -24
  128. package/dist/validations/validateCpf.d.ts.map +0 -1
  129. package/dist/validations/validateCpf.js +0 -54
  130. package/dist/validations/validateDate.d.ts +0 -34
  131. package/dist/validations/validateDate.d.ts.map +0 -1
  132. package/dist/validations/validateDate.js +0 -73
  133. package/dist/validations/validatePassword.d.ts +0 -21
  134. package/dist/validations/validatePassword.d.ts.map +0 -1
  135. package/dist/validations/validatePassword.js +0 -34
  136. package/dist/validations/validatePhone.d.ts +0 -29
  137. package/dist/validations/validatePhone.d.ts.map +0 -1
  138. package/dist/validations/validatePhone.js +0 -44
  139. package/dist/validations/validateRg.d.ts +0 -22
  140. package/dist/validations/validateRg.d.ts.map +0 -1
  141. package/dist/validations/validateRg.js +0 -31
  142. package/src/formats/formatDate.ts +0 -92
  143. package/src/formats/formatJsonObject.ts +0 -90
  144. package/src/formats/formatJsonString.ts +0 -50
  145. package/src/formats/formatToCapitalizeFirstWordLetter.ts +0 -46
  146. package/src/formats/formatToCep.ts +0 -39
  147. package/src/formats/formatToCnpj.ts +0 -40
  148. package/src/formats/formatToCpf.ts +0 -40
  149. package/src/formats/formatToCpfCnpj.ts +0 -38
  150. package/src/formats/formatToCurrency.ts +0 -63
  151. package/src/formats/formatToDate.ts +0 -70
  152. package/src/formats/formatToEllipsis.ts +0 -25
  153. package/src/formats/formatToHiddenDigits.ts +0 -92
  154. package/src/formats/formatToPhone.ts +0 -170
  155. package/src/generators/generateColorByString.ts +0 -33
  156. package/src/generators/generateId.ts +0 -61
  157. package/src/generators/generateSlug.ts +0 -31
  158. package/src/index.ts +0 -37
  159. package/src/services/calculateCardInstallment.ts +0 -73
  160. package/src/services/ensureQuotes.ts +0 -25
  161. package/src/services/maskSensitiveData.ts +0 -68
  162. package/src/services/removeCurrencySymbols.ts +0 -29
  163. package/src/services/removeNonNumeric.ts +0 -20
  164. package/src/services/stripHtmlTags.ts +0 -20
  165. package/src/services/truncateLargeFields.ts +0 -69
  166. package/src/validations/validateCep.ts +0 -41
  167. package/src/validations/validateCnpj.ts +0 -65
  168. package/src/validations/validateCpf.ts +0 -62
  169. package/src/validations/validateDate.ts +0 -86
  170. package/src/validations/validatePassword.ts +0 -41
  171. package/src/validations/validatePhone.ts +0 -50
  172. package/src/validations/validateRg.ts +0 -37
  173. package/tsconfig.json +0 -20
  174. package/vitest.config.ts +0 -5
package/README.md CHANGED
@@ -1,119 +1,394 @@
1
- The `@arkyn/shared` package provides reusable utilities such as formatting functions, generators, services, and validations. It is designed to be used across different parts of a project, offering common and practical solutions to streamline development.
1
+ # @arkyn/shared
2
2
 
3
- ---
3
+ Comprehensive collection of reusable utilities for data formatting, validation, generation, and manipulation, featuring Brazilian document validators and financial tools.
4
4
 
5
- ## Installation
5
+ [![npm version](https://img.shields.io/npm/v/@arkyn/shared.svg)](https://www.npmjs.com/package/@arkyn/shared)
6
+ [![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)
7
+ [![TypeScript](https://img.shields.io/badge/TypeScript-Ready-blue.svg)](https://www.typescriptlang.org/)
6
8
 
7
- Install the package using npm:
9
+ ## 🎯 What it solves
10
+
11
+ `@arkyn/shared` is a dependency-light, framework-agnostic toolkit for formatting, validating, generating, and parsing common data types in JavaScript/TypeScript applications. It has strong support for Brazilian documents (CPF, CNPJ, CEP) and locale-aware financial formatting, alongside general-purpose helpers for dates, JSON, strings, IDs, and sensitive-data masking. Because it has no framework assumptions, the same functions run identically on the client and the server — it's the shared foundation used by both `@arkyn/components` and `@arkyn/server`.
12
+
13
+ ## ✨ Features
14
+
15
+ - 📅 **Date formatting & parsing** — configurable input/output formats with timezone shifting
16
+ - 💰 **Currency & financial formatting** — locale-aware currency strings and card installment math
17
+ - 📝 **Text formatting** — capitalization, ellipsis truncation, digit masking
18
+ - 🇧🇷 **Brazilian document formatting** — CPF, CNPJ, CEP
19
+ - 📞 **Phone formatting** — country-mask-aware phone number formatting via `libphonenumber-js`
20
+ - 🔧 **Generators** — UUID (v4/v7) IDs, URL-friendly slugs, deterministic colors from strings
21
+ - 🧩 **JSON parsers** — pretty-printing, large-field truncation, sensitive-key masking
22
+ - 🛡️ **Sensitive-data masking** — hide digits/fields in strings and JSON payloads
23
+ - 🌐 **HTML utilities** — detect and strip HTML markup
24
+ - 🧹 **String/number utilities** — strip non-numeric characters, strip currency symbols, ensure quoting
25
+
26
+ ## 📋 Prerequisites
27
+
28
+ - **Node.js** `>=24.16.0`
29
+ - **Bun** `>=1.3.14`
30
+ - **`libphonenumber-js`** `>=1.13.7` — optional peer dependency, required only if you use `formatToPhone` or `findCountryMask`
31
+
32
+ ## 📦 Installation
33
+
34
+ > **ESM only.** This package ships as native ES modules with no CommonJS build — use `import`, not `require()`.
8
35
 
9
36
  ```bash
10
37
  npm install @arkyn/shared
11
38
  ```
12
39
 
13
- ---
40
+ If you plan to use `formatToPhone` or `findCountryMask`, also install the peer dependency:
41
+
42
+ ```bash
43
+ npm install libphonenumber-js
44
+ ```
45
+
46
+ ## 🚀 Quick Start
47
+
48
+ ```typescript
49
+ import {
50
+ formatToCpf,
51
+ formatToCurrency,
52
+ generateId,
53
+ generateSlug,
54
+ } from "@arkyn/shared";
55
+
56
+ // Format a raw CPF string (throws if it doesn't have 11 digits)
57
+ const cpf = formatToCpf("12345678909");
58
+ // "123.456.789-09"
59
+
60
+ // Format currency using a currency code from @arkyn/templates
61
+ const price = formatToCurrency(1234.56, "BRL");
62
+ // "R$ 1.234,56"
63
+
64
+ // Generate a UUID v4
65
+ const id = generateId("text", "v4");
66
+ // "550e8400-e29b-41d4-a716-446655440000"
67
+
68
+ // Generate a URL-friendly slug
69
+ const slug = generateSlug("Hello, World! This is a Test.");
70
+ // "hello-world-this-is-a-test"
71
+ ```
72
+
73
+ ## 📖 API Reference
74
+
75
+ ### Formats
76
+
77
+ #### formatDate
78
+
79
+ Formats a date (and optional time) string into a custom output format. Calculations are done in UTC+0; use the `timezone` parameter to shift the result. Throws `Error` if the resulting date is invalid or `inputFormat` is not recognized.
80
+
81
+ ```typescript
82
+ import { formatDate } from "@arkyn/shared";
83
+
84
+ formatDate(["25/12/2023", "15:30:00"], "brazilianDate", "YYYY-MM-DD hh:mm");
85
+ // "2023-12-25 15:30"
86
+
87
+ formatDate(["2023-12-25", "15:30:00"], "timestamp", "DD/MM/YYYY hh:mm", -3);
88
+ // "2023-12-25 12:30"
89
+ ```
90
+
91
+ #### formatJsonObject
92
+
93
+ Formats a JSON-compatible value (object, array, string, or primitive) into a human-readable, indented string. Strings that parse as JSON are recursively formatted.
94
+
95
+ ```typescript
96
+ import { formatJsonObject } from "@arkyn/shared";
97
+
98
+ const obj = { name: "John", age: 30, hobbies: ["reading", "gaming"] };
99
+ formatJsonObject(obj, 0);
100
+ // {
101
+ // "name": "John",
102
+ // "age": 30,
103
+ // "hobbies": [
104
+ // "reading",
105
+ // "gaming"
106
+ // ]
107
+ // }
108
+ ```
109
+
110
+ #### formatJsonString
111
+
112
+ Parses a JSON string and returns a pretty-printed representation. Throws `Error` if the input is not valid JSON.
113
+
114
+ ```typescript
115
+ import { formatJsonString } from "@arkyn/shared";
116
+
117
+ formatJsonString('{"name":"John","hobbies":["reading","gaming"]}');
118
+ // {
119
+ // "name": "John",
120
+ // "hobbies": [
121
+ // "reading",
122
+ // "gaming"
123
+ // ]
124
+ // }
125
+ ```
126
+
127
+ #### formatToCapitalizeFirstWordLetter
14
128
 
15
- ## Features
129
+ Capitalizes the first letter of each space-separated word and lowercases the rest.
16
130
 
17
- ### Formatting
131
+ ```typescript
132
+ import { formatToCapitalizeFirstWordLetter } from "@arkyn/shared";
18
133
 
19
- - **`formatDate(date: dateString[], inputFormat: "brazilianDate" | "isoDate" | "timestamp", outputFormat: string, timezone?: number): string`**
20
- Formats a date into a readable string.
134
+ formatToCapitalizeFirstWordLetter("HELLO WORLD"); // "Hello World"
135
+ ```
21
136
 
22
- - **`formatJsonObject(obj: object): string`**
23
- Converts a JSON object into a formatted string.
137
+ #### formatToCep
24
138
 
25
- - **`formatJsonString(json: string): object`**
26
- Converts a JSON string into an object.
139
+ Formats a string into the Brazilian postal code (CEP) pattern `XXXXX-XXX`. Throws `Error` if the cleaned input doesn't contain exactly 8 numeric digits.
27
140
 
28
- - **`formatToCep(value: string): string`**
29
- Formats a string into the CEP format (`XXXXX-XXX`).
141
+ ```typescript
142
+ import { formatToCep } from "@arkyn/shared";
143
+
144
+ formatToCep("12345678"); // "12345-678"
145
+ ```
146
+
147
+ #### formatToCnpj
148
+
149
+ Formats a string into the CNPJ pattern `XX.XXX.XXX/XXXX-XX`. Throws `Error` if the cleaned input doesn't contain exactly 14 numeric digits.
150
+
151
+ ```typescript
152
+ import { formatToCnpj } from "@arkyn/shared";
153
+
154
+ formatToCnpj("12345678000195"); // "12.345.678/0001-95"
155
+ ```
30
156
 
31
- - **`formatToCnpj(value: string): string`**
32
- Formats a string into the CNPJ format (`XX.XXX.XXX/XXXX-XX`).
157
+ #### formatToCpf
158
+
159
+ Formats a string into the CPF pattern `XXX.XXX.XXX-XX`. Throws `Error` if the cleaned input doesn't contain exactly 11 numeric digits.
160
+
161
+ ```typescript
162
+ import { formatToCpf } from "@arkyn/shared";
163
+
164
+ formatToCpf("12345678909"); // "123.456.789-09"
165
+ ```
166
+
167
+ #### formatToCurrency
168
+
169
+ Formats a number into a locale-aware currency string using `Intl.NumberFormat`, based on a currency code from `@arkyn/templates`. Set `config.showPrefix` to `false` to omit the currency symbol. Throws `Error` for unsupported currency codes.
170
+
171
+ ```typescript
172
+ import { formatToCurrency } from "@arkyn/shared";
173
+
174
+ formatToCurrency(1234.56, "BRL"); // "R$ 1.234,56"
175
+ formatToCurrency(1234.56, "USD", { showPrefix: false }); // "1,234.56"
176
+ ```
177
+
178
+ #### formatToEllipsis
179
+
180
+ Truncates a string to `maxLength`, avoiding breaking mid-word, and appends `"..."` if truncation occurred.
181
+
182
+ ```typescript
183
+ import { formatToEllipsis } from "@arkyn/shared";
184
+
185
+ formatToEllipsis("Hello, world!", 5); // "Hello..."
186
+ ```
33
187
 
34
- - **`formatToCpf(value: string): string`**
35
- Formats a string into the CPF format (`XXX.XXX.XXX-XX`).
188
+ #### formatToHiddenDigits
36
189
 
37
- - **`formatToCpfCnpj(value: string): string`**
38
- Formats a string into either CPF or CNPJ format, depending on its length.
190
+ Replaces specific digit positions in a string with a masking character, leaving non-digit characters unchanged. `options.range` can be a positive number (first N digits), a negative number (last N digits), or a `[start, end]` tuple (1-indexed, inclusive). Defaults to hiding the first 3 digits with `"*"`.
39
191
 
40
- - **`formatToCurrency(value: number): string`**
41
- Converts a number into a currency format.
192
+ ```typescript
193
+ import { formatToHiddenDigits } from "@arkyn/shared";
42
194
 
43
- - **`formatToDate(date: dateString[], inputFormat: "brazilianDate" | "isoDate" | "timestamp", timezone?: number): Date`**
44
- Formats a date into a Date class.
195
+ formatToHiddenDigits("123-456-7890", { range: 3 }); // "***-456-7890"
196
+ formatToHiddenDigits("123-456-7890", { range: [4, 6], hider: "#" });
197
+ // "123-###-7890"
198
+ ```
45
199
 
46
- - **`formatToEllipsis(value: string, maxLength: number): string`**
47
- Truncates a string and appends an ellipsis if it exceeds the maximum length.
200
+ #### formatToPhone
48
201
 
49
- - **`formatToHiddenDigits(value: string): string`**
50
- Masks part of a string by replacing it with asterisks.
202
+ Formats a phone number according to the country mask defined in `@arkyn/templates`, parsing it with `libphonenumber-js`. Throws `Error` if the number is invalid or no country mask is found. Requires the `libphonenumber-js` peer dependency.
51
203
 
52
- - **`formatToPhone(value: string): string`**
53
- Formats a string into a phone number format.
204
+ ```typescript
205
+ import { formatToPhone } from "@arkyn/shared";
54
206
 
55
- ---
207
+ formatToPhone("+5534920524282"); // "(34) 92052-4282"
208
+ formatToPhone("+12125550199"); // "(212) 555-0199"
209
+ ```
56
210
 
57
211
  ### Generators
58
212
 
59
- - **`generateColorByString(value: string): string`**
60
- Generates a hexadecimal color based on a string.
213
+ #### generateColorByString
214
+
215
+ Generates a deterministic hexadecimal color code from a hash of the input string — the same input always produces the same color.
216
+
217
+ ```typescript
218
+ import { generateColorByString } from "@arkyn/shared";
219
+
220
+ generateColorByString("example"); // "#5e8f9a" (consistent for the same input)
221
+ ```
222
+
223
+ #### generateId
224
+
225
+ Generates a UUID in the requested version (`"v4"` random or `"v7"` time-ordered) and representation (`"text"` string or `"binary"` `Uint8Array`). Throws `Error` for an invalid type/format combination.
226
+
227
+ ```typescript
228
+ import { generateId } from "@arkyn/shared";
229
+
230
+ generateId("text", "v4"); // "550e8400-e29b-41d4-a716-446655440000"
231
+ generateId("binary", "v7"); // Uint8Array([...])
232
+ ```
233
+
234
+ #### generateSlug
235
+
236
+ Converts a string into a URL-friendly slug: strips accents, removes non-alphanumeric characters (except spaces/hyphens), lowercases, replaces spaces with hyphens, and trims/collapses hyphens.
237
+
238
+ ```typescript
239
+ import { generateSlug } from "@arkyn/shared";
240
+
241
+ generateSlug("Hello, World! This is a Test.");
242
+ // "hello-world-this-is-a-test"
243
+ ```
244
+
245
+ ### Parsers
246
+
247
+ #### parseLargeFields
248
+
249
+ Parses a JSON string and truncates string fields exceeding `maxLength` (default `1000`), replacing them with a message indicating the original length. Traverses nested objects/arrays. Throws `Error` if the input is not valid JSON.
250
+
251
+ ```typescript
252
+ import { parseLargeFields } from "@arkyn/shared";
253
+
254
+ const json = JSON.stringify({
255
+ name: "John",
256
+ description: "A very long description that exceeds the maximum length...",
257
+ });
258
+
259
+ parseLargeFields(json, 50);
260
+ // '{"name":"John","description":"To large information: field as 57 characters"}'
261
+ ```
262
+
263
+ #### parseSensitiveData
264
+
265
+ Parses a JSON string and replaces the values of the given keys with `"****"`, recursively, including JSON-encoded string values. Defaults to masking `password`, `confirmPassword`, and `creditCard`. Returns the original string unchanged if it is not valid JSON (does not throw).
266
+
267
+ ```typescript
268
+ import { parseSensitiveData } from "@arkyn/shared";
269
+
270
+ const jsonString = JSON.stringify({
271
+ username: "user123",
272
+ password: "secret",
273
+ profile: { creditCard: "1234-5678-9012-3456" },
274
+ });
275
+
276
+ parseSensitiveData(jsonString, ["password", "creditCard"]);
277
+ // '{"username":"user123","password":"****","profile":{"creditCard":"****"}}'
278
+ ```
279
+
280
+ #### parseToDate
281
+
282
+ Parses a date (and optional time) string into a `Date` object. Calculations are done in UTC+0; use `timezone` to shift the result. Throws `Error` if the resulting date is invalid or `inputFormat` is not recognized.
61
283
 
62
- - **`generateId(): string`**
63
- Generates a unique identifier.
284
+ ```typescript
285
+ import { parseToDate } from "@arkyn/shared";
64
286
 
65
- - **`generateSlug(value: string): string`**
66
- Converts a string into a URL-friendly slug.
287
+ parseToDate(["25/12/2023", "15:30:00"], "brazilianDate", -3);
288
+ // Date: 2023-12-25T12:30:00.000Z
67
289
 
68
- ---
290
+ parseToDate(["2023-12-25"], "timestamp");
291
+ // Date: 2023-12-25T00:00:00.000Z
292
+ ```
69
293
 
70
294
  ### Services
71
295
 
72
- - **`calculateCardInstallment(total: number, installments: number): number[]`**
73
- Calculates the installment values for a given total.
296
+ #### ValidateDateService
297
+
298
+ Class used internally by `formatDate` and `parseToDate` to validate date components and input-format strings — enforces 4-digit years, month/day ranges, month-specific day counts, and leap-year rules. `validateDateParts` and `validateInputFormat` both throw `Error` on invalid input.
74
299
 
75
- - **`ensureQuotes(rawValue: string): string`**
76
- Ensures that a given rawValue string is enclosed in quotes.
300
+ ```typescript
301
+ import { ValidateDateService } from "@arkyn/shared";
77
302
 
78
- - **`maskSensitiveData(data: string): string`**
79
- Masks sensitive data in a string.
303
+ const service = new ValidateDateService();
304
+ service.validateDateParts(2024, 2, 29); // OK — leap year
305
+ service.validateDateParts(2023, 2, 29); // throws — not a leap year
306
+ service.validateInputFormat("brazilianDate"); // OK
307
+ service.validateInputFormat("custom"); // throws
308
+ ```
309
+
310
+ ### Utilities
80
311
 
81
- - **`removeNonNumeric(value: string): string`**
82
- Removes all non-numeric characters from a string.
312
+ #### calculateCardInstallment
83
313
 
84
- - **`truncateLargeFields(obj: object, maxLength: number): object`**
85
- Truncates large fields in an object to a specified maximum length.
314
+ Calculates the total price and per-installment price for a card payment plan with compound interest. No interest is applied when `fees` is `0` or `numberInstallments` is `1`. Throws `Error` if `numberInstallments <= 0` or `fees < 0`.
86
315
 
87
- ---
316
+ ```typescript
317
+ import { calculateCardInstallment } from "@arkyn/shared";
88
318
 
89
- ### Validations
319
+ calculateCardInstallment({ cashPrice: 1000, numberInstallments: 12, fees: 0.02 });
320
+ // { totalPrice: 1124.62, installmentPrice: 93.72 }
321
+ ```
90
322
 
91
- - **`validateCep(value: string): boolean`**
92
- Validates whether a string is a valid CEP.
323
+ #### ensureQuotes
93
324
 
94
- - **`validateCnpj(value: string): boolean`**
95
- Validates whether a string is a valid CNPJ.
325
+ Wraps a string in double quotes unless it is already enclosed in single or double quotes.
96
326
 
97
- - **`validateCpf(value: string): boolean`**
98
- Validates whether a string is a valid CPF.
327
+ ```typescript
328
+ import { ensureQuotes } from "@arkyn/shared";
99
329
 
100
- - **`validateDate(value: string): boolean`**
101
- Validates whether a string is a valid date.
330
+ ensureQuotes("example"); // '"example"'
331
+ ensureQuotes('"already quoted"'); // '"already quoted"'
332
+ ```
102
333
 
103
- - **`validatePhone(value: string): boolean`**
104
- Validates whether a string is a valid phone number.
334
+ #### findCountryMask
105
335
 
106
- - **`validateRg(value: string): boolean`**
107
- Validates whether a string is a valid ID (RG).
336
+ Resolves the matching phone mask (using `"_"` as digit placeholders) and country metadata for an E.164 phone number, using `libphonenumber-js` and `@arkyn/templates`. For countries with multiple mask lengths (e.g. Brazil with/without the ninth digit), the mask matching the number's digit count is returned. Throws `Error` if the number is invalid or no mask is found. Requires the `libphonenumber-js` peer dependency.
108
337
 
109
- ---
338
+ ```typescript
339
+ import { findCountryMask } from "@arkyn/shared";
340
+
341
+ const [mask, country] = findCountryMask("+5511999999999");
342
+ // mask: "(__) _____-____"
343
+ // country.name: "Brazil"
344
+ ```
345
+
346
+ #### isHtml
347
+
348
+ Checks whether a string contains HTML markup (opening or closing tags), case-insensitively.
349
+
350
+ ```typescript
351
+ import { isHtml } from "@arkyn/shared";
352
+
353
+ isHtml("<p>Hello world</p>"); // true
354
+ isHtml("Plain text"); // false
355
+ ```
110
356
 
111
- ## Contribution
357
+ #### removeCurrencySymbols
358
+
359
+ Removes currency symbols (`R$`, `$`, `€`, `¥`, `£`, and other Unicode currency symbols) from a string and trims whitespace.
360
+
361
+ ```typescript
362
+ import { removeCurrencySymbols } from "@arkyn/shared";
363
+
364
+ removeCurrencySymbols("R$13,45"); // "13,45"
365
+ removeCurrencySymbols("€99.99"); // "99.99"
366
+ ```
367
+
368
+ #### removeNonNumeric
369
+
370
+ Strips all non-numeric characters from a string.
371
+
372
+ ```typescript
373
+ import { removeNonNumeric } from "@arkyn/shared";
374
+
375
+ removeNonNumeric("abc123def456"); // "123456"
376
+ ```
377
+
378
+ #### stripHtmlTags
379
+
380
+ Removes HTML tags from a string, including `<script>` and `<style>` blocks and HTML comments.
381
+
382
+ ```typescript
383
+ import { stripHtmlTags } from "@arkyn/shared";
384
+
385
+ stripHtmlTags("<p>Hello <strong>World</strong></p>"); // "Hello World"
386
+ ```
112
387
 
113
- Contributions are welcome! Feel free to open issues or submit pull requests to improve the project.
388
+ ## 📚 Documentation
114
389
 
115
- ---
390
+ Full documentation: [https://docs.arkyn.dev/docs/shared/introduction](https://docs.arkyn.dev/docs/shared/introduction)
116
391
 
117
- ## License
392
+ ## 📄 License
118
393
 
119
- This project is licensed under the Apache 2.0 License. See the `LICENSE` file for more details.
394
+ This project is licensed under the Apache 2.0 License - see the [LICENSE](./LICENSE.txt) file for details.
@@ -1,34 +1,26 @@
1
- type InputFormatTypes = "brazilianDate" | "isoDate" | "timestamp";
2
- type FormatDateFunction = (date: string[], // [date: string, time?: string]
3
- inputFormat: InputFormatTypes, outputFormat: string, timezone?: number) => string;
4
1
  /**
5
- * Formats a date and time string based on the provided input and output formats.
2
+ * Formats a date (and optional time) string into a custom output format.
3
+ * All calculations are in UTC+0; use the `timezone` parameter to shift the result.
6
4
  *
7
- * @param {[string, string]} dateTime - An array containing the date and optional time.
8
- * - The first element is the date string.
9
- * - The second element is the time string (default is "00:00:00").
10
- * @param {"brazilianDate" | "isoDate" | "timestamp"} inputFormat - The format of the input date.
11
- * - "brazilianDate": Expects the date in "DD/MM/YYYY" format.
12
- * - "isoDate": Expects the date in "YYYY-MM-DD" format.
13
- * - "timestamp": Expects the date in "YYYY/MM/DD" format.
14
- * @param {string} outputFormat - The desired output format for the date.
15
- * - Use placeholders like "YYYY", "MM", "DD", "hh", "mm", "ss" to define the format.
16
- * @param {number} [timezone=0] - The timezone offset in hours to apply to the date.
17
- * - Defaults to 0 (UTC).
18
- * @returns {string} The formatted date string based on the output format.
19
- * @throws {Error} If the input format is invalid.
20
- * @throws {Error} If the date is invalid.
5
+ * @param date - Date string in the format determined by `inputFormat`.
6
+ * @param time - Optional time string `"HH:mm:ss"` (defaults to `"00:00:00"`).
7
+ * @param inputFormat - Parsing format:
8
+ * - `"brazilianDate"`: DD/MM/YYYY
9
+ * - `"isoDate"`: MM-DD-YYYY
10
+ * - `"timestamp"`: YYYY-MM-DD
11
+ * @param outputFormat - Output template using `YYYY`, `MM`, `DD`, `hh`, `mm`, `ss` placeholders.
12
+ * @param timezone - UTC offset in hours (e.g. `-3` for UTC-3). Defaults to `0`.
13
+ * @returns The formatted date string.
21
14
  *
22
15
  * @example
23
- * // Format a Brazilian date to ISO format
24
- * formatDate(["25/12/2023", "15:30:00"], "brazilianDate", "YYYY-MM-DD hh:mm:ss");
25
- * // Returns: "2023-12-25 15:30:00"
16
+ * ```typescript
17
+ * formatDate(["25/12/2023", "15:30:00"], "brazilianDate", "YYYY-MM-DD hh:mm");
18
+ * // "2023-12-25 15:30"
26
19
  *
27
- * @example
28
- * // Format an ISO date to a custom format with timezone adjustment
29
- * formatDate(["2023-12-25", "15:30:00"], "isoDate", "DD/MM/YYYY hh:mm:ss", -3);
30
- * // Returns: "25/12/2023 12:30:00"
20
+ * formatDate(["2023-12-25", "15:30:00"], "timestamp", "DD/MM/YYYY hh:mm", -3);
21
+ * // "2023-12-25 12:30"
22
+ * ```
31
23
  */
32
- declare const formatDate: FormatDateFunction;
24
+ declare function formatDate([date, time]: string[], inputFormat: "brazilianDate" | "isoDate" | "timestamp", outputFormat: string, timezone?: number): string;
33
25
  export { formatDate };
34
26
  //# sourceMappingURL=formatDate.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"formatDate.d.ts","sourceRoot":"","sources":["../../src/formats/formatDate.ts"],"names":[],"mappings":"AAAA,KAAK,gBAAgB,GAAG,eAAe,GAAG,SAAS,GAAG,WAAW,CAAC;AAElE,KAAK,kBAAkB,GAAG,CACxB,IAAI,EAAE,MAAM,EAAE,EAAE,gCAAgC;AAChD,WAAW,EAAE,gBAAgB,EAC7B,YAAY,EAAE,MAAM,EACpB,QAAQ,CAAC,EAAE,MAAM,KACd,MAAM,CAAC;AAqBZ;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AAEH,QAAA,MAAM,UAAU,EAAE,kBAgCjB,CAAC;AAEF,OAAO,EAAE,UAAU,EAAE,CAAC"}
1
+ {"version":3,"file":"formatDate.d.ts","sourceRoot":"","sources":["../../src/formats/formatDate.ts"],"names":[],"mappings":"AAqBA;;;;;;;;;;;;;;;;;;;;;;GAsBG;AAEH,iBAAS,UAAU,CAClB,CAAC,IAAI,EAAE,IAAiB,CAAC,EAAE,MAAM,EAAE,EACnC,WAAW,EAAE,eAAe,GAAG,SAAS,GAAG,WAAW,EACtD,YAAY,EAAE,MAAM,EACpB,QAAQ,GAAE,MAAU,GAClB,MAAM,CAkCR;AAED,OAAO,EAAE,UAAU,EAAE,CAAC"}
@@ -1,17 +1,15 @@
1
- type FormatJsonObjectFunction = (jsonString: any, identLevel: number) => string;
2
1
  /**
3
2
  * Formats a JSON object into a human-readable string with proper indentation.
4
3
  *
5
- * @param obj - The JSON object or value to format. It can be an object, array, string, or primitive value.
6
- * @param indentLevel - The current level of indentation to apply. This is used recursively to format nested structures.
7
- * @returns A formatted string representation of the JSON object.
8
- *
9
- * @remarks
10
4
  * - If the input is an object, it will be formatted with keys and values properly indented.
11
5
  * - If the input is an array, each element will be formatted and indented on a new line.
12
6
  * - If the input is a string that can be parsed as JSON, it will attempt to parse and format it.
13
7
  * - Primitive values (e.g., numbers, booleans, null) will be converted to their string representation.
14
8
  *
9
+ * @param json - The value to format: object, array, string, or primitive.
10
+ * @param indentLevel - Current indentation depth (used recursively; pass `0` at the top level).
11
+ * @returns A pretty-printed string representation of the value.
12
+ *
15
13
  * @example
16
14
  * ```typescript
17
15
  * const obj = { name: "John", age: 30, hobbies: ["reading", "gaming"] };
@@ -28,6 +26,6 @@ type FormatJsonObjectFunction = (jsonString: any, identLevel: number) => string;
28
26
  * // }
29
27
  * ```
30
28
  */
31
- declare const formatJsonObject: FormatJsonObjectFunction;
29
+ declare const formatJsonObject: (json: any, indentLevel: number) => string;
32
30
  export { formatJsonObject };
33
31
  //# sourceMappingURL=formatJsonObject.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"formatJsonObject.d.ts","sourceRoot":"","sources":["../../src/formats/formatJsonObject.ts"],"names":[],"mappings":"AAAA,KAAK,wBAAwB,GAAG,CAAC,UAAU,EAAE,GAAG,EAAE,UAAU,EAAE,MAAM,KAAK,MAAM,CAAC;AAEhF;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AAEH,QAAA,MAAM,gBAAgB,EAAE,wBAuDvB,CAAC;AAEF,OAAO,EAAE,gBAAgB,EAAE,CAAC"}
1
+ {"version":3,"file":"formatJsonObject.d.ts","sourceRoot":"","sources":["../../src/formats/formatJsonObject.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AAGH,QAAA,MAAM,gBAAgB,GAAI,MAAM,GAAG,EAAE,aAAa,MAAM,KAAG,MAsD1D,CAAC;AAEF,OAAO,EAAE,gBAAgB,EAAE,CAAC"}
@@ -1,37 +1,21 @@
1
1
  type FormatJsonStringFunction = (jsonString: string) => string;
2
2
  /**
3
- * Formats a JSON string into a more readable format.
3
+ * Parses a JSON string and returns a human-readable pretty-printed representation.
4
+ * Throws if the input is not valid JSON.
4
5
  *
5
- * This function attempts to parse the provided JSON string into a JavaScript object,
6
- * and then formats it using the `formatJsonObject` function. If the input string
7
- * is not a valid JSON, it logs an error to the console and returns an empty string.
8
- *
9
- * @param jsonString - The JSON string to be formatted.
10
- * @returns A formatted JSON string, or an empty string if the input is invalid.
11
- *
12
- * @throws Will log an error to the console if the input is not a valid JSON string.
6
+ * @param jsonString - A valid JSON string to format.
7
+ * @returns A pretty-printed string representation.
13
8
  *
14
9
  * @example
15
10
  * ```typescript
16
- * const jsonString = '{"name":"John","age":30,"hobbies":["reading","gaming"]}';
17
- * const formatted = formatJsonString(jsonString);
18
- * console.log(formatted);
19
- * // Output:
11
+ * formatJsonString('{"name":"John","hobbies":["reading","gaming"]}');
20
12
  * // {
21
13
  * // "name": "John",
22
- * // "age": 30,
23
14
  * // "hobbies": [
24
15
  * // "reading",
25
16
  * // "gaming"
26
17
  * // ]
27
18
  * // }
28
-
29
- * const invalidJsonString = '{"name":"John", "age":30,';
30
- * const formatted = formatJsonString(invalidJsonString);
31
- * console.log(formatted);
32
- * // Output:
33
- * // (Logs "Invalid JSON string: ..." to the console)
34
- * // ""
35
19
  * ```
36
20
  */
37
21
  declare const formatJsonString: FormatJsonStringFunction;
@@ -1 +1 @@
1
- {"version":3,"file":"formatJsonString.d.ts","sourceRoot":"","sources":["../../src/formats/formatJsonString.ts"],"names":[],"mappings":"AAEA,KAAK,wBAAwB,GAAG,CAAC,UAAU,EAAE,MAAM,KAAK,MAAM,CAAC;AAE/D;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AAEH,QAAA,MAAM,gBAAgB,EAAE,wBAOvB,CAAC;AAEF,OAAO,EAAE,gBAAgB,EAAE,CAAC"}
1
+ {"version":3,"file":"formatJsonString.d.ts","sourceRoot":"","sources":["../../src/formats/formatJsonString.ts"],"names":[],"mappings":"AAEA,KAAK,wBAAwB,GAAG,CAAC,UAAU,EAAE,MAAM,KAAK,MAAM,CAAC;AAE/D;;;;;;;;;;;;;;;;;;GAkBG;AAEH,QAAA,MAAM,gBAAgB,EAAE,wBAOvB,CAAC;AAEF,OAAO,EAAE,gBAAgB,EAAE,CAAC"}
@@ -1,33 +1,14 @@
1
1
  /**
2
- * Formats a sentence by capitalizing the first letter of each word.
3
- *
4
- * This function takes a string and capitalizes the first letter of each word
5
- * while the remaining letters are lowercase.
2
+ * Capitalizes the first letter of each word and lowercases the rest.
6
3
  * Words are separated by spaces.
7
4
  *
8
- * @param sentence - The sentence to be formatted.
9
- * @returns The sentence formatted with the first letter of each word capitalized.
5
+ * @param sentence - The string to format.
6
+ * @returns The sentence with every word title-cased.
10
7
  *
11
8
  * @example
12
9
  * ```typescript
13
- * // Basic example
14
- * formatToCapitalizeFirstWordLetter("hello world");
15
- * // Returns: "Hello World"
16
- *
17
- * // With capitalized text.
18
- * formatToCapitalizeFirstWordLetter("HELLO WORLD");
19
- * // Returns: "Hello World"
20
- *
21
- * // With mixed text.
22
- * formatToCapitalizeFirstWordLetter("hELLO WoRLd"); * // Returns: "Hello World"
23
- *
24
- * // With multiple words
25
- * formatToCapitalizeFirstWordLetter("javascript is an amazing language");
26
- * // Returns: "Javascript is an amazing language"
27
- *
28
- * // Empty string
29
- * formatToCapitalizeFirstWordLetter("");
30
- * // Returns: ""
10
+ * formatToCapitalizeFirstWordLetter("hello world"); // "Hello World"
11
+ * formatToCapitalizeFirstWordLetter("HELLO WORLD"); // "Hello World"
31
12
  * ```
32
13
  */
33
14
  declare function formatToCapitalizeFirstWordLetter(sentence: string): string;
@@ -1 +1 @@
1
- {"version":3,"file":"formatToCapitalizeFirstWordLetter.d.ts","sourceRoot":"","sources":["../../src/formats/formatToCapitalizeFirstWordLetter.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+BG;AAEH,iBAAS,iCAAiC,CAAC,QAAQ,EAAE,MAAM,UAU1D;AAED,OAAO,EAAE,iCAAiC,EAAE,CAAC"}
1
+ {"version":3,"file":"formatToCapitalizeFirstWordLetter.d.ts","sourceRoot":"","sources":["../../src/formats/formatToCapitalizeFirstWordLetter.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAEH,iBAAS,iCAAiC,CAAC,QAAQ,EAAE,MAAM,UAU1D;AAED,OAAO,EAAE,iCAAiC,EAAE,CAAC"}