@bccampus/utils 0.1.1 → 0.2.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/dist/array/chunk.cjs +9 -0
- package/dist/array/index.cjs +4 -0
- package/dist/date/index.cjs +8 -0
- package/dist/date/iso-date-time.cjs +256 -0
- package/dist/duration/add-durations.cjs +12 -0
- package/dist/duration/index.cjs +8 -0
- package/dist/duration/is-duration.cjs +7 -0
- package/dist/duration/to-number.cjs +14 -0
- package/dist/format/formatter.cjs +40 -0
- package/dist/format/index.cjs +13 -0
- package/dist/format/stringify.cjs +42 -0
- package/dist/format/template.cjs +35 -0
- package/dist/object/get.cjs +7 -0
- package/dist/object/index.cjs +12 -0
- package/dist/object/is-object.cjs +4 -0
- package/dist/object/omit.cjs +18 -0
- package/dist/object/pick.cjs +11 -0
- package/dist/object/set.cjs +14 -0
- package/dist/path/file-url-to-path.cjs +10 -0
- package/dist/path/file-url-to-path.d.ts +4 -0
- package/dist/path/file-url-to-path.js +10 -0
- package/dist/path/index.cjs +6 -0
- package/dist/path/index.d.ts +2 -0
- package/dist/path/index.js +6 -0
- package/dist/path/normalize.cjs +32 -0
- package/dist/path/normalize.d.ts +31 -0
- package/dist/path/normalize.js +32 -0
- package/dist/set/difference.cjs +10 -0
- package/dist/set/index.cjs +12 -0
- package/dist/set/intersection.cjs +13 -0
- package/dist/set/is-superset.cjs +12 -0
- package/dist/set/symmetric-difference.cjs +14 -0
- package/dist/set/union.cjs +10 -0
- package/dist/string/capitalize.cjs +4 -0
- package/dist/string/index.cjs +14 -0
- package/dist/string/normalize.cjs +6 -0
- package/dist/string/normalize.d.ts +5 -0
- package/dist/string/normalize.js +6 -0
- package/dist/string/pluralize/index.cjs +6 -0
- package/dist/string/pluralize/pluralize.cjs +210 -0
- package/dist/string/pluralize/rules/index.cjs +10 -0
- package/dist/string/pluralize/rules/irregular.cjs +62 -0
- package/dist/string/pluralize/rules/pluralization.cjs +39 -0
- package/dist/string/pluralize/rules/singularization.cjs +47 -0
- package/dist/string/pluralize/rules/uncountable.cjs +113 -0
- package/dist/string/pluralize/types.cjs +1 -0
- package/dist/string/slugify/index.cjs +2263 -0
- package/dist/string/strip-html-tags.cjs +7 -0
- package/dist/string/to-int.cjs +10 -0
- package/dist/string/to-sentence-case.cjs +8 -0
- package/dist/string/trim.cjs +11 -0
- package/dist/string/truncate.cjs +20 -0
- package/dist/string/truncate.d.ts +22 -1
- package/dist/string/truncate.js +14 -3
- package/dist/value/index.cjs +4 -0
- package/dist/value/index.d.ts +1 -0
- package/dist/value/index.js +4 -0
- package/dist/value/is-empty.cjs +14 -0
- package/dist/value/is-empty.d.ts +17 -0
- package/dist/value/is-empty.js +14 -0
- package/package.json +29 -10
- package/src/format/index.ts +3 -3
- package/src/path/file-url-to-path.ts +12 -0
- package/src/path/index.ts +2 -0
- package/src/path/normalize.ts +72 -0
- package/src/string/normalize.ts +7 -0
- package/src/string/truncate.ts +44 -11
- package/src/value/index.ts +1 -0
- package/src/value/is-empty.ts +31 -0
- package/tsconfig.lib.json +36 -35
- package/vite.config.ts +75 -77
- package/tsconfig.lib.tsbuildinfo +0 -1
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
function chunk(arr, size = 5) {
|
|
4
|
+
const chunks = [];
|
|
5
|
+
for (let i = 0; i < arr.length; i += size)
|
|
6
|
+
chunks.push(arr.slice(i, i + size));
|
|
7
|
+
return chunks;
|
|
8
|
+
}
|
|
9
|
+
exports.chunk = chunk;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
const date_isoDateTime = require("./iso-date-time.cjs");
|
|
4
|
+
exports.DefaultDateFormat = date_isoDateTime.DefaultDateFormat;
|
|
5
|
+
exports.DefaultTimeFormat = date_isoDateTime.DefaultTimeFormat;
|
|
6
|
+
exports.DefaultTimezone = date_isoDateTime.DefaultTimezone;
|
|
7
|
+
exports.ISODateTime = date_isoDateTime.ISODateTime;
|
|
8
|
+
exports.InvalidDate = date_isoDateTime.InvalidDate;
|
|
@@ -0,0 +1,256 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
const isoDateTimeRegEx = new RegExp("^([0-9-]+)(T([0-9:.,]+)(Z|[+-]([0-9:]+))?)?$");
|
|
4
|
+
const dateRegEx = new RegExp("^(\\d{4})-?(\\d{2})?-?(\\d{2})?$");
|
|
5
|
+
const timeRegEx = new RegExp("^(\\d{2}):?(\\d{2})?:?(\\d{2})?(\\.(\\d{3}))?$");
|
|
6
|
+
const timezoneRegEx = new RegExp("^Z|^[+-]((\\d{2}):?(\\d{2})?)$");
|
|
7
|
+
const InvalidDate = /* @__PURE__ */ Symbol("Invalid Date");
|
|
8
|
+
const DefaultDateFormat = {
|
|
9
|
+
year: "numeric",
|
|
10
|
+
month: "long",
|
|
11
|
+
day: "2-digit"
|
|
12
|
+
};
|
|
13
|
+
const DefaultTimeFormat = {
|
|
14
|
+
hour: "2-digit",
|
|
15
|
+
minute: "2-digit",
|
|
16
|
+
timeZoneName: "short"
|
|
17
|
+
};
|
|
18
|
+
const DefaultTimezone = "America/Vancouver";
|
|
19
|
+
class ISODateTime {
|
|
20
|
+
#parsed;
|
|
21
|
+
#date;
|
|
22
|
+
#string;
|
|
23
|
+
constructor(isoDatetime) {
|
|
24
|
+
if (typeof isoDatetime === "string") {
|
|
25
|
+
this.#parsed = ISODateTime.parse(isoDatetime);
|
|
26
|
+
if (this.#parsed) {
|
|
27
|
+
this.#date = new Date(ISODateTime.toString(this));
|
|
28
|
+
} else {
|
|
29
|
+
this.#date = InvalidDate;
|
|
30
|
+
console.error(`Failed to parse date for value: ${isoDatetime}`);
|
|
31
|
+
}
|
|
32
|
+
} else if (typeof isoDatetime === "number" && Number.isFinite(isoDatetime)) {
|
|
33
|
+
this.#date = new Date(isoDatetime);
|
|
34
|
+
this.#parsed = ISODateTime.parse(this.#date.toISOString());
|
|
35
|
+
} else {
|
|
36
|
+
this.#date = /* @__PURE__ */ new Date();
|
|
37
|
+
this.#parsed = ISODateTime.parse(this.#date.toISOString());
|
|
38
|
+
}
|
|
39
|
+
if (this.#date !== InvalidDate) this.#string = this.#date.toISOString();
|
|
40
|
+
else this.#string = "Invalid Date";
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Get date and time components
|
|
44
|
+
*/
|
|
45
|
+
get parts() {
|
|
46
|
+
return this.#parsed;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Get timezone
|
|
50
|
+
*/
|
|
51
|
+
get timezone() {
|
|
52
|
+
return this.parts?.timezone ?? "UTC";
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Parse a string in ISO DateTime format.
|
|
56
|
+
* If the input is valid, returns date and time components;
|
|
57
|
+
* otherwise, returns `undefined`.
|
|
58
|
+
*/
|
|
59
|
+
static parse(isoDatetime) {
|
|
60
|
+
const isoDatetimeParts = isoDatetime.match(isoDateTimeRegEx);
|
|
61
|
+
if (isoDatetimeParts) {
|
|
62
|
+
const dateParts = isoDatetimeParts[1] && isoDatetimeParts[1].match(dateRegEx);
|
|
63
|
+
if (!dateParts) return void 0;
|
|
64
|
+
const parsedDateTime = {
|
|
65
|
+
date: { year: dateParts[1], month: dateParts[2], day: dateParts[3] }
|
|
66
|
+
};
|
|
67
|
+
const timeParts = isoDatetimeParts[3] && isoDatetimeParts[3].match(timeRegEx);
|
|
68
|
+
if (timeParts) {
|
|
69
|
+
parsedDateTime.time = {
|
|
70
|
+
hours: timeParts[1],
|
|
71
|
+
minutes: timeParts[2],
|
|
72
|
+
seconds: timeParts[3],
|
|
73
|
+
milliseconds: timeParts[5]
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
const tzParts = isoDatetimeParts[4] && isoDatetimeParts[4].match(timezoneRegEx);
|
|
77
|
+
if (tzParts) {
|
|
78
|
+
parsedDateTime.timezone = tzParts[0];
|
|
79
|
+
}
|
|
80
|
+
return parsedDateTime;
|
|
81
|
+
}
|
|
82
|
+
return void 0;
|
|
83
|
+
}
|
|
84
|
+
static getISOString(parts) {
|
|
85
|
+
if (!parts) return "Invalid Date";
|
|
86
|
+
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"}`;
|
|
87
|
+
}
|
|
88
|
+
static toString(dateTime) {
|
|
89
|
+
const _p = typeof dateTime === "string" ? ISODateTime.parse(dateTime) : dateTime.parts;
|
|
90
|
+
return ISODateTime.getISOString(_p);
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Returns the stored time value in milliseconds since midnight, January 1, 1970 UTC.
|
|
94
|
+
*/
|
|
95
|
+
valueOf() {
|
|
96
|
+
if (this.#date === InvalidDate) return Number.NaN;
|
|
97
|
+
return this.#date.valueOf();
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Retruns ISO date and time string
|
|
101
|
+
*/
|
|
102
|
+
toString() {
|
|
103
|
+
return this.#string;
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Retruns Javascript date object
|
|
107
|
+
*/
|
|
108
|
+
toDate() {
|
|
109
|
+
return this.#date;
|
|
110
|
+
}
|
|
111
|
+
get [Symbol.toStringTag]() {
|
|
112
|
+
return "ISODateTime";
|
|
113
|
+
}
|
|
114
|
+
[Symbol.toPrimitive](hint) {
|
|
115
|
+
if (hint === "string") return this.toString();
|
|
116
|
+
return this.valueOf();
|
|
117
|
+
}
|
|
118
|
+
toJSON() {
|
|
119
|
+
return this.toString();
|
|
120
|
+
}
|
|
121
|
+
static getFormatter(isoDatetime, config) {
|
|
122
|
+
let _format = {};
|
|
123
|
+
if (config?.customFormat?.dateStyle || config?.customFormat?.timeStyle) {
|
|
124
|
+
_format = {
|
|
125
|
+
dateStyle: config?.customFormat?.dateStyle,
|
|
126
|
+
timeStyle: config?.customFormat?.timeStyle
|
|
127
|
+
};
|
|
128
|
+
} else {
|
|
129
|
+
if (config?.noDate === true && config?.noTime === true) {
|
|
130
|
+
_format = { ...config.customFormat };
|
|
131
|
+
} else if (config?.noDate === true && !config?.noTime) {
|
|
132
|
+
_format = { ...DefaultTimeFormat, ...config.customFormat };
|
|
133
|
+
} else if (config?.noTime === true && !config.noDate) {
|
|
134
|
+
_format = { ...DefaultDateFormat, ...config.customFormat };
|
|
135
|
+
} else {
|
|
136
|
+
_format = { ...DefaultDateFormat, ...DefaultTimeFormat, ...config?.customFormat };
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
if (config?.customFormat?.timeZone) _format.timeZone = config.customFormat.timeZone;
|
|
140
|
+
else if (isoDatetime.parts?.time === void 0) _format.timeZone = "UTC";
|
|
141
|
+
else _format.timeZone = DefaultTimezone;
|
|
142
|
+
return new Intl.DateTimeFormat("en", _format);
|
|
143
|
+
}
|
|
144
|
+
static format(isoDatetime, config) {
|
|
145
|
+
const _isoDatetime = typeof isoDatetime === "string" ? new ISODateTime(isoDatetime) : isoDatetime;
|
|
146
|
+
const _date = _isoDatetime.toDate();
|
|
147
|
+
if (_date === InvalidDate) return _isoDatetime.toString();
|
|
148
|
+
const formatter = ISODateTime.getFormatter(_isoDatetime, config);
|
|
149
|
+
return formatter.format(_date);
|
|
150
|
+
}
|
|
151
|
+
static formatRange(startDate, endDate, config) {
|
|
152
|
+
const _startISODateTime = typeof startDate === "string" ? new ISODateTime(startDate) : startDate;
|
|
153
|
+
const _startDate = _startISODateTime.toDate();
|
|
154
|
+
if (_startDate === InvalidDate) return _startISODateTime.toString();
|
|
155
|
+
const _endISODateTime = typeof endDate === "string" ? new ISODateTime(endDate) : endDate;
|
|
156
|
+
const _endDate = _endISODateTime.toDate();
|
|
157
|
+
if (_endDate === InvalidDate) return _endISODateTime.toString();
|
|
158
|
+
if (_startISODateTime.timezone !== _endISODateTime.timezone) throw "Start and end dates in different time zones";
|
|
159
|
+
const formatter = ISODateTime.getFormatter(_startISODateTime, config);
|
|
160
|
+
const formatedRange = formatter.formatRange(_startDate, _endDate).replace(/\s/g, " ");
|
|
161
|
+
return formatedRange.replace(/\s/g, " ");
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* Returns formated date and time string
|
|
165
|
+
*/
|
|
166
|
+
format(config) {
|
|
167
|
+
return ISODateTime.format(this, config);
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* Adds milliseconds to the date and returns it as a new ISODateTime object
|
|
171
|
+
*/
|
|
172
|
+
addMillisecond(milliseconds) {
|
|
173
|
+
return new ISODateTime(this.valueOf() + milliseconds);
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* Adds seconds to the date and returns it as a new ISODateTime object
|
|
177
|
+
*/
|
|
178
|
+
addSecond(seconds) {
|
|
179
|
+
return this.addMillisecond(seconds * 1e3);
|
|
180
|
+
}
|
|
181
|
+
/**
|
|
182
|
+
* Adds minutes to the date and returns it as a new ISODateTime object
|
|
183
|
+
*/
|
|
184
|
+
addMinute(minutes) {
|
|
185
|
+
return this.addMillisecond(minutes * 6e4);
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
* Adds hours to the date and returns it as a new ISODateTime object
|
|
189
|
+
*/
|
|
190
|
+
addHour(hours) {
|
|
191
|
+
return this.addMillisecond(hours * 36e5);
|
|
192
|
+
}
|
|
193
|
+
/**
|
|
194
|
+
* Adds days to the date and returns it as a new ISODateTime object
|
|
195
|
+
*/
|
|
196
|
+
addDay(days) {
|
|
197
|
+
return this.addMillisecond(days * 864e5);
|
|
198
|
+
}
|
|
199
|
+
/**
|
|
200
|
+
* Returns the start of the previous month as a new ISODateTime object
|
|
201
|
+
*/
|
|
202
|
+
static getPrevMonthStart(date) {
|
|
203
|
+
if (date.parts) {
|
|
204
|
+
const month = date.parts.date.month ? Number(date.parts.date.month) : 1;
|
|
205
|
+
const prevMonth = month === 1 ? 12 : month - 1;
|
|
206
|
+
const year = month === 1 ? (Number(date.parts.date.year) - 1).toString() : date.parts.date.year;
|
|
207
|
+
const prevParts = {
|
|
208
|
+
date: {
|
|
209
|
+
year,
|
|
210
|
+
month: prevMonth.toString().padStart(2, "0"),
|
|
211
|
+
day: "01"
|
|
212
|
+
},
|
|
213
|
+
timezone: date.parts.timezone
|
|
214
|
+
};
|
|
215
|
+
return new ISODateTime(ISODateTime.getISOString(prevParts));
|
|
216
|
+
}
|
|
217
|
+
return date;
|
|
218
|
+
}
|
|
219
|
+
/**
|
|
220
|
+
* Returns the start of the previous month as a new ISODateTime object
|
|
221
|
+
*/
|
|
222
|
+
getPrevMonthStart() {
|
|
223
|
+
return ISODateTime.getPrevMonthStart(this);
|
|
224
|
+
}
|
|
225
|
+
/**
|
|
226
|
+
* Returns the start of the next month as a new ISODateTime object
|
|
227
|
+
*/
|
|
228
|
+
static getNextMonthStart(date) {
|
|
229
|
+
if (date.parts) {
|
|
230
|
+
const month = date.parts.date.month ? Number(date.parts.date.month) : 1;
|
|
231
|
+
const nextMonth = month % 12 + 1;
|
|
232
|
+
const year = month === 12 ? (Number(date.parts.date.year) + 1).toString() : date.parts.date.year;
|
|
233
|
+
const nextParts = {
|
|
234
|
+
date: {
|
|
235
|
+
year,
|
|
236
|
+
month: nextMonth.toString().padStart(2, "0"),
|
|
237
|
+
day: "01"
|
|
238
|
+
},
|
|
239
|
+
timezone: date.parts.timezone
|
|
240
|
+
};
|
|
241
|
+
return new ISODateTime(ISODateTime.getISOString(nextParts));
|
|
242
|
+
}
|
|
243
|
+
return date;
|
|
244
|
+
}
|
|
245
|
+
/**
|
|
246
|
+
* Returns the start of the next month as a new ISODateTime object
|
|
247
|
+
*/
|
|
248
|
+
getNextMonthStart() {
|
|
249
|
+
return ISODateTime.getNextMonthStart(this);
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
exports.DefaultDateFormat = DefaultDateFormat;
|
|
253
|
+
exports.DefaultTimeFormat = DefaultTimeFormat;
|
|
254
|
+
exports.DefaultTimezone = DefaultTimezone;
|
|
255
|
+
exports.ISODateTime = ISODateTime;
|
|
256
|
+
exports.InvalidDate = InvalidDate;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
require("./is-duration.cjs");
|
|
4
|
+
const duration_toNumber = require("./to-number.cjs");
|
|
5
|
+
function addDuration(left, right) {
|
|
6
|
+
const totalDurationInSeconds = duration_toNumber.toNumber(left, "second") + duration_toNumber.toNumber(right, "second");
|
|
7
|
+
const hours = Math.floor(totalDurationInSeconds / 3600).toString().padStart(2, "0");
|
|
8
|
+
const minutes = Math.floor(totalDurationInSeconds % 3600 / 60).toString().padStart(2, "0");
|
|
9
|
+
const seconds = (totalDurationInSeconds % 60).toString().padStart(2, "0");
|
|
10
|
+
return `${hours}:${minutes}:${seconds}`;
|
|
11
|
+
}
|
|
12
|
+
exports.addDuration = addDuration;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
const duration_isDuration = require("./is-duration.cjs");
|
|
4
|
+
const duration_toNumber = require("./to-number.cjs");
|
|
5
|
+
const duration_addDurations = require("./add-durations.cjs");
|
|
6
|
+
exports.isDuration = duration_isDuration.isDuration;
|
|
7
|
+
exports.toNumber = duration_toNumber.toNumber;
|
|
8
|
+
exports.addDuration = duration_addDurations.addDuration;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
const duration_isDuration = require("./is-duration.cjs");
|
|
4
|
+
function toNumber(duration, unit = "hour") {
|
|
5
|
+
if (!duration_isDuration.isDuration(duration)) {
|
|
6
|
+
console.error(new Error(`Malformed duration value: ${duration}`));
|
|
7
|
+
return 0;
|
|
8
|
+
}
|
|
9
|
+
const parts = duration.split(":").map((p) => Number(p));
|
|
10
|
+
if (unit === "minute") return parts[0] * 60 + parts[1] + parts[2] / 60;
|
|
11
|
+
if (unit === "second") return parts[0] * 3600 + parts[1] * 60 + parts[2];
|
|
12
|
+
return parts[0] + parts[1] / 60 + parts[2] / 3600;
|
|
13
|
+
}
|
|
14
|
+
exports.toNumber = toNumber;
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
const dateFormatter = new Intl.DateTimeFormat("en", {
|
|
4
|
+
dateStyle: "full",
|
|
5
|
+
timeStyle: "short",
|
|
6
|
+
timeZone: "America/Vancouver"
|
|
7
|
+
});
|
|
8
|
+
const formatDate = (date) => {
|
|
9
|
+
if (date) return dateFormatter.format(date);
|
|
10
|
+
return "";
|
|
11
|
+
};
|
|
12
|
+
const listFormatter = new Intl.ListFormat("en", {
|
|
13
|
+
style: "long",
|
|
14
|
+
type: "conjunction"
|
|
15
|
+
});
|
|
16
|
+
const formatList = (list) => {
|
|
17
|
+
if (Array.isArray(list)) return listFormatter.format(list);
|
|
18
|
+
return "";
|
|
19
|
+
};
|
|
20
|
+
const numberFormatter = new Intl.NumberFormat("en", {
|
|
21
|
+
maximumFractionDigits: 2
|
|
22
|
+
});
|
|
23
|
+
const formatNumber = (num) => {
|
|
24
|
+
if (num) return numberFormatter.format(num);
|
|
25
|
+
return "";
|
|
26
|
+
};
|
|
27
|
+
const currencyFormatter = new Intl.NumberFormat("en", {
|
|
28
|
+
style: "currency",
|
|
29
|
+
currency: "CAD",
|
|
30
|
+
maximumFractionDigits: 2
|
|
31
|
+
});
|
|
32
|
+
const formatCurrency = (num) => {
|
|
33
|
+
if (num) return currencyFormatter.format(num);
|
|
34
|
+
return "";
|
|
35
|
+
};
|
|
36
|
+
exports.dateFormatter = dateFormatter;
|
|
37
|
+
exports.formatCurrency = formatCurrency;
|
|
38
|
+
exports.formatDate = formatDate;
|
|
39
|
+
exports.formatList = formatList;
|
|
40
|
+
exports.formatNumber = formatNumber;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
const format_formatter = require("./formatter.cjs");
|
|
4
|
+
const format_stringify = require("./stringify.cjs");
|
|
5
|
+
const format_template = require("./template.cjs");
|
|
6
|
+
exports.dateFormatter = format_formatter.dateFormatter;
|
|
7
|
+
exports.formatCurrency = format_formatter.formatCurrency;
|
|
8
|
+
exports.formatDate = format_formatter.formatDate;
|
|
9
|
+
exports.formatList = format_formatter.formatList;
|
|
10
|
+
exports.formatNumber = format_formatter.formatNumber;
|
|
11
|
+
exports.stringify = format_stringify.stringify;
|
|
12
|
+
exports.template = format_template.template;
|
|
13
|
+
exports.templateObject = format_template.templateObject;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
const format_formatter = require("./formatter.cjs");
|
|
4
|
+
const date_isoDateTime = require("../date/iso-date-time.cjs");
|
|
5
|
+
const stringify = (value, options) => {
|
|
6
|
+
const _options = {
|
|
7
|
+
invalidValueString: "\0",
|
|
8
|
+
// Unicode NUL
|
|
9
|
+
withTypeInfo: options?.verbose,
|
|
10
|
+
prettyObjects: options?.verbose,
|
|
11
|
+
...options
|
|
12
|
+
};
|
|
13
|
+
if (value === void 0)
|
|
14
|
+
return _options.withTypeInfo ? "[undefined]" : _options.invalidValueString;
|
|
15
|
+
if (value === null)
|
|
16
|
+
return _options.withTypeInfo ? "[null]" : _options.invalidValueString;
|
|
17
|
+
if (typeof value === "string")
|
|
18
|
+
return `${_options.withTypeInfo ? "[string] " : ""}${value}`;
|
|
19
|
+
if (typeof value === "number")
|
|
20
|
+
return `${_options.withTypeInfo ? "[number] " : ""}${format_formatter.formatNumber(value)}`;
|
|
21
|
+
if (typeof value === "bigint")
|
|
22
|
+
return `${_options.withTypeInfo ? "[bigint] " : ""}${value}`;
|
|
23
|
+
if (typeof value === "boolean")
|
|
24
|
+
return `${_options.withTypeInfo ? "[boolean] " : ""}${value}`;
|
|
25
|
+
if (typeof value === "object") {
|
|
26
|
+
if (Array.isArray(value)) {
|
|
27
|
+
const stringfiedValue = value.map((el) => stringify(el, _options));
|
|
28
|
+
return `${_options.withTypeInfo ? "[array] " : ""}${_options.verbose ? stringfiedValue.join(", ") : format_formatter.formatList(stringfiedValue)}`;
|
|
29
|
+
}
|
|
30
|
+
if (value instanceof Date)
|
|
31
|
+
return `${_options.withTypeInfo ? "[date] " : ""}${format_formatter.dateFormatter.format(value)}`;
|
|
32
|
+
if (value instanceof date_isoDateTime.ISODateTime)
|
|
33
|
+
return `${_options.withTypeInfo ? "[date] " : ""}${value.format()}`;
|
|
34
|
+
return `${_options.withTypeInfo ? "[object] " : ""}${JSON.stringify(value, null, _options.prettyObjects ? 2 : void 0)}`;
|
|
35
|
+
}
|
|
36
|
+
if (typeof value === "function")
|
|
37
|
+
return `${_options.withTypeInfo ? "[function] " : ""}${_options.callFunctions && value.length === 0 ? stringify(value(), _options) : value.toString()}`;
|
|
38
|
+
if (typeof value === "symbol")
|
|
39
|
+
return `${_options.withTypeInfo ? "[symbol] " : ""}${value.toString()}`;
|
|
40
|
+
return _options.invalidValueString;
|
|
41
|
+
};
|
|
42
|
+
exports.stringify = stringify;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
require("./formatter.cjs");
|
|
4
|
+
const format_stringify = require("./stringify.cjs");
|
|
5
|
+
const object_get = require("../object/get.cjs");
|
|
6
|
+
const tagRegExp = new RegExp(/\${([a-zA-Z0-9_.]+)}/gm);
|
|
7
|
+
const template = (text, templateVars) => {
|
|
8
|
+
return text.replace(tagRegExp, (match, key) => {
|
|
9
|
+
const templateVar = object_get.get(templateVars, key);
|
|
10
|
+
const value = typeof templateVar === "function" ? templateVar() : templateVar;
|
|
11
|
+
if (value === void 0) return match;
|
|
12
|
+
if (value === null) return "";
|
|
13
|
+
return format_stringify.stringify(value, { invalidValueString: "" });
|
|
14
|
+
});
|
|
15
|
+
};
|
|
16
|
+
const templateObject = (obj, templateVars) => {
|
|
17
|
+
const _objClone = {};
|
|
18
|
+
for (const [key, value] of Object.entries(obj)) {
|
|
19
|
+
const _key = template(key, templateVars);
|
|
20
|
+
if (typeof value === "string") {
|
|
21
|
+
_objClone[_key] = template(value, templateVars);
|
|
22
|
+
} else if (value && typeof value === "object") {
|
|
23
|
+
if (Array.isArray(value))
|
|
24
|
+
_objClone[_key] = value.map(
|
|
25
|
+
(valueItem) => typeof valueItem === "string" ? template(valueItem, templateVars) : valueItem
|
|
26
|
+
);
|
|
27
|
+
else _objClone[_key] = templateObject(value, templateVars);
|
|
28
|
+
} else {
|
|
29
|
+
_objClone[_key] = value;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
return _objClone;
|
|
33
|
+
};
|
|
34
|
+
exports.template = template;
|
|
35
|
+
exports.templateObject = templateObject;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
const get = (object, prop) => prop.split(".").reduce(
|
|
4
|
+
(reducedObject, key) => reducedObject && key in reducedObject ? reducedObject[key] : void 0,
|
|
5
|
+
object
|
|
6
|
+
);
|
|
7
|
+
exports.get = get;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
const object_isObject = require("./is-object.cjs");
|
|
4
|
+
const object_get = require("./get.cjs");
|
|
5
|
+
const object_set = require("./set.cjs");
|
|
6
|
+
const object_omit = require("./omit.cjs");
|
|
7
|
+
const object_pick = require("./pick.cjs");
|
|
8
|
+
exports.isObject = object_isObject.isObject;
|
|
9
|
+
exports.get = object_get.get;
|
|
10
|
+
exports.set = object_set.set;
|
|
11
|
+
exports.omit = object_omit.omit;
|
|
12
|
+
exports.pick = object_pick.pick;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
const omit = (object, props) => {
|
|
4
|
+
const result = { ...object };
|
|
5
|
+
props.forEach((prop) => {
|
|
6
|
+
const propChunks = prop.split(".");
|
|
7
|
+
const lastChunk = propChunks.pop();
|
|
8
|
+
if (lastChunk) {
|
|
9
|
+
const ref = propChunks.reduce(
|
|
10
|
+
(reducedObject, key) => reducedObject && key in reducedObject ? reducedObject[key] : void 0,
|
|
11
|
+
result
|
|
12
|
+
);
|
|
13
|
+
if (ref && lastChunk in ref) delete ref[lastChunk];
|
|
14
|
+
}
|
|
15
|
+
});
|
|
16
|
+
return result;
|
|
17
|
+
};
|
|
18
|
+
exports.omit = omit;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
const object_get = require("./get.cjs");
|
|
4
|
+
const object_set = require("./set.cjs");
|
|
5
|
+
const pick = (object, props) => {
|
|
6
|
+
return props.reduce((result, key) => {
|
|
7
|
+
object_set.set(result, key, object_get.get(object, key));
|
|
8
|
+
return result;
|
|
9
|
+
}, {});
|
|
10
|
+
};
|
|
11
|
+
exports.pick = pick;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
const set = (object, prop, value) => {
|
|
4
|
+
const propChunks = prop.split(".");
|
|
5
|
+
const lastChunk = propChunks.pop();
|
|
6
|
+
if (!lastChunk) return object;
|
|
7
|
+
const ref = propChunks.reduce((reducedObject, key) => {
|
|
8
|
+
if (reducedObject[key] === void 0) reducedObject[key] = {};
|
|
9
|
+
return reducedObject[key];
|
|
10
|
+
}, object);
|
|
11
|
+
ref[lastChunk] = value;
|
|
12
|
+
return object;
|
|
13
|
+
};
|
|
14
|
+
exports.set = set;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
const windowsDriveRegExp = /^\/[a-zA-Z1-9_-]+:\//;
|
|
4
|
+
function fileURLToPath(fileUrl) {
|
|
5
|
+
const url = new URL(fileUrl);
|
|
6
|
+
let path = decodeURIComponent(url.pathname);
|
|
7
|
+
if (windowsDriveRegExp.test(path)) path = path.slice(1);
|
|
8
|
+
return path;
|
|
9
|
+
}
|
|
10
|
+
exports.fileURLToPath = fileURLToPath;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
const windowsDriveRegExp = /^\/[a-zA-Z1-9_-]+:\//;
|
|
2
|
+
function fileURLToPath(fileUrl) {
|
|
3
|
+
const url = new URL(fileUrl);
|
|
4
|
+
let path = decodeURIComponent(url.pathname);
|
|
5
|
+
if (windowsDriveRegExp.test(path)) path = path.slice(1);
|
|
6
|
+
return path;
|
|
7
|
+
}
|
|
8
|
+
export {
|
|
9
|
+
fileURLToPath
|
|
10
|
+
};
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
const path_normalize = require("./normalize.cjs");
|
|
4
|
+
const path_fileUrlToPath = require("./file-url-to-path.cjs");
|
|
5
|
+
exports.normalize = path_normalize.normalize;
|
|
6
|
+
exports.fileURLToPath = path_fileUrlToPath.fileURLToPath;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
const path_fileUrlToPath = require("./file-url-to-path.cjs");
|
|
4
|
+
const defaultOptions = {
|
|
5
|
+
leadingSlash: "always",
|
|
6
|
+
trailingSlash: "never"
|
|
7
|
+
};
|
|
8
|
+
const windowsPathSepRegExp = /[\\]+/g;
|
|
9
|
+
const unixPathSepRegExp = /[/]+/g;
|
|
10
|
+
const windowsDriveRegExp = /^[a-zA-Z1-9_-]+:\//;
|
|
11
|
+
const fileProtocolRegExp = /^file:\/\//;
|
|
12
|
+
const leadingSlashRegExp = /^\//g;
|
|
13
|
+
const trailingSlashRegExp = /\/$/g;
|
|
14
|
+
function normalize(path, options) {
|
|
15
|
+
const conf = { ...defaultOptions, ...options };
|
|
16
|
+
const sep = "\\";
|
|
17
|
+
let normalPath = path.trim();
|
|
18
|
+
if (fileProtocolRegExp.test(normalPath))
|
|
19
|
+
normalPath = path_fileUrlToPath.fileURLToPath(normalPath);
|
|
20
|
+
normalPath = normalPath.replace(windowsPathSepRegExp, sep).replace(unixPathSepRegExp, sep);
|
|
21
|
+
if (conf.leadingSlash !== "as-is") {
|
|
22
|
+
normalPath = normalPath.replace(leadingSlashRegExp, "");
|
|
23
|
+
if (conf.leadingSlash === "always" && !windowsDriveRegExp.test(normalPath))
|
|
24
|
+
normalPath = `${sep}${normalPath}`;
|
|
25
|
+
}
|
|
26
|
+
if (conf.trailingSlash !== "as-is") {
|
|
27
|
+
normalPath = normalPath.replace(trailingSlashRegExp, "");
|
|
28
|
+
if (conf.trailingSlash === "always") normalPath = `${normalPath}${sep}`;
|
|
29
|
+
}
|
|
30
|
+
return normalPath;
|
|
31
|
+
}
|
|
32
|
+
exports.normalize = normalize;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
export interface NormalizeOptions {
|
|
2
|
+
/**
|
|
3
|
+
* - `always`: ensure leading slash exists
|
|
4
|
+
* - `never`: ensure leading slash does not exist
|
|
5
|
+
* - `as-is`: do not modify the leading char
|
|
6
|
+
*
|
|
7
|
+
* @default "always"
|
|
8
|
+
*/
|
|
9
|
+
leadingSlash?: "always" | "never" | "as-is";
|
|
10
|
+
/**
|
|
11
|
+
* - `always`: ensure trailing slash exists
|
|
12
|
+
* - `never`: ensure trailing slash does not exist
|
|
13
|
+
* - `as-is`: do not modify the trailing char
|
|
14
|
+
*
|
|
15
|
+
* @default "never"
|
|
16
|
+
*/
|
|
17
|
+
trailingSlash?: "always" | "never" | "as-is";
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Clears and converts the provided path to a Posix filepath
|
|
21
|
+
*
|
|
22
|
+
* By default, adds a leading slash and removes the trailing
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* ```
|
|
26
|
+
* normalize("src/test/"); //returns: /src/test
|
|
27
|
+
* normalize("C:\\Users\\test\\folder\\"); //returns: C:/Users/test/folder
|
|
28
|
+
* normalize("file:///C:/Users/test/folder/file.ts"); //returns: C:/Users/test/folder/file.ts
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
31
|
+
export declare function normalize(path: string, options?: NormalizeOptions): string;
|