@wikicasa-dev/node-common 4.7.12 → 5.0.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/dist/examples/callpool-demo.js +14 -13
- package/dist/examples/callpool-demo.js.map +1 -1
- package/dist/examples/console-demo.js +25 -25
- package/dist/examples/console-demo.js.map +1 -1
- package/dist/src/Cache.d.ts +39 -2
- package/dist/src/Cache.js +44 -6
- package/dist/src/Cache.js.map +1 -1
- package/dist/src/CacheES.d.ts +28 -0
- package/dist/src/CacheES.js +34 -4
- package/dist/src/CacheES.js.map +1 -1
- package/dist/src/CallPool.d.ts +71 -5
- package/dist/src/CallPool.js +82 -9
- package/dist/src/CallPool.js.map +1 -1
- package/dist/src/Console.d.ts +21 -58
- package/dist/src/Console.js +80 -79
- package/dist/src/Console.js.map +1 -1
- package/dist/src/Crawler.d.ts +18 -0
- package/dist/src/Crawler.js +20 -2
- package/dist/src/Crawler.js.map +1 -1
- package/dist/src/Pool.d.ts +19 -0
- package/dist/src/Pool.js +21 -2
- package/dist/src/Pool.js.map +1 -1
- package/dist/src/common.d.ts +74 -3
- package/dist/src/common.js +80 -7
- package/dist/src/common.js.map +1 -1
- package/dist/src/utils.d.ts +165 -26
- package/dist/src/utils.js +165 -26
- package/dist/src/utils.js.map +1 -1
- package/package.json +20 -23
package/dist/src/common.js
CHANGED
|
@@ -10,6 +10,14 @@ export class CustomError extends Error {
|
|
|
10
10
|
this.critical = critical;
|
|
11
11
|
}
|
|
12
12
|
}
|
|
13
|
+
/**
|
|
14
|
+
* Execute promise factories in sequential batches, resolving groups of `step` at a time.
|
|
15
|
+
*
|
|
16
|
+
* @param promises - Array of async factory functions
|
|
17
|
+
* @param step - Number of promises to run concurrently per batch
|
|
18
|
+
* @param exit - Reserved for future use
|
|
19
|
+
* @returns Flat array of all results
|
|
20
|
+
*/
|
|
13
21
|
export function sequentially(promises, step = 1, exit = false) {
|
|
14
22
|
return new Promise(resolve => {
|
|
15
23
|
let results = [];
|
|
@@ -34,6 +42,14 @@ export function sequentially(promises, step = 1, exit = false) {
|
|
|
34
42
|
});
|
|
35
43
|
});
|
|
36
44
|
}
|
|
45
|
+
/**
|
|
46
|
+
* Execute promise factories with bounded concurrency, optionally halting on error.
|
|
47
|
+
*
|
|
48
|
+
* @param promises - Array of async factory functions
|
|
49
|
+
* @param concurrent - Maximum number of concurrent executions
|
|
50
|
+
* @param stopIfError - If true, reject and stop the pool on the first error
|
|
51
|
+
* @returns Array of results in corresponding order
|
|
52
|
+
*/
|
|
37
53
|
export function pool(promises, concurrent, stopIfError = true) {
|
|
38
54
|
return new Promise((resolve, reject) => {
|
|
39
55
|
PromisePool.for(promises)
|
|
@@ -50,6 +66,13 @@ export function pool(promises, concurrent, stopIfError = true) {
|
|
|
50
66
|
.catch((poolResult) => reject(poolResult.errors));
|
|
51
67
|
});
|
|
52
68
|
}
|
|
69
|
+
/**
|
|
70
|
+
* Try each async factory in order and return the first truthy result; throw if none succeeds.
|
|
71
|
+
*
|
|
72
|
+
* @param fns - Array of async factory functions to try sequentially
|
|
73
|
+
* @returns First truthy resolved value
|
|
74
|
+
* @throws {string} When no factory returns a truthy value
|
|
75
|
+
*/
|
|
53
76
|
export async function promisesFirst(fns) {
|
|
54
77
|
for await (const f of fns) {
|
|
55
78
|
try {
|
|
@@ -59,12 +82,17 @@ export async function promisesFirst(fns) {
|
|
|
59
82
|
}
|
|
60
83
|
}
|
|
61
84
|
catch (e) {
|
|
62
|
-
//
|
|
63
|
-
//
|
|
85
|
+
// ignored: expected failure for sequential first-match pattern
|
|
64
86
|
}
|
|
65
87
|
}
|
|
66
88
|
throw "No promises resolve";
|
|
67
89
|
}
|
|
90
|
+
/**
|
|
91
|
+
* Read and decompress a Brotli-compressed JSON cache file from disk.
|
|
92
|
+
*
|
|
93
|
+
* @param path - Base file path (`.br` extension is appended automatically)
|
|
94
|
+
* @returns Parsed JSON data or null if the file is missing or corrupted
|
|
95
|
+
*/
|
|
68
96
|
export function readCache(path) {
|
|
69
97
|
const brotliPath = `${path}.br`;
|
|
70
98
|
if (!fs.existsSync(brotliPath))
|
|
@@ -74,19 +102,34 @@ export function readCache(path) {
|
|
|
74
102
|
data = JSON.parse(brotliDecompressSync(fs.readFileSync(brotliPath)).toString());
|
|
75
103
|
}
|
|
76
104
|
catch (e) {
|
|
105
|
+
// ignored: corrupted cache returns null by design
|
|
77
106
|
data = null;
|
|
78
107
|
}
|
|
79
108
|
return data;
|
|
80
109
|
}
|
|
110
|
+
/**
|
|
111
|
+
* Serialise data as JSON, compress with Brotli, and write to disk.
|
|
112
|
+
*
|
|
113
|
+
* @param path - Base file path (`.br` extension is appended automatically)
|
|
114
|
+
* @param data - Data to serialise and cache
|
|
115
|
+
*/
|
|
81
116
|
export function writeCache(path, data) {
|
|
82
117
|
const brotliPath = `${path}.br`;
|
|
83
118
|
try {
|
|
84
119
|
fs.writeFileSync(brotliPath, brotliCompressSync(JSON.stringify(data)), "binary");
|
|
85
120
|
}
|
|
86
121
|
catch (e) {
|
|
87
|
-
|
|
122
|
+
Console.error("Cache write to file failed", { path, error: e });
|
|
88
123
|
}
|
|
89
124
|
}
|
|
125
|
+
/**
|
|
126
|
+
* Read and parse a JSON file from disk.
|
|
127
|
+
*
|
|
128
|
+
* @param path - File path
|
|
129
|
+
* @param strict - If true, throw when the file does not exist
|
|
130
|
+
* @returns Parsed object or null on failure
|
|
131
|
+
* @throws {Error} When strict is true and the file is missing
|
|
132
|
+
*/
|
|
90
133
|
export function readJson(path, strict = false) {
|
|
91
134
|
if (!fs.existsSync(path) && strict)
|
|
92
135
|
throw new Error(`File ${path} not found`);
|
|
@@ -97,10 +140,20 @@ export function readJson(path, strict = false) {
|
|
|
97
140
|
json = JSON.parse(fs.readFileSync(path, "utf8"));
|
|
98
141
|
}
|
|
99
142
|
catch (e) {
|
|
143
|
+
// ignored: invalid JSON returns null by design
|
|
100
144
|
json = null;
|
|
101
145
|
}
|
|
102
146
|
return json;
|
|
103
147
|
}
|
|
148
|
+
/**
|
|
149
|
+
* Read and parse a CSV file into an array of row objects.
|
|
150
|
+
*
|
|
151
|
+
* @param path - File path
|
|
152
|
+
* @param delimiter - Column delimiter character
|
|
153
|
+
* @param strict - If true, throw when the file does not exist
|
|
154
|
+
* @returns Array of row objects or null on failure
|
|
155
|
+
* @throws {Error} When strict is true and the file is missing
|
|
156
|
+
*/
|
|
104
157
|
export function readCSV(path, delimiter = ",", strict = false) {
|
|
105
158
|
if (!fs.existsSync(path) && strict)
|
|
106
159
|
throw new Error(`File ${path} not found`);
|
|
@@ -115,10 +168,18 @@ export function readCSV(path, delimiter = ",", strict = false) {
|
|
|
115
168
|
});
|
|
116
169
|
}
|
|
117
170
|
catch (e) {
|
|
171
|
+
// ignored: invalid CSV returns null by design
|
|
118
172
|
csv = null;
|
|
119
173
|
}
|
|
120
174
|
return csv;
|
|
121
175
|
}
|
|
176
|
+
/**
|
|
177
|
+
* Group array elements into a Map keyed by the result of a getter function.
|
|
178
|
+
*
|
|
179
|
+
* @param list - Source array
|
|
180
|
+
* @param keyGetter - Function that extracts the grouping key from each item
|
|
181
|
+
* @returns Map of key to array of matching items
|
|
182
|
+
*/
|
|
122
183
|
export function groupBy(list, keyGetter) {
|
|
123
184
|
const map = new Map();
|
|
124
185
|
for (const item of list ?? []) {
|
|
@@ -133,6 +194,12 @@ export function groupBy(list, keyGetter) {
|
|
|
133
194
|
}
|
|
134
195
|
return map;
|
|
135
196
|
}
|
|
197
|
+
/**
|
|
198
|
+
* Pause execution for a given number of milliseconds, optionally logging a countdown spinner.
|
|
199
|
+
*
|
|
200
|
+
* @param ms - Duration in milliseconds
|
|
201
|
+
* @param feedback - If true, display a countdown spinner in the console
|
|
202
|
+
*/
|
|
136
203
|
export function sleep(ms, feedback = false) {
|
|
137
204
|
return new Promise(resolve => {
|
|
138
205
|
const t = performance.now();
|
|
@@ -140,19 +207,25 @@ export function sleep(ms, feedback = false) {
|
|
|
140
207
|
if (feedback)
|
|
141
208
|
setInterval(function () {
|
|
142
209
|
const left = (ms - (performance.now() - t)) / 1000;
|
|
143
|
-
Console.log(
|
|
210
|
+
Console.log("Time left", { seconds: Math.round(left) }, { spinner: true });
|
|
144
211
|
}, 1000);
|
|
145
212
|
});
|
|
146
213
|
}
|
|
214
|
+
/**
|
|
215
|
+
* Deduplicate an array using Set, preserving insertion order.
|
|
216
|
+
*
|
|
217
|
+
* @param array - Source array
|
|
218
|
+
* @returns New array without duplicates, or undefined if empty/null
|
|
219
|
+
*/
|
|
147
220
|
export function unique(array) {
|
|
148
221
|
if (!array || !array.length)
|
|
149
222
|
return;
|
|
150
223
|
return Array.from(new Set(array));
|
|
151
224
|
}
|
|
152
225
|
/**
|
|
153
|
-
*
|
|
154
|
-
* @param str
|
|
155
|
-
* @returns
|
|
226
|
+
* Checks whether a string is valid JSON
|
|
227
|
+
* @param str - String to validate
|
|
228
|
+
* @returns Whether the string is valid JSON
|
|
156
229
|
*/
|
|
157
230
|
export function isValidJSON(str) {
|
|
158
231
|
if (typeof str !== "string")
|
package/dist/src/common.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"common.js","sourceRoot":"","sources":["../../src/common.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,WAAW,EAAE,MAAM,2BAA2B,CAAC;AACxD,OAAO,EAAE,KAAK,EAAE,MAAM,gBAAgB,CAAC;AACvC,OAAO,EAAE,MAAM,IAAI,CAAC;AACpB,OAAO,EAAE,kBAAkB,EAAE,oBAAoB,EAAE,MAAM,MAAM,CAAC;AAChE,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAEvC,MAAM,OAAO,WAAY,SAAQ,KAAK;IAClC,QAAQ,CAAU;IAElB,YAAY,OAAe,EAAE,WAAoB,KAAK;QAClD,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;IAC7B,CAAC;CACJ;AAED,MAAM,UAAU,YAAY,CACxB,QAAuC,EAC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ChB,CAAC;IAAC,OAAO,CAAC,EAAE,CAAC;QACT,OAAO,KAAK,CAAC;IACjB,CAAC;AACL,CAAC"}
|
package/dist/src/utils.d.ts
CHANGED
|
@@ -1,68 +1,207 @@
|
|
|
1
1
|
import hash from "object-hash";
|
|
2
|
+
/**
|
|
3
|
+
* Rotate an array left by n positions, mutating it in place.
|
|
4
|
+
*
|
|
5
|
+
* @param arr - Array to rotate
|
|
6
|
+
* @param n - Number of positions to shift
|
|
7
|
+
* @returns The mutated array
|
|
8
|
+
*/
|
|
2
9
|
export declare function arrayRotate<T>(arr: Array<T>, n: any): Array<T>;
|
|
10
|
+
/**
|
|
11
|
+
* Strip all non-ASCII characters after normalising typographic quotes.
|
|
12
|
+
*
|
|
13
|
+
* @param str - Input string
|
|
14
|
+
* @returns ASCII-only string
|
|
15
|
+
*/
|
|
3
16
|
export declare function cleanASCII(str: string): string;
|
|
17
|
+
/**
|
|
18
|
+
* Replace typographic apostrophes and smart quotes with their ASCII equivalents.
|
|
19
|
+
*
|
|
20
|
+
* @param str - Input string
|
|
21
|
+
* @returns Normalised string
|
|
22
|
+
*/
|
|
4
23
|
export declare function cleanUpSpecialChars(str: string): string;
|
|
24
|
+
/**
|
|
25
|
+
* Normalise special characters and strip all non-word characters.
|
|
26
|
+
*
|
|
27
|
+
* @param str - Input string
|
|
28
|
+
* @returns Alphanumeric-only string
|
|
29
|
+
*/
|
|
5
30
|
export declare function normalizeString(str: string): string;
|
|
31
|
+
/**
|
|
32
|
+
* Collapse consecutive whitespace/tabs to a single space and consecutive newlines to one.
|
|
33
|
+
*
|
|
34
|
+
* @param str - Input string
|
|
35
|
+
* @returns Trimmed and collapsed string
|
|
36
|
+
*/
|
|
6
37
|
export declare function trimAll(str: string): string;
|
|
38
|
+
/**
|
|
39
|
+
* Replace accented Latin characters with their unaccented ASCII equivalents.
|
|
40
|
+
*
|
|
41
|
+
* @param str - Input string
|
|
42
|
+
* @returns Transliterated string
|
|
43
|
+
*/
|
|
7
44
|
export declare function cleanUpSpecialChars2(str: string): string;
|
|
45
|
+
/**
|
|
46
|
+
* Capitalise the first letter of each word and lowercase the rest.
|
|
47
|
+
*
|
|
48
|
+
* @param str - Input string
|
|
49
|
+
* @returns Title-cased string
|
|
50
|
+
*/
|
|
8
51
|
export declare function capitalizeWords(str: string): string;
|
|
52
|
+
/**
|
|
53
|
+
* Uppercase only the first character of a string, leaving the rest unchanged.
|
|
54
|
+
*
|
|
55
|
+
* @param str - Input string
|
|
56
|
+
* @returns String with first letter capitalised, or undefined if empty
|
|
57
|
+
*/
|
|
9
58
|
export declare function capitalizeFirstLetter(str: string): string | undefined;
|
|
59
|
+
/**
|
|
60
|
+
* Sort an array of objects in ascending order by a given property (in place).
|
|
61
|
+
*
|
|
62
|
+
* @param arr - Array to sort
|
|
63
|
+
* @param prop - Property key to sort by
|
|
64
|
+
* @returns The sorted array
|
|
65
|
+
*/
|
|
10
66
|
export declare function orderBy<T extends object>(arr: Array<T>, prop: any): Array<T>;
|
|
67
|
+
/**
|
|
68
|
+
* Zero-pad a number to a fixed width.
|
|
69
|
+
*
|
|
70
|
+
* @param number - Value to pad
|
|
71
|
+
* @param digits - Desired string length
|
|
72
|
+
* @returns Zero-padded string
|
|
73
|
+
*/
|
|
11
74
|
export declare function pan(number: number, digits: number): string;
|
|
75
|
+
/**
|
|
76
|
+
* Deduplicate an array using a custom equality comparator.
|
|
77
|
+
*
|
|
78
|
+
* @param array - Source array
|
|
79
|
+
* @param compareFn - Returns true when two elements are considered equal
|
|
80
|
+
* @returns New array with duplicates removed
|
|
81
|
+
*/
|
|
12
82
|
export declare function uniqWith<T>(array: Array<T>, compareFn: any): Array<T>;
|
|
83
|
+
/**
|
|
84
|
+
* Return the first n elements of an array without mutating the original.
|
|
85
|
+
*
|
|
86
|
+
* @param array - Source array
|
|
87
|
+
* @param n - Maximum number of elements to take
|
|
88
|
+
* @returns New array of up to n elements
|
|
89
|
+
*/
|
|
13
90
|
export declare function pop<T>(array: Array<T>, n: number): Array<T>;
|
|
91
|
+
/**
|
|
92
|
+
* Split an array into sub-arrays of a given size.
|
|
93
|
+
*
|
|
94
|
+
* @param array - Source array
|
|
95
|
+
* @param chunkSize - Maximum elements per chunk
|
|
96
|
+
* @returns Array of chunks
|
|
97
|
+
*/
|
|
14
98
|
export declare function splitChunks<T>(array: Array<T>, chunkSize: number): Array<T>;
|
|
99
|
+
/**
|
|
100
|
+
* Remove the first element matching a predicate by nullifying it, then filter nulls.
|
|
101
|
+
*
|
|
102
|
+
* @param list - Source array (mutated)
|
|
103
|
+
* @param fn - Predicate identifying the element to remove
|
|
104
|
+
* @returns Filtered array without the matched element
|
|
105
|
+
*/
|
|
15
106
|
export declare function newpopList<T>(list: Array<T>, fn?: (a: T) => boolean): Array<T>;
|
|
107
|
+
/**
|
|
108
|
+
* Extract elements common to both lists (intersection), mutating both lists to remove matched items.
|
|
109
|
+
*
|
|
110
|
+
* @param list1 - First list (mutated: matched items nullified)
|
|
111
|
+
* @param list2 - Second list (mutated: matched items nullified)
|
|
112
|
+
* @param fn - Optional hash function for comparison; defaults to object-hash
|
|
113
|
+
* @returns Array of elements found in both lists
|
|
114
|
+
*/
|
|
16
115
|
export declare function popList<T extends hash.NotUndefined>(list1: Array<T>, list2: Array<T>, fn?: (a: T) => string): Array<T>;
|
|
116
|
+
/**
|
|
117
|
+
* Compute the symmetric difference between two lists (elements unique to either side).
|
|
118
|
+
*
|
|
119
|
+
* @param list1 - First list
|
|
120
|
+
* @param list2 - Second list
|
|
121
|
+
* @param fn - Optional hash function for comparison; defaults to object-hash
|
|
122
|
+
* @returns Elements that appear in only one of the two lists
|
|
123
|
+
*/
|
|
17
124
|
export declare function diffList<T extends hash.NotUndefined>(list1: Array<T>, list2: Array<T>, fn?: any): Array<T>;
|
|
125
|
+
/**
|
|
126
|
+
* Truncate a number to a fixed number of decimal places (no rounding).
|
|
127
|
+
*
|
|
128
|
+
* @param num - Value to truncate
|
|
129
|
+
* @param digits - Number of decimal places to keep
|
|
130
|
+
* @returns Truncated number
|
|
131
|
+
*/
|
|
18
132
|
export declare function truncator(num: any, digits: any): number;
|
|
133
|
+
/**
|
|
134
|
+
* Parse a localised number string by stripping leading non-digits and thousand separators.
|
|
135
|
+
*
|
|
136
|
+
* @param num - Localised number string (e.g. "EUR 1.200")
|
|
137
|
+
* @param thousand - Thousand separator character to remove
|
|
138
|
+
* @returns Parsed float
|
|
139
|
+
*/
|
|
19
140
|
export declare function parseNumber(num: string, thousand?: string): number;
|
|
141
|
+
/**
|
|
142
|
+
* Pick a uniformly random element from an array.
|
|
143
|
+
*
|
|
144
|
+
* @param arr - Source array
|
|
145
|
+
* @returns Random element
|
|
146
|
+
*/
|
|
20
147
|
export declare function getRandom<T>(arr: Array<T>): T;
|
|
148
|
+
/**
|
|
149
|
+
* Randomly shuffle an array in place using the Fisher-Yates algorithm.
|
|
150
|
+
*
|
|
151
|
+
* @param array - Array to shuffle (mutated)
|
|
152
|
+
* @returns The shuffled array
|
|
153
|
+
*/
|
|
21
154
|
export declare function shuffleArray<T>(array: T[]): T[];
|
|
155
|
+
/**
|
|
156
|
+
* Extract the first number (including thousand separators and decimals) from a string.
|
|
157
|
+
*
|
|
158
|
+
* @param str - Input string containing a number
|
|
159
|
+
* @returns First matched number as a string, or undefined
|
|
160
|
+
*/
|
|
22
161
|
export declare function extractNumbers(str: string): string | undefined;
|
|
23
162
|
/**
|
|
24
|
-
*
|
|
25
|
-
* @param str -
|
|
26
|
-
* @param integerIfAmbiguous -
|
|
27
|
-
* @returns
|
|
163
|
+
* Parses a number from a string, stripping all non-numeric characters
|
|
164
|
+
* @param str - The string to parse
|
|
165
|
+
* @param integerIfAmbiguous - If true (default), ambiguous inputs are parsed as integers
|
|
166
|
+
* @returns The parsed number, or undefined if the string is invalid
|
|
28
167
|
*/
|
|
29
168
|
export declare function parseNumberFromString(str: string, integerIfAmbiguous?: boolean): number | undefined;
|
|
30
169
|
/**
|
|
31
|
-
* @deprecated
|
|
32
|
-
*
|
|
33
|
-
* @param str -
|
|
34
|
-
* @returns
|
|
170
|
+
* @deprecated Use {@link parseNumberFromString} instead
|
|
171
|
+
* Parses a float from a string, stripping non-numeric characters
|
|
172
|
+
* @param str - The string to parse
|
|
173
|
+
* @returns The parsed number, or undefined if the string is invalid
|
|
35
174
|
*/
|
|
36
175
|
export declare function parseFloatFromString(str: string): number | undefined;
|
|
37
176
|
/**
|
|
38
|
-
*
|
|
39
|
-
* @param
|
|
40
|
-
* @returns
|
|
177
|
+
* Rounds a value to the nearest 0.5
|
|
178
|
+
* @param value - The value to round
|
|
179
|
+
* @returns The rounded value, or null if the input is not a valid number
|
|
41
180
|
*/
|
|
42
181
|
export declare function roundToDot5(value: number | null | undefined): number | null;
|
|
43
182
|
/**
|
|
44
|
-
*
|
|
45
|
-
*
|
|
46
|
-
* 1. POLYGON
|
|
47
|
-
* 2. JSON
|
|
48
|
-
* 3. URL
|
|
183
|
+
* Converts various input formats into an array of {lat, lng} coordinates.
|
|
184
|
+
* Supported formats:
|
|
185
|
+
* 1. MySQL POLYGON: "POLYGON((lon1 lat1, lon2 lat2))"
|
|
186
|
+
* 2. JSON array of {LAT, LNG} objects (case insensitive)
|
|
187
|
+
* 3. URL-encoded JSON array
|
|
49
188
|
*
|
|
50
|
-
* @param
|
|
51
|
-
* @returns
|
|
189
|
+
* @param input - String in POLYGON, JSON, or URL-encoded format
|
|
190
|
+
* @returns Array of coordinates as [{lat, lng}]
|
|
52
191
|
*/
|
|
53
192
|
export declare function convertPolygonStringToArray(input: string): Array<{
|
|
54
193
|
lat: number;
|
|
55
194
|
lng: number;
|
|
56
195
|
}>;
|
|
57
196
|
/**
|
|
58
|
-
*
|
|
59
|
-
*
|
|
60
|
-
* 1.
|
|
61
|
-
* 2.
|
|
62
|
-
* 3.
|
|
63
|
-
* 4.
|
|
197
|
+
* Converts various input formats into a boolean value.
|
|
198
|
+
* Supported formats:
|
|
199
|
+
* 1. Strings: "true", "false", "1", "0", "yes", "no", "on", "off"
|
|
200
|
+
* 2. Booleans: true, false
|
|
201
|
+
* 3. Numbers: 1, 0
|
|
202
|
+
* 4. null, undefined
|
|
64
203
|
*
|
|
65
|
-
* @param
|
|
66
|
-
* @returns
|
|
204
|
+
* @param value - The value to convert
|
|
205
|
+
* @returns The corresponding boolean value
|
|
67
206
|
*/
|
|
68
207
|
export declare function parseBoolean(value: string | boolean | number | null | undefined): boolean;
|