df-script 1.5.0 → 1.6.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 +124 -8
- package/dist/api.d.ts +1 -1
- package/dist/columnExpressions/ColumnExpr.d.ts +2 -2
- package/dist/columnExpressions/functions/coalesce.d.ts +2 -2
- package/dist/columnExpressions/functions/element.d.ts +1 -1
- package/dist/columnExpressions/functions/when.d.ts +2 -2
- package/dist/columnExpressions/index.d.ts +1 -1
- package/dist/columnExpressions/mixins/AggregationExpr.d.ts +2 -2
- package/dist/columnExpressions/mixins/ArithmeticExpr.d.ts +11 -10
- package/dist/columnExpressions/mixins/ArrayExpr.d.ts +39 -0
- package/dist/columnExpressions/mixins/ListExpr.d.ts +2 -2
- package/dist/columnExpressions/mixins/StringExpr.d.ts +2 -2
- package/dist/columnExpressions/types.d.ts +1 -0
- package/dist/constants.d.ts +11 -0
- package/dist/dataframe/dataframe.d.ts +1 -1
- package/dist/datatypes/index.d.ts +3 -2
- package/dist/datatypes/types.d.ts +7 -7
- package/dist/index.js +6 -6
- package/dist/types.d.ts +4 -2
- package/dist/utils/array.d.ts +217 -0
- package/dist/utils/binary.d.ts +3 -0
- package/dist/utils/date.d.ts +21 -35
- package/dist/utils/guards.d.ts +11 -10
- package/dist/utils/index.d.ts +3 -2
- package/dist/utils/list.d.ts +23 -1
- package/dist/utils/number.d.ts +14 -29
- package/dist/utils/object.d.ts +26 -0
- package/dist/utils/string.d.ts +3 -5
- package/package.json +2 -2
- package/dist/api.js +0 -26
- package/dist/bundle.min.js +0 -1
- package/dist/columnExpressions/ColumnExpr.js +0 -219
- package/dist/columnExpressions/ExprBase.js +0 -92
- package/dist/columnExpressions/constants.js +0 -7
- package/dist/columnExpressions/functions/all.js +0 -11
- package/dist/columnExpressions/functions/coalesce.js +0 -44
- package/dist/columnExpressions/functions/element.js +0 -11
- package/dist/columnExpressions/functions/exclude.js +0 -13
- package/dist/columnExpressions/functions/implode.js +0 -7
- package/dist/columnExpressions/functions/lit.js +0 -17
- package/dist/columnExpressions/functions/repeat.d.ts +0 -47
- package/dist/columnExpressions/functions/repeat.js +0 -131
- package/dist/columnExpressions/functions/seq_range.js +0 -91
- package/dist/columnExpressions/functions/when.js +0 -85
- package/dist/columnExpressions/index.js +0 -38
- package/dist/columnExpressions/mixins/AggregationExpr.js +0 -76
- package/dist/columnExpressions/mixins/ArithmeticExpr.js +0 -131
- package/dist/columnExpressions/mixins/BooleanExpr.d.ts +0 -40
- package/dist/columnExpressions/mixins/BooleanExpr.js +0 -67
- package/dist/columnExpressions/mixins/ComparisonExpr.js +0 -250
- package/dist/columnExpressions/mixins/ListExpr.js +0 -224
- package/dist/columnExpressions/mixins/LogicalExpr.js +0 -53
- package/dist/columnExpressions/mixins/ManipulationExpr.js +0 -71
- package/dist/columnExpressions/mixins/NumericExpr.d.ts +0 -92
- package/dist/columnExpressions/mixins/NumericExpr.js +0 -259
- package/dist/columnExpressions/mixins/StringExpr.js +0 -237
- package/dist/columnExpressions/mixins/StructExpr.js +0 -139
- package/dist/columnExpressions/mixins/TemporalExpr.js +0 -126
- package/dist/columnExpressions/mixins/WindowExpr.js +0 -151
- package/dist/columnExpressions/types.js +0 -2
- package/dist/columnExpressions/utils.js +0 -45
- package/dist/dataframe/constants.js +0 -7
- package/dist/dataframe/dataframe.js +0 -990
- package/dist/dataframe/grouped/grouped.js +0 -114
- package/dist/dataframe/index.js +0 -20
- package/dist/dataframe/types.js +0 -2
- package/dist/dataframe/utils.js +0 -307
- package/dist/datatypes/DataType.js +0 -76
- package/dist/datatypes/index.js +0 -46
- package/dist/datatypes/types/Binary.d.ts +0 -9
- package/dist/datatypes/types/Binary.js +0 -18
- package/dist/datatypes/types/Boolean.d.ts +0 -9
- package/dist/datatypes/types/Boolean.js +0 -19
- package/dist/datatypes/types/Date.d.ts +0 -8
- package/dist/datatypes/types/Date.js +0 -21
- package/dist/datatypes/types/Datetime.d.ts +0 -8
- package/dist/datatypes/types/Datetime.js +0 -17
- package/dist/datatypes/types/Decimal.d.ts +0 -10
- package/dist/datatypes/types/Decimal.js +0 -28
- package/dist/datatypes/types/Duration.d.ts +0 -8
- package/dist/datatypes/types/Duration.js +0 -17
- package/dist/datatypes/types/Float32.d.ts +0 -8
- package/dist/datatypes/types/Float32.js +0 -17
- package/dist/datatypes/types/Float64.d.ts +0 -8
- package/dist/datatypes/types/Float64.js +0 -17
- package/dist/datatypes/types/Int16.d.ts +0 -8
- package/dist/datatypes/types/Int16.js +0 -17
- package/dist/datatypes/types/Int32.d.ts +0 -8
- package/dist/datatypes/types/Int32.js +0 -17
- package/dist/datatypes/types/Int64.d.ts +0 -8
- package/dist/datatypes/types/Int64.js +0 -17
- package/dist/datatypes/types/Int8.d.ts +0 -8
- package/dist/datatypes/types/Int8.js +0 -17
- package/dist/datatypes/types/List.d.ts +0 -10
- package/dist/datatypes/types/List.js +0 -31
- package/dist/datatypes/types/Null.d.ts +0 -9
- package/dist/datatypes/types/Null.js +0 -17
- package/dist/datatypes/types/Object.d.ts +0 -9
- package/dist/datatypes/types/Object.js +0 -17
- package/dist/datatypes/types/Struct.d.ts +0 -14
- package/dist/datatypes/types/Struct.js +0 -39
- package/dist/datatypes/types/Time.d.ts +0 -8
- package/dist/datatypes/types/Time.js +0 -29
- package/dist/datatypes/types/UInt16.d.ts +0 -8
- package/dist/datatypes/types/UInt16.js +0 -17
- package/dist/datatypes/types/UInt32.d.ts +0 -8
- package/dist/datatypes/types/UInt32.js +0 -17
- package/dist/datatypes/types/UInt64.d.ts +0 -8
- package/dist/datatypes/types/UInt64.js +0 -17
- package/dist/datatypes/types/UInt8.d.ts +0 -8
- package/dist/datatypes/types/UInt8.js +0 -17
- package/dist/datatypes/types/Utf8.d.ts +0 -10
- package/dist/datatypes/types/Utf8.js +0 -20
- package/dist/datatypes/types.js +0 -290
- package/dist/exceptions/index.js +0 -46
- package/dist/exceptions/utils.js +0 -28
- package/dist/functions/concat.js +0 -260
- package/dist/functions/index.js +0 -9
- package/dist/functions/read_csv.js +0 -53
- package/dist/functions/read_json.js +0 -21
- package/dist/functions/transpose.d.ts +0 -7
- package/dist/functions/transpose.js +0 -84
- package/dist/types.js +0 -2
- package/dist/utils/boolean.d.ts +0 -1
- package/dist/utils/boolean.js +0 -18
- package/dist/utils/csv.js +0 -313
- package/dist/utils/date.js +0 -348
- package/dist/utils/guards.js +0 -112
- package/dist/utils/index.js +0 -23
- package/dist/utils/json.js +0 -217
- package/dist/utils/list.js +0 -361
- package/dist/utils/number.js +0 -401
- package/dist/utils/string.js +0 -209
package/dist/utils/date.js
DELETED
|
@@ -1,348 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.STRFTIME_DIRECTIVES = exports.NS_PER_MS = exports.US_PER_MS = exports.MS_PER_DAY = exports.MS_PER_HOUR = exports.MS_PER_MINUTE = exports.MS_PER_SECOND = exports.UTC_INDICATOR_REGEX = exports.ZONE_OFFSET_REGEX = exports.TIME_PREFIX_REGEX = void 0;
|
|
4
|
-
exports.toEpoch = toEpoch;
|
|
5
|
-
exports.getCentury = getCentury;
|
|
6
|
-
exports.getISOWeek = getISOWeek;
|
|
7
|
-
exports.getMillennium = getMillennium;
|
|
8
|
-
exports.getMonthOffset = getMonthOffset;
|
|
9
|
-
exports.getOrdinalDay = getOrdinalDay;
|
|
10
|
-
exports.getQuarter = getQuarter;
|
|
11
|
-
exports.getUtcOffset = getUtcOffset;
|
|
12
|
-
exports.isLeapYear = isLeapYear;
|
|
13
|
-
exports.isUtcString = isUtcString;
|
|
14
|
-
exports.isValidDate = isValidDate;
|
|
15
|
-
exports.isValidDateObj = isValidDateObj;
|
|
16
|
-
exports.normalizeEpochToMs = normalizeEpochToMs;
|
|
17
|
-
exports.strftime = strftime;
|
|
18
|
-
exports.strptime = strptime;
|
|
19
|
-
exports.toValidDate = toValidDate;
|
|
20
|
-
const number_1 = require("./number");
|
|
21
|
-
const string_1 = require("./string");
|
|
22
|
-
/**
|
|
23
|
-
* Matches string values beginning with standard hour-and-minute formatting.
|
|
24
|
-
* Examples:
|
|
25
|
-
* - "12:34" (matches "12:34")
|
|
26
|
-
* - "10:37:16.123" (matches "10:37")
|
|
27
|
-
* - "2026-05-25" (does not match)
|
|
28
|
-
*/
|
|
29
|
-
exports.TIME_PREFIX_REGEX = /^\d{2}:\d{2}/;
|
|
30
|
-
/**
|
|
31
|
-
* Matches timezone offset indicators at the end of a string.
|
|
32
|
-
* Examples:
|
|
33
|
-
* - "12:34:56Z" (matches "Z")
|
|
34
|
-
* - "12:34:56+02:00" (matches "+02:00")
|
|
35
|
-
* - "12:34:56-0500" (matches "-0500")
|
|
36
|
-
* - "12:34:56-05" (matches "-05")
|
|
37
|
-
* - "12:34:56" (does not match)
|
|
38
|
-
*/
|
|
39
|
-
exports.ZONE_OFFSET_REGEX = /(?:Z|[+-]\d{2}(?::?\d{2})?)$/i;
|
|
40
|
-
/**
|
|
41
|
-
* Matches only UTC offset indicators at the end of a string.
|
|
42
|
-
* Examples:
|
|
43
|
-
* - "12:34:56Z" (matches "Z")
|
|
44
|
-
* - "12:34:56+00:00" (matches "+00:00")
|
|
45
|
-
* - "12:34:56-0000" (matches "-0000")
|
|
46
|
-
* - "12:34:56+02:00" (does not match)
|
|
47
|
-
*/
|
|
48
|
-
exports.UTC_INDICATOR_REGEX = /(?:Z|[+-]00:00|[+-]0000|[+-]00)$/i;
|
|
49
|
-
exports.MS_PER_SECOND = 1000;
|
|
50
|
-
exports.MS_PER_MINUTE = 60_000;
|
|
51
|
-
exports.MS_PER_HOUR = 3_600_000;
|
|
52
|
-
exports.MS_PER_DAY = 86_400_000;
|
|
53
|
-
exports.US_PER_MS = 1000;
|
|
54
|
-
exports.NS_PER_MS = 1_000_000;
|
|
55
|
-
function toEpoch(d, unit = "ms") {
|
|
56
|
-
const ms = d.getTime();
|
|
57
|
-
switch (unit) {
|
|
58
|
-
case "s": return Math.floor(ms / exports.MS_PER_SECOND);
|
|
59
|
-
case "ms": return ms;
|
|
60
|
-
case "us": return ms * exports.US_PER_MS;
|
|
61
|
-
case "ns": return ms * exports.NS_PER_MS;
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
function getCentury(d) {
|
|
65
|
-
const y = d.getUTCFullYear();
|
|
66
|
-
return Math.floor((y - 1) / 100) + 1;
|
|
67
|
-
}
|
|
68
|
-
function getISOWeek(d) {
|
|
69
|
-
const date = new Date(Date.UTC(d.getUTCFullYear(), d.getUTCMonth(), d.getUTCDate()));
|
|
70
|
-
const dayNum = date.getUTCDay() || 7;
|
|
71
|
-
date.setUTCDate(date.getUTCDate() + 4 - dayNum);
|
|
72
|
-
const yearStart = new Date(Date.UTC(date.getUTCFullYear(), 0, 1));
|
|
73
|
-
return Math.ceil((((date.getTime() - yearStart.getTime()) / exports.MS_PER_DAY) + 1) / 7);
|
|
74
|
-
}
|
|
75
|
-
function getMillennium(d) {
|
|
76
|
-
const y = d.getUTCFullYear();
|
|
77
|
-
return Math.floor((y - 1) / 1000) + 1;
|
|
78
|
-
}
|
|
79
|
-
function getMonthOffset(d, monthOffset, day = 1) {
|
|
80
|
-
return new Date(Date.UTC(d.getUTCFullYear(), d.getUTCMonth() + monthOffset, day, 0, 0, 0, 0));
|
|
81
|
-
}
|
|
82
|
-
function getOrdinalDay(d) {
|
|
83
|
-
const start = new Date(Date.UTC(d.getUTCFullYear(), 0, 1));
|
|
84
|
-
const diff = d.getTime() - start.getTime();
|
|
85
|
-
return Math.floor(diff / exports.MS_PER_DAY) + 1;
|
|
86
|
-
}
|
|
87
|
-
function getQuarter(d) {
|
|
88
|
-
return Math.floor(d.getUTCMonth() / 3) + 1;
|
|
89
|
-
}
|
|
90
|
-
function getUtcOffset(timeZone) {
|
|
91
|
-
const now = new Date();
|
|
92
|
-
const utcMs = now.getTime() + now.getTimezoneOffset() * 60_000;
|
|
93
|
-
const tzTime = new Date(now.toLocaleString('en-US', { timeZone }));
|
|
94
|
-
const tzMs = tzTime.getTime();
|
|
95
|
-
const offsetMin = Math.round((tzMs - utcMs) / 60_000);
|
|
96
|
-
const sign = offsetMin >= 0 ? "+" : "\u2212";
|
|
97
|
-
const h = Math.floor(Math.abs(offsetMin) / 60);
|
|
98
|
-
const m = Math.abs(offsetMin) % 60;
|
|
99
|
-
const formatted = `UTC${sign}${String(h).padStart(2, "0")}:${String(m).padStart(2, "0")}`;
|
|
100
|
-
return {
|
|
101
|
-
timeZoneTime: tzTime,
|
|
102
|
-
utcTime: now,
|
|
103
|
-
offset: offsetMin,
|
|
104
|
-
formatted
|
|
105
|
-
};
|
|
106
|
-
}
|
|
107
|
-
function isLeapYear(yOrDate) {
|
|
108
|
-
const y = yOrDate instanceof Date ? yOrDate.getUTCFullYear() : yOrDate;
|
|
109
|
-
return (y % 4 === 0 && y % 100 !== 0) || y % 400 === 0;
|
|
110
|
-
}
|
|
111
|
-
function isUtcString(timestamp) {
|
|
112
|
-
if (!timestamp)
|
|
113
|
-
return false;
|
|
114
|
-
const trimmed = timestamp.trim();
|
|
115
|
-
if (!trimmed)
|
|
116
|
-
return false;
|
|
117
|
-
return exports.UTC_INDICATOR_REGEX.test(trimmed);
|
|
118
|
-
}
|
|
119
|
-
function isValidDate(v) {
|
|
120
|
-
if (v == null)
|
|
121
|
-
return false;
|
|
122
|
-
if (v instanceof Date)
|
|
123
|
-
return isValidDateObj(v);
|
|
124
|
-
if (typeof v === "number") {
|
|
125
|
-
if (!(0, number_1.isValidNumber)(v))
|
|
126
|
-
return false;
|
|
127
|
-
return isValidDateObj(new Date(normalizeEpochToMs(v)));
|
|
128
|
-
}
|
|
129
|
-
if (typeof v === "bigint") {
|
|
130
|
-
return isValidDateObj(new Date(normalizeEpochToMs(Number(v))));
|
|
131
|
-
}
|
|
132
|
-
if (typeof v === "string") {
|
|
133
|
-
if ((0, string_1.isBlankString)(v))
|
|
134
|
-
return false;
|
|
135
|
-
return isValidDateObj(new Date(v));
|
|
136
|
-
}
|
|
137
|
-
return false;
|
|
138
|
-
}
|
|
139
|
-
function isValidDateObj(d) {
|
|
140
|
-
return d instanceof Date && !Number.isNaN(d.getTime());
|
|
141
|
-
}
|
|
142
|
-
function normalizeEpochToMs(n) {
|
|
143
|
-
const abs = Math.abs(n);
|
|
144
|
-
if (abs >= 1e12)
|
|
145
|
-
return n;
|
|
146
|
-
if (abs <= 1e10)
|
|
147
|
-
return n * 1000;
|
|
148
|
-
return n;
|
|
149
|
-
}
|
|
150
|
-
exports.STRFTIME_DIRECTIVES = [
|
|
151
|
-
"%ms", "%f", "%Y", "%y", "%m", "%d", "%e", "%H", "%I", "%p", "%M", "%S", "%A", "%a", "%B", "%b", "%h", "%j", "%u", "%w", "%Z", "%z"
|
|
152
|
-
];
|
|
153
|
-
function strftime(d, format, locale) {
|
|
154
|
-
let result = format;
|
|
155
|
-
// 1. Literal %% escaping
|
|
156
|
-
result = result.replace(/%%/g, "\0");
|
|
157
|
-
// 2. High-level shorthand formats
|
|
158
|
-
result = result.replace(/%F/g, "%Y-%m-%d");
|
|
159
|
-
result = result.replace(/%T/g, "%H:%M:%S");
|
|
160
|
-
result = result.replace(/%R/g, "%H:%M");
|
|
161
|
-
result = result.replace(/%D/g, "%m/%d/%y");
|
|
162
|
-
// 3. Directives replacements with lazy getters
|
|
163
|
-
const replacements = {
|
|
164
|
-
get "%Y"() { return String(d.getUTCFullYear()); },
|
|
165
|
-
get "%y"() { return String(d.getUTCFullYear() % 100).padStart(2, "0"); },
|
|
166
|
-
get "%m"() { return String(d.getUTCMonth() + 1).padStart(2, "0"); },
|
|
167
|
-
get "%d"() { return String(d.getUTCDate()).padStart(2, "0"); },
|
|
168
|
-
get "%e"() { return String(d.getUTCDate()).padStart(2, " "); },
|
|
169
|
-
get "%H"() { return String(d.getUTCHours()).padStart(2, "0"); },
|
|
170
|
-
get "%I"() { return String(d.getUTCHours() % 12 || 12).padStart(2, "0"); },
|
|
171
|
-
get "%p"() { return d.getUTCHours() >= 12 ? "PM" : "AM"; },
|
|
172
|
-
get "%M"() { return String(d.getUTCMinutes()).padStart(2, "0"); },
|
|
173
|
-
get "%S"() { return String(d.getUTCSeconds()).padStart(2, "0"); },
|
|
174
|
-
get "%A"() { return d.toLocaleDateString(locale, { weekday: "long", timeZone: "UTC" }); },
|
|
175
|
-
get "%a"() { return d.toLocaleDateString(locale, { weekday: "short", timeZone: "UTC" }); },
|
|
176
|
-
get "%B"() { return d.toLocaleDateString(locale, { month: "long", timeZone: "UTC" }); },
|
|
177
|
-
get "%b"() { return d.toLocaleDateString(locale, { month: "short", timeZone: "UTC" }); },
|
|
178
|
-
get "%h"() { return d.toLocaleDateString(locale, { month: "short", timeZone: "UTC" }); },
|
|
179
|
-
get "%j"() {
|
|
180
|
-
const start = new Date(Date.UTC(d.getUTCFullYear(), 0, 1));
|
|
181
|
-
const diff = d.getTime() - start.getTime();
|
|
182
|
-
const dayOfYear = Math.floor(diff / exports.MS_PER_DAY) + 1;
|
|
183
|
-
return String(dayOfYear).padStart(3, "0");
|
|
184
|
-
},
|
|
185
|
-
get "%u"() { return String(d.getUTCDay() || 7); },
|
|
186
|
-
get "%w"() { return String(d.getUTCDay()); },
|
|
187
|
-
get "%Z"() { return "UTC"; },
|
|
188
|
-
get "%z"() { return "+0000"; },
|
|
189
|
-
get "%ms"() { return String(d.getUTCMilliseconds()).padStart(3, "0"); },
|
|
190
|
-
get "%f"() { return String(d.getUTCMilliseconds() * 1000).padStart(6, "0"); }
|
|
191
|
-
};
|
|
192
|
-
for (const directive of exports.STRFTIME_DIRECTIVES) {
|
|
193
|
-
if (result.includes(directive)) {
|
|
194
|
-
result = result.replaceAll(directive, replacements[directive]);
|
|
195
|
-
}
|
|
196
|
-
}
|
|
197
|
-
// 4. Restore literal percent symbols
|
|
198
|
-
return result.replace(/\0/g, "%");
|
|
199
|
-
}
|
|
200
|
-
function strptime(str, format, strict = true) {
|
|
201
|
-
if (typeof str !== "string" || typeof format !== "string")
|
|
202
|
-
return null;
|
|
203
|
-
const placeholders = [];
|
|
204
|
-
let groupIndex = 1;
|
|
205
|
-
let regexStr = "";
|
|
206
|
-
let i = 0;
|
|
207
|
-
while (i < format.length) {
|
|
208
|
-
const char = format[i];
|
|
209
|
-
if (char === "%") {
|
|
210
|
-
if (i + 1 < format.length) {
|
|
211
|
-
const nextChar = format[i + 1];
|
|
212
|
-
if (nextChar === "%") {
|
|
213
|
-
regexStr += "%";
|
|
214
|
-
i += 2;
|
|
215
|
-
}
|
|
216
|
-
else if (format.slice(i, i + 3) === "%ms") {
|
|
217
|
-
placeholders.push({ name: "ms", index: groupIndex++ });
|
|
218
|
-
regexStr += "(\\d{1,3})";
|
|
219
|
-
i += 3;
|
|
220
|
-
}
|
|
221
|
-
else {
|
|
222
|
-
switch (nextChar) {
|
|
223
|
-
case "Y":
|
|
224
|
-
placeholders.push({ name: "year", index: groupIndex++ });
|
|
225
|
-
regexStr += "(\\d{4})";
|
|
226
|
-
break;
|
|
227
|
-
case "y":
|
|
228
|
-
placeholders.push({ name: "year_short", index: groupIndex++ });
|
|
229
|
-
regexStr += "(\\d{2})";
|
|
230
|
-
break;
|
|
231
|
-
case "m":
|
|
232
|
-
placeholders.push({ name: "month", index: groupIndex++ });
|
|
233
|
-
regexStr += "(\\d{1,2})";
|
|
234
|
-
break;
|
|
235
|
-
case "d":
|
|
236
|
-
case "e":
|
|
237
|
-
placeholders.push({ name: "day", index: groupIndex++ });
|
|
238
|
-
regexStr += "(\\d{1,2})";
|
|
239
|
-
break;
|
|
240
|
-
case "H":
|
|
241
|
-
case "I":
|
|
242
|
-
placeholders.push({ name: "hour", index: groupIndex++ });
|
|
243
|
-
regexStr += "(\\d{1,2})";
|
|
244
|
-
break;
|
|
245
|
-
case "M":
|
|
246
|
-
placeholders.push({ name: "minute", index: groupIndex++ });
|
|
247
|
-
regexStr += "(\\d{1,2})";
|
|
248
|
-
break;
|
|
249
|
-
case "S":
|
|
250
|
-
placeholders.push({ name: "second", index: groupIndex++ });
|
|
251
|
-
regexStr += "(\\d{1,2})";
|
|
252
|
-
break;
|
|
253
|
-
case "f":
|
|
254
|
-
placeholders.push({ name: "fractional", index: groupIndex++ });
|
|
255
|
-
regexStr += "(\\d{1,6})";
|
|
256
|
-
break;
|
|
257
|
-
default:
|
|
258
|
-
regexStr += char + nextChar;
|
|
259
|
-
}
|
|
260
|
-
i += 2;
|
|
261
|
-
}
|
|
262
|
-
}
|
|
263
|
-
else {
|
|
264
|
-
regexStr += char;
|
|
265
|
-
i++;
|
|
266
|
-
}
|
|
267
|
-
}
|
|
268
|
-
else {
|
|
269
|
-
if ("\\^$*+?.()|[]{}".indexOf(char) !== -1) {
|
|
270
|
-
regexStr += "\\" + char;
|
|
271
|
-
}
|
|
272
|
-
else {
|
|
273
|
-
regexStr += char;
|
|
274
|
-
}
|
|
275
|
-
i++;
|
|
276
|
-
}
|
|
277
|
-
}
|
|
278
|
-
const regex = new RegExp("^" + regexStr + "$");
|
|
279
|
-
const match = str.match(regex);
|
|
280
|
-
if (!match) {
|
|
281
|
-
if (strict)
|
|
282
|
-
return null;
|
|
283
|
-
const parsed = new Date(str);
|
|
284
|
-
return isValidDateObj(parsed) ? parsed : null;
|
|
285
|
-
}
|
|
286
|
-
let year = 1970;
|
|
287
|
-
let month = 1;
|
|
288
|
-
let day = 1;
|
|
289
|
-
let hour = 0;
|
|
290
|
-
let minute = 0;
|
|
291
|
-
let second = 0;
|
|
292
|
-
let ms = 0;
|
|
293
|
-
for (const p of placeholders) {
|
|
294
|
-
const valStr = match[p.index];
|
|
295
|
-
const val = parseInt(valStr, 10);
|
|
296
|
-
switch (p.name) {
|
|
297
|
-
case "year":
|
|
298
|
-
year = val;
|
|
299
|
-
break;
|
|
300
|
-
case "year_short":
|
|
301
|
-
year = val + (val >= 69 ? 1900 : 2000);
|
|
302
|
-
break;
|
|
303
|
-
case "month":
|
|
304
|
-
month = val;
|
|
305
|
-
break;
|
|
306
|
-
case "day":
|
|
307
|
-
day = val;
|
|
308
|
-
break;
|
|
309
|
-
case "hour":
|
|
310
|
-
hour = val;
|
|
311
|
-
break;
|
|
312
|
-
case "minute":
|
|
313
|
-
minute = val;
|
|
314
|
-
break;
|
|
315
|
-
case "second":
|
|
316
|
-
second = val;
|
|
317
|
-
break;
|
|
318
|
-
case "ms":
|
|
319
|
-
ms = parseInt(valStr.padEnd(3, "0").slice(0, 3), 10);
|
|
320
|
-
break;
|
|
321
|
-
case "fractional":
|
|
322
|
-
ms = parseInt(valStr.padEnd(6, "0").slice(0, 3), 10);
|
|
323
|
-
break;
|
|
324
|
-
}
|
|
325
|
-
}
|
|
326
|
-
const d = new Date(Date.UTC(year, month - 1, day, hour, minute, second, ms));
|
|
327
|
-
return isValidDateObj(d) ? d : null;
|
|
328
|
-
}
|
|
329
|
-
function toValidDate(input) {
|
|
330
|
-
if (input instanceof Date) {
|
|
331
|
-
return isValidDateObj(input) ? input : null;
|
|
332
|
-
}
|
|
333
|
-
if (typeof input === "number") {
|
|
334
|
-
const d = new Date(normalizeEpochToMs(input));
|
|
335
|
-
return isValidDateObj(d) ? d : null;
|
|
336
|
-
}
|
|
337
|
-
if (typeof input === "bigint") {
|
|
338
|
-
const d = new Date(normalizeEpochToMs(Number(input)));
|
|
339
|
-
return isValidDateObj(d) ? d : null;
|
|
340
|
-
}
|
|
341
|
-
if (typeof input === "string") {
|
|
342
|
-
if ((0, string_1.isBlankString)(input))
|
|
343
|
-
return null;
|
|
344
|
-
const d = new Date(input);
|
|
345
|
-
return isValidDateObj(d) ? d : null;
|
|
346
|
-
}
|
|
347
|
-
return null;
|
|
348
|
-
}
|
package/dist/utils/guards.js
DELETED
|
@@ -1,112 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.isTypedArray = isTypedArray;
|
|
4
|
-
exports.isArrayOrTypedArray = isArrayOrTypedArray;
|
|
5
|
-
exports.isNonEmptyArray = isNonEmptyArray;
|
|
6
|
-
exports.isNonEmptyArrayObjs = isNonEmptyArrayObjs;
|
|
7
|
-
exports.isNonEmptyObj = isNonEmptyObj;
|
|
8
|
-
exports.isObj = isObj;
|
|
9
|
-
exports.isPlainObj = isPlainObj;
|
|
10
|
-
exports.isClass = isClass;
|
|
11
|
-
exports.isScalar = isScalar;
|
|
12
|
-
exports.isValidBinary = isValidBinary;
|
|
13
|
-
exports.toValidBinary = toValidBinary;
|
|
14
|
-
exports.tryParseBoolean = tryParseBoolean;
|
|
15
|
-
const date_1 = require("./date");
|
|
16
|
-
const number_1 = require("./number");
|
|
17
|
-
function isTypedArray(v) {
|
|
18
|
-
return ArrayBuffer.isView(v) && !(v instanceof DataView);
|
|
19
|
-
}
|
|
20
|
-
function isArrayOrTypedArray(v) {
|
|
21
|
-
return Array.isArray(v) || isTypedArray(v);
|
|
22
|
-
}
|
|
23
|
-
function isNonEmptyArray(arr) {
|
|
24
|
-
return isArrayOrTypedArray(arr) && arr.length > 0;
|
|
25
|
-
}
|
|
26
|
-
function isNonEmptyArrayObjs(arr) {
|
|
27
|
-
if (!isNonEmptyArray(arr))
|
|
28
|
-
return false;
|
|
29
|
-
const len = arr.length;
|
|
30
|
-
for (let i = 0; i < len; i++) {
|
|
31
|
-
if (!isObj(arr[i]))
|
|
32
|
-
return false;
|
|
33
|
-
}
|
|
34
|
-
return true;
|
|
35
|
-
}
|
|
36
|
-
function isNonEmptyObj(v) {
|
|
37
|
-
return isObj(v) && Object.keys(v).length > 0;
|
|
38
|
-
}
|
|
39
|
-
function isObj(v) {
|
|
40
|
-
return v !== null && typeof v === "object" && !Array.isArray(v);
|
|
41
|
-
}
|
|
42
|
-
function isPlainObj(v) {
|
|
43
|
-
if (!isObj(v))
|
|
44
|
-
return false;
|
|
45
|
-
const proto = Object.getPrototypeOf(v);
|
|
46
|
-
return proto === null || proto === Object.prototype;
|
|
47
|
-
}
|
|
48
|
-
function isClass(v) {
|
|
49
|
-
if (typeof v !== "function")
|
|
50
|
-
return false;
|
|
51
|
-
return (/^class\s/.test(Function.prototype.toString.call(v)) ||
|
|
52
|
-
(v.prototype !== undefined &&
|
|
53
|
-
v.prototype.constructor === v &&
|
|
54
|
-
Object.getOwnPropertyDescriptor(v, "prototype")?.writable === false));
|
|
55
|
-
}
|
|
56
|
-
function isScalar(v) {
|
|
57
|
-
return (typeof v === "string" ||
|
|
58
|
-
typeof v === "number" ||
|
|
59
|
-
typeof v === "boolean" ||
|
|
60
|
-
typeof v === "bigint" ||
|
|
61
|
-
(0, date_1.isValidDateObj)(v) ||
|
|
62
|
-
v instanceof Uint8Array);
|
|
63
|
-
}
|
|
64
|
-
function isValidBinary(v) {
|
|
65
|
-
if (v == null)
|
|
66
|
-
return false;
|
|
67
|
-
if (v instanceof Uint8Array)
|
|
68
|
-
return true;
|
|
69
|
-
if (typeof v === "string")
|
|
70
|
-
return true;
|
|
71
|
-
if (Array.isArray(v)) {
|
|
72
|
-
const len = v.length;
|
|
73
|
-
for (let i = 0; i < len; i++) {
|
|
74
|
-
if (!(0, number_1.isValidInt)(v[i], { min: -128, max: 255 })) {
|
|
75
|
-
return false;
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
return true;
|
|
79
|
-
}
|
|
80
|
-
if (isTypedArray(v)) {
|
|
81
|
-
return true;
|
|
82
|
-
}
|
|
83
|
-
return false;
|
|
84
|
-
}
|
|
85
|
-
function toValidBinary(v) {
|
|
86
|
-
if (!isValidBinary(v))
|
|
87
|
-
return null;
|
|
88
|
-
if (v instanceof Uint8Array)
|
|
89
|
-
return v;
|
|
90
|
-
if (typeof v === "string") {
|
|
91
|
-
return new TextEncoder().encode(v);
|
|
92
|
-
}
|
|
93
|
-
if (ArrayBuffer.isView(v)) {
|
|
94
|
-
return new Uint8Array(v.buffer, v.byteOffset, v.byteLength);
|
|
95
|
-
}
|
|
96
|
-
return new Uint8Array(v);
|
|
97
|
-
}
|
|
98
|
-
const boolMap = {
|
|
99
|
-
true: true, "1": true, yes: true, y: true, on: true,
|
|
100
|
-
false: false, "0": false, no: false, n: false, off: false
|
|
101
|
-
};
|
|
102
|
-
function tryParseBoolean(v) {
|
|
103
|
-
if (typeof v === "boolean")
|
|
104
|
-
return v;
|
|
105
|
-
if (v === 1)
|
|
106
|
-
return true;
|
|
107
|
-
if (v === 0)
|
|
108
|
-
return false;
|
|
109
|
-
if (typeof v !== "string")
|
|
110
|
-
return undefined;
|
|
111
|
-
return boolMap[v.trim().toLowerCase()];
|
|
112
|
-
}
|
package/dist/utils/index.js
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
-
};
|
|
16
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
__exportStar(require("./guards"), exports);
|
|
18
|
-
__exportStar(require("./number"), exports);
|
|
19
|
-
__exportStar(require("./date"), exports);
|
|
20
|
-
__exportStar(require("./list"), exports);
|
|
21
|
-
__exportStar(require("./string"), exports);
|
|
22
|
-
__exportStar(require("./json"), exports);
|
|
23
|
-
__exportStar(require("./csv"), exports);
|
package/dist/utils/json.js
DELETED
|
@@ -1,217 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.isJsonString = isJsonString;
|
|
4
|
-
exports.safeJsonParse = safeJsonParse;
|
|
5
|
-
exports.createSafeJsonReplacer = createSafeJsonReplacer;
|
|
6
|
-
const guards_1 = require("./guards");
|
|
7
|
-
const date_1 = require("./date");
|
|
8
|
-
const NEWLINE_REGEX = /\r\n|\n|\r/g;
|
|
9
|
-
const NO_FALLBACK = Symbol("no_fallback");
|
|
10
|
-
const isWrapped = (str) => {
|
|
11
|
-
const len = str.length;
|
|
12
|
-
if (len < 2)
|
|
13
|
-
return false;
|
|
14
|
-
const fChar = str[0];
|
|
15
|
-
const lChar = str[len - 1];
|
|
16
|
-
return (fChar === "{" && lChar === "}") || (fChar === "[" && lChar === "]");
|
|
17
|
-
};
|
|
18
|
-
const isWrappedUntrimmed = (str) => {
|
|
19
|
-
let first = 0;
|
|
20
|
-
const len = str.length;
|
|
21
|
-
while (first < len && /\s/.test(str[first])) {
|
|
22
|
-
first++;
|
|
23
|
-
}
|
|
24
|
-
let last = len - 1;
|
|
25
|
-
while (last >= first && /\s/.test(str[last])) {
|
|
26
|
-
last--;
|
|
27
|
-
}
|
|
28
|
-
if (first >= last)
|
|
29
|
-
return false;
|
|
30
|
-
const fChar = str[first];
|
|
31
|
-
const lChar = str[last];
|
|
32
|
-
return (fChar === "{" && lChar === "}") || (fChar === "[" && lChar === "]");
|
|
33
|
-
};
|
|
34
|
-
/**
|
|
35
|
-
* Validates whether the given value is a valid JSON or NDJSON string.
|
|
36
|
-
* Leverages single-pass parsing under the hood to ensure zero duplicate loops.
|
|
37
|
-
*
|
|
38
|
-
* @param input - The value to check.
|
|
39
|
-
* @param options - Configuration options for validation.
|
|
40
|
-
* @returns `true` if the input is a valid JSON or NDJSON string; `false` otherwise.
|
|
41
|
-
*/
|
|
42
|
-
function isJsonString(input, options = {}) {
|
|
43
|
-
if (typeof input !== "string")
|
|
44
|
-
return false;
|
|
45
|
-
const sentinel = Symbol("invalid");
|
|
46
|
-
const result = safeJsonParse(input, {
|
|
47
|
-
...options,
|
|
48
|
-
fallback: sentinel,
|
|
49
|
-
onError: () => { } // Silence errors during pure structural checking
|
|
50
|
-
});
|
|
51
|
-
return result !== sentinel;
|
|
52
|
-
}
|
|
53
|
-
/**
|
|
54
|
-
* Safely parses a string containing JSON or NDJSON content in a single pass, returning the parsed value if successful
|
|
55
|
-
* and passing the guard validation. If parsing or validation fails, returns the fallback value (if provided)
|
|
56
|
-
* or the original input.
|
|
57
|
-
*
|
|
58
|
-
* @param input - The value to parse.
|
|
59
|
-
* @param options - Configuration options for parsing and validation.
|
|
60
|
-
* @returns The parsed value, the fallback, or the original input.
|
|
61
|
-
*/
|
|
62
|
-
function safeJsonParse(input, { format = "json", allowPrimitives = false, trimBeforeParse = false, reviver, ndjson: { skipInvalidLines = false, maxLines, skipLines } = {}, guard, onError, fallback = NO_FALLBACK } = {}) {
|
|
63
|
-
if (typeof input !== "string") {
|
|
64
|
-
return fallback !== NO_FALLBACK ? fallback : input;
|
|
65
|
-
}
|
|
66
|
-
const s = trimBeforeParse ? input.trim() : input;
|
|
67
|
-
if (s === "") {
|
|
68
|
-
return fallback !== NO_FALLBACK ? fallback : input;
|
|
69
|
-
}
|
|
70
|
-
let result;
|
|
71
|
-
try {
|
|
72
|
-
if (format === "ndjson") {
|
|
73
|
-
const parsedData = [];
|
|
74
|
-
let nonEmptyCount = 0;
|
|
75
|
-
let parsedCount = 0;
|
|
76
|
-
let lastIndex = 0;
|
|
77
|
-
NEWLINE_REGEX.lastIndex = 0;
|
|
78
|
-
let match;
|
|
79
|
-
while (true) {
|
|
80
|
-
match = NEWLINE_REGEX.exec(s);
|
|
81
|
-
const line = match ? s.substring(lastIndex, match.index) : s.substring(lastIndex);
|
|
82
|
-
if (match) {
|
|
83
|
-
lastIndex = NEWLINE_REGEX.lastIndex;
|
|
84
|
-
}
|
|
85
|
-
const trimmedLine = line.trim();
|
|
86
|
-
if (trimmedLine !== "") {
|
|
87
|
-
nonEmptyCount++;
|
|
88
|
-
if (skipLines === undefined || nonEmptyCount > skipLines) {
|
|
89
|
-
if (!allowPrimitives && !isWrapped(trimmedLine)) {
|
|
90
|
-
if (!skipInvalidLines) {
|
|
91
|
-
throw new Error("NDJSON line is not wrapped and primitives are disallowed");
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
else {
|
|
95
|
-
try {
|
|
96
|
-
parsedData.push(JSON.parse(trimmedLine, reviver));
|
|
97
|
-
parsedCount++;
|
|
98
|
-
if (maxLines !== undefined && parsedCount >= maxLines)
|
|
99
|
-
break;
|
|
100
|
-
}
|
|
101
|
-
catch (err) {
|
|
102
|
-
if (!skipInvalidLines)
|
|
103
|
-
throw err;
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
if (!match)
|
|
109
|
-
break;
|
|
110
|
-
}
|
|
111
|
-
if (parsedData.length === 0) {
|
|
112
|
-
throw new Error("No valid NDJSON lines processed");
|
|
113
|
-
}
|
|
114
|
-
result = parsedData;
|
|
115
|
-
}
|
|
116
|
-
else {
|
|
117
|
-
if (!allowPrimitives && !isWrappedUntrimmed(s)) {
|
|
118
|
-
throw new Error("JSON string is not wrapped and primitives are disallowed");
|
|
119
|
-
}
|
|
120
|
-
result = JSON.parse(s, reviver);
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
catch (err) {
|
|
124
|
-
onError?.(err);
|
|
125
|
-
return fallback !== NO_FALLBACK ? fallback : input;
|
|
126
|
-
}
|
|
127
|
-
if (guard && !guard(result)) {
|
|
128
|
-
onError?.(new Error("Parsed value failed guard validation"));
|
|
129
|
-
return fallback !== NO_FALLBACK ? fallback : input;
|
|
130
|
-
}
|
|
131
|
-
return result;
|
|
132
|
-
}
|
|
133
|
-
function createSafeJsonReplacer(options = {}) {
|
|
134
|
-
const bigintStrat = options.bigintStrategy ?? "string";
|
|
135
|
-
let seen = options.handleCircular ? new WeakSet() : null;
|
|
136
|
-
const isArrayReplacer = Array.isArray(options.replacer);
|
|
137
|
-
const whitelist = isArrayReplacer ? options.replacer.map(String) : null;
|
|
138
|
-
const handleBigInt = (val) => {
|
|
139
|
-
if (bigintStrat === "number") {
|
|
140
|
-
return val <= BigInt(Number.MAX_SAFE_INTEGER) && val >= BigInt(Number.MIN_SAFE_INTEGER)
|
|
141
|
-
? Number(val)
|
|
142
|
-
: val.toString();
|
|
143
|
-
}
|
|
144
|
-
return val.toString();
|
|
145
|
-
};
|
|
146
|
-
return function replacer(k, v) {
|
|
147
|
-
let val = v;
|
|
148
|
-
if (typeof options.replacer === "function") {
|
|
149
|
-
val = options.replacer.call(this, k, v);
|
|
150
|
-
}
|
|
151
|
-
else if (whitelist) {
|
|
152
|
-
if (k !== "" && !Array.isArray(this) && !whitelist.includes(k)) {
|
|
153
|
-
return undefined;
|
|
154
|
-
}
|
|
155
|
-
}
|
|
156
|
-
if (val === undefined)
|
|
157
|
-
return undefined;
|
|
158
|
-
const raw = (val === v && this != null) ? this[k] : val;
|
|
159
|
-
if (typeof options.onCustom === "function") {
|
|
160
|
-
const customVal = options.onCustom.call(this, k, raw);
|
|
161
|
-
if (customVal !== raw || (customVal === undefined && raw !== undefined)) {
|
|
162
|
-
return customVal;
|
|
163
|
-
}
|
|
164
|
-
}
|
|
165
|
-
if (raw !== null && typeof raw !== "object" && typeof raw !== "bigint") {
|
|
166
|
-
return val;
|
|
167
|
-
}
|
|
168
|
-
if (seen && raw !== null && typeof raw === "object") {
|
|
169
|
-
if (k === "") {
|
|
170
|
-
seen = new WeakSet();
|
|
171
|
-
}
|
|
172
|
-
if (seen.has(raw)) {
|
|
173
|
-
return options.onCircular ? options.onCircular.call(this, k, raw) : "[Circular]";
|
|
174
|
-
}
|
|
175
|
-
seen.add(raw);
|
|
176
|
-
}
|
|
177
|
-
if (typeof raw === "bigint") {
|
|
178
|
-
if (options.voidBigIntReplacement)
|
|
179
|
-
return val;
|
|
180
|
-
return options.onBigInt ? options.onBigInt(raw) : handleBigInt(raw);
|
|
181
|
-
}
|
|
182
|
-
if ((0, guards_1.isTypedArray)(raw)) {
|
|
183
|
-
if (options.voidTypedArrayReplacement)
|
|
184
|
-
return val;
|
|
185
|
-
return options.onTypedArray ? options.onTypedArray(raw) : Array.from(raw);
|
|
186
|
-
}
|
|
187
|
-
if (raw instanceof Set) {
|
|
188
|
-
if (options.voidSetReplacement)
|
|
189
|
-
return val;
|
|
190
|
-
return options.onSet ? options.onSet(raw) : Array.from(raw);
|
|
191
|
-
}
|
|
192
|
-
if (raw instanceof Map) {
|
|
193
|
-
if (options.voidMapReplacement)
|
|
194
|
-
return val;
|
|
195
|
-
return options.onMap ? options.onMap(raw) : Array.from(raw.entries());
|
|
196
|
-
}
|
|
197
|
-
if (raw instanceof RegExp) {
|
|
198
|
-
if (options.voidRegExpReplacement)
|
|
199
|
-
return val;
|
|
200
|
-
return options.onRegExp ? options.onRegExp(raw) : raw.toString();
|
|
201
|
-
}
|
|
202
|
-
if ((0, date_1.isValidDateObj)(raw)) {
|
|
203
|
-
if (options.voidDateReplacement)
|
|
204
|
-
return val;
|
|
205
|
-
if (options.onDate)
|
|
206
|
-
return options.onDate(raw);
|
|
207
|
-
return options.formatDate ? options.formatDate(raw) : raw.toISOString();
|
|
208
|
-
}
|
|
209
|
-
if (raw instanceof Error) {
|
|
210
|
-
return options.onError ? options.onError(raw) : { name: raw.name, message: raw.message, stack: raw.stack };
|
|
211
|
-
}
|
|
212
|
-
if (raw instanceof URLSearchParams) {
|
|
213
|
-
return options.onURLSearchParams ? options.onURLSearchParams(raw) : raw.toString();
|
|
214
|
-
}
|
|
215
|
-
return val;
|
|
216
|
-
};
|
|
217
|
-
}
|