@arkyn/shared 3.0.1-beta.99 → 3.0.3
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/LICENSE.txt +184 -1
- package/README.md +196 -306
- 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 -10
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +373 -27
- package/dist/index.js.map +1 -0
- package/dist/modules/formats/formatDate.js +37 -0
- package/dist/modules/formats/formatDate.js.map +1 -0
- package/dist/modules/formats/formatJsonObject.js +25 -0
- package/dist/modules/formats/formatJsonObject.js.map +1 -0
- package/dist/modules/formats/formatJsonString.js +13 -0
- package/dist/modules/formats/formatJsonString.js.map +1 -0
- package/dist/modules/formats/formatToCapitalizeFirstWordLetter.js +8 -0
- package/dist/modules/formats/formatToCapitalizeFirstWordLetter.js.map +1 -0
- package/dist/modules/formats/formatToCep.js +11 -0
- package/dist/modules/formats/formatToCep.js.map +1 -0
- package/dist/modules/formats/formatToCnpj.js +11 -0
- package/dist/modules/formats/formatToCnpj.js.map +1 -0
- package/dist/modules/formats/formatToCpf.js +11 -0
- package/dist/modules/formats/formatToCpf.js.map +1 -0
- package/dist/modules/formats/formatToCurrency.js +15 -0
- package/dist/modules/formats/formatToCurrency.js.map +1 -0
- package/dist/modules/formats/formatToEllipsis.js +12 -0
- package/dist/modules/formats/formatToEllipsis.js.map +1 -0
- package/dist/modules/formats/formatToHiddenDigits.js +24 -0
- package/dist/modules/formats/formatToHiddenDigits.js.map +1 -0
- package/dist/modules/formats/formatToPhone.js +16 -0
- package/dist/modules/formats/formatToPhone.js.map +1 -0
- package/dist/modules/generators/generateColorByString.js +11 -0
- package/dist/modules/generators/generateColorByString.js.map +1 -0
- package/dist/modules/generators/generateId.js +34 -0
- package/dist/modules/generators/generateId.js.map +1 -0
- package/dist/modules/generators/generateSlug.js +9 -0
- package/dist/modules/generators/generateSlug.js.map +1 -0
- package/dist/modules/parsers/parseLargeFields.js +19 -0
- package/dist/modules/parsers/parseLargeFields.js.map +1 -0
- package/dist/modules/parsers/parseSensitiveData.js +30 -0
- package/dist/modules/parsers/parseSensitiveData.js.map +1 -0
- package/dist/modules/parsers/parseToDate.js +25 -0
- package/dist/modules/parsers/parseToDate.js.map +1 -0
- package/dist/modules/services/validateDateService.js +63 -0
- package/dist/modules/services/validateDateService.js.map +1 -0
- package/dist/modules/utilities/calculateCardInstallment.js +19 -0
- package/dist/modules/utilities/calculateCardInstallment.js.map +1 -0
- package/dist/modules/utilities/ensureQuotes.js +9 -0
- package/dist/modules/utilities/ensureQuotes.js.map +1 -0
- package/dist/modules/utilities/findCountryMask.js +25 -0
- package/dist/modules/utilities/findCountryMask.js.map +1 -0
- package/dist/modules/utilities/isHtml.js +8 -0
- package/dist/modules/utilities/isHtml.js.map +1 -0
- package/dist/modules/utilities/removeCurrencySymbols.js +8 -0
- package/dist/modules/utilities/removeCurrencySymbols.js.map +1 -0
- package/dist/modules/utilities/removeNonNumeric.js +8 -0
- package/dist/modules/utilities/removeNonNumeric.js.map +1 -0
- package/dist/modules/utilities/stripHtmlTags.js +8 -0
- package/dist/modules/utilities/stripHtmlTags.js.map +1 -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 +255 -16
- package/dist/bundle.js +0 -2337
- package/dist/bundle.umd.cjs +0 -6
- 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/isHtml.d.ts +0 -22
- package/dist/services/isHtml.d.ts.map +0 -1
- package/dist/services/isHtml.js +0 -24
- 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/README.md
CHANGED
|
@@ -1,123 +1,152 @@
|
|
|
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
6
|
[](https://opensource.org/licenses/Apache-2.0)
|
|
7
7
|
[](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
|
|
12
|
-
-
|
|
13
|
-
-
|
|
14
|
-
-
|
|
15
|
-
-
|
|
16
|
-
-
|
|
17
|
-
-
|
|
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
|
|
36
|
-
const cpf = formatToCpf("
|
|
37
|
-
|
|
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(
|
|
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
|
|
43
|
-
const id = generateId(
|
|
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
|
-
##
|
|
73
|
+
## 📖 API Reference
|
|
47
74
|
|
|
48
|
-
###
|
|
75
|
+
### Formats
|
|
49
76
|
|
|
50
|
-
#### formatDate
|
|
77
|
+
#### formatDate
|
|
51
78
|
|
|
52
|
-
Formats
|
|
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
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
//
|
|
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
|
-
####
|
|
91
|
+
#### formatJsonObject
|
|
72
92
|
|
|
73
|
-
|
|
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 {
|
|
96
|
+
import { formatJsonObject } from "@arkyn/shared";
|
|
77
97
|
|
|
78
|
-
const
|
|
79
|
-
|
|
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
|
-
|
|
83
|
-
|
|
84
|
-
#### formatToCurrency(value: number): string
|
|
110
|
+
#### formatJsonString
|
|
85
111
|
|
|
86
|
-
|
|
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 {
|
|
115
|
+
import { formatJsonString } from "@arkyn/shared";
|
|
90
116
|
|
|
91
|
-
|
|
92
|
-
|
|
117
|
+
formatJsonString('{"name":"John","hobbies":["reading","gaming"]}');
|
|
118
|
+
// {
|
|
119
|
+
// "name": "John",
|
|
120
|
+
// "hobbies": [
|
|
121
|
+
// "reading",
|
|
122
|
+
// "gaming"
|
|
123
|
+
// ]
|
|
124
|
+
// }
|
|
93
125
|
```
|
|
94
126
|
|
|
95
|
-
####
|
|
127
|
+
#### formatToCapitalizeFirstWordLetter
|
|
96
128
|
|
|
97
|
-
|
|
129
|
+
Capitalizes the first letter of each space-separated word and lowercases the rest.
|
|
98
130
|
|
|
99
131
|
```typescript
|
|
100
|
-
import {
|
|
132
|
+
import { formatToCapitalizeFirstWordLetter } from "@arkyn/shared";
|
|
101
133
|
|
|
102
|
-
|
|
103
|
-
// Result: [100, 100, 100, 100, 100, 100, 100, 100, 100, 100]
|
|
134
|
+
formatToCapitalizeFirstWordLetter("HELLO WORLD"); // "Hello World"
|
|
104
135
|
```
|
|
105
136
|
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
#### formatToCpf(value: string): string
|
|
137
|
+
#### formatToCep
|
|
109
138
|
|
|
110
|
-
Formats
|
|
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 {
|
|
142
|
+
import { formatToCep } from "@arkyn/shared";
|
|
114
143
|
|
|
115
|
-
|
|
144
|
+
formatToCep("12345678"); // "12345-678"
|
|
116
145
|
```
|
|
117
146
|
|
|
118
|
-
#### formatToCnpj
|
|
147
|
+
#### formatToCnpj
|
|
119
148
|
|
|
120
|
-
Formats
|
|
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
|
-
####
|
|
157
|
+
#### formatToCpf
|
|
129
158
|
|
|
130
|
-
|
|
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 {
|
|
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
|
-
|
|
164
|
+
formatToCpf("12345678909"); // "123.456.789-09"
|
|
147
165
|
```
|
|
148
166
|
|
|
149
|
-
####
|
|
167
|
+
#### formatToCurrency
|
|
150
168
|
|
|
151
|
-
Formats
|
|
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 {
|
|
172
|
+
import { formatToCurrency } from "@arkyn/shared";
|
|
155
173
|
|
|
156
|
-
|
|
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
|
-
|
|
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("
|
|
185
|
+
formatToEllipsis("Hello, world!", 5); // "Hello..."
|
|
171
186
|
```
|
|
172
187
|
|
|
173
|
-
#### formatToHiddenDigits
|
|
188
|
+
#### formatToHiddenDigits
|
|
174
189
|
|
|
175
|
-
|
|
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("
|
|
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
|
-
####
|
|
200
|
+
#### formatToPhone
|
|
184
201
|
|
|
185
|
-
|
|
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 {
|
|
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
|
-
|
|
207
|
+
formatToPhone("+5534920524282"); // "(34) 92052-4282"
|
|
208
|
+
formatToPhone("+12125550199"); // "(212) 555-0199"
|
|
201
209
|
```
|
|
202
210
|
|
|
203
|
-
###
|
|
211
|
+
### Generators
|
|
204
212
|
|
|
205
|
-
####
|
|
213
|
+
#### generateColorByString
|
|
206
214
|
|
|
207
|
-
|
|
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 {
|
|
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
|
-
|
|
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
|
|
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
|
-
|
|
230
|
+
generateId("text", "v4"); // "550e8400-e29b-41d4-a716-446655440000"
|
|
231
|
+
generateId("binary", "v7"); // Uint8Array([...])
|
|
239
232
|
```
|
|
240
233
|
|
|
241
|
-
#### generateSlug
|
|
234
|
+
#### generateSlug
|
|
242
235
|
|
|
243
|
-
|
|
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!
|
|
241
|
+
generateSlug("Hello, World! This is a Test.");
|
|
242
|
+
// "hello-world-this-is-a-test"
|
|
249
243
|
```
|
|
250
244
|
|
|
251
|
-
|
|
245
|
+
### Parsers
|
|
252
246
|
|
|
253
|
-
|
|
247
|
+
#### parseLargeFields
|
|
254
248
|
|
|
255
|
-
|
|
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 {
|
|
252
|
+
import { parseLargeFields } from "@arkyn/shared";
|
|
269
253
|
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
254
|
+
const json = JSON.stringify({
|
|
255
|
+
name: "John",
|
|
256
|
+
description: "A very long description that exceeds the maximum length...",
|
|
257
|
+
});
|
|
274
258
|
|
|
275
|
-
|
|
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
|
-
####
|
|
263
|
+
#### parseSensitiveData
|
|
284
264
|
|
|
285
|
-
|
|
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 {
|
|
268
|
+
import { parseSensitiveData } from "@arkyn/shared";
|
|
289
269
|
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
270
|
+
const jsonString = JSON.stringify({
|
|
271
|
+
username: "user123",
|
|
272
|
+
password: "secret",
|
|
273
|
+
profile: { creditCard: "1234-5678-9012-3456" },
|
|
274
|
+
});
|
|
294
275
|
|
|
295
|
-
|
|
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
|
-
####
|
|
280
|
+
#### parseToDate
|
|
305
281
|
|
|
306
|
-
|
|
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 {
|
|
285
|
+
import { parseToDate } from "@arkyn/shared";
|
|
310
286
|
|
|
311
|
-
|
|
312
|
-
|
|
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
|
-
###
|
|
294
|
+
### Services
|
|
318
295
|
|
|
319
|
-
####
|
|
296
|
+
#### ValidateDateService
|
|
320
297
|
|
|
321
|
-
|
|
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 {
|
|
301
|
+
import { ValidateDateService } from "@arkyn/shared";
|
|
325
302
|
|
|
326
|
-
|
|
327
|
-
|
|
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
|
-
|
|
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
|
-
####
|
|
312
|
+
#### calculateCardInstallment
|
|
341
313
|
|
|
342
|
-
|
|
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 {
|
|
317
|
+
import { calculateCardInstallment } from "@arkyn/shared";
|
|
346
318
|
|
|
347
|
-
|
|
348
|
-
|
|
319
|
+
calculateCardInstallment({ cashPrice: 1000, numberInstallments: 12, fees: 0.02 });
|
|
320
|
+
// { totalPrice: 1124.62, installmentPrice: 93.72 }
|
|
349
321
|
```
|
|
350
322
|
|
|
351
|
-
####
|
|
323
|
+
#### ensureQuotes
|
|
352
324
|
|
|
353
|
-
|
|
325
|
+
Wraps a string in double quotes unless it is already enclosed in single or double quotes.
|
|
354
326
|
|
|
355
327
|
```typescript
|
|
356
|
-
import {
|
|
328
|
+
import { ensureQuotes } from "@arkyn/shared";
|
|
357
329
|
|
|
358
|
-
|
|
359
|
-
|
|
330
|
+
ensureQuotes("example"); // '"example"'
|
|
331
|
+
ensureQuotes('"already quoted"'); // '"already quoted"'
|
|
360
332
|
```
|
|
361
333
|
|
|
362
|
-
####
|
|
334
|
+
#### findCountryMask
|
|
363
335
|
|
|
364
|
-
|
|
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 {
|
|
339
|
+
import { findCountryMask } from "@arkyn/shared";
|
|
368
340
|
|
|
369
|
-
|
|
341
|
+
const [mask, country] = findCountryMask("+5511999999999");
|
|
342
|
+
// mask: "(__) _____-____"
|
|
343
|
+
// country.name: "Brazil"
|
|
370
344
|
```
|
|
371
345
|
|
|
372
|
-
####
|
|
346
|
+
#### isHtml
|
|
373
347
|
|
|
374
|
-
|
|
348
|
+
Checks whether a string contains HTML markup (opening or closing tags), case-insensitively.
|
|
375
349
|
|
|
376
350
|
```typescript
|
|
377
|
-
import {
|
|
351
|
+
import { isHtml } from "@arkyn/shared";
|
|
378
352
|
|
|
379
|
-
|
|
380
|
-
|
|
353
|
+
isHtml("<p>Hello world</p>"); // true
|
|
354
|
+
isHtml("Plain text"); // false
|
|
381
355
|
```
|
|
382
356
|
|
|
383
|
-
####
|
|
357
|
+
#### removeCurrencySymbols
|
|
384
358
|
|
|
385
|
-
|
|
359
|
+
Removes currency symbols (`R$`, `$`, `€`, `¥`, `£`, and other Unicode currency symbols) from a string and trims whitespace.
|
|
386
360
|
|
|
387
361
|
```typescript
|
|
388
|
-
import {
|
|
362
|
+
import { removeCurrencySymbols } from "@arkyn/shared";
|
|
389
363
|
|
|
390
|
-
|
|
364
|
+
removeCurrencySymbols("R$13,45"); // "13,45"
|
|
365
|
+
removeCurrencySymbols("€99.99"); // "99.99"
|
|
391
366
|
```
|
|
392
367
|
|
|
393
|
-
|
|
368
|
+
#### removeNonNumeric
|
|
394
369
|
|
|
395
|
-
|
|
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
|
-
|
|
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
|
-
|
|
378
|
+
#### stripHtmlTags
|
|
471
379
|
|
|
472
|
-
|
|
473
|
-
# Install dependencies
|
|
474
|
-
bun install
|
|
380
|
+
Removes HTML tags from a string, including `<script>` and `<style>` blocks and HTML comments.
|
|
475
381
|
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
# Run tests
|
|
480
|
-
bun run test
|
|
382
|
+
```typescript
|
|
383
|
+
import { stripHtmlTags } from "@arkyn/shared";
|
|
481
384
|
|
|
482
|
-
|
|
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
|
-
|
|
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
|