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

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 +342 -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,392 @@
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
8
33
 
9
34
  ```bash
10
35
  npm install @arkyn/shared
11
36
  ```
12
37
 
13
- ---
38
+ If you plan to use `formatToPhone` or `findCountryMask`, also install the peer dependency:
39
+
40
+ ```bash
41
+ npm install libphonenumber-js
42
+ ```
43
+
44
+ ## 🚀 Quick Start
45
+
46
+ ```typescript
47
+ import {
48
+ formatToCpf,
49
+ formatToCurrency,
50
+ generateId,
51
+ generateSlug,
52
+ } from "@arkyn/shared";
53
+
54
+ // Format a raw CPF string (throws if it doesn't have 11 digits)
55
+ const cpf = formatToCpf("12345678909");
56
+ // "123.456.789-09"
57
+
58
+ // Format currency using a currency code from @arkyn/templates
59
+ const price = formatToCurrency(1234.56, "BRL");
60
+ // "R$ 1.234,56"
61
+
62
+ // Generate a UUID v4
63
+ const id = generateId("text", "v4");
64
+ // "550e8400-e29b-41d4-a716-446655440000"
65
+
66
+ // Generate a URL-friendly slug
67
+ const slug = generateSlug("Hello, World! This is a Test.");
68
+ // "hello-world-this-is-a-test"
69
+ ```
70
+
71
+ ## 📖 API Reference
72
+
73
+ ### Formats
74
+
75
+ #### formatDate
76
+
77
+ 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.
78
+
79
+ ```typescript
80
+ import { formatDate } from "@arkyn/shared";
81
+
82
+ formatDate(["25/12/2023", "15:30:00"], "brazilianDate", "YYYY-MM-DD hh:mm");
83
+ // "2023-12-25 15:30"
84
+
85
+ formatDate(["2023-12-25", "15:30:00"], "timestamp", "DD/MM/YYYY hh:mm", -3);
86
+ // "2023-12-25 12:30"
87
+ ```
88
+
89
+ #### formatJsonObject
90
+
91
+ Formats a JSON-compatible value (object, array, string, or primitive) into a human-readable, indented string. Strings that parse as JSON are recursively formatted.
92
+
93
+ ```typescript
94
+ import { formatJsonObject } from "@arkyn/shared";
95
+
96
+ const obj = { name: "John", age: 30, hobbies: ["reading", "gaming"] };
97
+ formatJsonObject(obj, 0);
98
+ // {
99
+ // "name": "John",
100
+ // "age": 30,
101
+ // "hobbies": [
102
+ // "reading",
103
+ // "gaming"
104
+ // ]
105
+ // }
106
+ ```
107
+
108
+ #### formatJsonString
109
+
110
+ Parses a JSON string and returns a pretty-printed representation. Throws `Error` if the input is not valid JSON.
111
+
112
+ ```typescript
113
+ import { formatJsonString } from "@arkyn/shared";
114
+
115
+ formatJsonString('{"name":"John","hobbies":["reading","gaming"]}');
116
+ // {
117
+ // "name": "John",
118
+ // "hobbies": [
119
+ // "reading",
120
+ // "gaming"
121
+ // ]
122
+ // }
123
+ ```
14
124
 
15
- ## Features
125
+ #### formatToCapitalizeFirstWordLetter
16
126
 
17
- ### Formatting
127
+ Capitalizes the first letter of each space-separated word and lowercases the rest.
18
128
 
19
- - **`formatDate(date: dateString[], inputFormat: "brazilianDate" | "isoDate" | "timestamp", outputFormat: string, timezone?: number): string`**
20
- Formats a date into a readable string.
129
+ ```typescript
130
+ import { formatToCapitalizeFirstWordLetter } from "@arkyn/shared";
21
131
 
22
- - **`formatJsonObject(obj: object): string`**
23
- Converts a JSON object into a formatted string.
132
+ formatToCapitalizeFirstWordLetter("HELLO WORLD"); // "Hello World"
133
+ ```
24
134
 
25
- - **`formatJsonString(json: string): object`**
26
- Converts a JSON string into an object.
135
+ #### formatToCep
27
136
 
28
- - **`formatToCep(value: string): string`**
29
- Formats a string into the CEP format (`XXXXX-XXX`).
137
+ 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.
30
138
 
31
- - **`formatToCnpj(value: string): string`**
32
- Formats a string into the CNPJ format (`XX.XXX.XXX/XXXX-XX`).
139
+ ```typescript
140
+ import { formatToCep } from "@arkyn/shared";
33
141
 
34
- - **`formatToCpf(value: string): string`**
35
- Formats a string into the CPF format (`XXX.XXX.XXX-XX`).
142
+ formatToCep("12345678"); // "12345-678"
143
+ ```
36
144
 
37
- - **`formatToCpfCnpj(value: string): string`**
38
- Formats a string into either CPF or CNPJ format, depending on its length.
145
+ #### formatToCnpj
39
146
 
40
- - **`formatToCurrency(value: number): string`**
41
- Converts a number into a currency format.
147
+ 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.
42
148
 
43
- - **`formatToDate(date: dateString[], inputFormat: "brazilianDate" | "isoDate" | "timestamp", timezone?: number): Date`**
44
- Formats a date into a Date class.
149
+ ```typescript
150
+ import { formatToCnpj } from "@arkyn/shared";
45
151
 
46
- - **`formatToEllipsis(value: string, maxLength: number): string`**
47
- Truncates a string and appends an ellipsis if it exceeds the maximum length.
152
+ formatToCnpj("12345678000195"); // "12.345.678/0001-95"
153
+ ```
48
154
 
49
- - **`formatToHiddenDigits(value: string): string`**
50
- Masks part of a string by replacing it with asterisks.
155
+ #### formatToCpf
51
156
 
52
- - **`formatToPhone(value: string): string`**
53
- Formats a string into a phone number format.
157
+ Formats a string into the CPF pattern `XXX.XXX.XXX-XX`. Throws `Error` if the cleaned input doesn't contain exactly 11 numeric digits.
54
158
 
55
- ---
159
+ ```typescript
160
+ import { formatToCpf } from "@arkyn/shared";
161
+
162
+ formatToCpf("12345678909"); // "123.456.789-09"
163
+ ```
164
+
165
+ #### formatToCurrency
166
+
167
+ 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.
168
+
169
+ ```typescript
170
+ import { formatToCurrency } from "@arkyn/shared";
171
+
172
+ formatToCurrency(1234.56, "BRL"); // "R$ 1.234,56"
173
+ formatToCurrency(1234.56, "USD", { showPrefix: false }); // "1,234.56"
174
+ ```
175
+
176
+ #### formatToEllipsis
177
+
178
+ Truncates a string to `maxLength`, avoiding breaking mid-word, and appends `"..."` if truncation occurred.
179
+
180
+ ```typescript
181
+ import { formatToEllipsis } from "@arkyn/shared";
182
+
183
+ formatToEllipsis("Hello, world!", 5); // "Hello..."
184
+ ```
185
+
186
+ #### formatToHiddenDigits
187
+
188
+ 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 `"*"`.
189
+
190
+ ```typescript
191
+ import { formatToHiddenDigits } from "@arkyn/shared";
192
+
193
+ formatToHiddenDigits("123-456-7890", { range: 3 }); // "***-456-7890"
194
+ formatToHiddenDigits("123-456-7890", { range: [4, 6], hider: "#" });
195
+ // "123-###-7890"
196
+ ```
197
+
198
+ #### formatToPhone
199
+
200
+ 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.
201
+
202
+ ```typescript
203
+ import { formatToPhone } from "@arkyn/shared";
204
+
205
+ formatToPhone("+5534920524282"); // "(34) 92052-4282"
206
+ formatToPhone("+12125550199"); // "(212) 555-0199"
207
+ ```
56
208
 
57
209
  ### Generators
58
210
 
59
- - **`generateColorByString(value: string): string`**
60
- Generates a hexadecimal color based on a string.
211
+ #### generateColorByString
212
+
213
+ Generates a deterministic hexadecimal color code from a hash of the input string — the same input always produces the same color.
214
+
215
+ ```typescript
216
+ import { generateColorByString } from "@arkyn/shared";
217
+
218
+ generateColorByString("example"); // "#5e8f9a" (consistent for the same input)
219
+ ```
220
+
221
+ #### generateId
222
+
223
+ 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.
224
+
225
+ ```typescript
226
+ import { generateId } from "@arkyn/shared";
227
+
228
+ generateId("text", "v4"); // "550e8400-e29b-41d4-a716-446655440000"
229
+ generateId("binary", "v7"); // Uint8Array([...])
230
+ ```
231
+
232
+ #### generateSlug
233
+
234
+ 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.
235
+
236
+ ```typescript
237
+ import { generateSlug } from "@arkyn/shared";
238
+
239
+ generateSlug("Hello, World! This is a Test.");
240
+ // "hello-world-this-is-a-test"
241
+ ```
242
+
243
+ ### Parsers
244
+
245
+ #### parseLargeFields
246
+
247
+ 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.
248
+
249
+ ```typescript
250
+ import { parseLargeFields } from "@arkyn/shared";
251
+
252
+ const json = JSON.stringify({
253
+ name: "John",
254
+ description: "A very long description that exceeds the maximum length...",
255
+ });
256
+
257
+ parseLargeFields(json, 50);
258
+ // '{"name":"John","description":"To large information: field as 57 characters"}'
259
+ ```
260
+
261
+ #### parseSensitiveData
262
+
263
+ 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).
264
+
265
+ ```typescript
266
+ import { parseSensitiveData } from "@arkyn/shared";
267
+
268
+ const jsonString = JSON.stringify({
269
+ username: "user123",
270
+ password: "secret",
271
+ profile: { creditCard: "1234-5678-9012-3456" },
272
+ });
273
+
274
+ parseSensitiveData(jsonString, ["password", "creditCard"]);
275
+ // '{"username":"user123","password":"****","profile":{"creditCard":"****"}}'
276
+ ```
277
+
278
+ #### parseToDate
61
279
 
62
- - **`generateId(): string`**
63
- Generates a unique identifier.
280
+ 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.
64
281
 
65
- - **`generateSlug(value: string): string`**
66
- Converts a string into a URL-friendly slug.
282
+ ```typescript
283
+ import { parseToDate } from "@arkyn/shared";
67
284
 
68
- ---
285
+ parseToDate(["25/12/2023", "15:30:00"], "brazilianDate", -3);
286
+ // Date: 2023-12-25T12:30:00.000Z
287
+
288
+ parseToDate(["2023-12-25"], "timestamp");
289
+ // Date: 2023-12-25T00:00:00.000Z
290
+ ```
69
291
 
70
292
  ### Services
71
293
 
72
- - **`calculateCardInstallment(total: number, installments: number): number[]`**
73
- Calculates the installment values for a given total.
294
+ #### ValidateDateService
295
+
296
+ 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.
297
+
298
+ ```typescript
299
+ import { ValidateDateService } from "@arkyn/shared";
300
+
301
+ const service = new ValidateDateService();
302
+ service.validateDateParts(2024, 2, 29); // OK — leap year
303
+ service.validateDateParts(2023, 2, 29); // throws — not a leap year
304
+ service.validateInputFormat("brazilianDate"); // OK
305
+ service.validateInputFormat("custom"); // throws
306
+ ```
307
+
308
+ ### Utilities
309
+
310
+ #### calculateCardInstallment
311
+
312
+ 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`.
313
+
314
+ ```typescript
315
+ import { calculateCardInstallment } from "@arkyn/shared";
316
+
317
+ calculateCardInstallment({ cashPrice: 1000, numberInstallments: 12, fees: 0.02 });
318
+ // { totalPrice: 1124.62, installmentPrice: 93.72 }
319
+ ```
320
+
321
+ #### ensureQuotes
322
+
323
+ Wraps a string in double quotes unless it is already enclosed in single or double quotes.
74
324
 
75
- - **`ensureQuotes(rawValue: string): string`**
76
- Ensures that a given rawValue string is enclosed in quotes.
325
+ ```typescript
326
+ import { ensureQuotes } from "@arkyn/shared";
77
327
 
78
- - **`maskSensitiveData(data: string): string`**
79
- Masks sensitive data in a string.
328
+ ensureQuotes("example"); // '"example"'
329
+ ensureQuotes('"already quoted"'); // '"already quoted"'
330
+ ```
331
+
332
+ #### findCountryMask
333
+
334
+ 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.
335
+
336
+ ```typescript
337
+ import { findCountryMask } from "@arkyn/shared";
338
+
339
+ const [mask, country] = findCountryMask("+5511999999999");
340
+ // mask: "(__) _____-____"
341
+ // country.name: "Brazil"
342
+ ```
343
+
344
+ #### isHtml
80
345
 
81
- - **`removeNonNumeric(value: string): string`**
82
- Removes all non-numeric characters from a string.
346
+ Checks whether a string contains HTML markup (opening or closing tags), case-insensitively.
83
347
 
84
- - **`truncateLargeFields(obj: object, maxLength: number): object`**
85
- Truncates large fields in an object to a specified maximum length.
348
+ ```typescript
349
+ import { isHtml } from "@arkyn/shared";
86
350
 
87
- ---
351
+ isHtml("<p>Hello world</p>"); // true
352
+ isHtml("Plain text"); // false
353
+ ```
354
+
355
+ #### removeCurrencySymbols
88
356
 
89
- ### Validations
357
+ Removes currency symbols (`R$`, `$`, `€`, `¥`, `£`, and other Unicode currency symbols) from a string and trims whitespace.
90
358
 
91
- - **`validateCep(value: string): boolean`**
92
- Validates whether a string is a valid CEP.
359
+ ```typescript
360
+ import { removeCurrencySymbols } from "@arkyn/shared";
93
361
 
94
- - **`validateCnpj(value: string): boolean`**
95
- Validates whether a string is a valid CNPJ.
362
+ removeCurrencySymbols("R$13,45"); // "13,45"
363
+ removeCurrencySymbols("€99.99"); // "99.99"
364
+ ```
96
365
 
97
- - **`validateCpf(value: string): boolean`**
98
- Validates whether a string is a valid CPF.
366
+ #### removeNonNumeric
99
367
 
100
- - **`validateDate(value: string): boolean`**
101
- Validates whether a string is a valid date.
368
+ Strips all non-numeric characters from a string.
102
369
 
103
- - **`validatePhone(value: string): boolean`**
104
- Validates whether a string is a valid phone number.
370
+ ```typescript
371
+ import { removeNonNumeric } from "@arkyn/shared";
105
372
 
106
- - **`validateRg(value: string): boolean`**
107
- Validates whether a string is a valid ID (RG).
373
+ removeNonNumeric("abc123def456"); // "123456"
374
+ ```
108
375
 
109
- ---
376
+ #### stripHtmlTags
110
377
 
111
- ## Contribution
378
+ Removes HTML tags from a string, including `<script>` and `<style>` blocks and HTML comments.
379
+
380
+ ```typescript
381
+ import { stripHtmlTags } from "@arkyn/shared";
382
+
383
+ stripHtmlTags("<p>Hello <strong>World</strong></p>"); // "Hello World"
384
+ ```
112
385
 
113
- Contributions are welcome! Feel free to open issues or submit pull requests to improve the project.
386
+ ## 📚 Documentation
114
387
 
115
- ---
388
+ Full documentation: [https://docs.arkyn.dev/docs/shared/introduction](https://docs.arkyn.dev/docs/shared/introduction)
116
389
 
117
- ## License
390
+ ## 📄 License
118
391
 
119
- This project is licensed under the Apache 2.0 License. See the `LICENSE` file for more details.
392
+ 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"}