@arkyn/shared 1.5.0 → 1.5.1
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 +24 -0
- package/README.md +113 -0
- package/dist/formats/formatDate.d.ts +43 -0
- package/dist/formats/formatDate.d.ts.map +1 -0
- package/dist/formats/formatDate.js +77 -0
- package/dist/formats/formatJsonObject.d.ts +31 -1
- package/dist/formats/formatJsonObject.d.ts.map +1 -1
- package/dist/formats/formatJsonObject.js +67 -26
- package/dist/formats/formatJsonString.d.ts +37 -1
- package/dist/formats/formatJsonString.d.ts.map +1 -1
- package/dist/formats/formatJsonString.js +38 -4
- package/dist/formats/formatToCep.d.ts +27 -1
- package/dist/formats/formatToCep.d.ts.map +1 -1
- package/dist/formats/formatToCep.js +31 -6
- package/dist/formats/formatToCnpj.d.ts +30 -0
- package/dist/formats/formatToCnpj.d.ts.map +1 -0
- package/dist/formats/formatToCnpj.js +35 -0
- package/dist/formats/formatToCpf.d.ts +30 -0
- package/dist/formats/formatToCpf.d.ts.map +1 -0
- package/dist/formats/formatToCpf.js +35 -0
- package/dist/formats/formatToCpfCnpj.d.ts +22 -1
- package/dist/formats/formatToCpfCnpj.d.ts.map +1 -1
- package/dist/formats/formatToCpfCnpj.js +27 -7
- package/dist/formats/formatToCurrency.d.ts +29 -0
- package/dist/formats/formatToCurrency.d.ts.map +1 -0
- package/dist/formats/formatToCurrency.js +41 -0
- package/dist/formats/formatToEllipsis.d.ts +14 -1
- package/dist/formats/formatToEllipsis.d.ts.map +1 -1
- package/dist/formats/formatToEllipsis.js +20 -3
- package/dist/formats/formatToHiddenDigits.d.ts +30 -2
- package/dist/formats/formatToHiddenDigits.d.ts.map +1 -1
- package/dist/formats/formatToHiddenDigits.js +49 -3
- package/dist/formats/formatToPhone.d.ts +32 -1
- package/dist/formats/formatToPhone.d.ts.map +1 -1
- package/dist/formats/formatToPhone.js +128 -6
- package/dist/generators/generateColorByString.d.ts +13 -1
- package/dist/generators/generateColorByString.d.ts.map +1 -1
- package/dist/generators/generateColorByString.js +15 -4
- package/dist/generators/generateId.d.ts +26 -4
- package/dist/generators/generateId.d.ts.map +1 -1
- package/dist/generators/generateId.js +11 -5
- package/dist/generators/generateSlug.d.ts +15 -1
- package/dist/generators/generateSlug.d.ts.map +1 -1
- package/dist/generators/generateSlug.js +16 -2
- package/dist/index.d.ts +9 -10
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +10 -12
- package/dist/services/calculateCardInstallment.d.ts +31 -5
- package/dist/services/calculateCardInstallment.d.ts.map +1 -1
- package/dist/services/calculateCardInstallment.js +52 -11
- package/dist/services/maskSensitiveData.d.ts +24 -1
- package/dist/services/maskSensitiveData.d.ts.map +1 -1
- package/dist/services/maskSensitiveData.js +24 -2
- package/dist/services/removeCurrencySymbols.d.ts +20 -0
- package/dist/services/removeCurrencySymbols.d.ts.map +1 -0
- package/dist/services/removeCurrencySymbols.js +23 -0
- package/dist/services/removeNonNumeric.d.ts +15 -0
- package/dist/services/removeNonNumeric.d.ts.map +1 -0
- package/dist/services/removeNonNumeric.js +16 -0
- package/dist/services/truncateLargeFields.d.ts +30 -1
- package/dist/services/truncateLargeFields.d.ts.map +1 -1
- package/dist/services/truncateLargeFields.js +34 -6
- package/dist/validations/validateCep.d.ts +24 -0
- package/dist/validations/validateCep.d.ts.map +1 -0
- package/dist/validations/validateCep.js +33 -0
- package/dist/validations/validateCnpj.d.ts +22 -0
- package/dist/validations/validateCnpj.d.ts.map +1 -0
- package/dist/validations/validateCnpj.js +52 -0
- package/dist/validations/validateCpf.d.ts +24 -0
- package/dist/validations/validateCpf.d.ts.map +1 -0
- package/dist/validations/validateCpf.js +54 -0
- package/dist/validations/validateDate.d.ts +27 -6
- package/dist/validations/validateDate.d.ts.map +1 -1
- package/dist/validations/validateDate.js +38 -12
- package/dist/validations/validatePassword.d.ts +21 -0
- package/dist/validations/validatePassword.d.ts.map +1 -0
- package/dist/validations/validatePassword.js +34 -0
- package/dist/validations/validatePhone.d.ts +27 -1
- package/dist/validations/validatePhone.d.ts.map +1 -1
- package/dist/validations/validatePhone.js +29 -4
- package/dist/validations/validateRg.d.ts +22 -0
- package/dist/validations/validateRg.d.ts.map +1 -0
- package/dist/validations/validateRg.js +31 -0
- package/package.json +7 -2
- package/src/formats/__test__/formatDate.spec.ts +88 -0
- package/src/formats/__test__/formatJsonObject.spec.ts +87 -0
- package/src/formats/__test__/formatJsonString.spec.ts +83 -0
- package/src/formats/__test__/formatToCep.spec.ts +37 -0
- package/src/formats/__test__/formatToCnpj.spec.ts +35 -0
- package/src/formats/__test__/formatToCpf.spec.ts +37 -0
- package/src/formats/__test__/formatToCpfCnpj.spec.ts +43 -0
- package/src/formats/__test__/formatToCurrency.spec.ts +50 -0
- package/src/formats/__test__/formatToEllipsis.spec.ts +44 -0
- package/src/formats/__test__/formatToHiddenDigits.spec.ts +58 -0
- package/src/formats/__test__/formatToPhone.spec.ts +58 -0
- package/src/formats/formatDate.ts +97 -0
- package/src/formats/formatJsonObject.ts +68 -26
- package/src/formats/formatJsonString.ts +40 -4
- package/src/formats/formatToCep.ts +35 -7
- package/src/formats/formatToCnpj.ts +39 -0
- package/src/formats/formatToCpf.ts +39 -0
- package/src/formats/formatToCpfCnpj.ts +31 -7
- package/src/formats/formatToCurrency.ts +53 -0
- package/src/formats/formatToEllipsis.ts +23 -3
- package/src/formats/formatToHiddenDigits.ts +75 -7
- package/src/formats/formatToPhone.ts +156 -6
- package/src/generators/__test__/generateColorByString.spec.ts +37 -0
- package/src/generators/__test__/generateId.spec.ts +44 -0
- package/src/generators/__test__/generateSlug.spec.ts +47 -0
- package/src/generators/generateColorByString.ts +18 -4
- package/src/generators/generateId.ts +36 -5
- package/src/generators/generateSlug.ts +17 -2
- package/src/index.ts +10 -13
- package/src/services/__test__/calculateCardInstallment.spec.ts +87 -0
- package/src/services/__test__/maskSensitiveData.spec.ts +102 -0
- package/src/services/__test__/removeCurrencySymbols.spec.ts +41 -0
- package/src/services/__test__/removeNonNumeric.spec.ts +33 -0
- package/src/services/__test__/truncateLargeFields.spec.ts +90 -0
- package/src/services/calculateCardInstallment.ts +57 -12
- package/src/services/maskSensitiveData.ts +29 -4
- package/src/services/removeCurrencySymbols.ts +25 -0
- package/src/services/removeNonNumeric.ts +18 -0
- package/src/services/truncateLargeFields.ts +40 -6
- package/src/validations/__test__/validateCep.spec.ts +40 -0
- package/src/validations/__test__/validateCnpj.spec.ts +42 -0
- package/src/validations/__test__/validateCpf.spec.ts +38 -0
- package/src/validations/__test__/validateDate.spec.ts +81 -0
- package/src/validations/__test__/validatePassword.spec.ts +43 -0
- package/src/validations/__test__/validatePhone.spec.ts +39 -0
- package/src/validations/__test__/validateRg.spec.ts +48 -0
- package/src/validations/validateCep.ts +40 -0
- package/src/validations/validateCnpj.ts +64 -0
- package/src/validations/validateCpf.ts +61 -0
- package/src/validations/validateDate.ts +39 -17
- package/src/validations/validatePassword.ts +41 -0
- package/src/validations/validatePhone.ts +31 -4
- package/src/validations/validateRg.ts +37 -0
- package/tsconfig.json +2 -2
- package/vitest.config.ts +5 -0
- package/dist/formats/formatBrazilianDateHour.d.ts +0 -3
- package/dist/formats/formatBrazilianDateHour.d.ts.map +0 -1
- package/dist/formats/formatBrazilianDateHour.js +0 -12
- package/dist/formats/formatBrazilianDateToDate.d.ts +0 -3
- package/dist/formats/formatBrazilianDateToDate.d.ts.map +0 -1
- package/dist/formats/formatBrazilianDateToDate.js +0 -9
- package/dist/formats/formatDateHour.d.ts +0 -3
- package/dist/formats/formatDateHour.d.ts.map +0 -1
- package/dist/formats/formatDateHour.js +0 -11
- package/dist/formats/formatToBRL.d.ts +0 -3
- package/dist/formats/formatToBRL.d.ts.map +0 -1
- package/dist/formats/formatToBRL.js +0 -8
- package/dist/formats/formatToCNPJ.d.ts +0 -3
- package/dist/formats/formatToCNPJ.d.ts.map +0 -1
- package/dist/formats/formatToCNPJ.js +0 -9
- package/dist/formats/formatToCPF.d.ts +0 -3
- package/dist/formats/formatToCPF.d.ts.map +0 -1
- package/dist/formats/formatToCPF.js +0 -9
- package/dist/parsers/parseToCharacters.d.ts +0 -17
- package/dist/parsers/parseToCharacters.d.ts.map +0 -1
- package/dist/parsers/parseToCharacters.js +0 -13
- package/dist/services/range.d.ts +0 -4
- package/dist/services/range.d.ts.map +0 -1
- package/dist/services/range.js +0 -9
- package/dist/validations/regex.d.ts +0 -15
- package/dist/validations/regex.d.ts.map +0 -1
- package/dist/validations/regex.js +0 -25
- package/dist/validations/validateCPF.d.ts +0 -3
- package/dist/validations/validateCPF.d.ts.map +0 -1
- package/dist/validations/validateCPF.js +0 -36
- package/src/formats/formatBrazilianDateHour.ts +0 -17
- package/src/formats/formatBrazilianDateToDate.ts +0 -13
- package/src/formats/formatDateHour.ts +0 -15
- package/src/formats/formatToBRL.ts +0 -10
- package/src/formats/formatToCNPJ.ts +0 -10
- package/src/formats/formatToCPF.ts +0 -10
- package/src/parsers/parseToCharacters.ts +0 -34
- package/src/services/range.ts +0 -15
- package/src/validations/regex.ts +0 -41
- package/src/validations/validateCPF.ts +0 -38
package/LICENSE.txt
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
Copyright 2025 Lucas Gonçalves
|
|
8
|
+
|
|
9
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
10
|
+
you may not use this file except in compliance with the License.
|
|
11
|
+
You may obtain a copy of the License at
|
|
12
|
+
|
|
13
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
14
|
+
|
|
15
|
+
Unless required by applicable law or agreed to in writing, software
|
|
16
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
17
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
18
|
+
See the License for the specific language governing permissions and
|
|
19
|
+
limitations under the License.
|
|
20
|
+
|
|
21
|
+
---
|
|
22
|
+
|
|
23
|
+
For more information or to contribute, visit:
|
|
24
|
+
https://github.com/Lucas-Eduardo-Goncalves
|
package/README.md
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
The `@arkyn/shared` package provides reusable utilities such as formatting functions, generators, services, and validations. It is designed to be used across different parts of a project, offering common and practical solutions to streamline development.
|
|
2
|
+
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
Install the package using npm:
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install @arkyn/shared
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
---
|
|
14
|
+
|
|
15
|
+
## Features
|
|
16
|
+
|
|
17
|
+
### Formatting
|
|
18
|
+
|
|
19
|
+
- **`formatDate(date: Date): string`**
|
|
20
|
+
Formats a date into a readable string.
|
|
21
|
+
|
|
22
|
+
- **`formatJsonObject(obj: object): string`**
|
|
23
|
+
Converts a JSON object into a formatted string.
|
|
24
|
+
|
|
25
|
+
- **`formatJsonString(json: string): object`**
|
|
26
|
+
Converts a JSON string into an object.
|
|
27
|
+
|
|
28
|
+
- **`formatToCep(value: string): string`**
|
|
29
|
+
Formats a string into the CEP format (`XXXXX-XXX`).
|
|
30
|
+
|
|
31
|
+
- **`formatToCnpj(value: string): string`**
|
|
32
|
+
Formats a string into the CNPJ format (`XX.XXX.XXX/XXXX-XX`).
|
|
33
|
+
|
|
34
|
+
- **`formatToCpf(value: string): string`**
|
|
35
|
+
Formats a string into the CPF format (`XXX.XXX.XXX-XX`).
|
|
36
|
+
|
|
37
|
+
- **`formatToCpfCnpj(value: string): string`**
|
|
38
|
+
Formats a string into either CPF or CNPJ format, depending on its length.
|
|
39
|
+
|
|
40
|
+
- **`formatToCurrency(value: number): string`**
|
|
41
|
+
Converts a number into a currency format.
|
|
42
|
+
|
|
43
|
+
- **`formatToEllipsis(value: string, maxLength: number): string`**
|
|
44
|
+
Truncates a string and appends an ellipsis if it exceeds the maximum length.
|
|
45
|
+
|
|
46
|
+
- **`formatToHiddenDigits(value: string): string`**
|
|
47
|
+
Masks part of a string by replacing it with asterisks.
|
|
48
|
+
|
|
49
|
+
- **`formatToPhone(value: string): string`**
|
|
50
|
+
Formats a string into a phone number format.
|
|
51
|
+
|
|
52
|
+
---
|
|
53
|
+
|
|
54
|
+
### Generators
|
|
55
|
+
|
|
56
|
+
- **`generateColorByString(value: string): string`**
|
|
57
|
+
Generates a hexadecimal color based on a string.
|
|
58
|
+
|
|
59
|
+
- **`generateId(): string`**
|
|
60
|
+
Generates a unique identifier.
|
|
61
|
+
|
|
62
|
+
- **`generateSlug(value: string): string`**
|
|
63
|
+
Converts a string into a URL-friendly slug.
|
|
64
|
+
|
|
65
|
+
---
|
|
66
|
+
|
|
67
|
+
### Services
|
|
68
|
+
|
|
69
|
+
- **`calculateCardInstallment(total: number, installments: number): number[]`**
|
|
70
|
+
Calculates the installment values for a given total.
|
|
71
|
+
|
|
72
|
+
- **`maskSensitiveData(data: string): string`**
|
|
73
|
+
Masks sensitive data in a string.
|
|
74
|
+
|
|
75
|
+
- **`removeNonNumeric(value: string): string`**
|
|
76
|
+
Removes all non-numeric characters from a string.
|
|
77
|
+
|
|
78
|
+
- **`truncateLargeFields(obj: object, maxLength: number): object`**
|
|
79
|
+
Truncates large fields in an object to a specified maximum length.
|
|
80
|
+
|
|
81
|
+
---
|
|
82
|
+
|
|
83
|
+
### Validations
|
|
84
|
+
|
|
85
|
+
- **`validateCep(value: string): boolean`**
|
|
86
|
+
Validates whether a string is a valid CEP.
|
|
87
|
+
|
|
88
|
+
- **`validateCnpj(value: string): boolean`**
|
|
89
|
+
Validates whether a string is a valid CNPJ.
|
|
90
|
+
|
|
91
|
+
- **`validateCpf(value: string): boolean`**
|
|
92
|
+
Validates whether a string is a valid CPF.
|
|
93
|
+
|
|
94
|
+
- **`validateDate(value: string): boolean`**
|
|
95
|
+
Validates whether a string is a valid date.
|
|
96
|
+
|
|
97
|
+
- **`validatePhone(value: string): boolean`**
|
|
98
|
+
Validates whether a string is a valid phone number.
|
|
99
|
+
|
|
100
|
+
- **`validateRg(value: string): boolean`**
|
|
101
|
+
Validates whether a string is a valid ID (RG).
|
|
102
|
+
|
|
103
|
+
---
|
|
104
|
+
|
|
105
|
+
## Contribution
|
|
106
|
+
|
|
107
|
+
Contributions are welcome! Feel free to open issues or submit pull requests to improve the project.
|
|
108
|
+
|
|
109
|
+
---
|
|
110
|
+
|
|
111
|
+
## License
|
|
112
|
+
|
|
113
|
+
This project is licensed under the Apache 2.0 License. See the `LICENSE` file for more details.
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import type { FormatDateFunction } from "@arkyn/types";
|
|
2
|
+
/**
|
|
3
|
+
* Formats a date string from a given input format to a specified output format,
|
|
4
|
+
* with optional timezone adjustment.
|
|
5
|
+
*
|
|
6
|
+
* @param date - The date string to be formatted. It should match the specified `inputFormat`.
|
|
7
|
+
* @param time - The time string in the format `hh:mm:ss` (e.g., `14:30:00`).
|
|
8
|
+
* @param inputFormat - The format of the input date string. Supported formats:
|
|
9
|
+
* - `"brazilianDate"`: Expects the date in `DD/MM/YYYY` format.
|
|
10
|
+
* - `"isoDate"`: Expects the date in `YYYY-MM-DD` format.
|
|
11
|
+
* - `"timestamp"`: Expects the date as a numeric timestamp (e.g., `YYYY-MM-DD`).
|
|
12
|
+
*
|
|
13
|
+
* @param outputFormat - The desired format for the output date string. Supported tokens:
|
|
14
|
+
* - `YYYY`: Full year (e.g., 2023)
|
|
15
|
+
* - `YY`: Last two digits of the year (e.g., 23)
|
|
16
|
+
* - `MM`: Month (zero-padded, e.g., 01)
|
|
17
|
+
* - `DD`: Day of the month (zero-padded, e.g., 09)
|
|
18
|
+
* - `hh`: Hours (zero-padded, 24-hour format, e.g., 08)
|
|
19
|
+
* - `mm`: Minutes (zero-padded, e.g., 05)
|
|
20
|
+
* - `ss`: Seconds (zero-padded, e.g., 07)
|
|
21
|
+
* @param timezone - (Optional) The timezone offset in hours to adjust the date. Defaults to `0`.
|
|
22
|
+
*
|
|
23
|
+
* @returns The formatted date string in the specified `outputFormat`.
|
|
24
|
+
*
|
|
25
|
+
* @throws Will throw an error if:
|
|
26
|
+
* - The `inputFormat` is invalid.
|
|
27
|
+
* - The `date` string does not match the expected `inputFormat`.
|
|
28
|
+
* - The `time` string is not in the format `hh:mm:ss`.
|
|
29
|
+
* - The resulting date is invalid.
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
32
|
+
* ```typescript
|
|
33
|
+
* import { formatDate } from "./formatDate";
|
|
34
|
+
*
|
|
35
|
+
* const date = "25/12/2023";
|
|
36
|
+
* const time = "14:30:00";
|
|
37
|
+
* const formatted = formatDate(date, time, "brazilianDate", "YYYY-MM-DD hh:mm:ss", -3);
|
|
38
|
+
* console.log(formatted); // Outputs: "2023-12-25 14:30:00"
|
|
39
|
+
* ```
|
|
40
|
+
*/
|
|
41
|
+
declare const formatDate: FormatDateFunction;
|
|
42
|
+
export { formatDate };
|
|
43
|
+
//# sourceMappingURL=formatDate.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"formatDate.d.ts","sourceRoot":"","sources":["../../src/formats/formatDate.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,cAAc,CAAC;AAqBvD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAsCG;AAEH,QAAA,MAAM,UAAU,EAAE,kBAiCjB,CAAC;AAEF,OAAO,EAAE,UAAU,EAAE,CAAC"}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
function formatDateString(date, format) {
|
|
2
|
+
const pad = (num) => num.toString().padStart(2, "0");
|
|
3
|
+
const replacements = {
|
|
4
|
+
YYYY: date.getFullYear().toString(),
|
|
5
|
+
YY: date.getFullYear().toString().slice(-2),
|
|
6
|
+
MM: pad(date.getMonth() + 1),
|
|
7
|
+
DD: pad(date.getDate()),
|
|
8
|
+
hh: pad(date.getHours()),
|
|
9
|
+
mm: pad(date.getMinutes()),
|
|
10
|
+
ss: pad(date.getSeconds()),
|
|
11
|
+
};
|
|
12
|
+
return format.replace(/YYYY|YY|MM|DD|hh|mm|ss/g, (match) => replacements[match]);
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Formats a date string from a given input format to a specified output format,
|
|
16
|
+
* with optional timezone adjustment.
|
|
17
|
+
*
|
|
18
|
+
* @param date - The date string to be formatted. It should match the specified `inputFormat`.
|
|
19
|
+
* @param time - The time string in the format `hh:mm:ss` (e.g., `14:30:00`).
|
|
20
|
+
* @param inputFormat - The format of the input date string. Supported formats:
|
|
21
|
+
* - `"brazilianDate"`: Expects the date in `DD/MM/YYYY` format.
|
|
22
|
+
* - `"isoDate"`: Expects the date in `YYYY-MM-DD` format.
|
|
23
|
+
* - `"timestamp"`: Expects the date as a numeric timestamp (e.g., `YYYY-MM-DD`).
|
|
24
|
+
*
|
|
25
|
+
* @param outputFormat - The desired format for the output date string. Supported tokens:
|
|
26
|
+
* - `YYYY`: Full year (e.g., 2023)
|
|
27
|
+
* - `YY`: Last two digits of the year (e.g., 23)
|
|
28
|
+
* - `MM`: Month (zero-padded, e.g., 01)
|
|
29
|
+
* - `DD`: Day of the month (zero-padded, e.g., 09)
|
|
30
|
+
* - `hh`: Hours (zero-padded, 24-hour format, e.g., 08)
|
|
31
|
+
* - `mm`: Minutes (zero-padded, e.g., 05)
|
|
32
|
+
* - `ss`: Seconds (zero-padded, e.g., 07)
|
|
33
|
+
* @param timezone - (Optional) The timezone offset in hours to adjust the date. Defaults to `0`.
|
|
34
|
+
*
|
|
35
|
+
* @returns The formatted date string in the specified `outputFormat`.
|
|
36
|
+
*
|
|
37
|
+
* @throws Will throw an error if:
|
|
38
|
+
* - The `inputFormat` is invalid.
|
|
39
|
+
* - The `date` string does not match the expected `inputFormat`.
|
|
40
|
+
* - The `time` string is not in the format `hh:mm:ss`.
|
|
41
|
+
* - The resulting date is invalid.
|
|
42
|
+
*
|
|
43
|
+
* @example
|
|
44
|
+
* ```typescript
|
|
45
|
+
* import { formatDate } from "./formatDate";
|
|
46
|
+
*
|
|
47
|
+
* const date = "25/12/2023";
|
|
48
|
+
* const time = "14:30:00";
|
|
49
|
+
* const formatted = formatDate(date, time, "brazilianDate", "YYYY-MM-DD hh:mm:ss", -3);
|
|
50
|
+
* console.log(formatted); // Outputs: "2023-12-25 14:30:00"
|
|
51
|
+
* ```
|
|
52
|
+
*/
|
|
53
|
+
const formatDate = (date, time, inputFormat, outputFormat, timezone = 0) => {
|
|
54
|
+
const dateParts = date.split(/[-/]/).map(Number);
|
|
55
|
+
const timeParts = time.split(":").map(Number);
|
|
56
|
+
let day, month, year;
|
|
57
|
+
const [hours = 0, minutes = 0, seconds = 0] = timeParts;
|
|
58
|
+
switch (inputFormat) {
|
|
59
|
+
case "brazilianDate":
|
|
60
|
+
[day, month, year] = dateParts;
|
|
61
|
+
break;
|
|
62
|
+
case "isoDate":
|
|
63
|
+
[year, month, day] = dateParts;
|
|
64
|
+
break;
|
|
65
|
+
case "timestamp":
|
|
66
|
+
[year, month, day] = dateParts.map(Number);
|
|
67
|
+
break;
|
|
68
|
+
default:
|
|
69
|
+
throw new Error("Invalid input format");
|
|
70
|
+
}
|
|
71
|
+
const formattedDate = new Date(year, month - 1, day, hours, minutes, seconds);
|
|
72
|
+
if (isNaN(formattedDate.getTime()))
|
|
73
|
+
throw new Error("Invalid date");
|
|
74
|
+
formattedDate.setUTCHours(formattedDate.getUTCHours() + timezone);
|
|
75
|
+
return formatDateString(formattedDate, outputFormat);
|
|
76
|
+
};
|
|
77
|
+
export { formatDate };
|
|
@@ -1,3 +1,33 @@
|
|
|
1
|
-
|
|
1
|
+
import type { FormatJsonObjectFunction } from "@arkyn/types";
|
|
2
|
+
/**
|
|
3
|
+
* Formats a JSON object into a human-readable string with proper indentation.
|
|
4
|
+
*
|
|
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
|
+
* - If the input is an object, it will be formatted with keys and values properly indented.
|
|
11
|
+
* - If the input is an array, each element will be formatted and indented on a new line.
|
|
12
|
+
* - If the input is a string that can be parsed as JSON, it will attempt to parse and format it.
|
|
13
|
+
* - Primitive values (e.g., numbers, booleans, null) will be converted to their string representation.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```typescript
|
|
17
|
+
* const obj = { name: "John", age: 30, hobbies: ["reading", "gaming"] };
|
|
18
|
+
* const formatted = formatJsonObject(obj, 0);
|
|
19
|
+
* console.log(formatted);
|
|
20
|
+
* // Output:
|
|
21
|
+
* // {
|
|
22
|
+
* // "name": "John",
|
|
23
|
+
* // "age": 30,
|
|
24
|
+
* // "hobbies": [
|
|
25
|
+
* // "reading",
|
|
26
|
+
* // "gaming"
|
|
27
|
+
* // ]
|
|
28
|
+
* // }
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
31
|
+
declare const formatJsonObject: FormatJsonObjectFunction;
|
|
2
32
|
export { formatJsonObject };
|
|
3
33
|
//# 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,OAAO,KAAK,EAAE,wBAAwB,EAAE,MAAM,cAAc,CAAC;AAE7D;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AAEH,QAAA,MAAM,gBAAgB,EAAE,wBAuDvB,CAAC;AAEF,OAAO,EAAE,gBAAgB,EAAE,CAAC"}
|
|
@@ -1,35 +1,76 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Formats a JSON object into a human-readable string with proper indentation.
|
|
3
|
+
*
|
|
4
|
+
* @param obj - The JSON object or value to format. It can be an object, array, string, or primitive value.
|
|
5
|
+
* @param indentLevel - The current level of indentation to apply. This is used recursively to format nested structures.
|
|
6
|
+
* @returns A formatted string representation of the JSON object.
|
|
7
|
+
*
|
|
8
|
+
* @remarks
|
|
9
|
+
* - If the input is an object, it will be formatted with keys and values properly indented.
|
|
10
|
+
* - If the input is an array, each element will be formatted and indented on a new line.
|
|
11
|
+
* - If the input is a string that can be parsed as JSON, it will attempt to parse and format it.
|
|
12
|
+
* - Primitive values (e.g., numbers, booleans, null) will be converted to their string representation.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```typescript
|
|
16
|
+
* const obj = { name: "John", age: 30, hobbies: ["reading", "gaming"] };
|
|
17
|
+
* const formatted = formatJsonObject(obj, 0);
|
|
18
|
+
* console.log(formatted);
|
|
19
|
+
* // Output:
|
|
20
|
+
* // {
|
|
21
|
+
* // "name": "John",
|
|
22
|
+
* // "age": 30,
|
|
23
|
+
* // "hobbies": [
|
|
24
|
+
* // "reading",
|
|
25
|
+
* // "gaming"
|
|
26
|
+
* // ]
|
|
27
|
+
* // }
|
|
28
|
+
* ```
|
|
29
|
+
*/
|
|
30
|
+
const formatJsonObject = (obj, indentLevel) => {
|
|
2
31
|
const indent = " ".repeat(indentLevel);
|
|
3
32
|
let formattedString = "";
|
|
4
33
|
if (typeof obj === "object" && obj !== null) {
|
|
5
34
|
if (Array.isArray(obj)) {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
formattedString +=
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
35
|
+
if (obj.length === 0) {
|
|
36
|
+
// Caso especial para arrays vazios
|
|
37
|
+
formattedString += "[]";
|
|
38
|
+
}
|
|
39
|
+
else {
|
|
40
|
+
formattedString += "[\n";
|
|
41
|
+
obj.forEach((item, index) => {
|
|
42
|
+
formattedString +=
|
|
43
|
+
indent + " " + formatJsonObject(item, indentLevel + 1);
|
|
44
|
+
if (index < obj.length - 1) {
|
|
45
|
+
formattedString += ",";
|
|
46
|
+
}
|
|
47
|
+
formattedString += "\n";
|
|
48
|
+
});
|
|
49
|
+
formattedString += indent + "]";
|
|
50
|
+
}
|
|
16
51
|
}
|
|
17
52
|
else {
|
|
18
|
-
formattedString += "{\n";
|
|
19
53
|
const keys = Object.keys(obj);
|
|
20
|
-
keys.
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
54
|
+
if (keys.length === 0) {
|
|
55
|
+
// Caso especial para objetos vazios
|
|
56
|
+
formattedString += "{}";
|
|
57
|
+
}
|
|
58
|
+
else {
|
|
59
|
+
formattedString += "{\n";
|
|
60
|
+
keys.forEach((key, index) => {
|
|
61
|
+
formattedString +=
|
|
62
|
+
indent +
|
|
63
|
+
' "' +
|
|
64
|
+
key +
|
|
65
|
+
'": ' +
|
|
66
|
+
formatJsonObject(obj[key], indentLevel + 1);
|
|
67
|
+
if (index < keys.length - 1) {
|
|
68
|
+
formattedString += ",";
|
|
69
|
+
}
|
|
70
|
+
formattedString += "\n";
|
|
71
|
+
});
|
|
72
|
+
formattedString += indent + "}";
|
|
73
|
+
}
|
|
33
74
|
}
|
|
34
75
|
}
|
|
35
76
|
else if (typeof obj === "string") {
|
|
@@ -45,5 +86,5 @@ function formatJsonObject(obj, indentLevel) {
|
|
|
45
86
|
formattedString += obj;
|
|
46
87
|
}
|
|
47
88
|
return formattedString;
|
|
48
|
-
}
|
|
89
|
+
};
|
|
49
90
|
export { formatJsonObject };
|
|
@@ -1,3 +1,39 @@
|
|
|
1
|
-
|
|
1
|
+
import type { FormatJsonStringFunction } from "@arkyn/types";
|
|
2
|
+
/**
|
|
3
|
+
* Formats a JSON string into a more readable format.
|
|
4
|
+
*
|
|
5
|
+
* This function attempts to parse the provided JSON string into a JavaScript object,
|
|
6
|
+
* and then formats it using the `formatJsonObject` function. If the input string
|
|
7
|
+
* is not a valid JSON, it logs an error to the console and returns an empty string.
|
|
8
|
+
*
|
|
9
|
+
* @param jsonString - The JSON string to be formatted.
|
|
10
|
+
* @returns A formatted JSON string, or an empty string if the input is invalid.
|
|
11
|
+
*
|
|
12
|
+
* @throws Will log an error to the console if the input is not a valid JSON string.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```typescript
|
|
16
|
+
* const jsonString = '{"name":"John","age":30,"hobbies":["reading","gaming"]}';
|
|
17
|
+
* const formatted = formatJsonString(jsonString);
|
|
18
|
+
* console.log(formatted);
|
|
19
|
+
* // Output:
|
|
20
|
+
* // {
|
|
21
|
+
* // "name": "John",
|
|
22
|
+
* // "age": 30,
|
|
23
|
+
* // "hobbies": [
|
|
24
|
+
* // "reading",
|
|
25
|
+
* // "gaming"
|
|
26
|
+
* // ]
|
|
27
|
+
* // }
|
|
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
|
+
* ```
|
|
36
|
+
*/
|
|
37
|
+
declare const formatJsonString: FormatJsonStringFunction;
|
|
2
38
|
export { formatJsonString };
|
|
3
39
|
//# sourceMappingURL=formatJsonString.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"formatJsonString.d.ts","sourceRoot":"","sources":["../../src/formats/formatJsonString.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"formatJsonString.d.ts","sourceRoot":"","sources":["../../src/formats/formatJsonString.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,wBAAwB,EAAE,MAAM,cAAc,CAAC;AAG7D;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AAEH,QAAA,MAAM,gBAAgB,EAAE,wBAOvB,CAAC;AAEF,OAAO,EAAE,gBAAgB,EAAE,CAAC"}
|
|
@@ -1,12 +1,46 @@
|
|
|
1
1
|
import { formatJsonObject } from "./formatJsonObject";
|
|
2
|
-
|
|
2
|
+
/**
|
|
3
|
+
* Formats a JSON string into a more readable format.
|
|
4
|
+
*
|
|
5
|
+
* This function attempts to parse the provided JSON string into a JavaScript object,
|
|
6
|
+
* and then formats it using the `formatJsonObject` function. If the input string
|
|
7
|
+
* is not a valid JSON, it logs an error to the console and returns an empty string.
|
|
8
|
+
*
|
|
9
|
+
* @param jsonString - The JSON string to be formatted.
|
|
10
|
+
* @returns A formatted JSON string, or an empty string if the input is invalid.
|
|
11
|
+
*
|
|
12
|
+
* @throws Will log an error to the console if the input is not a valid JSON string.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```typescript
|
|
16
|
+
* const jsonString = '{"name":"John","age":30,"hobbies":["reading","gaming"]}';
|
|
17
|
+
* const formatted = formatJsonString(jsonString);
|
|
18
|
+
* console.log(formatted);
|
|
19
|
+
* // Output:
|
|
20
|
+
* // {
|
|
21
|
+
* // "name": "John",
|
|
22
|
+
* // "age": 30,
|
|
23
|
+
* // "hobbies": [
|
|
24
|
+
* // "reading",
|
|
25
|
+
* // "gaming"
|
|
26
|
+
* // ]
|
|
27
|
+
* // }
|
|
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
|
+
* ```
|
|
36
|
+
*/
|
|
37
|
+
const formatJsonString = (jsonString) => {
|
|
3
38
|
try {
|
|
4
39
|
const jsonObject = JSON.parse(jsonString);
|
|
5
40
|
return formatJsonObject(jsonObject, 0);
|
|
6
41
|
}
|
|
7
42
|
catch (error) {
|
|
8
|
-
|
|
9
|
-
return "";
|
|
43
|
+
throw new Error(`Invalid JSON string \n ${error}`);
|
|
10
44
|
}
|
|
11
|
-
}
|
|
45
|
+
};
|
|
12
46
|
export { formatJsonString };
|
|
@@ -1,3 +1,29 @@
|
|
|
1
|
-
|
|
1
|
+
import type { FormatToCepFunction } from "@arkyn/types";
|
|
2
|
+
/**
|
|
3
|
+
* Formats a given string into a Brazilian postal code (CEP) format.
|
|
4
|
+
*
|
|
5
|
+
* The function removes all non-numeric characters from the input string
|
|
6
|
+
* and attempts to format it as a CEP in the pattern `XXXXX-XXX`.
|
|
7
|
+
* If the input does not match the expected format, an error is thrown.
|
|
8
|
+
*
|
|
9
|
+
* @param value - The input string to be formatted as a CEP.
|
|
10
|
+
* @returns The formatted CEP string in the pattern `XXXXX-XXX`.
|
|
11
|
+
* @throws {Error} If the input does not match the expected CEP format.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```typescript
|
|
15
|
+
* import { formatToCep } from "./formatToCep";
|
|
16
|
+
*
|
|
17
|
+
* const formattedCep = formatToCep("12345678");
|
|
18
|
+
* console.log(formattedCep); // Output: "12345-678"
|
|
19
|
+
*
|
|
20
|
+
* try {
|
|
21
|
+
* formatToCep("1234");
|
|
22
|
+
* } catch (error) {
|
|
23
|
+
* console.error(error.message); // Output: "Invalid CEP format"
|
|
24
|
+
* }
|
|
25
|
+
* ```
|
|
26
|
+
*/
|
|
27
|
+
declare const formatToCep: FormatToCepFunction;
|
|
2
28
|
export { formatToCep };
|
|
3
29
|
//# sourceMappingURL=formatToCep.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"formatToCep.d.ts","sourceRoot":"","sources":["../../src/formats/formatToCep.ts"],"names":[],"mappings":"AAAA,
|
|
1
|
+
{"version":3,"file":"formatToCep.d.ts","sourceRoot":"","sources":["../../src/formats/formatToCep.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,cAAc,CAAC;AAGxD;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AAEH,QAAA,MAAM,WAAW,EAAE,mBAMlB,CAAC;AAEF,OAAO,EAAE,WAAW,EAAE,CAAC"}
|
|
@@ -1,9 +1,34 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
import { removeNonNumeric } from "../services/removeNonNumeric";
|
|
2
|
+
/**
|
|
3
|
+
* Formats a given string into a Brazilian postal code (CEP) format.
|
|
4
|
+
*
|
|
5
|
+
* The function removes all non-numeric characters from the input string
|
|
6
|
+
* and attempts to format it as a CEP in the pattern `XXXXX-XXX`.
|
|
7
|
+
* If the input does not match the expected format, an error is thrown.
|
|
8
|
+
*
|
|
9
|
+
* @param value - The input string to be formatted as a CEP.
|
|
10
|
+
* @returns The formatted CEP string in the pattern `XXXXX-XXX`.
|
|
11
|
+
* @throws {Error} If the input does not match the expected CEP format.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```typescript
|
|
15
|
+
* import { formatToCep } from "./formatToCep";
|
|
16
|
+
*
|
|
17
|
+
* const formattedCep = formatToCep("12345678");
|
|
18
|
+
* console.log(formattedCep); // Output: "12345-678"
|
|
19
|
+
*
|
|
20
|
+
* try {
|
|
21
|
+
* formatToCep("1234");
|
|
22
|
+
* } catch (error) {
|
|
23
|
+
* console.error(error.message); // Output: "Invalid CEP format"
|
|
24
|
+
* }
|
|
25
|
+
* ```
|
|
26
|
+
*/
|
|
27
|
+
const formatToCep = (value) => {
|
|
28
|
+
const cleaned = removeNonNumeric(value);
|
|
3
29
|
const match = cleaned.match(/^(\d{5})(\d{3})$/);
|
|
4
|
-
if (match)
|
|
30
|
+
if (match)
|
|
5
31
|
return `${match[1]}-${match[2]}`;
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
}
|
|
32
|
+
throw new Error("Invalid CEP format");
|
|
33
|
+
};
|
|
9
34
|
export { formatToCep };
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { FormatToCnpjFunction } from "@arkyn/types";
|
|
2
|
+
/**
|
|
3
|
+
* Formats a given string or number into a CNPJ (Cadastro Nacional da Pessoa Jurídica) format.
|
|
4
|
+
*
|
|
5
|
+
* The CNPJ format is: `XX.XXX.XXX/XXXX-XX`, where `X` represents a digit.
|
|
6
|
+
*
|
|
7
|
+
* @param value - The input value to be formatted. It can be a string or number containing the CNPJ digits.
|
|
8
|
+
* Non-numeric characters will be removed before formatting.
|
|
9
|
+
*
|
|
10
|
+
* @returns A string formatted as a CNPJ.
|
|
11
|
+
*
|
|
12
|
+
* @throws {Error} Throws an error if the input does not contain exactly 14 numeric digits.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```typescript
|
|
16
|
+
* import { formatToCnpj } from "./formatToCNPJ";
|
|
17
|
+
*
|
|
18
|
+
* const formattedCnpj = formatToCnpj("12345678000195");
|
|
19
|
+
* console.log(formattedCnpj); // Output: "12.345.678/0001-95"
|
|
20
|
+
*
|
|
21
|
+
* try {
|
|
22
|
+
* formatToCnpj("12345");
|
|
23
|
+
* } catch (error) {
|
|
24
|
+
* console.error(error.message); // Output: "Invalid CNPJ length"
|
|
25
|
+
* }
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
declare const formatToCnpj: FormatToCnpjFunction;
|
|
29
|
+
export { formatToCnpj };
|
|
30
|
+
//# sourceMappingURL=formatToCnpj.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"formatToCnpj.d.ts","sourceRoot":"","sources":["../../src/formats/formatToCnpj.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,cAAc,CAAC;AAGzD;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AAEH,QAAA,MAAM,YAAY,EAAE,oBAMnB,CAAC;AAEF,OAAO,EAAE,YAAY,EAAE,CAAC"}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { removeNonNumeric } from "../services/removeNonNumeric";
|
|
2
|
+
/**
|
|
3
|
+
* Formats a given string or number into a CNPJ (Cadastro Nacional da Pessoa Jurídica) format.
|
|
4
|
+
*
|
|
5
|
+
* The CNPJ format is: `XX.XXX.XXX/XXXX-XX`, where `X` represents a digit.
|
|
6
|
+
*
|
|
7
|
+
* @param value - The input value to be formatted. It can be a string or number containing the CNPJ digits.
|
|
8
|
+
* Non-numeric characters will be removed before formatting.
|
|
9
|
+
*
|
|
10
|
+
* @returns A string formatted as a CNPJ.
|
|
11
|
+
*
|
|
12
|
+
* @throws {Error} Throws an error if the input does not contain exactly 14 numeric digits.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```typescript
|
|
16
|
+
* import { formatToCnpj } from "./formatToCNPJ";
|
|
17
|
+
*
|
|
18
|
+
* const formattedCnpj = formatToCnpj("12345678000195");
|
|
19
|
+
* console.log(formattedCnpj); // Output: "12.345.678/0001-95"
|
|
20
|
+
*
|
|
21
|
+
* try {
|
|
22
|
+
* formatToCnpj("12345");
|
|
23
|
+
* } catch (error) {
|
|
24
|
+
* console.error(error.message); // Output: "Invalid CNPJ length"
|
|
25
|
+
* }
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
const formatToCnpj = (value) => {
|
|
29
|
+
const cleaned = removeNonNumeric(value);
|
|
30
|
+
const match = cleaned.match(/^(\d{2})(\d{3})(\d{3})(\d{4})(\d{2})$/);
|
|
31
|
+
if (match)
|
|
32
|
+
return `${match[1]}.${match[2]}.${match[3]}/${match[4]}-${match[5]}`;
|
|
33
|
+
throw new Error("Invalid CNPJ length");
|
|
34
|
+
};
|
|
35
|
+
export { formatToCnpj };
|