@arkyn/shared 2.0.4 → 2.2.0
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 +3 -0
- package/dist/formats/formatDate.d.ts +22 -33
- package/dist/formats/formatDate.d.ts.map +1 -1
- package/dist/formats/formatDate.js +23 -34
- package/dist/formats/formatToDate.d.ts +30 -0
- package/dist/formats/formatToDate.d.ts.map +1 -0
- package/dist/formats/formatToDate.js +24 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/services/ensureQuotes.d.ts +14 -0
- package/dist/services/ensureQuotes.d.ts.map +1 -0
- package/dist/services/ensureQuotes.js +19 -0
- package/package.json +1 -1
- package/src/formats/formatDate.ts +23 -35
- package/src/formats/formatToDate.ts +63 -0
- package/src/index.ts +1 -0
- package/src/services/ensureQuotes.ts +25 -0
package/README.md
CHANGED
|
@@ -69,6 +69,9 @@ npm install @arkyn/shared
|
|
|
69
69
|
- **`calculateCardInstallment(total: number, installments: number): number[]`**
|
|
70
70
|
Calculates the installment values for a given total.
|
|
71
71
|
|
|
72
|
+
- **`ensureQuotes(rawValue: string): string`**
|
|
73
|
+
Ensures that a given rawValue string is enclosed in quotes.
|
|
74
|
+
|
|
72
75
|
- **`maskSensitiveData(data: string): string`**
|
|
73
76
|
Masks sensitive data in a string.
|
|
74
77
|
|
|
@@ -1,42 +1,31 @@
|
|
|
1
1
|
import type { FormatDateFunction } from "@arkyn/types";
|
|
2
2
|
/**
|
|
3
|
-
* Formats a date
|
|
4
|
-
* with optional timezone adjustment.
|
|
3
|
+
* Formats a date and time string based on the provided input and output formats.
|
|
5
4
|
*
|
|
6
|
-
* @param
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
* -
|
|
11
|
-
* -
|
|
12
|
-
*
|
|
13
|
-
* @param outputFormat - The desired format for the
|
|
14
|
-
* -
|
|
15
|
-
*
|
|
16
|
-
* -
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
*
|
|
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.
|
|
5
|
+
* @param {[string, string]} dateTime - An array containing the date and optional time.
|
|
6
|
+
* - The first element is the date string.
|
|
7
|
+
* - The second element is the time string (default is "00:00:00").
|
|
8
|
+
* @param {"brazilianDate" | "isoDate" | "timestamp"} inputFormat - The format of the input date.
|
|
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 in "YYYY/MM/DD" format.
|
|
12
|
+
* @param {string} outputFormat - The desired output format for the date.
|
|
13
|
+
* - Use placeholders like "YYYY", "MM", "DD", "hh", "mm", "ss" to define the format.
|
|
14
|
+
* @param {number} [timezone=0] - The timezone offset in hours to apply to the date.
|
|
15
|
+
* - Defaults to 0 (UTC).
|
|
16
|
+
* @returns {string} The formatted date string based on the output format.
|
|
17
|
+
* @throws {Error} If the input format is invalid.
|
|
18
|
+
* @throws {Error} If the date is invalid.
|
|
30
19
|
*
|
|
31
20
|
* @example
|
|
32
|
-
*
|
|
33
|
-
*
|
|
21
|
+
* // Format a Brazilian date to ISO format
|
|
22
|
+
* formatDate(["25/12/2023", "15:30:00"], "brazilianDate", "YYYY-MM-DD hh:mm:ss");
|
|
23
|
+
* // Returns: "2023-12-25 15:30:00"
|
|
34
24
|
*
|
|
35
|
-
*
|
|
36
|
-
*
|
|
37
|
-
*
|
|
38
|
-
*
|
|
39
|
-
* ```
|
|
25
|
+
* @example
|
|
26
|
+
* // Format an ISO date to a custom format with timezone adjustment
|
|
27
|
+
* formatDate(["2023-12-25", "15:30:00"], "isoDate", "DD/MM/YYYY hh:mm:ss", -3);
|
|
28
|
+
* // Returns: "25/12/2023 12:30:00"
|
|
40
29
|
*/
|
|
41
30
|
declare const formatDate: FormatDateFunction;
|
|
42
31
|
export { formatDate };
|
|
@@ -1 +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
|
|
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;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AAEH,QAAA,MAAM,UAAU,EAAE,kBAgCjB,CAAC;AAEF,OAAO,EAAE,UAAU,EAAE,CAAC"}
|
|
@@ -12,45 +12,34 @@ function formatDateString(date, format) {
|
|
|
12
12
|
return format.replace(/YYYY|YY|MM|DD|hh|mm|ss/g, (match) => replacements[match]);
|
|
13
13
|
}
|
|
14
14
|
/**
|
|
15
|
-
* Formats a date
|
|
16
|
-
* with optional timezone adjustment.
|
|
15
|
+
* Formats a date and time string based on the provided input and output formats.
|
|
17
16
|
*
|
|
18
|
-
* @param
|
|
19
|
-
*
|
|
20
|
-
*
|
|
21
|
-
*
|
|
22
|
-
* -
|
|
23
|
-
* -
|
|
24
|
-
*
|
|
25
|
-
* @param outputFormat - The desired format for the
|
|
26
|
-
* -
|
|
27
|
-
*
|
|
28
|
-
* -
|
|
29
|
-
*
|
|
30
|
-
*
|
|
31
|
-
*
|
|
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.
|
|
17
|
+
* @param {[string, string]} dateTime - An array containing the date and optional time.
|
|
18
|
+
* - The first element is the date string.
|
|
19
|
+
* - The second element is the time string (default is "00:00:00").
|
|
20
|
+
* @param {"brazilianDate" | "isoDate" | "timestamp"} inputFormat - The format of the input date.
|
|
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 in "YYYY/MM/DD" format.
|
|
24
|
+
* @param {string} outputFormat - The desired output format for the date.
|
|
25
|
+
* - Use placeholders like "YYYY", "MM", "DD", "hh", "mm", "ss" to define the format.
|
|
26
|
+
* @param {number} [timezone=0] - The timezone offset in hours to apply to the date.
|
|
27
|
+
* - Defaults to 0 (UTC).
|
|
28
|
+
* @returns {string} The formatted date string based on the output format.
|
|
29
|
+
* @throws {Error} If the input format is invalid.
|
|
30
|
+
* @throws {Error} If the date is invalid.
|
|
42
31
|
*
|
|
43
32
|
* @example
|
|
44
|
-
*
|
|
45
|
-
*
|
|
33
|
+
* // Format a Brazilian date to ISO format
|
|
34
|
+
* formatDate(["25/12/2023", "15:30:00"], "brazilianDate", "YYYY-MM-DD hh:mm:ss");
|
|
35
|
+
* // Returns: "2023-12-25 15:30:00"
|
|
46
36
|
*
|
|
47
|
-
*
|
|
48
|
-
*
|
|
49
|
-
*
|
|
50
|
-
*
|
|
51
|
-
* ```
|
|
37
|
+
* @example
|
|
38
|
+
* // Format an ISO date to a custom format with timezone adjustment
|
|
39
|
+
* formatDate(["2023-12-25", "15:30:00"], "isoDate", "DD/MM/YYYY hh:mm:ss", -3);
|
|
40
|
+
* // Returns: "25/12/2023 12:30:00"
|
|
52
41
|
*/
|
|
53
|
-
const formatDate = (date, time, inputFormat, outputFormat, timezone = 0) => {
|
|
42
|
+
const formatDate = ([date, time = "00:00:00"], inputFormat, outputFormat, timezone = 0) => {
|
|
54
43
|
const dateParts = date.split(/[-/]/).map(Number);
|
|
55
44
|
const timeParts = time.split(":").map(Number);
|
|
56
45
|
let day, month, year;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Converts a date and time input into a JavaScript `Date` object, formatted according to the specified input format and timezone.
|
|
3
|
+
*
|
|
4
|
+
* @param param0 - A tuple containing the date string and an optional time string.
|
|
5
|
+
* The date string format depends on the `inputFormat` parameter.
|
|
6
|
+
* The time string defaults to "00:00:00" if not provided.
|
|
7
|
+
* @param inputFormat - The format of the input date string.
|
|
8
|
+
* Supported formats are:
|
|
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 in `YYYY-MM-DD` format (similar to `isoDate`).
|
|
12
|
+
* @param timezone - An optional timezone offset in hours. Defaults to `0` (UTC).
|
|
13
|
+
*
|
|
14
|
+
* @returns A `Date` object representing the parsed date and time, adjusted for the specified timezone.
|
|
15
|
+
*
|
|
16
|
+
* @throws {Error} If the `inputFormat` is invalid.
|
|
17
|
+
* @throws {Error} If the provided date or time is invalid.
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* ```typescript
|
|
21
|
+
* import { formatToDate } from "./formatToIsoDate";
|
|
22
|
+
*
|
|
23
|
+
* const date = formatToDate(["25/12/2023", "15:30:00"], "brazilianDate", -3);
|
|
24
|
+
* console.log(date); // Outputs a Date object for "2023-12-25T18:30:00.000Z" (UTC)
|
|
25
|
+
* ```
|
|
26
|
+
*/
|
|
27
|
+
import type { FormatToDateFunction } from "@arkyn/types";
|
|
28
|
+
declare const formatToDate: FormatToDateFunction;
|
|
29
|
+
export { formatToDate };
|
|
30
|
+
//# sourceMappingURL=formatToDate.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"formatToDate.d.ts","sourceRoot":"","sources":["../../src/formats/formatToDate.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,cAAc,CAAC;AAEzD,QAAA,MAAM,YAAY,EAAE,oBAgCnB,CAAC;AAEF,OAAO,EAAE,YAAY,EAAE,CAAC"}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
const formatToDate = ([date, time = "00:00:00"], inputFormat, timezone = 0) => {
|
|
2
|
+
const dateParts = date.split(/[-/]/).map(Number);
|
|
3
|
+
const timeParts = time.split(":").map(Number);
|
|
4
|
+
let day, month, year;
|
|
5
|
+
const [hours = 0, minutes = 0, seconds = 0] = timeParts;
|
|
6
|
+
switch (inputFormat) {
|
|
7
|
+
case "brazilianDate":
|
|
8
|
+
[day, month, year] = dateParts;
|
|
9
|
+
break;
|
|
10
|
+
case "isoDate":
|
|
11
|
+
[year, month, day] = dateParts;
|
|
12
|
+
break;
|
|
13
|
+
case "timestamp":
|
|
14
|
+
[year, month, day] = dateParts.map(Number);
|
|
15
|
+
break;
|
|
16
|
+
default:
|
|
17
|
+
throw new Error("Invalid input format");
|
|
18
|
+
}
|
|
19
|
+
const utcDate = new Date(Date.UTC(year, month - 1, day, hours - timezone, minutes, seconds));
|
|
20
|
+
if (isNaN(utcDate.getTime()))
|
|
21
|
+
throw new Error("Invalid date");
|
|
22
|
+
return utcDate;
|
|
23
|
+
};
|
|
24
|
+
export { formatToDate };
|
package/dist/index.d.ts
CHANGED
|
@@ -9,6 +9,7 @@ export { formatToCurrency } from "./formats/formatToCurrency";
|
|
|
9
9
|
export { formatToEllipsis } from "./formats/formatToEllipsis";
|
|
10
10
|
export { formatToHiddenDigits } from "./formats/formatToHiddenDigits";
|
|
11
11
|
export { formatToPhone } from "./formats/formatToPhone";
|
|
12
|
+
export { formatToDate } from "./formats/formatToDate";
|
|
12
13
|
export { generateColorByString } from "./generators/generateColorByString";
|
|
13
14
|
export { generateId } from "./generators/generateId";
|
|
14
15
|
export { generateSlug } from "./generators/generateSlug";
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,UAAU,EAAE,MAAM,sBAAsB,CAAC;AAClD,OAAO,EAAE,gBAAgB,EAAE,MAAM,4BAA4B,CAAC;AAC9D,OAAO,EAAE,gBAAgB,EAAE,MAAM,4BAA4B,CAAC;AAC9D,OAAO,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAC;AACpD,OAAO,EAAE,YAAY,EAAE,MAAM,wBAAwB,CAAC;AACtD,OAAO,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAC;AACpD,OAAO,EAAE,eAAe,EAAE,MAAM,2BAA2B,CAAC;AAC5D,OAAO,EAAE,gBAAgB,EAAE,MAAM,4BAA4B,CAAC;AAC9D,OAAO,EAAE,gBAAgB,EAAE,MAAM,4BAA4B,CAAC;AAC9D,OAAO,EAAE,oBAAoB,EAAE,MAAM,gCAAgC,CAAC;AACtE,OAAO,EAAE,aAAa,EAAE,MAAM,yBAAyB,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,UAAU,EAAE,MAAM,sBAAsB,CAAC;AAClD,OAAO,EAAE,gBAAgB,EAAE,MAAM,4BAA4B,CAAC;AAC9D,OAAO,EAAE,gBAAgB,EAAE,MAAM,4BAA4B,CAAC;AAC9D,OAAO,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAC;AACpD,OAAO,EAAE,YAAY,EAAE,MAAM,wBAAwB,CAAC;AACtD,OAAO,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAC;AACpD,OAAO,EAAE,eAAe,EAAE,MAAM,2BAA2B,CAAC;AAC5D,OAAO,EAAE,gBAAgB,EAAE,MAAM,4BAA4B,CAAC;AAC9D,OAAO,EAAE,gBAAgB,EAAE,MAAM,4BAA4B,CAAC;AAC9D,OAAO,EAAE,oBAAoB,EAAE,MAAM,gCAAgC,CAAC;AACtE,OAAO,EAAE,aAAa,EAAE,MAAM,yBAAyB,CAAC;AACxD,OAAO,EAAE,YAAY,EAAE,MAAM,wBAAwB,CAAC;AAGtD,OAAO,EAAE,qBAAqB,EAAE,MAAM,oCAAoC,CAAC;AAC3E,OAAO,EAAE,UAAU,EAAE,MAAM,yBAAyB,CAAC;AACrD,OAAO,EAAE,YAAY,EAAE,MAAM,2BAA2B,CAAC;AAGzD,OAAO,EAAE,wBAAwB,EAAE,MAAM,qCAAqC,CAAC;AAC/E,OAAO,EAAE,iBAAiB,EAAE,MAAM,8BAA8B,CAAC;AACjE,OAAO,EAAE,gBAAgB,EAAE,MAAM,6BAA6B,CAAC;AAC/D,OAAO,EAAE,mBAAmB,EAAE,MAAM,gCAAgC,CAAC;AAGrE,OAAO,EAAE,WAAW,EAAE,MAAM,2BAA2B,CAAC;AACxD,OAAO,EAAE,YAAY,EAAE,MAAM,4BAA4B,CAAC;AAC1D,OAAO,EAAE,WAAW,EAAE,MAAM,2BAA2B,CAAC;AACxD,OAAO,EAAE,YAAY,EAAE,MAAM,4BAA4B,CAAC;AAC1D,OAAO,EAAE,aAAa,EAAE,MAAM,6BAA6B,CAAC;AAC5D,OAAO,EAAE,UAAU,EAAE,MAAM,0BAA0B,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -10,6 +10,7 @@ export { formatToCurrency } from "./formats/formatToCurrency";
|
|
|
10
10
|
export { formatToEllipsis } from "./formats/formatToEllipsis";
|
|
11
11
|
export { formatToHiddenDigits } from "./formats/formatToHiddenDigits";
|
|
12
12
|
export { formatToPhone } from "./formats/formatToPhone";
|
|
13
|
+
export { formatToDate } from "./formats/formatToDate";
|
|
13
14
|
// generators
|
|
14
15
|
export { generateColorByString } from "./generators/generateColorByString";
|
|
15
16
|
export { generateId } from "./generators/generateId";
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { EnsureQuotesFunction } from "@arkyn/types";
|
|
2
|
+
/**
|
|
3
|
+
* Ensures that a given rawValue string is enclosed in quotes.
|
|
4
|
+
*
|
|
5
|
+
* This function checks if the input string is already enclosed in either single
|
|
6
|
+
* quotes (`'`) or double quotes (`"`). If the string is already quoted, it is
|
|
7
|
+
* returned as-is. Otherwise, the function wraps the string in double quotes.
|
|
8
|
+
*
|
|
9
|
+
* @param url - The URL string to be checked and potentially quoted.
|
|
10
|
+
* @returns The input string, either unchanged if it is already quoted, or wrapped in double quotes.
|
|
11
|
+
*/
|
|
12
|
+
declare const ensureQuotes: EnsureQuotesFunction;
|
|
13
|
+
export { ensureQuotes };
|
|
14
|
+
//# sourceMappingURL=ensureQuotes.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ensureQuotes.d.ts","sourceRoot":"","sources":["../../src/services/ensureQuotes.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,cAAc,CAAC;AAEzD;;;;;;;;;GASG;AAEH,QAAA,MAAM,YAAY,EAAE,oBASnB,CAAC;AAEF,OAAO,EAAE,YAAY,EAAE,CAAC"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Ensures that a given rawValue string is enclosed in quotes.
|
|
3
|
+
*
|
|
4
|
+
* This function checks if the input string is already enclosed in either single
|
|
5
|
+
* quotes (`'`) or double quotes (`"`). If the string is already quoted, it is
|
|
6
|
+
* returned as-is. Otherwise, the function wraps the string in double quotes.
|
|
7
|
+
*
|
|
8
|
+
* @param url - The URL string to be checked and potentially quoted.
|
|
9
|
+
* @returns The input string, either unchanged if it is already quoted, or wrapped in double quotes.
|
|
10
|
+
*/
|
|
11
|
+
const ensureQuotes = (rawValue) => {
|
|
12
|
+
const hasSingleQuotes = rawValue.startsWith("'") && rawValue.endsWith("'");
|
|
13
|
+
const hasDoubleQuotes = rawValue.startsWith('"') && rawValue.endsWith('"');
|
|
14
|
+
if (hasSingleQuotes || hasDoubleQuotes) {
|
|
15
|
+
return rawValue;
|
|
16
|
+
}
|
|
17
|
+
return `"${rawValue}"`;
|
|
18
|
+
};
|
|
19
|
+
export { ensureQuotes };
|
package/package.json
CHANGED
|
@@ -20,48 +20,36 @@ function formatDateString(date: Date, format: string): string {
|
|
|
20
20
|
}
|
|
21
21
|
|
|
22
22
|
/**
|
|
23
|
-
* Formats a date
|
|
24
|
-
* with optional timezone adjustment.
|
|
23
|
+
* Formats a date and time string based on the provided input and output formats.
|
|
25
24
|
*
|
|
26
|
-
* @param
|
|
27
|
-
*
|
|
28
|
-
*
|
|
29
|
-
*
|
|
30
|
-
* -
|
|
31
|
-
* -
|
|
32
|
-
*
|
|
33
|
-
* @param outputFormat - The desired format for the
|
|
34
|
-
* -
|
|
35
|
-
*
|
|
36
|
-
* -
|
|
37
|
-
*
|
|
38
|
-
*
|
|
39
|
-
*
|
|
40
|
-
* - `ss`: Seconds (zero-padded, e.g., 07)
|
|
41
|
-
* @param timezone - (Optional) The timezone offset in hours to adjust the date. Defaults to `0`.
|
|
42
|
-
*
|
|
43
|
-
* @returns The formatted date string in the specified `outputFormat`.
|
|
44
|
-
*
|
|
45
|
-
* @throws Will throw an error if:
|
|
46
|
-
* - The `inputFormat` is invalid.
|
|
47
|
-
* - The `date` string does not match the expected `inputFormat`.
|
|
48
|
-
* - The `time` string is not in the format `hh:mm:ss`.
|
|
49
|
-
* - The resulting date is invalid.
|
|
25
|
+
* @param {[string, string]} dateTime - An array containing the date and optional time.
|
|
26
|
+
* - The first element is the date string.
|
|
27
|
+
* - The second element is the time string (default is "00:00:00").
|
|
28
|
+
* @param {"brazilianDate" | "isoDate" | "timestamp"} inputFormat - The format of the input date.
|
|
29
|
+
* - "brazilianDate": Expects the date in "DD/MM/YYYY" format.
|
|
30
|
+
* - "isoDate": Expects the date in "YYYY-MM-DD" format.
|
|
31
|
+
* - "timestamp": Expects the date in "YYYY/MM/DD" format.
|
|
32
|
+
* @param {string} outputFormat - The desired output format for the date.
|
|
33
|
+
* - Use placeholders like "YYYY", "MM", "DD", "hh", "mm", "ss" to define the format.
|
|
34
|
+
* @param {number} [timezone=0] - The timezone offset in hours to apply to the date.
|
|
35
|
+
* - Defaults to 0 (UTC).
|
|
36
|
+
* @returns {string} The formatted date string based on the output format.
|
|
37
|
+
* @throws {Error} If the input format is invalid.
|
|
38
|
+
* @throws {Error} If the date is invalid.
|
|
50
39
|
*
|
|
51
40
|
* @example
|
|
52
|
-
*
|
|
53
|
-
*
|
|
41
|
+
* // Format a Brazilian date to ISO format
|
|
42
|
+
* formatDate(["25/12/2023", "15:30:00"], "brazilianDate", "YYYY-MM-DD hh:mm:ss");
|
|
43
|
+
* // Returns: "2023-12-25 15:30:00"
|
|
54
44
|
*
|
|
55
|
-
*
|
|
56
|
-
*
|
|
57
|
-
*
|
|
58
|
-
*
|
|
59
|
-
* ```
|
|
45
|
+
* @example
|
|
46
|
+
* // Format an ISO date to a custom format with timezone adjustment
|
|
47
|
+
* formatDate(["2023-12-25", "15:30:00"], "isoDate", "DD/MM/YYYY hh:mm:ss", -3);
|
|
48
|
+
* // Returns: "25/12/2023 12:30:00"
|
|
60
49
|
*/
|
|
61
50
|
|
|
62
51
|
const formatDate: FormatDateFunction = (
|
|
63
|
-
date,
|
|
64
|
-
time,
|
|
52
|
+
[date, time = "00:00:00"],
|
|
65
53
|
inputFormat,
|
|
66
54
|
outputFormat,
|
|
67
55
|
timezone = 0
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Converts a date and time input into a JavaScript `Date` object, formatted according to the specified input format and timezone.
|
|
3
|
+
*
|
|
4
|
+
* @param param0 - A tuple containing the date string and an optional time string.
|
|
5
|
+
* The date string format depends on the `inputFormat` parameter.
|
|
6
|
+
* The time string defaults to "00:00:00" if not provided.
|
|
7
|
+
* @param inputFormat - The format of the input date string.
|
|
8
|
+
* Supported formats are:
|
|
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 in `YYYY-MM-DD` format (similar to `isoDate`).
|
|
12
|
+
* @param timezone - An optional timezone offset in hours. Defaults to `0` (UTC).
|
|
13
|
+
*
|
|
14
|
+
* @returns A `Date` object representing the parsed date and time, adjusted for the specified timezone.
|
|
15
|
+
*
|
|
16
|
+
* @throws {Error} If the `inputFormat` is invalid.
|
|
17
|
+
* @throws {Error} If the provided date or time is invalid.
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* ```typescript
|
|
21
|
+
* import { formatToDate } from "./formatToIsoDate";
|
|
22
|
+
*
|
|
23
|
+
* const date = formatToDate(["25/12/2023", "15:30:00"], "brazilianDate", -3);
|
|
24
|
+
* console.log(date); // Outputs a Date object for "2023-12-25T18:30:00.000Z" (UTC)
|
|
25
|
+
* ```
|
|
26
|
+
*/
|
|
27
|
+
import type { FormatToDateFunction } from "@arkyn/types";
|
|
28
|
+
|
|
29
|
+
const formatToDate: FormatToDateFunction = (
|
|
30
|
+
[date, time = "00:00:00"],
|
|
31
|
+
inputFormat,
|
|
32
|
+
timezone = 0
|
|
33
|
+
) => {
|
|
34
|
+
const dateParts = date.split(/[-/]/).map(Number);
|
|
35
|
+
const timeParts = time.split(":").map(Number);
|
|
36
|
+
|
|
37
|
+
let day, month, year;
|
|
38
|
+
const [hours = 0, minutes = 0, seconds = 0] = timeParts;
|
|
39
|
+
|
|
40
|
+
switch (inputFormat) {
|
|
41
|
+
case "brazilianDate":
|
|
42
|
+
[day, month, year] = dateParts;
|
|
43
|
+
break;
|
|
44
|
+
case "isoDate":
|
|
45
|
+
[year, month, day] = dateParts;
|
|
46
|
+
break;
|
|
47
|
+
case "timestamp":
|
|
48
|
+
[year, month, day] = dateParts.map(Number);
|
|
49
|
+
break;
|
|
50
|
+
default:
|
|
51
|
+
throw new Error("Invalid input format");
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const utcDate = new Date(
|
|
55
|
+
Date.UTC(year, month - 1, day, hours - timezone, minutes, seconds)
|
|
56
|
+
);
|
|
57
|
+
|
|
58
|
+
if (isNaN(utcDate.getTime())) throw new Error("Invalid date");
|
|
59
|
+
|
|
60
|
+
return utcDate;
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
export { formatToDate };
|
package/src/index.ts
CHANGED
|
@@ -10,6 +10,7 @@ export { formatToCurrency } from "./formats/formatToCurrency";
|
|
|
10
10
|
export { formatToEllipsis } from "./formats/formatToEllipsis";
|
|
11
11
|
export { formatToHiddenDigits } from "./formats/formatToHiddenDigits";
|
|
12
12
|
export { formatToPhone } from "./formats/formatToPhone";
|
|
13
|
+
export { formatToDate } from "./formats/formatToDate";
|
|
13
14
|
|
|
14
15
|
// generators
|
|
15
16
|
export { generateColorByString } from "./generators/generateColorByString";
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { EnsureQuotesFunction } from "@arkyn/types";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Ensures that a given rawValue string is enclosed in quotes.
|
|
5
|
+
*
|
|
6
|
+
* This function checks if the input string is already enclosed in either single
|
|
7
|
+
* quotes (`'`) or double quotes (`"`). If the string is already quoted, it is
|
|
8
|
+
* returned as-is. Otherwise, the function wraps the string in double quotes.
|
|
9
|
+
*
|
|
10
|
+
* @param url - The URL string to be checked and potentially quoted.
|
|
11
|
+
* @returns The input string, either unchanged if it is already quoted, or wrapped in double quotes.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
const ensureQuotes: EnsureQuotesFunction = (rawValue) => {
|
|
15
|
+
const hasSingleQuotes = rawValue.startsWith("'") && rawValue.endsWith("'");
|
|
16
|
+
const hasDoubleQuotes = rawValue.startsWith('"') && rawValue.endsWith('"');
|
|
17
|
+
|
|
18
|
+
if (hasSingleQuotes || hasDoubleQuotes) {
|
|
19
|
+
return rawValue;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
return `"${rawValue}"`;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
export { ensureQuotes };
|