@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.
- package/README.md +344 -69
- package/dist/formats/formatDate.d.ts +18 -26
- package/dist/formats/formatDate.d.ts.map +1 -1
- package/dist/formats/formatJsonObject.d.ts +5 -7
- package/dist/formats/formatJsonObject.d.ts.map +1 -1
- package/dist/formats/formatJsonString.d.ts +5 -21
- package/dist/formats/formatJsonString.d.ts.map +1 -1
- package/dist/formats/formatToCapitalizeFirstWordLetter.d.ts +5 -24
- package/dist/formats/formatToCapitalizeFirstWordLetter.d.ts.map +1 -1
- package/dist/formats/formatToCep.d.ts +3 -11
- package/dist/formats/formatToCep.d.ts.map +1 -1
- package/dist/formats/formatToCnpj.d.ts +3 -14
- package/dist/formats/formatToCnpj.d.ts.map +1 -1
- package/dist/formats/formatToCpf.d.ts +4 -13
- package/dist/formats/formatToCpf.d.ts.map +1 -1
- package/dist/formats/formatToCurrency.d.ts +10 -23
- package/dist/formats/formatToCurrency.d.ts.map +1 -1
- package/dist/formats/formatToEllipsis.d.ts +5 -5
- package/dist/formats/formatToEllipsis.d.ts.map +1 -1
- package/dist/formats/formatToHiddenDigits.d.ts +15 -26
- package/dist/formats/formatToHiddenDigits.d.ts.map +1 -1
- package/dist/formats/formatToPhone.d.ts +11 -23
- package/dist/formats/formatToPhone.d.ts.map +1 -1
- package/dist/generators/generateColorByString.d.ts +6 -4
- package/dist/generators/generateColorByString.d.ts.map +1 -1
- package/dist/generators/generateId.d.ts +8 -19
- package/dist/generators/generateId.d.ts.map +1 -1
- package/dist/generators/generateSlug.d.ts +8 -2
- package/dist/generators/generateSlug.d.ts.map +1 -1
- package/dist/index.d.ts +11 -16
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +371 -34
- package/dist/modules/formats/formatDate.js +35 -0
- package/dist/modules/formats/formatJsonObject.js +23 -0
- package/dist/modules/formats/formatJsonString.js +11 -0
- package/dist/modules/formats/formatToCapitalizeFirstWordLetter.js +6 -0
- package/dist/modules/formats/formatToCep.js +9 -0
- package/dist/modules/formats/formatToCnpj.js +9 -0
- package/dist/modules/formats/formatToCpf.js +9 -0
- package/dist/modules/formats/formatToCurrency.js +13 -0
- package/dist/modules/formats/formatToEllipsis.js +10 -0
- package/dist/modules/formats/formatToHiddenDigits.js +22 -0
- package/dist/modules/formats/formatToPhone.js +14 -0
- package/dist/modules/generators/generateColorByString.js +9 -0
- package/dist/modules/generators/generateId.js +32 -0
- package/dist/modules/generators/generateSlug.js +7 -0
- package/dist/modules/index.js +26 -0
- package/dist/modules/parsers/parseLargeFields.js +17 -0
- package/dist/modules/parsers/parseSensitiveData.js +28 -0
- package/dist/modules/parsers/parseToDate.js +23 -0
- package/dist/modules/services/validateDateService.js +61 -0
- package/dist/modules/utilities/calculateCardInstallment.js +17 -0
- package/dist/modules/utilities/ensureQuotes.js +7 -0
- package/dist/modules/utilities/findCountryMask.js +23 -0
- package/dist/modules/utilities/isHtml.js +6 -0
- package/dist/modules/utilities/removeCurrencySymbols.js +6 -0
- package/dist/modules/utilities/removeNonNumeric.js +6 -0
- package/dist/modules/utilities/stripHtmlTags.js +6 -0
- package/dist/{services/truncateLargeFields.d.ts → parsers/parseLargeFields.d.ts} +5 -8
- package/dist/parsers/parseLargeFields.d.ts.map +1 -0
- package/dist/parsers/parseSensitiveData.d.ts +22 -0
- package/dist/parsers/parseSensitiveData.d.ts.map +1 -0
- package/dist/parsers/parseToDate.d.ts +25 -0
- package/dist/parsers/parseToDate.d.ts.map +1 -0
- package/dist/services/validateDateService.d.ts +34 -0
- package/dist/services/validateDateService.d.ts.map +1 -0
- package/dist/utilities/calculateCardInstallment.d.ts +25 -0
- package/dist/utilities/calculateCardInstallment.d.ts.map +1 -0
- package/dist/utilities/ensureQuotes.d.ts +20 -0
- package/dist/utilities/ensureQuotes.d.ts.map +1 -0
- package/dist/utilities/findCountryMask.d.ts +21 -0
- package/dist/utilities/findCountryMask.d.ts.map +1 -0
- package/dist/utilities/isHtml.d.ts +21 -0
- package/dist/utilities/isHtml.d.ts.map +1 -0
- package/dist/{services/removeCurrencySymbols.js → utilities/removeCurrencySymbols.d.ts} +6 -7
- package/dist/utilities/removeCurrencySymbols.d.ts.map +1 -0
- package/dist/utilities/removeNonNumeric.d.ts +15 -0
- package/dist/utilities/removeNonNumeric.d.ts.map +1 -0
- package/dist/utilities/stripHtmlTags.d.ts +18 -0
- package/dist/utilities/stripHtmlTags.d.ts.map +1 -0
- package/package.json +183 -13
- package/dist/formats/formatDate.js +0 -66
- package/dist/formats/formatJsonObject.js +0 -90
- package/dist/formats/formatJsonString.js +0 -46
- package/dist/formats/formatToCapitalizeFirstWordLetter.js +0 -42
- package/dist/formats/formatToCep.js +0 -34
- package/dist/formats/formatToCnpj.js +0 -35
- package/dist/formats/formatToCpf.js +0 -35
- package/dist/formats/formatToCpfCnpj.d.ts +0 -24
- package/dist/formats/formatToCpfCnpj.d.ts.map +0 -1
- package/dist/formats/formatToCpfCnpj.js +0 -33
- package/dist/formats/formatToCurrency.js +0 -41
- package/dist/formats/formatToDate.d.ts +0 -32
- package/dist/formats/formatToDate.d.ts.map +0 -1
- package/dist/formats/formatToDate.js +0 -50
- package/dist/formats/formatToEllipsis.js +0 -21
- package/dist/formats/formatToHiddenDigits.js +0 -60
- package/dist/formats/formatToPhone.js +0 -131
- package/dist/generators/generateColorByString.js +0 -25
- package/dist/generators/generateId.js +0 -29
- package/dist/generators/generateSlug.js +0 -25
- package/dist/services/calculateCardInstallment.d.ts +0 -40
- package/dist/services/calculateCardInstallment.d.ts.map +0 -1
- package/dist/services/calculateCardInstallment.js +0 -55
- package/dist/services/ensureQuotes.d.ts +0 -14
- package/dist/services/ensureQuotes.d.ts.map +0 -1
- package/dist/services/ensureQuotes.js +0 -19
- package/dist/services/maskSensitiveData.d.ts +0 -26
- package/dist/services/maskSensitiveData.d.ts.map +0 -1
- package/dist/services/maskSensitiveData.js +0 -60
- package/dist/services/removeCurrencySymbols.d.ts +0 -21
- package/dist/services/removeCurrencySymbols.d.ts.map +0 -1
- package/dist/services/removeNonNumeric.d.ts +0 -16
- package/dist/services/removeNonNumeric.d.ts.map +0 -1
- package/dist/services/removeNonNumeric.js +0 -16
- package/dist/services/stripHtmlTags.d.ts +0 -16
- package/dist/services/stripHtmlTags.d.ts.map +0 -1
- package/dist/services/stripHtmlTags.js +0 -16
- package/dist/services/truncateLargeFields.d.ts.map +0 -1
- package/dist/services/truncateLargeFields.js +0 -57
- package/dist/validations/validateCep.d.ts +0 -24
- package/dist/validations/validateCep.d.ts.map +0 -1
- package/dist/validations/validateCep.js +0 -33
- package/dist/validations/validateCnpj.d.ts +0 -22
- package/dist/validations/validateCnpj.d.ts.map +0 -1
- package/dist/validations/validateCnpj.js +0 -52
- package/dist/validations/validateCpf.d.ts +0 -24
- package/dist/validations/validateCpf.d.ts.map +0 -1
- package/dist/validations/validateCpf.js +0 -54
- package/dist/validations/validateDate.d.ts +0 -34
- package/dist/validations/validateDate.d.ts.map +0 -1
- package/dist/validations/validateDate.js +0 -73
- package/dist/validations/validatePassword.d.ts +0 -21
- package/dist/validations/validatePassword.d.ts.map +0 -1
- package/dist/validations/validatePassword.js +0 -34
- package/dist/validations/validatePhone.d.ts +0 -29
- package/dist/validations/validatePhone.d.ts.map +0 -1
- package/dist/validations/validatePhone.js +0 -44
- package/dist/validations/validateRg.d.ts +0 -22
- package/dist/validations/validateRg.d.ts.map +0 -1
- package/dist/validations/validateRg.js +0 -31
- package/src/formats/formatDate.ts +0 -92
- package/src/formats/formatJsonObject.ts +0 -90
- package/src/formats/formatJsonString.ts +0 -50
- package/src/formats/formatToCapitalizeFirstWordLetter.ts +0 -46
- package/src/formats/formatToCep.ts +0 -39
- package/src/formats/formatToCnpj.ts +0 -40
- package/src/formats/formatToCpf.ts +0 -40
- package/src/formats/formatToCpfCnpj.ts +0 -38
- package/src/formats/formatToCurrency.ts +0 -63
- package/src/formats/formatToDate.ts +0 -70
- package/src/formats/formatToEllipsis.ts +0 -25
- package/src/formats/formatToHiddenDigits.ts +0 -92
- package/src/formats/formatToPhone.ts +0 -170
- package/src/generators/generateColorByString.ts +0 -33
- package/src/generators/generateId.ts +0 -61
- package/src/generators/generateSlug.ts +0 -31
- package/src/index.ts +0 -37
- package/src/services/calculateCardInstallment.ts +0 -73
- package/src/services/ensureQuotes.ts +0 -25
- package/src/services/maskSensitiveData.ts +0 -68
- package/src/services/removeCurrencySymbols.ts +0 -29
- package/src/services/removeNonNumeric.ts +0 -20
- package/src/services/stripHtmlTags.ts +0 -20
- package/src/services/truncateLargeFields.ts +0 -69
- package/src/validations/validateCep.ts +0 -41
- package/src/validations/validateCnpj.ts +0 -65
- package/src/validations/validateCpf.ts +0 -62
- package/src/validations/validateDate.ts +0 -86
- package/src/validations/validatePassword.ts +0 -41
- package/src/validations/validatePhone.ts +0 -50
- package/src/validations/validateRg.ts +0 -37
- package/tsconfig.json +0 -20
- package/vitest.config.ts +0 -5
package/README.md
CHANGED
|
@@ -1,119 +1,394 @@
|
|
|
1
|
-
|
|
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
|
-
|
|
5
|
+
[](https://www.npmjs.com/package/@arkyn/shared)
|
|
6
|
+
[](https://opensource.org/licenses/Apache-2.0)
|
|
7
|
+
[](https://www.typescriptlang.org/)
|
|
6
8
|
|
|
7
|
-
|
|
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
|
-
|
|
129
|
+
Capitalizes the first letter of each space-separated word and lowercases the rest.
|
|
16
130
|
|
|
17
|
-
|
|
131
|
+
```typescript
|
|
132
|
+
import { formatToCapitalizeFirstWordLetter } from "@arkyn/shared";
|
|
18
133
|
|
|
19
|
-
|
|
20
|
-
|
|
134
|
+
formatToCapitalizeFirstWordLetter("HELLO WORLD"); // "Hello World"
|
|
135
|
+
```
|
|
21
136
|
|
|
22
|
-
|
|
23
|
-
Converts a JSON object into a formatted string.
|
|
137
|
+
#### formatToCep
|
|
24
138
|
|
|
25
|
-
|
|
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
|
-
|
|
29
|
-
|
|
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
|
-
|
|
32
|
-
|
|
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
|
-
|
|
35
|
-
Formats a string into the CPF format (`XXX.XXX.XXX-XX`).
|
|
188
|
+
#### formatToHiddenDigits
|
|
36
189
|
|
|
37
|
-
-
|
|
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
|
-
|
|
41
|
-
|
|
192
|
+
```typescript
|
|
193
|
+
import { formatToHiddenDigits } from "@arkyn/shared";
|
|
42
194
|
|
|
43
|
-
|
|
44
|
-
|
|
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
|
-
|
|
47
|
-
Truncates a string and appends an ellipsis if it exceeds the maximum length.
|
|
200
|
+
#### formatToPhone
|
|
48
201
|
|
|
49
|
-
-
|
|
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
|
-
|
|
53
|
-
|
|
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
|
-
|
|
60
|
-
|
|
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
|
-
|
|
63
|
-
|
|
284
|
+
```typescript
|
|
285
|
+
import { parseToDate } from "@arkyn/shared";
|
|
64
286
|
|
|
65
|
-
|
|
66
|
-
|
|
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
|
-
|
|
73
|
-
|
|
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
|
-
|
|
76
|
-
|
|
300
|
+
```typescript
|
|
301
|
+
import { ValidateDateService } from "@arkyn/shared";
|
|
77
302
|
|
|
78
|
-
|
|
79
|
-
|
|
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
|
-
|
|
82
|
-
Removes all non-numeric characters from a string.
|
|
312
|
+
#### calculateCardInstallment
|
|
83
313
|
|
|
84
|
-
-
|
|
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
|
-
|
|
319
|
+
calculateCardInstallment({ cashPrice: 1000, numberInstallments: 12, fees: 0.02 });
|
|
320
|
+
// { totalPrice: 1124.62, installmentPrice: 93.72 }
|
|
321
|
+
```
|
|
90
322
|
|
|
91
|
-
|
|
92
|
-
Validates whether a string is a valid CEP.
|
|
323
|
+
#### ensureQuotes
|
|
93
324
|
|
|
94
|
-
|
|
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
|
-
|
|
98
|
-
|
|
327
|
+
```typescript
|
|
328
|
+
import { ensureQuotes } from "@arkyn/shared";
|
|
99
329
|
|
|
100
|
-
|
|
101
|
-
|
|
330
|
+
ensureQuotes("example"); // '"example"'
|
|
331
|
+
ensureQuotes('"already quoted"'); // '"already quoted"'
|
|
332
|
+
```
|
|
102
333
|
|
|
103
|
-
|
|
104
|
-
Validates whether a string is a valid phone number.
|
|
334
|
+
#### findCountryMask
|
|
105
335
|
|
|
106
|
-
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
|
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
|
|
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
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
* - "
|
|
12
|
-
* - "
|
|
13
|
-
*
|
|
14
|
-
* @param
|
|
15
|
-
*
|
|
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
|
-
*
|
|
24
|
-
* formatDate(["25/12/2023", "15:30:00"], "brazilianDate", "YYYY-MM-DD hh:mm
|
|
25
|
-
* //
|
|
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
|
-
*
|
|
28
|
-
* //
|
|
29
|
-
*
|
|
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
|
|
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":"
|
|
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:
|
|
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,
|
|
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
|
-
*
|
|
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
|
-
*
|
|
6
|
-
*
|
|
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
|
-
*
|
|
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
|
|
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
|
-
*
|
|
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
|
|
9
|
-
* @returns The sentence
|
|
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
|
-
* //
|
|
14
|
-
* formatToCapitalizeFirstWordLetter("
|
|
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
|
|
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"}
|