@valkyriestudios/utils 12.47.0 → 12.49.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/cjs/array/join.js +1 -1
- package/cjs/date/convertToDate.js +15 -8
- package/cjs/date/diff.js +10 -10
- package/cjs/date/format.js +33 -55
- package/cjs/date/is.js +1 -1
- package/cjs/date/isFormat.js +47 -38
- package/cjs/deep/get.js +46 -72
- package/cjs/deep/set.js +74 -37
- package/cjs/formdata/toObject.js +96 -61
- package/cjs/hash/djb2.js +1 -1
- package/cjs/hash/fnv1A.js +5 -4
- package/cjs/hash/guid.js +30 -18
- package/cjs/hash/hexId.js +23 -15
- package/cjs/hash/utils.js +25 -33
- package/cjs/hash/uuidv7.js +29 -28
- package/cjs/number/round.js +1 -1
- package/cjs/string/forbiddenKeys.js +8 -0
- package/cjs/string/humanizeNumber.js +2 -1
- package/esm/array/join.js +1 -1
- package/esm/date/convertToDate.js +15 -8
- package/esm/date/diff.js +10 -10
- package/esm/date/format.js +33 -55
- package/esm/date/is.js +1 -1
- package/esm/date/isFormat.js +47 -38
- package/esm/deep/get.js +46 -72
- package/esm/deep/set.js +74 -37
- package/esm/formdata/toObject.js +96 -61
- package/esm/hash/djb2.js +1 -1
- package/esm/hash/fnv1A.js +5 -4
- package/esm/hash/guid.js +30 -18
- package/esm/hash/hexId.js +23 -15
- package/esm/hash/utils.js +25 -33
- package/esm/hash/uuidv7.js +29 -28
- package/esm/number/round.js +1 -1
- package/esm/string/forbiddenKeys.js +5 -0
- package/esm/string/humanizeNumber.js +2 -1
- package/index.d.ts +13 -10
- package/package.json +7 -7
- package/string/forbiddenKeys.d.ts +1 -0
package/cjs/array/join.js
CHANGED
|
@@ -4,7 +4,7 @@ exports.join = join;
|
|
|
4
4
|
exports.default = join;
|
|
5
5
|
const round_1 = require("../number/round");
|
|
6
6
|
const isIntegerAboveOrEqual_1 = require("../number/isIntegerAboveOrEqual");
|
|
7
|
-
const SPACE_RGX =
|
|
7
|
+
const SPACE_RGX = /\s+/g;
|
|
8
8
|
function join(val, opts) {
|
|
9
9
|
let normalized;
|
|
10
10
|
if (Array.isArray(val)) {
|
|
@@ -2,14 +2,21 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.convertToDate = convertToDate;
|
|
4
4
|
exports.default = convertToDate;
|
|
5
|
-
const is_1 = require("./is");
|
|
6
5
|
function convertToDate(val) {
|
|
7
|
-
|
|
8
|
-
|
|
6
|
+
switch (typeof val) {
|
|
7
|
+
case 'number': {
|
|
8
|
+
if (val !== val)
|
|
9
|
+
return null;
|
|
10
|
+
const d = new Date(val);
|
|
11
|
+
return d.getTime() === d.getTime() ? d : null;
|
|
12
|
+
}
|
|
13
|
+
case 'object':
|
|
14
|
+
return val instanceof Date && val.getTime() === val.getTime() ? val : null;
|
|
15
|
+
case 'string': {
|
|
16
|
+
const d = new Date(val);
|
|
17
|
+
return d.getTime() === d.getTime() ? d : null;
|
|
18
|
+
}
|
|
19
|
+
default:
|
|
20
|
+
return null;
|
|
9
21
|
}
|
|
10
|
-
else if (typeof val === 'string') {
|
|
11
|
-
const date = new Date(val);
|
|
12
|
-
return Number.isNaN(date.getTime()) ? null : date;
|
|
13
|
-
}
|
|
14
|
-
return null;
|
|
15
22
|
}
|
package/cjs/date/diff.js
CHANGED
|
@@ -3,11 +3,11 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.diff = diff;
|
|
4
4
|
exports.default = diff;
|
|
5
5
|
const convertToDate_1 = require("./convertToDate");
|
|
6
|
-
const
|
|
7
|
-
const
|
|
8
|
-
const
|
|
9
|
-
const
|
|
10
|
-
const
|
|
6
|
+
const INV_SECOND_IN_MILLISECONDS = 1.0 / 1000;
|
|
7
|
+
const INV_MINUTE_IN_MILLISECONDS = 1.0 / (60 * 1000);
|
|
8
|
+
const INV_HOUR_IN_MILLISECONDS = 1.0 / (60 * 60 * 1000);
|
|
9
|
+
const INV_DAY_IN_MILLISECONDS = 1.0 / (24 * 60 * 60 * 1000);
|
|
10
|
+
const INV_WEEK_IN_MILLISECONDS = 1.0 / (7 * 24 * 60 * 60 * 1000);
|
|
11
11
|
function diff(val_a, val_b, key = 'millisecond') {
|
|
12
12
|
const n_val_a = (0, convertToDate_1.convertToDate)(val_a);
|
|
13
13
|
const n_val_b = (0, convertToDate_1.convertToDate)(val_b);
|
|
@@ -18,19 +18,19 @@ function diff(val_a, val_b, key = 'millisecond') {
|
|
|
18
18
|
switch (key) {
|
|
19
19
|
case 'week':
|
|
20
20
|
case 'weeks':
|
|
21
|
-
return diff_in_ms
|
|
21
|
+
return diff_in_ms * INV_WEEK_IN_MILLISECONDS;
|
|
22
22
|
case 'day':
|
|
23
23
|
case 'days':
|
|
24
|
-
return diff_in_ms
|
|
24
|
+
return diff_in_ms * INV_DAY_IN_MILLISECONDS;
|
|
25
25
|
case 'hour':
|
|
26
26
|
case 'hours':
|
|
27
|
-
return diff_in_ms
|
|
27
|
+
return diff_in_ms * INV_HOUR_IN_MILLISECONDS;
|
|
28
28
|
case 'minute':
|
|
29
29
|
case 'minutes':
|
|
30
|
-
return diff_in_ms
|
|
30
|
+
return diff_in_ms * INV_MINUTE_IN_MILLISECONDS;
|
|
31
31
|
case 'second':
|
|
32
32
|
case 'seconds':
|
|
33
|
-
return diff_in_ms
|
|
33
|
+
return diff_in_ms * INV_SECOND_IN_MILLISECONDS;
|
|
34
34
|
default:
|
|
35
35
|
return diff_in_ms;
|
|
36
36
|
}
|
package/cjs/date/format.js
CHANGED
|
@@ -6,7 +6,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
6
6
|
exports.format = format;
|
|
7
7
|
exports.default = format;
|
|
8
8
|
const convertToDate_1 = require("./convertToDate");
|
|
9
|
-
const isFormat_1 = require("./isFormat");
|
|
10
9
|
const LRU_1 = __importDefault(require("../caching/LRU"));
|
|
11
10
|
const WEEK_STARTS = {
|
|
12
11
|
mon: 'mon',
|
|
@@ -16,7 +15,6 @@ const WEEK_STARTS = {
|
|
|
16
15
|
let DEFAULT_LOCALE = 'en-US';
|
|
17
16
|
let DEFAULT_TZ = Intl?.DateTimeFormat?.().resolvedOptions?.().timeZone || 'UTC';
|
|
18
17
|
let DEFAULT_SOW = 'mon';
|
|
19
|
-
const ESCAPE_RGX = /\[([^\]]*)]/g;
|
|
20
18
|
const intl_formatters = new LRU_1.default({ max_size: 100 });
|
|
21
19
|
const spec_cache = new LRU_1.default({ max_size: 100 });
|
|
22
20
|
const zone_offset_cache = new LRU_1.default({ max_size: 100 });
|
|
@@ -42,15 +40,8 @@ function WeekNr(d, sow) {
|
|
|
42
40
|
}
|
|
43
41
|
}
|
|
44
42
|
function toZone(d, zone) {
|
|
45
|
-
const year = d.getUTCFullYear();
|
|
46
|
-
const month = d.getUTCMonth();
|
|
47
|
-
const day = d.getUTCDate();
|
|
48
43
|
const time = d.getTime();
|
|
49
|
-
const
|
|
50
|
-
let doy = day;
|
|
51
|
-
for (let i = 0; i <= month; i++)
|
|
52
|
-
doy += daysInMonths[i];
|
|
53
|
-
const ckey = zone + ':' + year + ':' + doy;
|
|
44
|
+
const ckey = zone + ':' + Math.floor(time / 900000);
|
|
54
45
|
const cached = zone_offset_cache.get(ckey);
|
|
55
46
|
if (cached !== undefined)
|
|
56
47
|
return new Date(time + cached);
|
|
@@ -80,54 +71,54 @@ function runIntl(loc, token, props, val) {
|
|
|
80
71
|
return formatter.format(val);
|
|
81
72
|
}
|
|
82
73
|
const Tokens = [
|
|
83
|
-
['YYYY', d => d.getFullYear()],
|
|
84
|
-
['Q', d => ((d.getMonth() + 3) / 3) | 0],
|
|
74
|
+
['YYYY', d => d.getFullYear().toString()],
|
|
75
|
+
['Q', d => (((d.getMonth() + 3) / 3) | 0).toString()],
|
|
85
76
|
['MMMM', (d, loc) => runIntl(loc, 'MMMM', { month: 'long' }, d)],
|
|
86
77
|
['MMM', (d, loc) => runIntl(loc, 'MMM', { month: 'short' }, d)],
|
|
87
78
|
['MM', d => {
|
|
88
79
|
const val = d.getMonth() + 1;
|
|
89
80
|
return (val < 10 ? '0' : '') + val;
|
|
90
81
|
}],
|
|
91
|
-
['M', d => d.getMonth() + 1],
|
|
82
|
+
['M', d => (d.getMonth() + 1).toString()],
|
|
92
83
|
['WW', (d, loc, sow) => {
|
|
93
84
|
const val = WeekNr(d, sow);
|
|
94
85
|
return (val < 10 ? '0' : '') + val;
|
|
95
86
|
}],
|
|
96
|
-
['W', (d, loc, sow) => WeekNr(d, sow)],
|
|
87
|
+
['W', (d, loc, sow) => WeekNr(d, sow).toString()],
|
|
97
88
|
['DD', d => {
|
|
98
89
|
const val = d.getDate();
|
|
99
90
|
return (val < 10 ? '0' : '') + val;
|
|
100
91
|
}],
|
|
101
|
-
['D', d => d.getDate()],
|
|
92
|
+
['D', d => d.getDate().toString()],
|
|
102
93
|
['dddd', (d, loc) => runIntl(loc, 'dddd', { weekday: 'long' }, d)],
|
|
103
94
|
['ddd', (d, loc) => runIntl(loc, 'ddd', { weekday: 'short' }, d)],
|
|
104
95
|
['HH', d => {
|
|
105
96
|
const val = d.getHours();
|
|
106
97
|
return (val < 10 ? '0' : '') + val;
|
|
107
98
|
}],
|
|
108
|
-
['H', d => d.getHours()],
|
|
99
|
+
['H', d => d.getHours().toString()],
|
|
109
100
|
['hh', d => {
|
|
110
101
|
const val = ((d.getHours() + 11) % 12) + 1;
|
|
111
102
|
return (val < 10 ? '0' : '') + val;
|
|
112
103
|
}],
|
|
113
|
-
['h', d => ((d.getHours() + 11) % 12) + 1],
|
|
104
|
+
['h', d => (((d.getHours() + 11) % 12) + 1).toString()],
|
|
114
105
|
['mm', d => {
|
|
115
106
|
const val = d.getMinutes();
|
|
116
107
|
return (val < 10 ? '0' : '') + val;
|
|
117
108
|
}],
|
|
118
|
-
['m', d => d.getMinutes()],
|
|
109
|
+
['m', d => d.getMinutes().toString()],
|
|
119
110
|
['ss', d => {
|
|
120
111
|
const val = d.getSeconds();
|
|
121
112
|
return (val < 10 ? '0' : '') + val;
|
|
122
113
|
}],
|
|
123
|
-
['s', d => d.getSeconds()],
|
|
114
|
+
['s', d => d.getSeconds().toString()],
|
|
124
115
|
['SSS', d => {
|
|
125
116
|
const val = d.getMilliseconds();
|
|
126
117
|
return val < 10
|
|
127
118
|
? '00' + val
|
|
128
119
|
: val < 100
|
|
129
120
|
? '0' + val
|
|
130
|
-
: val;
|
|
121
|
+
: val.toString();
|
|
131
122
|
}],
|
|
132
123
|
['A', d => d.getHours() < 12 ? 'AM' : 'PM'],
|
|
133
124
|
['a', d => d.getHours() < 12 ? 'am' : 'pm'],
|
|
@@ -141,38 +132,35 @@ const Tokens = [
|
|
|
141
132
|
const token_map = {};
|
|
142
133
|
for (const t of Tokens)
|
|
143
134
|
token_map[t[0]] = t;
|
|
144
|
-
const
|
|
135
|
+
const PARSE_RGX = new RegExp('\\[[^\\]]*\\]|' + Tokens.map(([tok]) => tok).join('|'), 'g');
|
|
145
136
|
function getSpecChain(spec) {
|
|
146
137
|
const cached = spec_cache.get(spec);
|
|
147
138
|
if (cached !== undefined)
|
|
148
139
|
return cached;
|
|
149
|
-
let base = spec;
|
|
150
|
-
const repl = [];
|
|
151
|
-
let repl_len = 0;
|
|
152
|
-
if (base.indexOf('[') >= 0) {
|
|
153
|
-
base = base.replace(ESCAPE_RGX, (_, inner) => {
|
|
154
|
-
const escape_token = '$' + repl_len++ + '$';
|
|
155
|
-
repl.push([escape_token, inner]);
|
|
156
|
-
return escape_token;
|
|
157
|
-
});
|
|
158
|
-
}
|
|
159
|
-
TOKENS_RGX.lastIndex = 0;
|
|
160
140
|
const parts = [];
|
|
141
|
+
PARSE_RGX.lastIndex = 0;
|
|
161
142
|
let last_idx = 0;
|
|
162
143
|
let has_token = false;
|
|
163
144
|
let m;
|
|
164
|
-
while (m =
|
|
165
|
-
|
|
166
|
-
|
|
145
|
+
while (m = PARSE_RGX.exec(spec)) {
|
|
146
|
+
const match = m[0];
|
|
147
|
+
const match_start = m.index;
|
|
148
|
+
if (match_start > last_idx) {
|
|
149
|
+
parts.push(spec.slice(last_idx, match_start));
|
|
150
|
+
}
|
|
151
|
+
if (match.charCodeAt(0) === 91) {
|
|
152
|
+
parts.push(match.slice(1, -1));
|
|
167
153
|
}
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
154
|
+
else {
|
|
155
|
+
parts.push(token_map[match][1]);
|
|
156
|
+
has_token = true;
|
|
157
|
+
}
|
|
158
|
+
last_idx = match_start + match.length;
|
|
171
159
|
}
|
|
172
|
-
if (last_idx <
|
|
173
|
-
parts.push(
|
|
160
|
+
if (last_idx < spec.length) {
|
|
161
|
+
parts.push(spec.slice(last_idx));
|
|
174
162
|
}
|
|
175
|
-
const result = has_token ?
|
|
163
|
+
const result = has_token ? parts : null;
|
|
176
164
|
spec_cache.set(spec, result);
|
|
177
165
|
return result;
|
|
178
166
|
}
|
|
@@ -189,26 +177,16 @@ function format(val, spec, locale = DEFAULT_LOCALE, zone = DEFAULT_TZ, sow = DEF
|
|
|
189
177
|
throw new TypeError('format: locale must be a string');
|
|
190
178
|
if (typeof zone !== 'string')
|
|
191
179
|
throw new TypeError('format: zone must be a string');
|
|
192
|
-
const
|
|
193
|
-
if (!
|
|
180
|
+
const parts = getSpecChain(SPEC_ALIASES[spec] || spec);
|
|
181
|
+
if (!parts)
|
|
194
182
|
return n_val.toISOString();
|
|
195
183
|
const d = toZone(n_val, zone);
|
|
196
|
-
const { parts, repl } = n_spec;
|
|
197
184
|
let out = '';
|
|
198
185
|
for (let i = 0; i < parts.length; i++) {
|
|
199
186
|
const part = parts[i];
|
|
200
|
-
|
|
201
|
-
out += part.literal;
|
|
202
|
-
}
|
|
203
|
-
else {
|
|
204
|
-
out += part.token[1](d, locale, sow);
|
|
205
|
-
}
|
|
187
|
+
out += typeof part === 'string' ? part : part(d, locale, sow);
|
|
206
188
|
}
|
|
207
|
-
|
|
208
|
-
for (let i = 0; i < repl.length; i++) {
|
|
209
|
-
result = result.replace(repl[i][0], repl[i][1]);
|
|
210
|
-
}
|
|
211
|
-
return result;
|
|
189
|
+
return out;
|
|
212
190
|
}
|
|
213
191
|
format.getLocale = function () {
|
|
214
192
|
return DEFAULT_LOCALE;
|
package/cjs/date/is.js
CHANGED
package/cjs/date/isFormat.js
CHANGED
|
@@ -8,47 +8,49 @@ exports.isDateFormat = isDateFormat;
|
|
|
8
8
|
exports.default = isDateFormat;
|
|
9
9
|
const LRU_1 = __importDefault(require("../caching/LRU"));
|
|
10
10
|
const SPECIAL_CHARS = /[.*+?^${}()|[\]\\]/g;
|
|
11
|
+
const CTX = { year: 0, month: 0, day: 0, hour: -1, is12: 0 };
|
|
11
12
|
const TOKENS = [
|
|
12
|
-
['YYYY', /\d{4}/.source,
|
|
13
|
-
|
|
14
|
-
return
|
|
13
|
+
['YYYY', /\d{4}/.source, raw => {
|
|
14
|
+
CTX.year = +raw;
|
|
15
|
+
return CTX.year > 0;
|
|
15
16
|
}],
|
|
16
|
-
['MM', /(?:0[1-9]|1[0-2])/.source,
|
|
17
|
-
|
|
18
|
-
return
|
|
17
|
+
['MM', /(?:0[1-9]|1[0-2])/.source, raw => {
|
|
18
|
+
CTX.month = +raw;
|
|
19
|
+
return CTX.month >= 1 && CTX.month <= 12;
|
|
19
20
|
}],
|
|
20
|
-
['DD', /(?:0[1-9]|[12][0-9]|3[01])/.source,
|
|
21
|
-
|
|
22
|
-
return
|
|
21
|
+
['DD', /(?:0[1-9]|[12][0-9]|3[01])/.source, raw => {
|
|
22
|
+
CTX.day = +raw;
|
|
23
|
+
return CTX.day >= 1 && CTX.day <= 31;
|
|
23
24
|
}],
|
|
24
|
-
['HH', /(?:[01][0-9]|2[0-3])/.source,
|
|
25
|
-
|
|
26
|
-
return
|
|
25
|
+
['HH', /(?:[01][0-9]|2[0-3])/.source, raw => {
|
|
26
|
+
CTX.hour = +raw;
|
|
27
|
+
return CTX.hour >= 0 && CTX.hour <= 23;
|
|
27
28
|
}],
|
|
28
29
|
['mm', /[0-5][0-9]/.source, () => true],
|
|
29
30
|
['ss', /[0-5][0-9]/.source, () => true],
|
|
30
31
|
['SSS', /\d{3}/.source, () => true],
|
|
31
32
|
['Q', /[1-4]/.source, () => true],
|
|
32
|
-
['A', /(?:AM|PM)/.source,
|
|
33
|
-
|
|
33
|
+
['A', /(?:AM|PM)/.source, raw => {
|
|
34
|
+
CTX.is12 = 1;
|
|
34
35
|
return raw === 'AM' || raw === 'PM';
|
|
35
36
|
}],
|
|
36
|
-
['a', /(?:am|pm)/.source,
|
|
37
|
-
|
|
37
|
+
['a', /(?:am|pm)/.source, raw => {
|
|
38
|
+
CTX.is12 = 1;
|
|
38
39
|
return raw === 'am' || raw === 'pm';
|
|
39
40
|
}],
|
|
40
41
|
['Z', /Z|[+-](?:0[0-9]|1[0-4]):[0-5][0-9]/.source, raw => {
|
|
41
42
|
if (raw === 'Z')
|
|
42
43
|
return true;
|
|
43
|
-
let hour = (raw[1] + raw[2])
|
|
44
|
-
if (raw
|
|
44
|
+
let hour = +(raw[1] + raw[2]);
|
|
45
|
+
if (raw.charCodeAt(0) === 45)
|
|
45
46
|
hour = -hour;
|
|
46
|
-
const minutes = (raw[4] + raw[5])
|
|
47
|
+
const minutes = +(raw[4] + raw[5]);
|
|
47
48
|
if (hour === 14 || hour === -12)
|
|
48
49
|
return minutes === 0;
|
|
49
|
-
return hour >= -11 && hour < 14 &&
|
|
50
|
+
return hour >= -11 && hour < 14 && (minutes === 0 || minutes === 15 || minutes === 30 || minutes === 45);
|
|
50
51
|
}],
|
|
51
52
|
];
|
|
53
|
+
TOKENS.sort((a, b) => b[0].length - a[0].length);
|
|
52
54
|
const SPEC_ALIASES = {
|
|
53
55
|
ISO: 'YYYY-MM-DDTHH:mm:ss{.SSS}Z',
|
|
54
56
|
};
|
|
@@ -85,14 +87,18 @@ function compileSpec(spec, is_chunk = false) {
|
|
|
85
87
|
cursor = end_idx + 1;
|
|
86
88
|
}
|
|
87
89
|
else {
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
const [token_key, token_rgx] = TOKENS[
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
90
|
+
let matched = false;
|
|
91
|
+
for (let i = 0; i < TOKENS.length; i++) {
|
|
92
|
+
const [token_key, token_rgx] = TOKENS[i];
|
|
93
|
+
if (spec.startsWith(token_key, cursor)) {
|
|
94
|
+
pat += '(' + token_rgx + ')';
|
|
95
|
+
tokens.push(i);
|
|
96
|
+
cursor += token_key.length;
|
|
97
|
+
matched = true;
|
|
98
|
+
break;
|
|
99
|
+
}
|
|
94
100
|
}
|
|
95
|
-
|
|
101
|
+
if (!matched) {
|
|
96
102
|
pat += spec[cursor].replace(SPECIAL_CHARS, '\\$&');
|
|
97
103
|
cursor++;
|
|
98
104
|
}
|
|
@@ -107,23 +113,26 @@ function isDateFormat(input, spec) {
|
|
|
107
113
|
return false;
|
|
108
114
|
if (typeof spec !== 'string')
|
|
109
115
|
throw new TypeError('isDateFormat: spec must be a string');
|
|
110
|
-
const
|
|
111
|
-
if (!tokens.length)
|
|
116
|
+
const compiled = compileSpec(SPEC_ALIASES[spec] || spec);
|
|
117
|
+
if (!compiled.tokens.length)
|
|
112
118
|
return false;
|
|
113
|
-
const patMatch = rgx.exec(input);
|
|
119
|
+
const patMatch = compiled.rgx.exec(input);
|
|
114
120
|
if (!patMatch)
|
|
115
121
|
return false;
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
122
|
+
CTX.year = 0;
|
|
123
|
+
CTX.month = 0;
|
|
124
|
+
CTX.day = 0;
|
|
125
|
+
CTX.hour = -1;
|
|
126
|
+
CTX.is12 = 0;
|
|
127
|
+
const { tokens } = compiled;
|
|
128
|
+
for (let i = 0; i < tokens.length; i++) {
|
|
129
|
+
const match = patMatch[i + 1];
|
|
130
|
+
if (match !== undefined && !TOKENS[tokens[i]][2](match))
|
|
121
131
|
return false;
|
|
122
132
|
}
|
|
123
|
-
|
|
124
|
-
if (day && month && !isValidDay(year || 2024, month, day))
|
|
133
|
+
if (CTX.day > 0 && CTX.month > 0 && !isValidDay(CTX.year || 2024, CTX.month, CTX.day))
|
|
125
134
|
return false;
|
|
126
|
-
if (is12
|
|
135
|
+
if (CTX.is12 === 1 && CTX.hour > 11)
|
|
127
136
|
return false;
|
|
128
137
|
return true;
|
|
129
138
|
}
|
package/cjs/deep/get.js
CHANGED
|
@@ -3,87 +3,61 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.deepGet = deepGet;
|
|
4
4
|
exports.default = deepGet;
|
|
5
5
|
function deepGet(obj, path, get_parent = false) {
|
|
6
|
-
if (Object.prototype.toString.call(obj) !== '[object Object]' &&
|
|
7
|
-
!Array.isArray(obj))
|
|
6
|
+
if (Object.prototype.toString.call(obj) !== '[object Object]' && !Array.isArray(obj)) {
|
|
8
7
|
throw new TypeError('deepGet: Requires object or array');
|
|
9
|
-
|
|
10
|
-
|
|
8
|
+
}
|
|
9
|
+
if (!path || typeof path !== 'string') {
|
|
11
10
|
throw new TypeError('deepGet: Invalid path provided');
|
|
12
|
-
|
|
13
|
-
let
|
|
14
|
-
let
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
11
|
+
}
|
|
12
|
+
let cursor = obj;
|
|
13
|
+
let parent = obj;
|
|
14
|
+
let keyStart = 0;
|
|
15
|
+
const len = path.length;
|
|
16
|
+
for (let i = 0; i <= len; i++) {
|
|
17
|
+
const code = i === len ? 46 : path.charCodeAt(i);
|
|
18
|
+
if (code === 46 || code === 91 || code === 93) {
|
|
19
|
+
if (i === keyStart) {
|
|
20
|
+
keyStart = i + 1;
|
|
21
|
+
continue;
|
|
22
|
+
}
|
|
23
|
+
const key = path.substring(keyStart, i);
|
|
24
|
+
keyStart = i + 1;
|
|
25
|
+
parent = cursor;
|
|
26
|
+
if (Array.isArray(cursor)) {
|
|
27
|
+
const idx = +key;
|
|
28
|
+
if (idx === idx) {
|
|
29
|
+
cursor = cursor[idx | 0];
|
|
30
|
+
}
|
|
31
|
+
else {
|
|
32
|
+
const mapped = [];
|
|
33
|
+
const cLen = cursor.length;
|
|
34
|
+
for (let j = 0; j < cLen; j++) {
|
|
35
|
+
const item = cursor[j];
|
|
36
|
+
if (item && typeof item === 'object') {
|
|
37
|
+
const val = item[key];
|
|
38
|
+
if (val !== undefined) {
|
|
39
|
+
if (Array.isArray(val)) {
|
|
40
|
+
const vLen = val.length;
|
|
41
|
+
for (let k = 0; k < vLen; k++)
|
|
42
|
+
mapped.push(val[k]);
|
|
43
|
+
}
|
|
44
|
+
else {
|
|
45
|
+
mapped.push(val);
|
|
46
|
+
}
|
|
38
47
|
}
|
|
39
48
|
}
|
|
40
|
-
node = extracted;
|
|
41
|
-
nodes.push(node);
|
|
42
49
|
}
|
|
50
|
+
if (mapped.length === 0)
|
|
51
|
+
return undefined;
|
|
52
|
+
cursor = mapped;
|
|
43
53
|
}
|
|
44
|
-
else if (typeof node === 'object' && node !== null) {
|
|
45
|
-
node = node[key];
|
|
46
|
-
nodes.push(node);
|
|
47
|
-
}
|
|
48
|
-
key = '';
|
|
49
|
-
break;
|
|
50
|
-
default:
|
|
51
|
-
key += char;
|
|
52
|
-
break;
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
if (key) {
|
|
56
|
-
if (Array.isArray(node)) {
|
|
57
|
-
let ix = Number(key);
|
|
58
|
-
if (!isNaN(ix)) {
|
|
59
|
-
ix = ix | 0;
|
|
60
|
-
if (ix < 0 || ix > node.length - 1)
|
|
61
|
-
return undefined;
|
|
62
|
-
node = node[ix];
|
|
63
|
-
nodes.push(node);
|
|
64
54
|
}
|
|
65
|
-
else {
|
|
66
|
-
|
|
67
|
-
for (let i = 0; i < node.length; i++) {
|
|
68
|
-
const val = node[i]?.[key];
|
|
69
|
-
if (val !== undefined)
|
|
70
|
-
extracted.push(...Array.isArray(val) ? val : [val]);
|
|
71
|
-
}
|
|
72
|
-
node = extracted.length ? extracted : undefined;
|
|
73
|
-
nodes.push(node);
|
|
55
|
+
else if (cursor) {
|
|
56
|
+
cursor = cursor[key];
|
|
74
57
|
}
|
|
75
|
-
|
|
76
|
-
else if (typeof node === 'object' && node !== null) {
|
|
77
|
-
node = node[key];
|
|
78
|
-
nodes.push(node);
|
|
79
|
-
if (node === undefined)
|
|
58
|
+
if (cursor === undefined)
|
|
80
59
|
return undefined;
|
|
81
60
|
}
|
|
82
|
-
else {
|
|
83
|
-
return undefined;
|
|
84
|
-
}
|
|
85
61
|
}
|
|
86
|
-
|
|
87
|
-
nodes.pop();
|
|
88
|
-
return nodes.length ? nodes.pop() : obj;
|
|
62
|
+
return get_parent ? parent : cursor;
|
|
89
63
|
}
|
package/cjs/deep/set.js
CHANGED
|
@@ -9,45 +9,82 @@ function deepSet(obj, path, value, define = false) {
|
|
|
9
9
|
throw new TypeError('Deepset is only supported for objects');
|
|
10
10
|
if (typeof path !== 'string')
|
|
11
11
|
throw new TypeError('No path was given');
|
|
12
|
-
if (
|
|
13
|
-
throw new TypeError('Malicious path provided');
|
|
14
|
-
const path_s = path.trim();
|
|
15
|
-
if (!path_s.length)
|
|
12
|
+
if (path.trim().length === 0) {
|
|
16
13
|
throw new TypeError('No path was given');
|
|
17
|
-
const parts = path_s
|
|
18
|
-
.replace(/\[/g, '.')
|
|
19
|
-
.replace(/(\.){2,}/g, '.')
|
|
20
|
-
.replace(/(^\.|\.$|\])/g, '')
|
|
21
|
-
.split('.');
|
|
22
|
-
const last_part_ix = parts.length - 1;
|
|
23
|
-
for (let i = 0; i < last_part_ix; i++) {
|
|
24
|
-
if (Array.isArray(obj)) {
|
|
25
|
-
const idx = parseInt(parts[i]);
|
|
26
|
-
if (!Number.isInteger(idx) || idx < 0)
|
|
27
|
-
throw new TypeError('Invalid path provided');
|
|
28
|
-
if (!obj[idx])
|
|
29
|
-
obj[idx] = {};
|
|
30
|
-
obj = obj[idx];
|
|
31
|
-
}
|
|
32
|
-
else {
|
|
33
|
-
if (!obj[parts[i]])
|
|
34
|
-
obj[parts[i]] = {};
|
|
35
|
-
obj = obj[parts[i]];
|
|
36
|
-
}
|
|
37
14
|
}
|
|
38
|
-
if (
|
|
39
|
-
|
|
40
|
-
if (define) {
|
|
41
|
-
Object.defineProperty(obj, parts[last_part_ix], value);
|
|
42
|
-
}
|
|
43
|
-
else if (Array.isArray(obj)) {
|
|
44
|
-
const idx = parseInt(parts[last_part_ix]);
|
|
45
|
-
if (!Number.isInteger(idx) || idx < 0)
|
|
46
|
-
throw new TypeError('Invalid path provided');
|
|
47
|
-
obj[idx] = value;
|
|
15
|
+
if (RGX_MALICIOUS.test(path)) {
|
|
16
|
+
throw new TypeError('Malicious path provided');
|
|
48
17
|
}
|
|
49
|
-
|
|
50
|
-
|
|
18
|
+
let cursor = obj;
|
|
19
|
+
let keyStart = 0;
|
|
20
|
+
const len = path.length;
|
|
21
|
+
for (let i = 0; i <= len; i++) {
|
|
22
|
+
const code = i === len ? 46 : path.charCodeAt(i);
|
|
23
|
+
if (code === 46 || code === 91 || code === 93) {
|
|
24
|
+
if (i === keyStart) {
|
|
25
|
+
keyStart = i + 1;
|
|
26
|
+
continue;
|
|
27
|
+
}
|
|
28
|
+
const key = path.substring(keyStart, i);
|
|
29
|
+
keyStart = i + 1;
|
|
30
|
+
let isEnd = i === len;
|
|
31
|
+
if (!isEnd) {
|
|
32
|
+
let j = i + 1;
|
|
33
|
+
isEnd = true;
|
|
34
|
+
for (; j < len; j++) {
|
|
35
|
+
const c = path.charCodeAt(j);
|
|
36
|
+
if (c !== 46 && c !== 91 && c !== 93) {
|
|
37
|
+
isEnd = false;
|
|
38
|
+
break;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
if (isEnd) {
|
|
43
|
+
if (Array.isArray(cursor)) {
|
|
44
|
+
const idx = +key;
|
|
45
|
+
if (idx !== idx || (idx | 0) !== idx || idx < 0) {
|
|
46
|
+
throw new TypeError('Invalid path provided');
|
|
47
|
+
}
|
|
48
|
+
if (define) {
|
|
49
|
+
Object.defineProperty(cursor, key, value);
|
|
50
|
+
}
|
|
51
|
+
else {
|
|
52
|
+
cursor[idx] = value;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
else if (define) {
|
|
56
|
+
Object.defineProperty(cursor, key, value);
|
|
57
|
+
}
|
|
58
|
+
else {
|
|
59
|
+
cursor[key] = value;
|
|
60
|
+
}
|
|
61
|
+
return true;
|
|
62
|
+
}
|
|
63
|
+
let nextCursor;
|
|
64
|
+
if (Array.isArray(cursor)) {
|
|
65
|
+
const idx = +key;
|
|
66
|
+
if (idx !== idx || (idx | 0) !== idx || idx < 0) {
|
|
67
|
+
throw new TypeError('Invalid path provided');
|
|
68
|
+
}
|
|
69
|
+
nextCursor = cursor[idx];
|
|
70
|
+
}
|
|
71
|
+
else {
|
|
72
|
+
nextCursor = cursor[key];
|
|
73
|
+
}
|
|
74
|
+
if (nextCursor === undefined || nextCursor === null) {
|
|
75
|
+
nextCursor = {};
|
|
76
|
+
if (Array.isArray(cursor)) {
|
|
77
|
+
cursor[+key] = nextCursor;
|
|
78
|
+
}
|
|
79
|
+
else {
|
|
80
|
+
cursor[key] = nextCursor;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
else if (typeof nextCursor !== 'object') {
|
|
84
|
+
return false;
|
|
85
|
+
}
|
|
86
|
+
cursor = nextCursor;
|
|
87
|
+
}
|
|
51
88
|
}
|
|
52
|
-
return
|
|
89
|
+
return false;
|
|
53
90
|
}
|