@bccampus/utils 0.2.2 → 0.3.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 -8
- package/dist/array/chunk.cjs +13 -5
- package/dist/array/chunk.d.ts +7 -0
- package/dist/array/chunk.js +14 -7
- package/dist/array/chunk.test.d.ts +1 -0
- package/dist/date/iso-date-time/constants.cjs +27 -0
- package/dist/date/iso-date-time/constants.d.ts +9 -0
- package/dist/date/iso-date-time/constants.js +19 -0
- package/dist/date/iso-date-time/index.cjs +8 -0
- package/dist/date/iso-date-time/index.d.ts +5 -0
- package/dist/date/iso-date-time/index.js +3 -0
- package/dist/date/iso-date-time/iso-date-time.arithmetic.test.d.ts +1 -0
- package/dist/date/iso-date-time/iso-date-time.cjs +232 -0
- package/dist/date/{iso-date-time.d.ts → iso-date-time/iso-date-time.d.ts} +3 -32
- package/dist/date/iso-date-time/iso-date-time.format.test.d.ts +1 -0
- package/dist/date/iso-date-time/iso-date-time.js +231 -0
- package/dist/date/iso-date-time/iso-date-time.test.d.ts +1 -0
- package/dist/date/iso-date-time/types.cjs +0 -0
- package/dist/date/iso-date-time/types.d.ts +25 -0
- package/dist/date/iso-date-time/types.js +0 -0
- package/dist/duration/add-durations.cjs +5 -8
- package/dist/duration/add-durations.js +5 -9
- package/dist/duration/duration.test.d.ts +1 -0
- package/dist/duration/is-duration.cjs +4 -3
- package/dist/duration/is-duration.js +5 -5
- package/dist/duration/to-number.cjs +11 -10
- package/dist/duration/to-number.d.ts +1 -1
- package/dist/duration/to-number.js +11 -11
- package/dist/format/formatter.cjs +31 -31
- package/dist/format/formatter.d.ts +4 -4
- package/dist/format/formatter.js +32 -37
- package/dist/format/formatter.test.d.ts +1 -0
- package/dist/format/stringfy.test.d.ts +1 -0
- package/dist/format/stringify.cjs +34 -40
- package/dist/format/stringify.d.ts +1 -1
- package/dist/format/stringify.js +35 -42
- package/dist/format/template.cjs +25 -32
- package/dist/format/template.d.ts +3 -3
- package/dist/format/template.js +24 -33
- package/dist/format/template.test.d.ts +1 -0
- package/dist/index.cjs +0 -4
- package/dist/index.d.ts +0 -1
- package/dist/index.js +0 -4
- package/dist/object/get.cjs +5 -5
- package/dist/object/get.d.ts +1 -1
- package/dist/object/get.js +6 -7
- package/dist/object/get.test.d.ts +1 -0
- package/dist/object/is-object.cjs +5 -2
- package/dist/object/is-object.d.ts +1 -1
- package/dist/object/is-object.js +6 -4
- package/dist/object/is-object.test.d.ts +1 -0
- package/dist/object/omit.cjs +14 -16
- package/dist/object/omit.d.ts +1 -1
- package/dist/object/omit.js +15 -18
- package/dist/object/omit.test.d.ts +1 -0
- package/dist/object/pick.cjs +10 -9
- package/dist/object/pick.d.ts +1 -1
- package/dist/object/pick.js +9 -9
- package/dist/object/pick.test.d.ts +1 -0
- package/dist/object/set.cjs +13 -12
- package/dist/object/set.d.ts +1 -1
- package/dist/object/set.js +14 -14
- package/dist/object/set.test.d.ts +1 -0
- package/dist/path/file-url-to-path.cjs +10 -6
- package/dist/path/file-url-to-path.js +11 -8
- package/dist/path/file-url-to-path.test.d.ts +1 -0
- package/dist/path/normalize.cjs +41 -27
- package/dist/path/normalize.d.ts +8 -2
- package/dist/path/normalize.js +41 -28
- package/dist/path/normalize.test.d.ts +1 -0
- package/dist/set/difference.cjs +5 -6
- package/dist/set/difference.d.ts +1 -1
- package/dist/set/difference.js +6 -8
- package/dist/set/intersection.cjs +6 -9
- package/dist/set/intersection.d.ts +1 -1
- package/dist/set/intersection.js +7 -11
- package/dist/set/is-superset.cjs +5 -8
- package/dist/set/is-superset.d.ts +1 -1
- package/dist/set/is-superset.js +6 -10
- package/dist/set/set.test.d.ts +1 -0
- package/dist/set/symmetric-difference.cjs +6 -10
- package/dist/set/symmetric-difference.d.ts +1 -1
- package/dist/set/symmetric-difference.js +7 -12
- package/dist/set/union.cjs +5 -6
- package/dist/set/union.d.ts +1 -1
- package/dist/set/union.js +6 -8
- package/dist/string/capitalize.cjs +5 -2
- package/dist/string/capitalize.d.ts +1 -1
- package/dist/string/capitalize.js +6 -4
- package/dist/string/capitalize.test.d.ts +1 -0
- package/dist/string/normalize.cjs +7 -2
- package/dist/string/normalize.js +8 -4
- package/dist/string/normalize.test.d.ts +1 -0
- package/dist/string/pluralize/index.cjs +5 -4
- package/dist/string/pluralize/index.js +4 -5
- package/dist/string/pluralize/pluralize.cjs +162 -208
- package/dist/string/pluralize/pluralize.d.ts +4 -8
- package/dist/string/pluralize/pluralize.js +158 -205
- package/dist/string/pluralize/pluralize.test.d.ts +1 -0
- package/dist/string/pluralize/rules/index.cjs +8 -9
- package/dist/string/pluralize/rules/index.js +1 -6
- package/dist/string/pluralize/rules/irregular.cjs +55 -59
- package/dist/string/pluralize/rules/irregular.js +56 -61
- package/dist/string/pluralize/rules/pluralization.cjs +28 -36
- package/dist/string/pluralize/rules/pluralization.js +29 -38
- package/dist/string/pluralize/rules/singularization.cjs +27 -44
- package/dist/string/pluralize/rules/singularization.js +28 -46
- package/dist/string/pluralize/rules/uncountable.cjs +104 -110
- package/dist/string/pluralize/rules/uncountable.js +105 -112
- package/dist/string/pluralize/types.cjs +0 -1
- package/dist/string/pluralize/types.js +0 -1
- package/dist/string/slugify/index.cjs +2129 -2246
- package/dist/string/slugify/index.js +2130 -2248
- package/dist/string/slugify/slugify.test.d.ts +1 -0
- package/dist/string/strip-html-tags.cjs +4 -3
- package/dist/string/strip-html-tags.js +5 -5
- package/dist/string/strip-html-tags.test.d.ts +1 -0
- package/dist/string/to-int.cjs +14 -6
- package/dist/string/to-int.js +15 -8
- package/dist/string/to-int.test.d.ts +1 -0
- package/dist/string/to-sentence-case.cjs +6 -6
- package/dist/string/to-sentence-case.d.ts +1 -1
- package/dist/string/to-sentence-case.js +7 -8
- package/dist/string/to-sentence-case.test.d.ts +1 -0
- package/dist/string/trim.cjs +8 -7
- package/dist/string/trim.js +9 -9
- package/dist/string/trim.test.d.ts +1 -0
- package/dist/string/truncate.cjs +16 -15
- package/dist/string/truncate.js +17 -17
- package/dist/string/truncate.test.d.ts +1 -0
- package/dist/value/is-empty.cjs +30 -12
- package/dist/value/is-empty.d.ts +1 -1
- package/dist/value/is-empty.js +31 -14
- package/dist/value/value.test.d.ts +1 -0
- package/package.json +183 -67
- package/dist/array/index.cjs +0 -4
- package/dist/array/index.d.ts +0 -1
- package/dist/array/index.js +0 -4
- package/dist/date/index.cjs +0 -8
- package/dist/date/index.d.ts +0 -1
- package/dist/date/index.js +0 -8
- package/dist/date/iso-date-time.cjs +0 -256
- package/dist/date/iso-date-time.js +0 -256
- package/dist/duration/index.cjs +0 -8
- package/dist/duration/index.d.ts +0 -3
- package/dist/duration/index.js +0 -8
- package/dist/format/index.cjs +0 -13
- package/dist/format/index.d.ts +0 -3
- package/dist/format/index.js +0 -13
- package/dist/object/index.cjs +0 -12
- package/dist/object/index.d.ts +0 -5
- package/dist/object/index.js +0 -12
- package/dist/path/index.cjs +0 -6
- package/dist/path/index.d.ts +0 -2
- package/dist/path/index.js +0 -6
- package/dist/set/index.cjs +0 -12
- package/dist/set/index.d.ts +0 -5
- package/dist/set/index.js +0 -12
- package/dist/string/index.cjs +0 -14
- package/dist/string/index.d.ts +0 -6
- package/dist/string/index.js +0 -14
- package/dist/value/index.cjs +0 -4
- package/dist/value/index.d.ts +0 -1
- package/dist/value/index.js +0 -4
- package/eslint.config.js +0 -19
- package/src/array/chunk.ts +0 -6
- package/src/array/index.ts +0 -1
- package/src/date/index.ts +0 -1
- package/src/date/iso-date-time.ts +0 -356
- package/src/duration/add-durations.ts +0 -16
- package/src/duration/index.ts +0 -3
- package/src/duration/is-duration.ts +0 -5
- package/src/duration/to-number.ts +0 -18
- package/src/format/formatter.ts +0 -37
- package/src/format/index.ts +0 -3
- package/src/format/stringify.ts +0 -87
- package/src/format/template.ts +0 -48
- package/src/index.ts +0 -1
- package/src/object/get.ts +0 -9
- package/src/object/index.ts +0 -5
- package/src/object/is-object.ts +0 -2
- package/src/object/omit.ts +0 -21
- package/src/object/pick.ts +0 -10
- package/src/object/set.ts +0 -15
- package/src/path/file-url-to-path.ts +0 -12
- package/src/path/index.ts +0 -2
- package/src/path/normalize.ts +0 -72
- package/src/set/difference.ts +0 -9
- package/src/set/index.ts +0 -5
- package/src/set/intersection.ts +0 -12
- package/src/set/is-superset.ts +0 -12
- package/src/set/symmetric-difference.ts +0 -12
- package/src/set/union.ts +0 -8
- package/src/string/capitalize.ts +0 -2
- package/src/string/index.ts +0 -6
- package/src/string/normalize.ts +0 -7
- package/src/string/pluralize/index.ts +0 -7
- package/src/string/pluralize/pluralize.ts +0 -296
- package/src/string/pluralize/rules/index.ts +0 -4
- package/src/string/pluralize/rules/irregular.ts +0 -59
- package/src/string/pluralize/rules/pluralization.ts +0 -40
- package/src/string/pluralize/rules/singularization.ts +0 -46
- package/src/string/pluralize/rules/uncountable.ts +0 -107
- package/src/string/pluralize/types.ts +0 -4
- package/src/string/slugify/index.ts +0 -3
- package/src/string/strip-html-tags.ts +0 -4
- package/src/string/to-int.ts +0 -20
- package/src/string/to-sentence-case.ts +0 -6
- package/src/string/trim.ts +0 -11
- package/src/string/truncate.ts +0 -44
- package/src/value/index.ts +0 -1
- package/src/value/is-empty.ts +0 -31
- package/tsconfig.json +0 -18
- package/tsconfig.lib.json +0 -37
- package/tsconfig.node.json +0 -25
- package/vite.config.ts +0 -76
package/dist/string/index.cjs
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
-
const string_truncate = require("./truncate.cjs");
|
|
4
|
-
const string_trim = require("./trim.cjs");
|
|
5
|
-
const string_stripHtmlTags = require("./strip-html-tags.cjs");
|
|
6
|
-
const string_capitalize = require("./capitalize.cjs");
|
|
7
|
-
const string_toSentenceCase = require("./to-sentence-case.cjs");
|
|
8
|
-
const string_toInt = require("./to-int.cjs");
|
|
9
|
-
exports.truncate = string_truncate.truncate;
|
|
10
|
-
exports.trim = string_trim.trim;
|
|
11
|
-
exports.stripHtmlTags = string_stripHtmlTags.stripHtmlTags;
|
|
12
|
-
exports.capitalize = string_capitalize.capitalize;
|
|
13
|
-
exports.toSentenceCase = string_toSentenceCase.toSentenceCase;
|
|
14
|
-
exports.stringToInt = string_toInt.stringToInt;
|
package/dist/string/index.d.ts
DELETED
package/dist/string/index.js
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import { truncate } from "./truncate.js";
|
|
2
|
-
import { trim } from "./trim.js";
|
|
3
|
-
import { stripHtmlTags } from "./strip-html-tags.js";
|
|
4
|
-
import { capitalize } from "./capitalize.js";
|
|
5
|
-
import { toSentenceCase } from "./to-sentence-case.js";
|
|
6
|
-
import { stringToInt } from "./to-int.js";
|
|
7
|
-
export {
|
|
8
|
-
capitalize,
|
|
9
|
-
stringToInt,
|
|
10
|
-
stripHtmlTags,
|
|
11
|
-
toSentenceCase,
|
|
12
|
-
trim,
|
|
13
|
-
truncate
|
|
14
|
-
};
|
package/dist/value/index.cjs
DELETED
package/dist/value/index.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from './is-empty';
|
package/dist/value/index.js
DELETED
package/eslint.config.js
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import js from '@eslint/js'
|
|
2
|
-
import globals from 'globals'
|
|
3
|
-
import tseslint from 'typescript-eslint'
|
|
4
|
-
import { defineConfig, globalIgnores } from 'eslint/config'
|
|
5
|
-
|
|
6
|
-
export default defineConfig([
|
|
7
|
-
globalIgnores(['dist']),
|
|
8
|
-
{
|
|
9
|
-
files: ['**/*.{ts,tsx}'],
|
|
10
|
-
extends: [
|
|
11
|
-
js.configs.recommended,
|
|
12
|
-
tseslint.configs.recommended,
|
|
13
|
-
],
|
|
14
|
-
languageOptions: {
|
|
15
|
-
ecmaVersion: 2020,
|
|
16
|
-
globals: globals.browser,
|
|
17
|
-
},
|
|
18
|
-
}
|
|
19
|
-
])
|
package/src/array/chunk.ts
DELETED
package/src/array/index.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from "./chunk";
|
package/src/date/index.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from "./iso-date-time";
|
|
@@ -1,356 +0,0 @@
|
|
|
1
|
-
const isoDateTimeRegEx = new RegExp('^([0-9-]+)(T([0-9:.,]+)(Z|[+-]([0-9:]+))?)?$');
|
|
2
|
-
const dateRegEx = new RegExp('^(\\d{4})-?(\\d{2})?-?(\\d{2})?$');
|
|
3
|
-
const timeRegEx = new RegExp('^(\\d{2}):?(\\d{2})?:?(\\d{2})?(\\.(\\d{3}))?$');
|
|
4
|
-
const timezoneRegEx = new RegExp('^Z|^[+-]((\\d{2}):?(\\d{2})?)$');
|
|
5
|
-
|
|
6
|
-
export const InvalidDate: unique symbol = Symbol('Invalid Date');
|
|
7
|
-
export type InvalidDate = typeof InvalidDate;
|
|
8
|
-
|
|
9
|
-
export const DefaultDateFormat: DateFormatOptions = {
|
|
10
|
-
year: 'numeric',
|
|
11
|
-
month: 'long',
|
|
12
|
-
day: '2-digit',
|
|
13
|
-
};
|
|
14
|
-
|
|
15
|
-
export const DefaultTimeFormat: DateFormatOptions = {
|
|
16
|
-
hour: '2-digit',
|
|
17
|
-
minute: '2-digit',
|
|
18
|
-
timeZoneName: 'short',
|
|
19
|
-
};
|
|
20
|
-
|
|
21
|
-
export const DefaultTimezone = 'America/Vancouver';
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* Enables storage, retrieval and formating of ISO dates and times
|
|
25
|
-
* with time zone information.
|
|
26
|
-
*/
|
|
27
|
-
export class ISODateTime {
|
|
28
|
-
#parsed?: ParsedDateTime;
|
|
29
|
-
#date: Date | InvalidDate;
|
|
30
|
-
#string: string;
|
|
31
|
-
|
|
32
|
-
constructor();
|
|
33
|
-
constructor(isoDatetime: number);
|
|
34
|
-
constructor(isoDatetime: string);
|
|
35
|
-
constructor(isoDatetime?: string | number) {
|
|
36
|
-
if (typeof isoDatetime === 'string') {
|
|
37
|
-
this.#parsed = ISODateTime.parse(isoDatetime);
|
|
38
|
-
if (this.#parsed) {
|
|
39
|
-
this.#date = new Date(ISODateTime.toString(this));
|
|
40
|
-
} else {
|
|
41
|
-
this.#date = InvalidDate;
|
|
42
|
-
console.error(`Failed to parse date for value: ${isoDatetime}`);
|
|
43
|
-
}
|
|
44
|
-
} else if (typeof isoDatetime === 'number' && Number.isFinite(isoDatetime)) {
|
|
45
|
-
this.#date = new Date(isoDatetime);
|
|
46
|
-
this.#parsed = ISODateTime.parse(this.#date.toISOString());
|
|
47
|
-
} else {
|
|
48
|
-
this.#date = new Date();
|
|
49
|
-
this.#parsed = ISODateTime.parse(this.#date.toISOString());
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
if (this.#date !== InvalidDate) this.#string = this.#date.toISOString();
|
|
53
|
-
else this.#string = 'Invalid Date';
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* Get date and time components
|
|
58
|
-
*/
|
|
59
|
-
get parts() {
|
|
60
|
-
return this.#parsed;
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
/**
|
|
64
|
-
* Get timezone
|
|
65
|
-
*/
|
|
66
|
-
get timezone() {
|
|
67
|
-
return this.parts?.timezone ?? 'UTC';
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
/**
|
|
71
|
-
* Parse a string in ISO DateTime format.
|
|
72
|
-
* If the input is valid, returns date and time components;
|
|
73
|
-
* otherwise, returns `undefined`.
|
|
74
|
-
*/
|
|
75
|
-
static parse(isoDatetime: string) {
|
|
76
|
-
const isoDatetimeParts = isoDatetime.match(isoDateTimeRegEx);
|
|
77
|
-
if (isoDatetimeParts) {
|
|
78
|
-
const dateParts = isoDatetimeParts[1] && isoDatetimeParts[1].match(dateRegEx);
|
|
79
|
-
if (!dateParts) return undefined;
|
|
80
|
-
const parsedDateTime: ParsedDateTime = {
|
|
81
|
-
date: { year: dateParts[1], month: dateParts[2], day: dateParts[3] },
|
|
82
|
-
};
|
|
83
|
-
|
|
84
|
-
const timeParts = isoDatetimeParts[3] && isoDatetimeParts[3].match(timeRegEx);
|
|
85
|
-
if (timeParts) {
|
|
86
|
-
parsedDateTime.time = {
|
|
87
|
-
hours: timeParts[1],
|
|
88
|
-
minutes: timeParts[2],
|
|
89
|
-
seconds: timeParts[3],
|
|
90
|
-
milliseconds: timeParts[5],
|
|
91
|
-
};
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
const tzParts = isoDatetimeParts[4] && isoDatetimeParts[4].match(timezoneRegEx);
|
|
95
|
-
if (tzParts) {
|
|
96
|
-
parsedDateTime.timezone = tzParts[0] as ISOTimezone;
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
return parsedDateTime;
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
return undefined;
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
static getISOString(parts?: ParsedDateTime) {
|
|
106
|
-
if (!parts) return 'Invalid Date';
|
|
107
|
-
return `${parts.date.year}-${parts.date.month ?? '01'}-${parts.date.day ?? '01'}T${parts.time?.hours ?? '00'}:${parts.time?.minutes ?? '00'}:${parts.time?.seconds ?? '00'}.${parts.time?.milliseconds ?? '000'}${parts.timezone ?? 'Z'}`;
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
/**
|
|
111
|
-
* Retruns ISO date and time string
|
|
112
|
-
*/
|
|
113
|
-
static toString(parsedDateTime: string): string;
|
|
114
|
-
static toString(parsedDateTime: ISODateTime): string;
|
|
115
|
-
static toString(dateTime: ISODateTime | string) {
|
|
116
|
-
const _p = typeof dateTime === 'string' ? ISODateTime.parse(dateTime) : dateTime.parts;
|
|
117
|
-
|
|
118
|
-
return ISODateTime.getISOString(_p);
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
/**
|
|
122
|
-
* Returns the stored time value in milliseconds since midnight, January 1, 1970 UTC.
|
|
123
|
-
*/
|
|
124
|
-
valueOf() {
|
|
125
|
-
if (this.#date === InvalidDate) return Number.NaN;
|
|
126
|
-
|
|
127
|
-
return this.#date.valueOf();
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
/**
|
|
131
|
-
* Retruns ISO date and time string
|
|
132
|
-
*/
|
|
133
|
-
toString() {
|
|
134
|
-
return this.#string;
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
/**
|
|
138
|
-
* Retruns Javascript date object
|
|
139
|
-
*/
|
|
140
|
-
toDate() {
|
|
141
|
-
return this.#date;
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
get [Symbol.toStringTag]() {
|
|
145
|
-
return 'ISODateTime';
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
[Symbol.toPrimitive](hint: 'default'): number;
|
|
149
|
-
[Symbol.toPrimitive](hint: 'string'): string;
|
|
150
|
-
[Symbol.toPrimitive](hint: 'number'): number;
|
|
151
|
-
[Symbol.toPrimitive](hint: string) {
|
|
152
|
-
if (hint === 'string') return this.toString();
|
|
153
|
-
|
|
154
|
-
return this.valueOf();
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
toJSON() {
|
|
158
|
-
return this.toString();
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
private static getFormatter(isoDatetime: ISODateTime, config?: DateFormatterConfig) {
|
|
162
|
-
let _format: Partial<Intl.DateTimeFormatOptions> = {};
|
|
163
|
-
|
|
164
|
-
if (config?.customFormat?.dateStyle || config?.customFormat?.timeStyle) {
|
|
165
|
-
_format = {
|
|
166
|
-
dateStyle: config?.customFormat?.dateStyle,
|
|
167
|
-
timeStyle: config?.customFormat?.timeStyle,
|
|
168
|
-
};
|
|
169
|
-
} else {
|
|
170
|
-
if (config?.noDate === true && config?.noTime === true) {
|
|
171
|
-
_format = { ...config.customFormat };
|
|
172
|
-
} else if (config?.noDate === true && !config?.noTime) {
|
|
173
|
-
_format = { ...DefaultTimeFormat, ...config.customFormat };
|
|
174
|
-
} else if (config?.noTime === true && !config.noDate) {
|
|
175
|
-
_format = { ...DefaultDateFormat, ...config.customFormat };
|
|
176
|
-
} else {
|
|
177
|
-
_format = { ...DefaultDateFormat, ...DefaultTimeFormat, ...config?.customFormat };
|
|
178
|
-
}
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
if (config?.customFormat?.timeZone) _format.timeZone = config.customFormat.timeZone;
|
|
182
|
-
else if (isoDatetime.parts?.time === undefined) _format.timeZone = 'UTC';
|
|
183
|
-
else _format.timeZone = DefaultTimezone;
|
|
184
|
-
|
|
185
|
-
return new Intl.DateTimeFormat('en', _format);
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
/**
|
|
189
|
-
* Returns formated date and time string
|
|
190
|
-
*/
|
|
191
|
-
static format(isoDatetime: string, config?: DateFormatterConfig): string;
|
|
192
|
-
static format(isoDatetime: ISODateTime, config?: DateFormatterConfig): string;
|
|
193
|
-
static format(isoDatetime: ISODateTime | string, config?: DateFormatterConfig) {
|
|
194
|
-
const _isoDatetime = typeof isoDatetime === 'string' ? new ISODateTime(isoDatetime) : isoDatetime;
|
|
195
|
-
|
|
196
|
-
const _date = _isoDatetime.toDate();
|
|
197
|
-
if (_date === InvalidDate) return _isoDatetime.toString();
|
|
198
|
-
|
|
199
|
-
const formatter = ISODateTime.getFormatter(_isoDatetime, config);
|
|
200
|
-
return formatter.format(_date);
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
/**
|
|
204
|
-
* Returns formated date and time range string
|
|
205
|
-
*/
|
|
206
|
-
static formatRange(startDate: string, endDate: string, config?: DateFormatterConfig): string;
|
|
207
|
-
static formatRange(startDate: ISODateTime, endDate: ISODateTime, config?: DateFormatterConfig): string;
|
|
208
|
-
static formatRange(startDate: ISODateTime | string, endDate: ISODateTime | string, config?: DateFormatterConfig) {
|
|
209
|
-
const _startISODateTime = typeof startDate === 'string' ? new ISODateTime(startDate) : startDate;
|
|
210
|
-
|
|
211
|
-
const _startDate = _startISODateTime.toDate();
|
|
212
|
-
if (_startDate === InvalidDate) return _startISODateTime.toString();
|
|
213
|
-
|
|
214
|
-
const _endISODateTime = typeof endDate === 'string' ? new ISODateTime(endDate) : endDate;
|
|
215
|
-
const _endDate = _endISODateTime.toDate();
|
|
216
|
-
if (_endDate === InvalidDate) return _endISODateTime.toString();
|
|
217
|
-
|
|
218
|
-
if (_startISODateTime.timezone !== _endISODateTime.timezone) throw 'Start and end dates in different time zones';
|
|
219
|
-
|
|
220
|
-
const formatter = ISODateTime.getFormatter(_startISODateTime, config);
|
|
221
|
-
|
|
222
|
-
const formatedRange = formatter.formatRange(_startDate, _endDate).replace(/\s/g, ' ');
|
|
223
|
-
|
|
224
|
-
// normalize space usage,
|
|
225
|
-
// `formatRange` uses u+202f to separate the parts in some systems and the standard space in the others
|
|
226
|
-
return formatedRange.replace(/\s/g, ' ');
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
/**
|
|
230
|
-
* Returns formated date and time string
|
|
231
|
-
*/
|
|
232
|
-
format(config?: DateFormatterConfig) {
|
|
233
|
-
return ISODateTime.format(this, config);
|
|
234
|
-
}
|
|
235
|
-
|
|
236
|
-
/**
|
|
237
|
-
* Adds milliseconds to the date and returns it as a new ISODateTime object
|
|
238
|
-
*/
|
|
239
|
-
addMillisecond(milliseconds: number) {
|
|
240
|
-
return new ISODateTime(this.valueOf() + milliseconds);
|
|
241
|
-
}
|
|
242
|
-
/**
|
|
243
|
-
* Adds seconds to the date and returns it as a new ISODateTime object
|
|
244
|
-
*/
|
|
245
|
-
addSecond(seconds: number) {
|
|
246
|
-
return this.addMillisecond(seconds * 1000);
|
|
247
|
-
}
|
|
248
|
-
/**
|
|
249
|
-
* Adds minutes to the date and returns it as a new ISODateTime object
|
|
250
|
-
*/
|
|
251
|
-
addMinute(minutes: number) {
|
|
252
|
-
return this.addMillisecond(minutes * 60_000);
|
|
253
|
-
}
|
|
254
|
-
/**
|
|
255
|
-
* Adds hours to the date and returns it as a new ISODateTime object
|
|
256
|
-
*/
|
|
257
|
-
addHour(hours: number) {
|
|
258
|
-
return this.addMillisecond(hours * 3_600_000);
|
|
259
|
-
}
|
|
260
|
-
/**
|
|
261
|
-
* Adds days to the date and returns it as a new ISODateTime object
|
|
262
|
-
*/
|
|
263
|
-
addDay(days: number) {
|
|
264
|
-
return this.addMillisecond(days * 86_400_000);
|
|
265
|
-
}
|
|
266
|
-
|
|
267
|
-
/**
|
|
268
|
-
* Returns the start of the previous month as a new ISODateTime object
|
|
269
|
-
*/
|
|
270
|
-
static getPrevMonthStart(date: ISODateTime) {
|
|
271
|
-
if (date.parts) {
|
|
272
|
-
const month = date.parts.date.month ? Number(date.parts.date.month) : 1;
|
|
273
|
-
const prevMonth = month === 1 ? 12 : month - 1;
|
|
274
|
-
const year = month === 1 ? (Number(date.parts.date.year) - 1).toString() : date.parts.date.year;
|
|
275
|
-
const prevParts: ParsedDateTime = {
|
|
276
|
-
date: {
|
|
277
|
-
year: year,
|
|
278
|
-
month: prevMonth.toString().padStart(2, '0'),
|
|
279
|
-
day: '01',
|
|
280
|
-
},
|
|
281
|
-
timezone: date.parts.timezone,
|
|
282
|
-
};
|
|
283
|
-
|
|
284
|
-
return new ISODateTime(ISODateTime.getISOString(prevParts));
|
|
285
|
-
}
|
|
286
|
-
|
|
287
|
-
return date;
|
|
288
|
-
}
|
|
289
|
-
|
|
290
|
-
/**
|
|
291
|
-
* Returns the start of the previous month as a new ISODateTime object
|
|
292
|
-
*/
|
|
293
|
-
getPrevMonthStart() {
|
|
294
|
-
return ISODateTime.getPrevMonthStart(this);
|
|
295
|
-
}
|
|
296
|
-
|
|
297
|
-
/**
|
|
298
|
-
* Returns the start of the next month as a new ISODateTime object
|
|
299
|
-
*/
|
|
300
|
-
static getNextMonthStart(date: ISODateTime) {
|
|
301
|
-
if (date.parts) {
|
|
302
|
-
const month = date.parts.date.month ? Number(date.parts.date.month) : 1;
|
|
303
|
-
const nextMonth = (month % 12) + 1;
|
|
304
|
-
const year = month === 12 ? (Number(date.parts.date.year) + 1).toString() : date.parts.date.year;
|
|
305
|
-
|
|
306
|
-
const nextParts: ParsedDateTime = {
|
|
307
|
-
date: {
|
|
308
|
-
year: year,
|
|
309
|
-
month: nextMonth.toString().padStart(2, '0'),
|
|
310
|
-
day: '01',
|
|
311
|
-
},
|
|
312
|
-
timezone: date.parts.timezone,
|
|
313
|
-
};
|
|
314
|
-
|
|
315
|
-
return new ISODateTime(ISODateTime.getISOString(nextParts));
|
|
316
|
-
}
|
|
317
|
-
|
|
318
|
-
return date;
|
|
319
|
-
}
|
|
320
|
-
|
|
321
|
-
/**
|
|
322
|
-
* Returns the start of the next month as a new ISODateTime object
|
|
323
|
-
*/
|
|
324
|
-
getNextMonthStart() {
|
|
325
|
-
return ISODateTime.getNextMonthStart(this);
|
|
326
|
-
}
|
|
327
|
-
}
|
|
328
|
-
|
|
329
|
-
interface ISODateParts {
|
|
330
|
-
year: string;
|
|
331
|
-
month?: string;
|
|
332
|
-
day?: string;
|
|
333
|
-
}
|
|
334
|
-
|
|
335
|
-
interface ISOTimeParts {
|
|
336
|
-
hours: string;
|
|
337
|
-
minutes?: string;
|
|
338
|
-
seconds?: string;
|
|
339
|
-
milliseconds?: string;
|
|
340
|
-
}
|
|
341
|
-
|
|
342
|
-
type ISOTimezone = 'Z' | `${'+' | '-'}${string}`;
|
|
343
|
-
|
|
344
|
-
export interface ParsedDateTime {
|
|
345
|
-
date: ISODateParts;
|
|
346
|
-
time?: ISOTimeParts;
|
|
347
|
-
timezone?: ISOTimezone;
|
|
348
|
-
}
|
|
349
|
-
|
|
350
|
-
export type DateFormatOptions = Partial<Intl.DateTimeFormatOptions>;
|
|
351
|
-
|
|
352
|
-
export interface DateFormatterConfig {
|
|
353
|
-
noDate?: boolean;
|
|
354
|
-
noTime?: boolean;
|
|
355
|
-
customFormat?: DateFormatOptions;
|
|
356
|
-
}
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import { toNumber } from "./to-number";
|
|
2
|
-
|
|
3
|
-
export function addDuration(left: string, right: string) {
|
|
4
|
-
const totalDurationInSeconds =
|
|
5
|
-
toNumber(left, "second") + toNumber(right, "second");
|
|
6
|
-
|
|
7
|
-
const hours = Math.floor(totalDurationInSeconds / 3600)
|
|
8
|
-
.toString()
|
|
9
|
-
.padStart(2, "0");
|
|
10
|
-
const minutes = Math.floor((totalDurationInSeconds % 3600) / 60)
|
|
11
|
-
.toString()
|
|
12
|
-
.padStart(2, "0");
|
|
13
|
-
const seconds = (totalDurationInSeconds % 60).toString().padStart(2, "0");
|
|
14
|
-
|
|
15
|
-
return `${hours}:${minutes}:${seconds}`;
|
|
16
|
-
}
|
package/src/duration/index.ts
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import { isDuration } from "./is-duration";
|
|
2
|
-
|
|
3
|
-
export function toNumber(
|
|
4
|
-
duration: string,
|
|
5
|
-
unit: "hour" | "minute" | "second" = "hour",
|
|
6
|
-
) {
|
|
7
|
-
if (!isDuration(duration)) {
|
|
8
|
-
console.error(new Error(`Malformed duration value: ${duration}`));
|
|
9
|
-
return 0;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
const parts = duration.split(":").map((p) => Number(p));
|
|
13
|
-
|
|
14
|
-
if (unit === "minute") return parts[0] * 60 + parts[1] + parts[2] / 60;
|
|
15
|
-
if (unit === "second") return parts[0] * 3600 + parts[1] * 60 + parts[2];
|
|
16
|
-
|
|
17
|
-
return parts[0] + parts[1] / 60 + parts[2] / 3600;
|
|
18
|
-
}
|
package/src/format/formatter.ts
DELETED
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
export const dateFormatter = new Intl.DateTimeFormat("en", {
|
|
2
|
-
dateStyle: "full",
|
|
3
|
-
timeStyle: "short",
|
|
4
|
-
timeZone: "America/Vancouver",
|
|
5
|
-
});
|
|
6
|
-
export const formatDate = (date?: Date | null) => {
|
|
7
|
-
if (date) return dateFormatter.format(date);
|
|
8
|
-
return "";
|
|
9
|
-
};
|
|
10
|
-
|
|
11
|
-
const listFormatter = new Intl.ListFormat("en", {
|
|
12
|
-
style: "long",
|
|
13
|
-
type: "conjunction",
|
|
14
|
-
});
|
|
15
|
-
|
|
16
|
-
export const formatList = (list?: string[] | null) => {
|
|
17
|
-
if (Array.isArray(list)) return listFormatter.format(list);
|
|
18
|
-
return "";
|
|
19
|
-
};
|
|
20
|
-
|
|
21
|
-
const numberFormatter = new Intl.NumberFormat("en", {
|
|
22
|
-
maximumFractionDigits: 2,
|
|
23
|
-
});
|
|
24
|
-
export const formatNumber = (num?: number | null) => {
|
|
25
|
-
if (num) return numberFormatter.format(num);
|
|
26
|
-
return "";
|
|
27
|
-
};
|
|
28
|
-
|
|
29
|
-
const currencyFormatter = new Intl.NumberFormat("en", {
|
|
30
|
-
style: "currency",
|
|
31
|
-
currency: "CAD",
|
|
32
|
-
maximumFractionDigits: 2,
|
|
33
|
-
});
|
|
34
|
-
export const formatCurrency = (num?: number | null) => {
|
|
35
|
-
if (num) return currencyFormatter.format(num);
|
|
36
|
-
return "";
|
|
37
|
-
};
|
package/src/format/index.ts
DELETED
package/src/format/stringify.ts
DELETED
|
@@ -1,87 +0,0 @@
|
|
|
1
|
-
import { dateFormatter, formatList, formatNumber } from "./formatter";
|
|
2
|
-
import { ISODateTime } from "../date/iso-date-time";
|
|
3
|
-
|
|
4
|
-
export interface StringifyValueOptions {
|
|
5
|
-
/**
|
|
6
|
-
* String value to return when the value undefined or null.
|
|
7
|
-
*
|
|
8
|
-
* Default Unicode NUL(\u0000) character,
|
|
9
|
-
* guarantees that the function always returns a string and
|
|
10
|
-
* allows to distinguish invalid values from an empty string.
|
|
11
|
-
*
|
|
12
|
-
* ```ts
|
|
13
|
-
* const stringValue = stringify(value);
|
|
14
|
-
* console.log(stringValue !== '\u0000' ? 'Invalid' : stringValue)
|
|
15
|
-
* ```
|
|
16
|
-
*/
|
|
17
|
-
invalidValueString?: string;
|
|
18
|
-
/**
|
|
19
|
-
* `stringify` prepends type info to the return value.
|
|
20
|
-
*/
|
|
21
|
-
withTypeInfo?: boolean;
|
|
22
|
-
/**
|
|
23
|
-
* If the value is a function that doesn't expect an argument,
|
|
24
|
-
* `stringify` calls the function recursively.
|
|
25
|
-
*/
|
|
26
|
-
callFunctions?: boolean;
|
|
27
|
-
/**
|
|
28
|
-
* `stringify` adds indentation and new lines to object return values.
|
|
29
|
-
*/
|
|
30
|
-
prettyObjects?: boolean;
|
|
31
|
-
/**
|
|
32
|
-
* `stringify` returns verbose value.
|
|
33
|
-
*/
|
|
34
|
-
verbose?: boolean;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
/**
|
|
38
|
-
* Returns string representation of the value;
|
|
39
|
-
*
|
|
40
|
-
* If the valid is null or undefined, returns `options.invalidValueString`.
|
|
41
|
-
*/
|
|
42
|
-
export const stringify = (
|
|
43
|
-
value: unknown,
|
|
44
|
-
options?: StringifyValueOptions,
|
|
45
|
-
): string => {
|
|
46
|
-
const _options = {
|
|
47
|
-
invalidValueString: "\u0000", // Unicode NUL
|
|
48
|
-
withTypeInfo: options?.verbose,
|
|
49
|
-
prettyObjects: options?.verbose,
|
|
50
|
-
...options,
|
|
51
|
-
};
|
|
52
|
-
|
|
53
|
-
if (value === undefined)
|
|
54
|
-
return _options.withTypeInfo ? "[undefined]" : _options.invalidValueString;
|
|
55
|
-
if (value === null)
|
|
56
|
-
return _options.withTypeInfo ? "[null]" : _options.invalidValueString;
|
|
57
|
-
|
|
58
|
-
if (typeof value === "string")
|
|
59
|
-
return `${_options.withTypeInfo ? "[string] " : ""}${value}`;
|
|
60
|
-
if (typeof value === "number")
|
|
61
|
-
return `${_options.withTypeInfo ? "[number] " : ""}${formatNumber(value)}`;
|
|
62
|
-
if (typeof value === "bigint")
|
|
63
|
-
return `${_options.withTypeInfo ? "[bigint] " : ""}${value}`;
|
|
64
|
-
if (typeof value === "boolean")
|
|
65
|
-
return `${_options.withTypeInfo ? "[boolean] " : ""}${value}`;
|
|
66
|
-
if (typeof value === "object") {
|
|
67
|
-
if (Array.isArray(value)) {
|
|
68
|
-
const stringfiedValue = value.map((el) => stringify(el, _options));
|
|
69
|
-
return `${_options.withTypeInfo ? "[array] " : ""}${_options.verbose ? stringfiedValue.join(", ") : formatList(stringfiedValue)}`;
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
if (value instanceof Date)
|
|
73
|
-
return `${_options.withTypeInfo ? "[date] " : ""}${dateFormatter.format(value)}`;
|
|
74
|
-
if (value instanceof ISODateTime)
|
|
75
|
-
return `${_options.withTypeInfo ? "[date] " : ""}${value.format()}`;
|
|
76
|
-
|
|
77
|
-
return `${_options.withTypeInfo ? "[object] " : ""}${JSON.stringify(value, null, _options.prettyObjects ? 2 : undefined)}`;
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
if (typeof value === "function")
|
|
81
|
-
return `${_options.withTypeInfo ? "[function] " : ""}${_options.callFunctions && value.length === 0 ? stringify(value(), _options) : value.toString()}`;
|
|
82
|
-
|
|
83
|
-
if (typeof value === "symbol")
|
|
84
|
-
return `${_options.withTypeInfo ? "[symbol] " : ""}${value.toString()}`;
|
|
85
|
-
|
|
86
|
-
return _options.invalidValueString;
|
|
87
|
-
};
|
package/src/format/template.ts
DELETED
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
import { get } from "../object/get";
|
|
2
|
-
import { stringify } from "./stringify";
|
|
3
|
-
|
|
4
|
-
type TemplateVarValue = unknown | undefined | null;
|
|
5
|
-
export type TemplateVar = TemplateVarValue | (() => TemplateVarValue);
|
|
6
|
-
export type TemplateVars = Record<string, TemplateVar>;
|
|
7
|
-
|
|
8
|
-
const tagRegExp = new RegExp(/\${([a-zA-Z0-9_.]+)}/gm);
|
|
9
|
-
|
|
10
|
-
export const template = (text: string, templateVars: TemplateVars) => {
|
|
11
|
-
return text.replace(tagRegExp, (match, key) => {
|
|
12
|
-
const templateVar = get(templateVars, key) as TemplateVar;
|
|
13
|
-
const value =
|
|
14
|
-
typeof templateVar === "function" ? templateVar() : templateVar;
|
|
15
|
-
|
|
16
|
-
if (value === undefined) return match;
|
|
17
|
-
if (value === null) return "";
|
|
18
|
-
|
|
19
|
-
return stringify(value, { invalidValueString: "" });
|
|
20
|
-
});
|
|
21
|
-
};
|
|
22
|
-
|
|
23
|
-
export const templateObject = <T extends object>(
|
|
24
|
-
obj: T,
|
|
25
|
-
templateVars: TemplateVars,
|
|
26
|
-
) => {
|
|
27
|
-
const _objClone: Record<string, unknown> = {};
|
|
28
|
-
|
|
29
|
-
for (const [key, value] of Object.entries(obj)) {
|
|
30
|
-
const _key = template(key, templateVars);
|
|
31
|
-
|
|
32
|
-
if (typeof value === "string") {
|
|
33
|
-
_objClone[_key] = template(value, templateVars);
|
|
34
|
-
} else if (value && typeof value === "object") {
|
|
35
|
-
if (Array.isArray(value))
|
|
36
|
-
_objClone[_key] = value.map((valueItem) =>
|
|
37
|
-
typeof valueItem === "string"
|
|
38
|
-
? template(valueItem, templateVars)
|
|
39
|
-
: valueItem,
|
|
40
|
-
);
|
|
41
|
-
else _objClone[_key] = templateObject(value, templateVars);
|
|
42
|
-
} else {
|
|
43
|
-
_objClone[_key] = value;
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
return _objClone as T;
|
|
48
|
-
};
|
package/src/index.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export const USE_SUBPATH_EXPORTS = true;
|
package/src/object/get.ts
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
2
|
-
export const get = <T extends object>(object: T, prop: string) =>
|
|
3
|
-
prop
|
|
4
|
-
.split(".")
|
|
5
|
-
.reduce<any>(
|
|
6
|
-
(reducedObject, key) =>
|
|
7
|
-
reducedObject && key in reducedObject ? reducedObject[key] : undefined,
|
|
8
|
-
object,
|
|
9
|
-
);
|