@ix-xs/node-comfort 1.1.3 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +623 -748
- package/index.js +39 -13
- package/package.json +74 -24
- package/src/Arr.js +433 -0
- package/src/Checker.js +547 -188
- package/src/FS.js +127 -1
- package/src/Func.js +375 -0
- package/src/Id.js +235 -0
- package/src/Logger.js +71 -0
- package/src/Num.js +366 -0
- package/src/Obj.js +421 -0
- package/src/SQLite.js +20 -9
- package/src/Str.js +452 -0
- package/src/Time.js +485 -0
- package/types/index.d.ts +526 -0
- package/types/index.d.ts.map +1 -0
- package/types/src/Arr.d.ts +252 -0
- package/types/src/Arr.d.ts.map +1 -0
- package/types/src/Checker.d.ts +407 -0
- package/types/src/Checker.d.ts.map +1 -0
- package/types/src/FS.d.ts +303 -0
- package/types/src/FS.d.ts.map +1 -0
- package/types/src/Func.d.ts +210 -0
- package/types/src/Func.d.ts.map +1 -0
- package/types/src/Id.d.ts +119 -0
- package/types/src/Id.d.ts.map +1 -0
- package/types/src/Logger.d.ts +68 -0
- package/types/src/Logger.d.ts.map +1 -0
- package/types/src/Num.d.ts +242 -0
- package/types/src/Num.d.ts.map +1 -0
- package/types/src/Obj.d.ts +189 -0
- package/types/src/Obj.d.ts.map +1 -0
- package/types/src/SQLite.d.ts +396 -0
- package/types/src/SQLite.d.ts.map +1 -0
- package/types/src/Str.d.ts +293 -0
- package/types/src/Str.d.ts.map +1 -0
- package/types/src/Time.d.ts +205 -0
- package/types/src/Time.d.ts.map +1 -0
- package/types/src/Utils.d.ts +124 -0
- package/types/src/Utils.d.ts.map +1 -0
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
declare namespace _exports {
|
|
2
|
+
export { TimeModule };
|
|
3
|
+
}
|
|
4
|
+
declare namespace _exports {
|
|
5
|
+
/**
|
|
6
|
+
* Sets the global default locale used by `relative`, `formatDuration` and
|
|
7
|
+
* `format` (named tokens). Chainable.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* nodeComfort.time.setLocale("fr");
|
|
11
|
+
*
|
|
12
|
+
* @param {string} locale - A BCP-47 locale tag (e.g. `"fr"`, `"en-GB"`, `"ja"`).
|
|
13
|
+
* @returns {TimeModule} The time module (for chaining).
|
|
14
|
+
*/
|
|
15
|
+
function setLocale(locale: string): TimeModule;
|
|
16
|
+
/**
|
|
17
|
+
* Sets the global default time zone used by `format` and `isSameDay`.
|
|
18
|
+
* Chainable. Pass `undefined` to fall back to the system local zone.
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* nodeComfort.time.setTimezone("Europe/Paris");
|
|
22
|
+
*
|
|
23
|
+
* @param {string|undefined} timeZone - An IANA time-zone name (e.g. `"America/New_York"`).
|
|
24
|
+
* @returns {TimeModule} The time module (for chaining).
|
|
25
|
+
*/
|
|
26
|
+
function setTimezone(timeZone: string | undefined): TimeModule;
|
|
27
|
+
/**
|
|
28
|
+
* Returns the current global `{ locale, timeZone }` defaults.
|
|
29
|
+
*
|
|
30
|
+
* @returns {{ locale: string, timeZone: string|undefined }} A copy of the config.
|
|
31
|
+
*/
|
|
32
|
+
function getConfig(): {
|
|
33
|
+
locale: string;
|
|
34
|
+
timeZone: string | undefined;
|
|
35
|
+
};
|
|
36
|
+
/**
|
|
37
|
+
* Parses a human duration string into milliseconds.
|
|
38
|
+
*
|
|
39
|
+
* Accepts things like `"1h"`, `"30m"`, `"1h30m"`, `"2 days"`, `"500ms"`.
|
|
40
|
+
* A plain number is treated as milliseconds.
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
* nodeComfort.time.parseDuration("1h30m"); // 5400000
|
|
44
|
+
* nodeComfort.time.parseDuration("2 days"); // 172800000
|
|
45
|
+
*
|
|
46
|
+
* @param {string|number} input - Duration string or millisecond count.
|
|
47
|
+
* @returns {number|null} Milliseconds, or null if it can't be parsed.
|
|
48
|
+
*/
|
|
49
|
+
function parseDuration(input: string | number): number | null;
|
|
50
|
+
/**
|
|
51
|
+
* Formats a millisecond duration into a human string.
|
|
52
|
+
*
|
|
53
|
+
* The compact form (default) is language-neutral (`"1h 30m"`). The `long`
|
|
54
|
+
* form is fully localized through `Intl` • pass `options.locale` or set a
|
|
55
|
+
* global locale with {@link module:Time.setLocale}.
|
|
56
|
+
*
|
|
57
|
+
* @example
|
|
58
|
+
* nodeComfort.time.formatDuration(5400000); // "1h 30m"
|
|
59
|
+
* nodeComfort.time.formatDuration(90000, { long: true }); // "1 minute 30 seconds"
|
|
60
|
+
* nodeComfort.time.formatDuration(90000, { long: true, locale: "fr" }); // "1 minute 30 secondes"
|
|
61
|
+
*
|
|
62
|
+
* @param {number} ms - Duration in milliseconds.
|
|
63
|
+
* @param {{ long?: boolean, units?: number, locale?: string }} [options] - Formatting options (`units` caps how many parts show).
|
|
64
|
+
* @returns {string} The formatted duration.
|
|
65
|
+
*/
|
|
66
|
+
function formatDuration(ms: number, options?: {
|
|
67
|
+
long?: boolean;
|
|
68
|
+
units?: number;
|
|
69
|
+
locale?: string;
|
|
70
|
+
}): string;
|
|
71
|
+
/**
|
|
72
|
+
* Returns a human-friendly, localized relative time string.
|
|
73
|
+
*
|
|
74
|
+
* Built on `Intl.RelativeTimeFormat`, so it speaks every language. Set
|
|
75
|
+
* `options.locale` (or a global locale) to translate; `options.numeric`
|
|
76
|
+
* controls wording (`"always"` → "1 day ago", `"auto"` → "yesterday").
|
|
77
|
+
*
|
|
78
|
+
* @example
|
|
79
|
+
* nodeComfort.time.relative(Date.now() - 3600e3); // "1 hour ago"
|
|
80
|
+
* nodeComfort.time.relative(Date.now() - 3600e3, undefined, { locale: "fr" }); // "il y a 1 heure"
|
|
81
|
+
* nodeComfort.time.relative(Date.now() - 86400e3, undefined, { numeric: "auto" }); // "yesterday"
|
|
82
|
+
*
|
|
83
|
+
* @param {Date|number|string} date - Target date.
|
|
84
|
+
* @param {Date|number|string} [from] - Reference date (defaults to now).
|
|
85
|
+
* @param {{ locale?: string, numeric?: "always"|"auto" }} [options] - Formatting options.
|
|
86
|
+
* @returns {string} The relative time string.
|
|
87
|
+
*/
|
|
88
|
+
function relative(date: Date | number | string, from?: Date | number | string, options?: {
|
|
89
|
+
locale?: string;
|
|
90
|
+
numeric?: "always" | "auto";
|
|
91
|
+
}): string;
|
|
92
|
+
/**
|
|
93
|
+
* Formats a date using a small token syntax, with optional locale & time zone.
|
|
94
|
+
*
|
|
95
|
+
* **Numeric tokens:** `YYYY` `YY` `MM` `M` `DD` `D` `HH` `H` (24h) `hh` `h`
|
|
96
|
+
* (12h) `mm` `m` `ss` `s` `SSS`.
|
|
97
|
+
* **Localized tokens:** `MMMM` (month name) `MMM` (short month) `dddd` (weekday)
|
|
98
|
+
* `ddd` (short weekday) `A` (AM/PM) `a` (am/pm).
|
|
99
|
+
*
|
|
100
|
+
* Wrap literal text in square brackets to keep it verbatim: `"[Today is] dddd"`.
|
|
101
|
+
*
|
|
102
|
+
* @example
|
|
103
|
+
* nodeComfort.time.format(new Date(), "YYYY-MM-DD HH:mm:ss");
|
|
104
|
+
* nodeComfort.time.format(Date.now(), "dddd D MMMM YYYY", { locale: "fr" });
|
|
105
|
+
* nodeComfort.time.format(Date.now(), "HH:mm", { timeZone: "Asia/Tokyo" });
|
|
106
|
+
*
|
|
107
|
+
* @param {Date|number|string} date - Date to format.
|
|
108
|
+
* @param {string} [pattern="YYYY-MM-DD HH:mm:ss"] - Format pattern.
|
|
109
|
+
* @param {{ locale?: string, timeZone?: string }} [options] - Locale / time-zone overrides.
|
|
110
|
+
* @returns {string} The formatted date.
|
|
111
|
+
*/
|
|
112
|
+
function format(date: Date | number | string, pattern?: string, options?: {
|
|
113
|
+
locale?: string;
|
|
114
|
+
timeZone?: string;
|
|
115
|
+
}): string;
|
|
116
|
+
/**
|
|
117
|
+
* Adds a duration to a date and returns a new Date.
|
|
118
|
+
*
|
|
119
|
+
* The duration may be a millisecond number or a string like `"1h30m"`.
|
|
120
|
+
*
|
|
121
|
+
* @example
|
|
122
|
+
* nodeComfort.time.add(new Date(), "2h");
|
|
123
|
+
* nodeComfort.time.add(new Date(), -1, "days");
|
|
124
|
+
*
|
|
125
|
+
* @param {Date|number|string} date - Base date.
|
|
126
|
+
* @param {number|string} amount - Amount (ms/string) or a number paired with `unit`.
|
|
127
|
+
* @param {"ms"|"s"|"m"|"h"|"d"|"w"} [unit] - Unit when `amount` is a plain number.
|
|
128
|
+
* @returns {Date} The resulting date.
|
|
129
|
+
*/
|
|
130
|
+
function add(date: Date | number | string, amount: number | string, unit?: "ms" | "s" | "m" | "h" | "d" | "w"): Date;
|
|
131
|
+
/**
|
|
132
|
+
* Subtracts a duration from a date and returns a new Date.
|
|
133
|
+
*
|
|
134
|
+
* @param {Date|number|string} date - Base date.
|
|
135
|
+
* @param {number|string} amount - Amount (ms/string) or a number paired with `unit`.
|
|
136
|
+
* @param {"ms"|"s"|"m"|"h"|"d"|"w"} [unit] - Unit when `amount` is a plain number.
|
|
137
|
+
* @returns {Date} The resulting date.
|
|
138
|
+
*/
|
|
139
|
+
function subtract(date: Date | number | string, amount: number | string, unit?: "ms" | "s" | "m" | "h" | "d" | "w"): Date;
|
|
140
|
+
/**
|
|
141
|
+
* Returns the difference between two dates in the requested unit.
|
|
142
|
+
*
|
|
143
|
+
* @example
|
|
144
|
+
* nodeComfort.time.diff("2024-01-02", "2024-01-01", "d"); // 1
|
|
145
|
+
*
|
|
146
|
+
* @param {Date|number|string} a - First date.
|
|
147
|
+
* @param {Date|number|string} b - Second date.
|
|
148
|
+
* @param {"ms"|"s"|"m"|"h"|"d"|"w"} [unit="ms"] - Result unit.
|
|
149
|
+
* @returns {number} The signed difference (`a - b`) in the given unit.
|
|
150
|
+
*/
|
|
151
|
+
function diff(a: Date | number | string, b: Date | number | string, unit?: "ms" | "s" | "m" | "h" | "d" | "w"): number;
|
|
152
|
+
/**
|
|
153
|
+
* Checks whether two dates fall on the same calendar day.
|
|
154
|
+
*
|
|
155
|
+
* Uses local time by default; pass `options.timeZone` (or set a global zone)
|
|
156
|
+
* to compare calendar days in a specific zone.
|
|
157
|
+
*
|
|
158
|
+
* @param {Date|number|string} a - First date.
|
|
159
|
+
* @param {Date|number|string} b - Second date.
|
|
160
|
+
* @param {{ timeZone?: string }} [options] - Time-zone override.
|
|
161
|
+
* @returns {boolean} True if both are the same day.
|
|
162
|
+
*/
|
|
163
|
+
function isSameDay(a: Date | number | string, b: Date | number | string, options?: {
|
|
164
|
+
timeZone?: string;
|
|
165
|
+
}): boolean;
|
|
166
|
+
/**
|
|
167
|
+
* Returns a new Date at the start of the given unit (day/hour/etc).
|
|
168
|
+
*
|
|
169
|
+
* @example
|
|
170
|
+
* nodeComfort.time.startOf(new Date(), "day"); // midnight today
|
|
171
|
+
*
|
|
172
|
+
* @param {Date|number|string} date - Base date.
|
|
173
|
+
* @param {"year"|"month"|"day"|"hour"|"minute"|"second"} unit - Unit to reset to.
|
|
174
|
+
* @returns {Date} The start-of-unit date.
|
|
175
|
+
*/
|
|
176
|
+
function startOf(date: Date | number | string, unit: "year" | "month" | "day" | "hour" | "minute" | "second"): Date;
|
|
177
|
+
/**
|
|
178
|
+
* Returns the current Unix timestamp in seconds.
|
|
179
|
+
*
|
|
180
|
+
* @returns {number} Seconds since the Unix epoch.
|
|
181
|
+
*/
|
|
182
|
+
function unix(): number;
|
|
183
|
+
/**
|
|
184
|
+
* Returns a high-resolution stopwatch you can `.stop()` to get elapsed ms.
|
|
185
|
+
*
|
|
186
|
+
* @example
|
|
187
|
+
* const timer = nodeComfort.time.stopwatch();
|
|
188
|
+
* doWork();
|
|
189
|
+
* console.log(timer.stop()); // e.g. 12.34 (ms, sub-ms precise)
|
|
190
|
+
* console.log(timer.stop(true)); // "12.34ms"
|
|
191
|
+
*
|
|
192
|
+
* @returns {{ elapsed: () => number, stop: (format?: boolean) => number|string }} A stopwatch handle.
|
|
193
|
+
*/
|
|
194
|
+
function stopwatch(): {
|
|
195
|
+
elapsed: () => number;
|
|
196
|
+
stop: (format?: boolean) => number | string;
|
|
197
|
+
};
|
|
198
|
+
}
|
|
199
|
+
export = _exports;
|
|
200
|
+
/**
|
|
201
|
+
* The time module itself • used as the return type of the chainable setters so
|
|
202
|
+
* `setLocale().setTimezone()...` stays fully typed without inlining the type.
|
|
203
|
+
*/
|
|
204
|
+
type TimeModule = typeof import("./Time");
|
|
205
|
+
//# sourceMappingURL=Time.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Time.d.ts","sourceRoot":"","sources":["../../src/Time.js"],"names":[],"mappings":";;;;IA2HE;;;;;;;;;OASG;IACH,2BAHW,MAAM,GACJ,UAAU,CAKtB;IAED;;;;;;;;;OASG;IACH,+BAHW,MAAM,GAAC,SAAS,GACd,UAAU,CAKtB;IAED;;;;OAIG;IACH,sBAFa;QAAE,MAAM,EAAE,MAAM,CAAC;QAAC,QAAQ,EAAE,MAAM,GAAC,SAAS,CAAA;KAAE,CAI1D;IAED;;;;;;;;;;;;OAYG;IACH,8BAHW,MAAM,GAAC,MAAM,GACX,MAAM,GAAC,IAAI,CAkBvB;IAED;;;;;;;;;;;;;;;OAeG;IACH,4BAJW,MAAM,YACN;QAAE,IAAI,CAAC,EAAE,OAAO,CAAC;QAAC,KAAK,CAAC,EAAE,MAAM,CAAC;QAAC,MAAM,CAAC,EAAE,MAAM,CAAA;KAAE,GACjD,MAAM,CAgClB;IAED;;;;;;;;;;;;;;;;OAgBG;IACH,wBALW,IAAI,GAAC,MAAM,GAAC,MAAM,SAClB,IAAI,GAAC,MAAM,GAAC,MAAM,YAClB;QAAE,MAAM,CAAC,EAAE,MAAM,CAAC;QAAC,OAAO,CAAC,EAAE,QAAQ,GAAC,MAAM,CAAA;KAAE,GAC5C,MAAM,CAgClB;IAED;;;;;;;;;;;;;;;;;;;OAmBG;IACH,sBALW,IAAI,GAAC,MAAM,GAAC,MAAM,YAClB,MAAM,YACN;QAAE,MAAM,CAAC,EAAE,MAAM,CAAC;QAAC,QAAQ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|
|
3
|
+
*
|
|
4
|
+
* @example
|
|
5
|
+
* await nodeComfort.wait(500); // waits 500 ms
|
|
6
|
+
*
|
|
7
|
+
* @param {number} ms - Delay in milliseconds.
|
|
8
|
+
* @returns {Promise<void>} A promise that resolves after the delay.
|
|
9
|
+
*/
|
|
10
|
+
export function wait(ms: number): Promise<void>;
|
|
11
|
+
/**
|
|
12
|
+
* Polls a predicate at a given interval and emits events when it matches or times out.
|
|
13
|
+
*
|
|
14
|
+
* The predicate is evaluated every `options.interval` milliseconds (default: 50 ms).
|
|
15
|
+
* Be careful with the default interval: 50 ms is very frequent polling and can be
|
|
16
|
+
* unnecessarily CPU‑intensive if your predicate or trigger handlers are heavy.
|
|
17
|
+
* When `options.max` is `null`, the predicate can match indefinitely (infinite triggers),
|
|
18
|
+
* so you should choose an interval that makes sense for your use‑case.
|
|
19
|
+
*
|
|
20
|
+
* A `when(...)` task is one‑shot: once it has been stopped (manually or via `max`/`timeout`),
|
|
21
|
+
* calling `.start()` again on the same task will not restart it.
|
|
22
|
+
*
|
|
23
|
+
* @param {boolean|Promise<boolean>|(() => boolean|Promise<boolean>)} predicate
|
|
24
|
+
* Condition to evaluate at each tick, or a function returning it.
|
|
25
|
+
* @param {*} [payload={}]
|
|
26
|
+
* Value passed to listeners when events are emitted (e.g. an object or message).
|
|
27
|
+
* @param {object} [options]
|
|
28
|
+
* @param {number} [options.interval=50]
|
|
29
|
+
* Polling interval in milliseconds. Increase this value if your predicate is
|
|
30
|
+
* expensive or you expect many triggers without a `max` limit.
|
|
31
|
+
* @param {number|null} [options.timeout=null]
|
|
32
|
+
* Maximum time in milliseconds before emitting a "timeout" event and stopping.
|
|
33
|
+
* If `null`, no timeout is applied.
|
|
34
|
+
* @param {number|null} [options.max=null]
|
|
35
|
+
* Maximum number of successful predicate evaluations ("trigger" events) before
|
|
36
|
+
* automatically stopping. If `null`, triggers are infinite until `stop()` is called.
|
|
37
|
+
*
|
|
38
|
+
* @example
|
|
39
|
+
* // Basic usage: infinite triggers every 50 ms (default interval) – use with caution.
|
|
40
|
+
* const task = nodeComfort.when(() => true, { message: "Ok" });
|
|
41
|
+
*
|
|
42
|
+
* task
|
|
43
|
+
* .on("trigger", (payload) => {
|
|
44
|
+
* console.log(payload.message);
|
|
45
|
+
* // task.stop(); // stop when you’re done
|
|
46
|
+
* })
|
|
47
|
+
* .start();
|
|
48
|
+
*
|
|
49
|
+
* @example
|
|
50
|
+
* // Safer usage: limit triggers and/or lower polling frequency.
|
|
51
|
+
* nodeComfort.when(() => true, { message: "Ok" }, { max: 1, interval: 500 })
|
|
52
|
+
* .on("trigger", (payload) => {
|
|
53
|
+
* console.log(payload.message);
|
|
54
|
+
* })
|
|
55
|
+
* .on("timeout", (payload) => {
|
|
56
|
+
* console.log("Timed out", payload);
|
|
57
|
+
* })
|
|
58
|
+
* .start();
|
|
59
|
+
*
|
|
60
|
+
* @returns {{
|
|
61
|
+
* start: () => any,
|
|
62
|
+
* stop: () => any,
|
|
63
|
+
* on: <E extends "error"|"trigger"|"timeout">(event: E, handler: E extends "error"
|
|
64
|
+
* ? (error: unknown) => void|Promise<void>
|
|
65
|
+
* : (payload: any) => void|Promise<void>) => any
|
|
66
|
+
* }} A small task controller with `start`, `stop` and `on`.
|
|
67
|
+
*/
|
|
68
|
+
export function when(predicate: boolean | Promise<boolean> | (() => boolean | Promise<boolean>), payload?: any, options?: {
|
|
69
|
+
interval?: number | undefined;
|
|
70
|
+
timeout?: number | null | undefined;
|
|
71
|
+
max?: number | null | undefined;
|
|
72
|
+
}): {
|
|
73
|
+
start: () => any;
|
|
74
|
+
stop: () => any;
|
|
75
|
+
on: <E extends "error" | "trigger" | "timeout">(event: E, handler: E extends "error" ? (error: unknown) => void | Promise<void> : (payload: any) => void | Promise<void>) => any;
|
|
76
|
+
};
|
|
77
|
+
/**
|
|
78
|
+
* Installs process-level safety nets for errors, exits and signals.
|
|
79
|
+
*
|
|
80
|
+
* By default, it:
|
|
81
|
+
* - logs uncaught exceptions and unhandled rejections,
|
|
82
|
+
* - logs process exit codes,
|
|
83
|
+
* - logs SIGINT/SIGTERM/SIGQUIT and exits with code 0.
|
|
84
|
+
*
|
|
85
|
+
* You can override each handler with `.on(event, handler)`.
|
|
86
|
+
*
|
|
87
|
+
* @example
|
|
88
|
+
* // Use defaults:
|
|
89
|
+
* nodeComfort.dontCrash();
|
|
90
|
+
*
|
|
91
|
+
* @example
|
|
92
|
+
* // Custom error handler:
|
|
93
|
+
* nodeComfort.dontCrash().on("error", (err) => {
|
|
94
|
+
* console.error("Global error:", err);
|
|
95
|
+
* process.exit(1);
|
|
96
|
+
* });
|
|
97
|
+
*
|
|
98
|
+
* @returns {{
|
|
99
|
+
* on: <E extends "error"|"exit"|"sig"|"beforeExit">(event: E, handler?: (
|
|
100
|
+
* E extends "error" ? (error: Error|unknown) => void|Promise<void> :
|
|
101
|
+
* E extends "exit" ? (code: number) => void|Promise<void> :
|
|
102
|
+
* E extends "sig" ? (signal: "SIGINT"|"SIGTERM"|"SIGQUIT") => void|Promise<void> :
|
|
103
|
+
* (code: number) => void|Promise<void>
|
|
104
|
+
* )) => any
|
|
105
|
+
* }} A small controller to configure global handlers.
|
|
106
|
+
*/
|
|
107
|
+
export function dontCrash(): {
|
|
108
|
+
on: <E extends "error" | "exit" | "sig" | "beforeExit">(event: E, handler?: (E extends "error" ? (error: Error | unknown) => void | Promise<void> : E extends "exit" ? (code: number) => void | Promise<void> : E extends "sig" ? (signal: "SIGINT" | "SIGTERM" | "SIGQUIT") => void | Promise<void> : (code: number) => void | Promise<void>)) => any;
|
|
109
|
+
};
|
|
110
|
+
/**
|
|
111
|
+
* Safely stringifies a value to pretty-printed JSON (4 spaces indent).
|
|
112
|
+
*
|
|
113
|
+
* @param {*} value - Value to stringify.
|
|
114
|
+
* @returns {string} JSON string representation.
|
|
115
|
+
*/
|
|
116
|
+
export function JSONString(value: any): string;
|
|
117
|
+
/**
|
|
118
|
+
* Parses a JSON string.
|
|
119
|
+
*
|
|
120
|
+
* @param {string} value - JSON string to parse.
|
|
121
|
+
* @returns {*} Parsed value.
|
|
122
|
+
*/
|
|
123
|
+
export function JSONParse(value: string): any;
|
|
124
|
+
//# sourceMappingURL=Utils.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Utils.d.ts","sourceRoot":"","sources":["../../src/Utils.js"],"names":[],"mappings":"AAME;;;;;;;;GAQG;AACH,yBAHW,MAAM,GACJ,OAAO,CAAC,IAAI,CAAC,CAIzB;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAwDG;AACH,gCA7CW,OAAO,GAAC,OAAO,CAAC,OAAO,CAAC,GAAC,CAAC,MAAM,OAAO,GAAC,OAAO,CAAC,OAAO,CAAC,CAAC,YAEzD,GAAC,YAGT;IAAyB,QAAQ;IAGH,OAAO;IAGP,GAAG;CAIjC,GAsBU;IACR,KAAK,EAAE,MAAM,GAAG,CAAC;IACjB,IAAI,EAAE,MAAM,GAAG,CAAC;IAChB,EAAE,EAAE,CAAC,CAAC,SAAS,OAAO,GAAC,SAAS,GAAC,SAAS,EAAE,KAAK,EAAE,CAAC,EAAE,OAAO,EAAE,CAAC,SAAS,OAAO,GAC1E,CAAC,KAAK,EAAE,OAAO,KAAK,IAAI,GAAC,OAAO,CAAC,IAAI,CAAC,GACtC,CAAC,OAAO,EAAE,GAAG,KAAK,IAAI,GAAC,OAAO,CAAC,IAAI,CAAC,KAAK,GAAG,CAAA;CACnD,CAsIH;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AACH,6BATa;IACR,EAAE,EAAE,CAAC,CAAC,SAAS,OAAO,GAAC,MAAM,GAAC,KAAK,GAAC,YAAY,EAAE,KAAK,EAAE,CAAC,EAAE,OAAO,CAAC,EAAE,CACpE,CAAC,SAAS,OAAO,GAAG,CAAC,KAAK,EAAE,KAAK,GAAC,OAAO,KAAK,IAAI,GAAC,OAAO,CAAC,IAAI,CAAC,G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