@arkyn/shared 3.0.1-beta.99 → 3.0.2

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 (151) hide show
  1. package/LICENSE.txt +184 -1
  2. package/README.md +196 -306
  3. package/dist/formats/formatDate.d.ts +18 -26
  4. package/dist/formats/formatDate.d.ts.map +1 -1
  5. package/dist/formats/formatJsonObject.d.ts +5 -7
  6. package/dist/formats/formatJsonObject.d.ts.map +1 -1
  7. package/dist/formats/formatJsonString.d.ts +5 -21
  8. package/dist/formats/formatJsonString.d.ts.map +1 -1
  9. package/dist/formats/formatToCapitalizeFirstWordLetter.d.ts +5 -24
  10. package/dist/formats/formatToCapitalizeFirstWordLetter.d.ts.map +1 -1
  11. package/dist/formats/formatToCep.d.ts +3 -11
  12. package/dist/formats/formatToCep.d.ts.map +1 -1
  13. package/dist/formats/formatToCnpj.d.ts +3 -14
  14. package/dist/formats/formatToCnpj.d.ts.map +1 -1
  15. package/dist/formats/formatToCpf.d.ts +4 -13
  16. package/dist/formats/formatToCpf.d.ts.map +1 -1
  17. package/dist/formats/formatToCurrency.d.ts +10 -23
  18. package/dist/formats/formatToCurrency.d.ts.map +1 -1
  19. package/dist/formats/formatToEllipsis.d.ts +5 -5
  20. package/dist/formats/formatToEllipsis.d.ts.map +1 -1
  21. package/dist/formats/formatToHiddenDigits.d.ts +15 -26
  22. package/dist/formats/formatToHiddenDigits.d.ts.map +1 -1
  23. package/dist/formats/formatToPhone.d.ts +11 -23
  24. package/dist/formats/formatToPhone.d.ts.map +1 -1
  25. package/dist/generators/generateColorByString.d.ts +6 -4
  26. package/dist/generators/generateColorByString.d.ts.map +1 -1
  27. package/dist/generators/generateId.d.ts +8 -19
  28. package/dist/generators/generateId.d.ts.map +1 -1
  29. package/dist/generators/generateSlug.d.ts +8 -2
  30. package/dist/generators/generateSlug.d.ts.map +1 -1
  31. package/dist/index.d.ts +11 -10
  32. package/dist/index.d.ts.map +1 -1
  33. package/dist/index.js +373 -27
  34. package/dist/index.js.map +1 -0
  35. package/dist/modules/formats/formatDate.js +37 -0
  36. package/dist/modules/formats/formatDate.js.map +1 -0
  37. package/dist/modules/formats/formatJsonObject.js +25 -0
  38. package/dist/modules/formats/formatJsonObject.js.map +1 -0
  39. package/dist/modules/formats/formatJsonString.js +13 -0
  40. package/dist/modules/formats/formatJsonString.js.map +1 -0
  41. package/dist/modules/formats/formatToCapitalizeFirstWordLetter.js +8 -0
  42. package/dist/modules/formats/formatToCapitalizeFirstWordLetter.js.map +1 -0
  43. package/dist/modules/formats/formatToCep.js +11 -0
  44. package/dist/modules/formats/formatToCep.js.map +1 -0
  45. package/dist/modules/formats/formatToCnpj.js +11 -0
  46. package/dist/modules/formats/formatToCnpj.js.map +1 -0
  47. package/dist/modules/formats/formatToCpf.js +11 -0
  48. package/dist/modules/formats/formatToCpf.js.map +1 -0
  49. package/dist/modules/formats/formatToCurrency.js +15 -0
  50. package/dist/modules/formats/formatToCurrency.js.map +1 -0
  51. package/dist/modules/formats/formatToEllipsis.js +12 -0
  52. package/dist/modules/formats/formatToEllipsis.js.map +1 -0
  53. package/dist/modules/formats/formatToHiddenDigits.js +24 -0
  54. package/dist/modules/formats/formatToHiddenDigits.js.map +1 -0
  55. package/dist/modules/formats/formatToPhone.js +16 -0
  56. package/dist/modules/formats/formatToPhone.js.map +1 -0
  57. package/dist/modules/generators/generateColorByString.js +11 -0
  58. package/dist/modules/generators/generateColorByString.js.map +1 -0
  59. package/dist/modules/generators/generateId.js +34 -0
  60. package/dist/modules/generators/generateId.js.map +1 -0
  61. package/dist/modules/generators/generateSlug.js +9 -0
  62. package/dist/modules/generators/generateSlug.js.map +1 -0
  63. package/dist/modules/parsers/parseLargeFields.js +19 -0
  64. package/dist/modules/parsers/parseLargeFields.js.map +1 -0
  65. package/dist/modules/parsers/parseSensitiveData.js +30 -0
  66. package/dist/modules/parsers/parseSensitiveData.js.map +1 -0
  67. package/dist/modules/parsers/parseToDate.js +25 -0
  68. package/dist/modules/parsers/parseToDate.js.map +1 -0
  69. package/dist/modules/services/validateDateService.js +63 -0
  70. package/dist/modules/services/validateDateService.js.map +1 -0
  71. package/dist/modules/utilities/calculateCardInstallment.js +19 -0
  72. package/dist/modules/utilities/calculateCardInstallment.js.map +1 -0
  73. package/dist/modules/utilities/ensureQuotes.js +9 -0
  74. package/dist/modules/utilities/ensureQuotes.js.map +1 -0
  75. package/dist/modules/utilities/findCountryMask.js +25 -0
  76. package/dist/modules/utilities/findCountryMask.js.map +1 -0
  77. package/dist/modules/utilities/isHtml.js +8 -0
  78. package/dist/modules/utilities/isHtml.js.map +1 -0
  79. package/dist/modules/utilities/removeCurrencySymbols.js +8 -0
  80. package/dist/modules/utilities/removeCurrencySymbols.js.map +1 -0
  81. package/dist/modules/utilities/removeNonNumeric.js +8 -0
  82. package/dist/modules/utilities/removeNonNumeric.js.map +1 -0
  83. package/dist/modules/utilities/stripHtmlTags.js +8 -0
  84. package/dist/modules/utilities/stripHtmlTags.js.map +1 -0
  85. package/dist/{services/truncateLargeFields.d.ts → parsers/parseLargeFields.d.ts} +5 -8
  86. package/dist/parsers/parseLargeFields.d.ts.map +1 -0
  87. package/dist/parsers/parseSensitiveData.d.ts +22 -0
  88. package/dist/parsers/parseSensitiveData.d.ts.map +1 -0
  89. package/dist/parsers/parseToDate.d.ts +25 -0
  90. package/dist/parsers/parseToDate.d.ts.map +1 -0
  91. package/dist/services/validateDateService.d.ts +34 -0
  92. package/dist/services/validateDateService.d.ts.map +1 -0
  93. package/dist/utilities/calculateCardInstallment.d.ts +25 -0
  94. package/dist/utilities/calculateCardInstallment.d.ts.map +1 -0
  95. package/dist/utilities/ensureQuotes.d.ts +20 -0
  96. package/dist/utilities/ensureQuotes.d.ts.map +1 -0
  97. package/dist/utilities/findCountryMask.d.ts +21 -0
  98. package/dist/utilities/findCountryMask.d.ts.map +1 -0
  99. package/dist/utilities/isHtml.d.ts +21 -0
  100. package/dist/utilities/isHtml.d.ts.map +1 -0
  101. package/dist/{services/removeCurrencySymbols.js → utilities/removeCurrencySymbols.d.ts} +6 -7
  102. package/dist/utilities/removeCurrencySymbols.d.ts.map +1 -0
  103. package/dist/utilities/removeNonNumeric.d.ts +15 -0
  104. package/dist/utilities/removeNonNumeric.d.ts.map +1 -0
  105. package/dist/utilities/stripHtmlTags.d.ts +18 -0
  106. package/dist/utilities/stripHtmlTags.d.ts.map +1 -0
  107. package/package.json +255 -16
  108. package/dist/bundle.js +0 -2337
  109. package/dist/bundle.umd.cjs +0 -6
  110. package/dist/formats/formatDate.js +0 -66
  111. package/dist/formats/formatJsonObject.js +0 -90
  112. package/dist/formats/formatJsonString.js +0 -46
  113. package/dist/formats/formatToCapitalizeFirstWordLetter.js +0 -42
  114. package/dist/formats/formatToCep.js +0 -34
  115. package/dist/formats/formatToCnpj.js +0 -35
  116. package/dist/formats/formatToCpf.js +0 -35
  117. package/dist/formats/formatToCpfCnpj.d.ts +0 -24
  118. package/dist/formats/formatToCpfCnpj.d.ts.map +0 -1
  119. package/dist/formats/formatToCpfCnpj.js +0 -33
  120. package/dist/formats/formatToCurrency.js +0 -41
  121. package/dist/formats/formatToDate.d.ts +0 -32
  122. package/dist/formats/formatToDate.d.ts.map +0 -1
  123. package/dist/formats/formatToDate.js +0 -50
  124. package/dist/formats/formatToEllipsis.js +0 -21
  125. package/dist/formats/formatToHiddenDigits.js +0 -60
  126. package/dist/formats/formatToPhone.js +0 -131
  127. package/dist/generators/generateColorByString.js +0 -25
  128. package/dist/generators/generateId.js +0 -29
  129. package/dist/generators/generateSlug.js +0 -25
  130. package/dist/services/calculateCardInstallment.d.ts +0 -40
  131. package/dist/services/calculateCardInstallment.d.ts.map +0 -1
  132. package/dist/services/calculateCardInstallment.js +0 -55
  133. package/dist/services/ensureQuotes.d.ts +0 -14
  134. package/dist/services/ensureQuotes.d.ts.map +0 -1
  135. package/dist/services/ensureQuotes.js +0 -19
  136. package/dist/services/isHtml.d.ts +0 -22
  137. package/dist/services/isHtml.d.ts.map +0 -1
  138. package/dist/services/isHtml.js +0 -24
  139. package/dist/services/maskSensitiveData.d.ts +0 -26
  140. package/dist/services/maskSensitiveData.d.ts.map +0 -1
  141. package/dist/services/maskSensitiveData.js +0 -60
  142. package/dist/services/removeCurrencySymbols.d.ts +0 -21
  143. package/dist/services/removeCurrencySymbols.d.ts.map +0 -1
  144. package/dist/services/removeNonNumeric.d.ts +0 -16
  145. package/dist/services/removeNonNumeric.d.ts.map +0 -1
  146. package/dist/services/removeNonNumeric.js +0 -16
  147. package/dist/services/stripHtmlTags.d.ts +0 -16
  148. package/dist/services/stripHtmlTags.d.ts.map +0 -1
  149. package/dist/services/stripHtmlTags.js +0 -16
  150. package/dist/services/truncateLargeFields.d.ts.map +0 -1
  151. package/dist/services/truncateLargeFields.js +0 -57
package/README.md CHANGED
@@ -1,123 +1,152 @@
1
1
  # @arkyn/shared
2
2
 
3
- A comprehensive collection of reusable utilities for consistent data handling across your applications. Provides formatting functions, validation tools, generators, and services to streamline common development tasks.
3
+ Comprehensive collection of reusable utilities for data formatting, validation, generation, and manipulation, featuring Brazilian document validators and financial tools.
4
4
 
5
5
  [![npm version](https://img.shields.io/npm/v/@arkyn/shared.svg)](https://www.npmjs.com/package/@arkyn/shared)
6
6
  [![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)
7
7
  [![TypeScript](https://img.shields.io/badge/TypeScript-Ready-blue.svg)](https://www.typescriptlang.org/)
8
8
 
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
+
9
13
  ## ✨ Features
10
14
 
11
- - 📅 **Date Utilities** - Flexible date manipulation and timezone support
12
- - 🏦 **Financial Tools** - Currency formatting and installment calculations
13
- - 🔒 **Brazilian Validators** - CPF, CNPJ, CEP, RG, and phone number validation
14
- - 🎨 **String Utilities** - Formatting, masking, and text manipulation
15
- - 🔧 **Generators** - ID generation, slugs, colors, and more
16
- - 🛡️ **Data Security** - Sensitive data masking and sanitization
17
- - 🌐 **Internationalization** - Multi-locale support for formatting
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** `>=18.0.0`
29
+ - **Bun** `>=1.0.0`
30
+ - **`libphonenumber-js`** `>=1.13.7` — optional peer dependency, required only if you use `formatToPhone` or `findCountryMask`
18
31
 
19
32
  ## 📦 Installation
20
33
 
34
+ > **ESM only.** This package ships as native ES modules with no CommonJS build — use `import`, not `require()`.
35
+
21
36
  ```bash
22
37
  npm install @arkyn/shared
23
38
  ```
24
39
 
40
+ If you plan to use `formatToPhone` or `findCountryMask`, also install the peer dependency:
41
+
42
+ ```bash
43
+ npm install libphonenumber-js
44
+ ```
45
+
25
46
  ## 🚀 Quick Start
26
47
 
27
48
  ```typescript
28
49
  import {
29
50
  formatToCpf,
30
- validateCpf,
31
51
  formatToCurrency,
32
52
  generateId,
53
+ generateSlug,
33
54
  } from "@arkyn/shared";
34
55
 
35
- // Format and validate CPF
36
- const cpf = formatToCpf("12345678901"); // "123.456.789-01"
37
- const isValid = validateCpf(cpf); // true
56
+ // Format a raw CPF string (throws if it doesn't have 11 digits)
57
+ const cpf = formatToCpf("12345678909");
58
+ // "123.456.789-09"
38
59
 
39
- // Format currency
40
- const price = formatToCurrency(1299.99); // "R$ 1.299,99"
60
+ // Format currency using a currency code from @arkyn/templates
61
+ const price = formatToCurrency(1234.56, "BRL");
62
+ // "R$ 1.234,56"
41
63
 
42
- // Generate unique ID
43
- const id = generateId(); // "uuid-v4-string"
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"
44
71
  ```
45
72
 
46
- ## 📋 API Reference
73
+ ## 📖 API Reference
47
74
 
48
- ### 📅 Date Formatting
75
+ ### Formats
49
76
 
50
- #### formatDate(date, inputFormat, outputFormat, timezone?)
77
+ #### formatDate
51
78
 
52
- Formats dates with timezone support.
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.
53
80
 
54
81
  ```typescript
55
82
  import { formatDate } from "@arkyn/shared";
56
83
 
57
- // Format ISO date to Brazilian format
58
- const formatted = formatDate("2023-12-25", "isoDate", "DD/MM/YYYY");
59
- // Result: "25/12/2023"
60
-
61
- // With timezone
62
- const withTz = formatDate(
63
- "2023-12-25T10:00:00Z",
64
- "isoDate",
65
- "DD/MM/YYYY HH:mm",
66
- -3
67
- );
68
- // Result: "25/12/2023 07:00"
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"
69
89
  ```
70
90
 
71
- #### formatToDate(date, inputFormat, timezone?)
91
+ #### formatJsonObject
72
92
 
73
- Converts strings to Date objects.
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.
74
94
 
75
95
  ```typescript
76
- import { formatToDate } from "@arkyn/shared";
96
+ import { formatJsonObject } from "@arkyn/shared";
77
97
 
78
- const date = formatToDate("25/12/2023", "brazilianDate");
79
- // Result: Date object
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
+ // }
80
108
  ```
81
109
 
82
- ### 🏦 Financial Formatting
83
-
84
- #### formatToCurrency(value: number): string
110
+ #### formatJsonString
85
111
 
86
- Formats numbers to Brazilian currency format.
112
+ Parses a JSON string and returns a pretty-printed representation. Throws `Error` if the input is not valid JSON.
87
113
 
88
114
  ```typescript
89
- import { formatToCurrency } from "@arkyn/shared";
115
+ import { formatJsonString } from "@arkyn/shared";
90
116
 
91
- formatToCurrency(1299.99); // "R$ 1.299,99"
92
- formatToCurrency(0.5); // "R$ 0,50"
117
+ formatJsonString('{"name":"John","hobbies":["reading","gaming"]}');
118
+ // {
119
+ // "name": "John",
120
+ // "hobbies": [
121
+ // "reading",
122
+ // "gaming"
123
+ // ]
124
+ // }
93
125
  ```
94
126
 
95
- #### calculateCardInstallment(total: number, installments: number): number[]
127
+ #### formatToCapitalizeFirstWordLetter
96
128
 
97
- Calculates installment values for credit card payments.
129
+ Capitalizes the first letter of each space-separated word and lowercases the rest.
98
130
 
99
131
  ```typescript
100
- import { calculateCardInstallment } from "@arkyn/shared";
132
+ import { formatToCapitalizeFirstWordLetter } from "@arkyn/shared";
101
133
 
102
- const installments = calculateCardInstallment(1000, 10);
103
- // Result: [100, 100, 100, 100, 100, 100, 100, 100, 100, 100]
134
+ formatToCapitalizeFirstWordLetter("HELLO WORLD"); // "Hello World"
104
135
  ```
105
136
 
106
- ### 🇧🇷 Brazilian Document Formatting
107
-
108
- #### formatToCpf(value: string): string
137
+ #### formatToCep
109
138
 
110
- Formats strings to CPF format.
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.
111
140
 
112
141
  ```typescript
113
- import { formatToCpf } from "@arkyn/shared";
142
+ import { formatToCep } from "@arkyn/shared";
114
143
 
115
- formatToCpf("12345678901"); // "123.456.789-01"
144
+ formatToCep("12345678"); // "12345-678"
116
145
  ```
117
146
 
118
- #### formatToCnpj(value: string): string
147
+ #### formatToCnpj
119
148
 
120
- Formats strings to CNPJ format.
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.
121
150
 
122
151
  ```typescript
123
152
  import { formatToCnpj } from "@arkyn/shared";
@@ -125,380 +154,241 @@ import { formatToCnpj } from "@arkyn/shared";
125
154
  formatToCnpj("12345678000195"); // "12.345.678/0001-95"
126
155
  ```
127
156
 
128
- #### formatToCpfCnpj(value: string): string
157
+ #### formatToCpf
129
158
 
130
- Auto-detects and formats CPF or CNPJ.
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.
131
160
 
132
161
  ```typescript
133
- import { formatToCpfCnpj } from "@arkyn/shared";
134
-
135
- formatToCpfCnpj("12345678901"); // "123.456.789-01" (CPF)
136
- formatToCpfCnpj("12345678000195"); // "12.345.678/0001-95" (CNPJ)
137
- ```
138
-
139
- #### formatToCep(value: string): string
140
-
141
- Formats strings to CEP format.
142
-
143
- ```typescript
144
- import { formatToCep } from "@arkyn/shared";
162
+ import { formatToCpf } from "@arkyn/shared";
145
163
 
146
- formatToCep("01234567"); // "01234-567"
164
+ formatToCpf("12345678909"); // "123.456.789-09"
147
165
  ```
148
166
 
149
- #### formatToPhone(value: string): string
167
+ #### formatToCurrency
150
168
 
151
- Formats phone numbers.
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.
152
170
 
153
171
  ```typescript
154
- import { formatToPhone } from "@arkyn/shared";
172
+ import { formatToCurrency } from "@arkyn/shared";
155
173
 
156
- formatToPhone("11987654321"); // "(11) 98765-4321"
174
+ formatToCurrency(1234.56, "BRL"); // "R$ 1.234,56"
175
+ formatToCurrency(1234.56, "USD", { showPrefix: false }); // "1,234.56"
157
176
  ```
158
177
 
159
- ---
160
-
161
- ### 🎨 String Utilities
178
+ #### formatToEllipsis
162
179
 
163
- #### formatToEllipsis(value: string, maxLength: number): string
164
-
165
- Truncates strings with ellipsis.
180
+ Truncates a string to `maxLength`, avoiding breaking mid-word, and appends `"..."` if truncation occurred.
166
181
 
167
182
  ```typescript
168
183
  import { formatToEllipsis } from "@arkyn/shared";
169
184
 
170
- formatToEllipsis("This is a very long text", 10); // "This is a..."
185
+ formatToEllipsis("Hello, world!", 5); // "Hello..."
171
186
  ```
172
187
 
173
- #### formatToHiddenDigits(value: string): string
188
+ #### formatToHiddenDigits
174
189
 
175
- Masks sensitive information.
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 `"*"`.
176
191
 
177
192
  ```typescript
178
193
  import { formatToHiddenDigits } from "@arkyn/shared";
179
194
 
180
- formatToHiddenDigits("123456789"); // "12***6789"
195
+ formatToHiddenDigits("123-456-7890", { range: 3 }); // "***-456-7890"
196
+ formatToHiddenDigits("123-456-7890", { range: [4, 6], hider: "#" });
197
+ // "123-###-7890"
181
198
  ```
182
199
 
183
- #### formatToCapitalizeFirstWordLetter(value: string): string
200
+ #### formatToPhone
184
201
 
185
- Capitalizes the first letter of each word.
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.
186
203
 
187
204
  ```typescript
188
- import { formatToCapitalizeFirstWordLetter } from "@arkyn/shared";
189
-
190
- formatToCapitalizeFirstWordLetter("hello world"); // "Hello World"
191
- ```
192
-
193
- #### stripHtmlTags(value: string): string
194
-
195
- Removes HTML tags from strings.
196
-
197
- ```typescript
198
- import { stripHtmlTags } from "@arkyn/shared";
205
+ import { formatToPhone } from "@arkyn/shared";
199
206
 
200
- stripHtmlTags("<p>Hello <strong>World</strong></p>"); // "Hello World"
207
+ formatToPhone("+5534920524282"); // "(34) 92052-4282"
208
+ formatToPhone("+12125550199"); // "(212) 555-0199"
201
209
  ```
202
210
 
203
- ### 📄 JSON Utilities
211
+ ### Generators
204
212
 
205
- #### formatJsonObject(obj: object): string
213
+ #### generateColorByString
206
214
 
207
- Converts objects to formatted JSON strings.
215
+ Generates a deterministic hexadecimal color code from a hash of the input string — the same input always produces the same color.
208
216
 
209
217
  ```typescript
210
- import { formatJsonObject } from "@arkyn/shared";
211
-
212
- const obj = { name: "John", age: 30 };
213
- formatJsonObject(obj); // Pretty-printed JSON string
214
- ```
215
-
216
- #### formatJsonString(json: string): object
217
-
218
- Parses JSON strings safely.
219
-
220
- ```typescript
221
- import { formatJsonString } from "@arkyn/shared";
218
+ import { generateColorByString } from "@arkyn/shared";
222
219
 
223
- const parsed = formatJsonString('{"name":"John"}');
224
- // Result: { name: "John" }
220
+ generateColorByString("example"); // "#5e8f9a" (consistent for the same input)
225
221
  ```
226
222
 
227
- ---
228
-
229
- ### 🔧 Generators
230
-
231
- #### generateId(): string
223
+ #### generateId
232
224
 
233
- Generates unique identifiers using UUID v4.
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.
234
226
 
235
227
  ```typescript
236
228
  import { generateId } from "@arkyn/shared";
237
229
 
238
- const id = generateId(); // "f47ac10b-58cc-4372-a567-0e02b2c3d479"
230
+ generateId("text", "v4"); // "550e8400-e29b-41d4-a716-446655440000"
231
+ generateId("binary", "v7"); // Uint8Array([...])
239
232
  ```
240
233
 
241
- #### generateSlug(value: string): string
234
+ #### generateSlug
242
235
 
243
- Creates URL-friendly slugs.
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.
244
237
 
245
238
  ```typescript
246
239
  import { generateSlug } from "@arkyn/shared";
247
240
 
248
- generateSlug("Hello World! How are you?"); // "hello-world-how-are-you"
241
+ generateSlug("Hello, World! This is a Test.");
242
+ // "hello-world-this-is-a-test"
249
243
  ```
250
244
 
251
- #### generateColorByString(value: string): string
245
+ ### Parsers
252
246
 
253
- Generates consistent colors from strings.
247
+ #### parseLargeFields
254
248
 
255
- ```typescript
256
- import { generateColorByString } from "@arkyn/shared";
257
-
258
- generateColorByString("John Doe"); // "#3498db" (always same color for same input)
259
- ```
260
-
261
- ### 🛡️ Data Security & Sanitization
262
-
263
- #### maskSensitiveData(data: string): string
264
-
265
- Masks sensitive information in logs and outputs.
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.
266
250
 
267
251
  ```typescript
268
- import { maskSensitiveData } from "@arkyn/shared";
252
+ import { parseLargeFields } from "@arkyn/shared";
269
253
 
270
- maskSensitiveData("user@email.com"); // "us***@email.com"
271
- ```
272
-
273
- #### removeNonNumeric(value: string): string
254
+ const json = JSON.stringify({
255
+ name: "John",
256
+ description: "A very long description that exceeds the maximum length...",
257
+ });
274
258
 
275
- Removes all non-numeric characters.
276
-
277
- ```typescript
278
- import { removeNonNumeric } from "@arkyn/shared";
279
-
280
- removeNonNumeric("(11) 98765-4321"); // "11987654321"
259
+ parseLargeFields(json, 50);
260
+ // '{"name":"John","description":"To large information: field as 57 characters"}'
281
261
  ```
282
262
 
283
- #### removeCurrencySymbols(value: string): string
263
+ #### parseSensitiveData
284
264
 
285
- Removes currency symbols and formatting.
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).
286
266
 
287
267
  ```typescript
288
- import { removeCurrencySymbols } from "@arkyn/shared";
268
+ import { parseSensitiveData } from "@arkyn/shared";
289
269
 
290
- removeCurrencySymbols("R$ 1.299,99"); // "1299.99"
291
- ```
292
-
293
- #### ensureQuotes(rawValue: string): string
270
+ const jsonString = JSON.stringify({
271
+ username: "user123",
272
+ password: "secret",
273
+ profile: { creditCard: "1234-5678-9012-3456" },
274
+ });
294
275
 
295
- Ensures strings are properly quoted.
296
-
297
- ```typescript
298
- import { ensureQuotes } from "@arkyn/shared";
299
-
300
- ensureQuotes("hello"); // '"hello"'
301
- ensureQuotes('"hello"'); // '"hello"' (no double quotes)
276
+ parseSensitiveData(jsonString, ["password", "creditCard"]);
277
+ // '{"username":"user123","password":"****","profile":{"creditCard":"****"}}'
302
278
  ```
303
279
 
304
- #### truncateLargeFields(obj: object, maxLength: number): object
280
+ #### parseToDate
305
281
 
306
- Truncates large text fields in objects.
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.
307
283
 
308
284
  ```typescript
309
- import { truncateLargeFields } from "@arkyn/shared";
285
+ import { parseToDate } from "@arkyn/shared";
310
286
 
311
- const data = { description: "Very long text..." };
312
- truncateLargeFields(data, 10); // { description: 'Very long...' }
313
- ```
287
+ parseToDate(["25/12/2023", "15:30:00"], "brazilianDate", -3);
288
+ // Date: 2023-12-25T12:30:00.000Z
314
289
 
315
- ---
290
+ parseToDate(["2023-12-25"], "timestamp");
291
+ // Date: 2023-12-25T00:00:00.000Z
292
+ ```
316
293
 
317
- ### ✅ Validation Functions
294
+ ### Services
318
295
 
319
- #### validateCpf(value: string): boolean
296
+ #### ValidateDateService
320
297
 
321
- Validates Brazilian CPF numbers.
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.
322
299
 
323
300
  ```typescript
324
- import { validateCpf } from "@arkyn/shared";
301
+ import { ValidateDateService } from "@arkyn/shared";
325
302
 
326
- validateCpf("123.456.789-01"); // true/false
327
- validateCpf("12345678901"); // true/false (accepts without formatting)
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
328
308
  ```
329
309
 
330
- #### validateCnpj(value: string): boolean
331
-
332
- Validates Brazilian CNPJ numbers.
333
-
334
- ```typescript
335
- import { validateCnpj } from "@arkyn/shared";
336
-
337
- validateCnpj("12.345.678/0001-95"); // true/false
338
- ```
310
+ ### Utilities
339
311
 
340
- #### validateCep(value: string): boolean
312
+ #### calculateCardInstallment
341
313
 
342
- Validates Brazilian postal codes.
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`.
343
315
 
344
316
  ```typescript
345
- import { validateCep } from "@arkyn/shared";
317
+ import { calculateCardInstallment } from "@arkyn/shared";
346
318
 
347
- validateCep("01234-567"); // true/false
348
- validateCep("01234567"); // true/false (accepts without formatting)
319
+ calculateCardInstallment({ cashPrice: 1000, numberInstallments: 12, fees: 0.02 });
320
+ // { totalPrice: 1124.62, installmentPrice: 93.72 }
349
321
  ```
350
322
 
351
- #### validatePhone(value: string): boolean
323
+ #### ensureQuotes
352
324
 
353
- Validates Brazilian phone numbers.
325
+ Wraps a string in double quotes unless it is already enclosed in single or double quotes.
354
326
 
355
327
  ```typescript
356
- import { validatePhone } from "@arkyn/shared";
328
+ import { ensureQuotes } from "@arkyn/shared";
357
329
 
358
- validatePhone("(11) 98765-4321"); // true/false
359
- validatePhone("11987654321"); // true/false
330
+ ensureQuotes("example"); // '"example"'
331
+ ensureQuotes('"already quoted"'); // '"already quoted"'
360
332
  ```
361
333
 
362
- #### validateRg(value: string): boolean
334
+ #### findCountryMask
363
335
 
364
- Validates Brazilian RG (ID) numbers.
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.
365
337
 
366
338
  ```typescript
367
- import { validateRg } from "@arkyn/shared";
339
+ import { findCountryMask } from "@arkyn/shared";
368
340
 
369
- validateRg("12.345.678-9"); // true/false
341
+ const [mask, country] = findCountryMask("+5511999999999");
342
+ // mask: "(__) _____-____"
343
+ // country.name: "Brazil"
370
344
  ```
371
345
 
372
- #### validateDate(value: string): boolean
346
+ #### isHtml
373
347
 
374
- Validates date strings.
348
+ Checks whether a string contains HTML markup (opening or closing tags), case-insensitively.
375
349
 
376
350
  ```typescript
377
- import { validateDate } from "@arkyn/shared";
351
+ import { isHtml } from "@arkyn/shared";
378
352
 
379
- validateDate("25/12/2023"); // true/false
380
- validateDate("2023-12-25"); // true/false
353
+ isHtml("<p>Hello world</p>"); // true
354
+ isHtml("Plain text"); // false
381
355
  ```
382
356
 
383
- #### validatePassword(value: string): boolean
357
+ #### removeCurrencySymbols
384
358
 
385
- Validates password strength.
359
+ Removes currency symbols (`R$`, `$`, `€`, `¥`, `£`, and other Unicode currency symbols) from a string and trims whitespace.
386
360
 
387
361
  ```typescript
388
- import { validatePassword } from "@arkyn/shared";
362
+ import { removeCurrencySymbols } from "@arkyn/shared";
389
363
 
390
- validatePassword("MyPassword123!"); // true/false
364
+ removeCurrencySymbols("R$13,45"); // "13,45"
365
+ removeCurrencySymbols("€99.99"); // "99.99"
391
366
  ```
392
367
 
393
- ## 🔧 Advanced Usage
368
+ #### removeNonNumeric
394
369
 
395
- ### Complete Form Validation
370
+ Strips all non-numeric characters from a string.
396
371
 
397
372
  ```typescript
398
- import {
399
- validateCpf,
400
- validatePhone,
401
- validateCep,
402
- formatToCpf,
403
- formatToPhone,
404
- formatToCep,
405
- } from "@arkyn/shared";
406
-
407
- function validateUserForm(data) {
408
- const errors = {};
409
-
410
- // Validate and format CPF
411
- if (!validateCpf(data.cpf)) {
412
- errors.cpf = "Invalid CPF";
413
- } else {
414
- data.cpf = formatToCpf(data.cpf);
415
- }
416
-
417
- // Validate and format phone
418
- if (!validatePhone(data.phone)) {
419
- errors.phone = "Invalid phone number";
420
- } else {
421
- data.phone = formatToPhone(data.phone);
422
- }
423
-
424
- // Validate and format CEP
425
- if (!validateCep(data.cep)) {
426
- errors.cep = "Invalid postal code";
427
- } else {
428
- data.cep = formatToCep(data.cep);
429
- }
430
-
431
- return { isValid: Object.keys(errors).length === 0, errors, data };
432
- }
433
- ```
434
-
435
- ### Data Processing Pipeline
436
-
437
- ```typescript
438
- import {
439
- removeNonNumeric,
440
- formatToCurrency,
441
- calculateCardInstallment,
442
- generateId,
443
- } from "@arkyn/shared";
373
+ import { removeNonNumeric } from "@arkyn/shared";
444
374
 
445
- function processOrder(orderData) {
446
- // Clean price input
447
- const numericPrice = removeNonNumeric(orderData.price);
448
- const price = parseFloat(numericPrice) / 100; // Convert cents to reais
449
-
450
- // Format for display
451
- const formattedPrice = formatToCurrency(price);
452
-
453
- // Calculate installments
454
- const installments = calculateCardInstallment(price, 12);
455
-
456
- // Generate order ID
457
- const orderId = generateId();
458
-
459
- return {
460
- id: orderId,
461
- price: formattedPrice,
462
- installmentOptions: installments.map((value, index) => ({
463
- number: index + 1,
464
- value: formatToCurrency(value),
465
- })),
466
- };
467
- }
375
+ removeNonNumeric("abc123def456"); // "123456"
468
376
  ```
469
377
 
470
- ## 🧪 Development
378
+ #### stripHtmlTags
471
379
 
472
- ```bash
473
- # Install dependencies
474
- bun install
380
+ Removes HTML tags from a string, including `<script>` and `<style>` blocks and HTML comments.
475
381
 
476
- # Build the package
477
- bun run build
478
-
479
- # Run tests
480
- bun run test
382
+ ```typescript
383
+ import { stripHtmlTags } from "@arkyn/shared";
481
384
 
482
- # Type check
483
- bun run typecheck
385
+ stripHtmlTags("<p>Hello <strong>World</strong></p>"); // "Hello World"
484
386
  ```
485
387
 
486
- ---
487
-
488
- ## 🤝 Contributing
388
+ ## 📚 Documentation
489
389
 
490
- Contributions are welcome! Please read our contributing guidelines and submit pull requests to help improve the package.
390
+ Full documentation: [https://docs.arkyn.dev/docs/shared/introduction](https://docs.arkyn.dev/docs/shared/introduction)
491
391
 
492
392
  ## 📄 License
493
393
 
494
394
  This project is licensed under the Apache 2.0 License - see the [LICENSE](./LICENSE.txt) file for details.
495
-
496
- ## 🔗 Links
497
-
498
- - [GitHub Repository](https://github.com/Lucas-Eduardo-Goncalves/arkyn)
499
- - [NPM Package](https://www.npmjs.com/package/@arkyn/shared)
500
- - [Full Documentation](https://github.com/Lucas-Eduardo-Goncalves/arkyn#readme)
501
-
502
- ---
503
-
504
- Made with ❤️ by the Arkyn team