@alexify/metaschema 3.1.0 → 3.3.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/CHANGELOG.md +314 -1
- package/README.md +535 -20
- package/SECURITY.md +26 -7
- package/index.d.ts +496 -2
- package/package.json +53 -5
- package/src/codec.js +239 -74
- package/src/coerce/index.d.ts +34 -0
- package/src/coerce/index.js +352 -0
- package/src/compile/code.js +1 -1
- package/src/compile/emit.js +32 -7
- package/src/compile/index.d.ts +6 -0
- package/src/compile/index.js +12 -1
- package/src/context.js +11 -10
- package/src/derive/index.d.ts +86 -0
- package/src/derive/index.js +266 -0
- package/src/errors.js +28 -1
- package/src/formats/index.d.ts +31 -0
- package/src/formats/index.js +557 -0
- package/src/generators/fromjsonschema.js +16 -14
- package/src/generators/index.js +2 -1
- package/src/generators/jsonschema.js +47 -13
- package/src/issues.js +37 -20
- package/src/kinds.js +11 -2
- package/src/lint.js +24 -0
- package/src/locales/en.js +11 -2
- package/src/locales/uk.js +10 -2
- package/src/metadata.js +7 -2
- package/src/model/data.js +17 -0
- package/src/model/diff.js +1 -0
- package/src/model/index.d.ts +14 -2
- package/src/model/index.js +13 -10
- package/src/model/infer.js +483 -0
- package/src/model/merge.js +10 -6
- package/src/model/restore.js +3 -3
- package/src/model/sample.js +8 -5
- package/src/model.js +34 -4
- package/src/mongodb/bson.js +118 -0
- package/src/mongodb/document.js +209 -0
- package/src/mongodb/index.d.ts +133 -0
- package/src/mongodb/index.js +24 -0
- package/src/mongodb/indexes.js +79 -0
- package/src/mongodb/update.js +258 -0
- package/src/preprocessor.js +1 -0
- package/src/prototypes/abstract.js +9 -3
- package/src/prototypes/collections.js +4 -3
- package/src/prototypes/scalars.js +4 -4
- package/src/prototypes/union.js +2 -2
- package/src/result.js +6 -0
- package/src/rules.js +53 -4
- package/src/schema.js +53 -28
- package/src/snapshot.js +1 -1
- package/src/stream/index.d.ts +68 -0
- package/src/stream/index.js +247 -0
- package/src/struct.js +1 -1
- package/src/types.js +33 -1
- package/src/util.js +18 -0
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
EachAsyncOptions,
|
|
3
|
+
EachOptions,
|
|
4
|
+
Infer,
|
|
5
|
+
InferOutput,
|
|
6
|
+
ParseEachOptions,
|
|
7
|
+
ParseResult,
|
|
8
|
+
RecordEntry,
|
|
9
|
+
Schema,
|
|
10
|
+
TransformOptions,
|
|
11
|
+
ValidationResult,
|
|
12
|
+
} from '../../index.js';
|
|
13
|
+
|
|
14
|
+
// `check` on every record of an iterable, as a generator of
|
|
15
|
+
// `{ index, record, result }` in the order of the records; the options are
|
|
16
|
+
// those of `check`, plus `maxInvalid`, the number of invalid records after
|
|
17
|
+
// which the iteration ends. A target that is not a Schema, or a source that
|
|
18
|
+
// is not iterable, is a `TypeError` with the code `ERR_INVALID_TARGET`.
|
|
19
|
+
export function checkEach<T>(
|
|
20
|
+
schema: Schema,
|
|
21
|
+
records: Iterable<T>,
|
|
22
|
+
options?: EachOptions,
|
|
23
|
+
): Generator<RecordEntry<T, ValidationResult>, void, undefined>;
|
|
24
|
+
|
|
25
|
+
// `parse` on every record of an iterable: the same, with the result of
|
|
26
|
+
// `parse` in each entry.
|
|
27
|
+
export function parseEach<T, D extends string | object>(
|
|
28
|
+
schema: Schema<D>,
|
|
29
|
+
records: Iterable<T>,
|
|
30
|
+
options?: ParseEachOptions,
|
|
31
|
+
): Generator<RecordEntry<T, ParseResult<InferOutput<D>>>, void, undefined>;
|
|
32
|
+
|
|
33
|
+
// `checkAsync` on every record of a sync or async iterable, as an async
|
|
34
|
+
// generator in the order of the records, with up to `concurrency` records
|
|
35
|
+
// in flight (one by default).
|
|
36
|
+
export function checkEachAsync<T>(
|
|
37
|
+
schema: Schema,
|
|
38
|
+
records: Iterable<T> | AsyncIterable<T>,
|
|
39
|
+
options?: EachOptions & EachAsyncOptions,
|
|
40
|
+
): AsyncGenerator<RecordEntry<T, ValidationResult>, void, undefined>;
|
|
41
|
+
|
|
42
|
+
// `parseAsync` on every record of a sync or async iterable: the same, with
|
|
43
|
+
// the result of `parseAsync` in each entry.
|
|
44
|
+
export function parseEachAsync<T, D extends string | object>(
|
|
45
|
+
schema: Schema<D>,
|
|
46
|
+
records: Iterable<T> | AsyncIterable<T>,
|
|
47
|
+
options?: ParseEachOptions & EachAsyncOptions,
|
|
48
|
+
): AsyncGenerator<RecordEntry<T, ParseResult<InferOutput<D>>>, void, undefined>;
|
|
49
|
+
|
|
50
|
+
// A TransformStream over the records of a stream: a valid record goes on as
|
|
51
|
+
// its parsed value (`mode: 'parse'`, the default) or as it is
|
|
52
|
+
// (`mode: 'check'`); an invalid one is handed to `onInvalid` and dropped, or
|
|
53
|
+
// ends the stream with a `TypeError` of the code `ERR_INVALID_RECORD` (with
|
|
54
|
+
// `index`, `record` and `result` on it) when there is no `onInvalid` or
|
|
55
|
+
// `maxInvalid` is reached. `async: true` runs the async methods.
|
|
56
|
+
export function transform<D extends string | object>(
|
|
57
|
+
schema: Schema<D>,
|
|
58
|
+
options: TransformOptions & { mode: 'check' },
|
|
59
|
+
): TransformStream<unknown, Infer<D>>;
|
|
60
|
+
export function transform<D extends string | object>(
|
|
61
|
+
schema: Schema<D>,
|
|
62
|
+
options?: TransformOptions,
|
|
63
|
+
): TransformStream<unknown, InferOutput<D>>;
|
|
64
|
+
|
|
65
|
+
// Installs `schema.checkEach`, `schema.parseEach`, `schema.checkEachAsync`,
|
|
66
|
+
// `schema.parseEachAsync` and `schema.transform`. Once per process; later
|
|
67
|
+
// calls do nothing.
|
|
68
|
+
export function register(): void;
|
|
@@ -0,0 +1,247 @@
|
|
|
1
|
+
// The `./stream` subpath: a schema over a sequence of records. `checkEach`
|
|
2
|
+
// and `parseEach` run `check` or `parse` on every record of an iterable and
|
|
3
|
+
// yield `{ index, record, result }` in the order of the records; the async
|
|
4
|
+
// pair does the same over a sync or async iterable with `checkAsync` or
|
|
5
|
+
// `parseAsync`, keeping up to `concurrency` records in flight; `transform`
|
|
6
|
+
// is a TransformStream that lets the valid records (or their parsed values)
|
|
7
|
+
// through and reports the others. Kept out of the core entry so that a
|
|
8
|
+
// program that validates one value at a time does not carry it; `register()`
|
|
9
|
+
// installs the methods on Schema for the programs that want them.
|
|
10
|
+
const { hasBrand } = require('../util.js');
|
|
11
|
+
const { abandon } = require('../issues.js');
|
|
12
|
+
const { invalidTarget, invalidOption: invalid } = require('../errors.js');
|
|
13
|
+
const { Schema } = require('../schema.js');
|
|
14
|
+
|
|
15
|
+
const optionsOf = (options) => {
|
|
16
|
+
if (options === undefined || (options !== null && typeof options === 'object')) return options;
|
|
17
|
+
throw invalid('options', 'an object', options);
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
// How many invalid records end the iteration: a whole number, or no limit.
|
|
21
|
+
const limitOf = (options) => {
|
|
22
|
+
const limit = optionsOf(options)?.maxInvalid;
|
|
23
|
+
if (limit === undefined) return Infinity;
|
|
24
|
+
if ((Number.isInteger(limit) || limit === Infinity) && limit >= 1) return limit;
|
|
25
|
+
throw invalid('maxInvalid', 'an integer of at least 1', limit);
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
// How many records an async iteration keeps in flight: one unless asked.
|
|
29
|
+
const widthOf = (options) => {
|
|
30
|
+
const width = options?.concurrency;
|
|
31
|
+
if (width === undefined) return 1;
|
|
32
|
+
if (Number.isInteger(width) && width >= 1) return width;
|
|
33
|
+
throw invalid('concurrency', 'an integer of at least 1', width);
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
const modeOf = (options) => {
|
|
37
|
+
const mode = options?.mode;
|
|
38
|
+
if (mode === undefined) return 'parse';
|
|
39
|
+
if (mode === 'parse' || mode === 'check') return mode;
|
|
40
|
+
throw invalid('mode', '"parse" or "check"', mode);
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
const asyncOf = (options) => {
|
|
44
|
+
const async = options?.async;
|
|
45
|
+
if (async === undefined || typeof async === 'boolean') return async === true;
|
|
46
|
+
throw invalid('async', 'a boolean', async);
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
const callbackOf = (options) => {
|
|
50
|
+
const onInvalid = options?.onInvalid;
|
|
51
|
+
if (onInvalid === undefined || typeof onInvalid === 'function') return onInvalid;
|
|
52
|
+
throw invalid('onInvalid', 'a function', onInvalid);
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
const schemaOf = (schema) => {
|
|
56
|
+
if (!hasBrand(schema, 'Schema')) throw invalidTarget('A Schema is expected');
|
|
57
|
+
return schema;
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
// A sequence of records: anything with an iterator, or an async one where
|
|
61
|
+
// the iteration is async. A string is iterable and surely a mistake.
|
|
62
|
+
const recordsOf = (records, async) => {
|
|
63
|
+
const iterable =
|
|
64
|
+
records !== null &&
|
|
65
|
+
records !== undefined &&
|
|
66
|
+
typeof records !== 'string' &&
|
|
67
|
+
(typeof records[Symbol.iterator] === 'function' ||
|
|
68
|
+
(async && typeof records[Symbol.asyncIterator] === 'function'));
|
|
69
|
+
if (!iterable) throw invalidTarget('An iterable of records is expected');
|
|
70
|
+
return records;
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
// The method of the schema a function of the subpath runs on each record.
|
|
74
|
+
const CHECK = (schema, record, options) => schema.check(record, options);
|
|
75
|
+
const PARSE = (schema, record, options) => schema.parse(record, options);
|
|
76
|
+
const CHECK_ASYNC = (schema, record, options) => schema.checkAsync(record, options);
|
|
77
|
+
const PARSE_ASYNC = (schema, record, options) => schema.parseAsync(record, options);
|
|
78
|
+
|
|
79
|
+
// One record after the other. The entry of a record carries its position,
|
|
80
|
+
// since an iterator has none, and the record itself, since a stream cannot
|
|
81
|
+
// be read again. The options are handed to check or parse as given (they
|
|
82
|
+
// read the keys they know), so nothing is allocated per record beyond what
|
|
83
|
+
// the check itself allocates and the entry.
|
|
84
|
+
const each = function* (schema, records, options, limit, call) {
|
|
85
|
+
let failed = 0;
|
|
86
|
+
let index = 0;
|
|
87
|
+
for (const record of records) {
|
|
88
|
+
const result = call(schema, record, options);
|
|
89
|
+
yield { index, record, result };
|
|
90
|
+
index += 1;
|
|
91
|
+
if (!result.valid && (failed += 1) >= limit) return;
|
|
92
|
+
}
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
// The async pair over a sync or async iterable: up to `width` records are
|
|
96
|
+
// checked at once, the entries come out in the order of the records, and the
|
|
97
|
+
// promises still in flight when the iteration ends early (`maxInvalid`, a
|
|
98
|
+
// consumer that stops, a record whose parse throws) are observed, never
|
|
99
|
+
// awaited. The queue is as long as `width`, which is small by design.
|
|
100
|
+
const eachAsync = async function* (schema, records, options, limit, width, call) {
|
|
101
|
+
const queue = [];
|
|
102
|
+
let failed = 0;
|
|
103
|
+
let index = 0;
|
|
104
|
+
try {
|
|
105
|
+
for await (const record of records) {
|
|
106
|
+
const result = call(schema, record, options);
|
|
107
|
+
// Observed from the start: a record that rejects while an earlier one
|
|
108
|
+
// is still awaited would reject unhandled before its turn (the await
|
|
109
|
+
// of its entry still throws it).
|
|
110
|
+
abandon(result);
|
|
111
|
+
queue.push({ index, record, result });
|
|
112
|
+
index += 1;
|
|
113
|
+
if (queue.length < width) continue;
|
|
114
|
+
const entry = queue.shift();
|
|
115
|
+
entry.result = await entry.result;
|
|
116
|
+
yield entry;
|
|
117
|
+
if (!entry.result.valid && (failed += 1) >= limit) return;
|
|
118
|
+
}
|
|
119
|
+
while (queue.length > 0) {
|
|
120
|
+
const entry = queue.shift();
|
|
121
|
+
entry.result = await entry.result;
|
|
122
|
+
yield entry;
|
|
123
|
+
if (!entry.result.valid && (failed += 1) >= limit) return;
|
|
124
|
+
}
|
|
125
|
+
} finally {
|
|
126
|
+
for (let at = 0; at < queue.length; at += 1) abandon(queue[at].result);
|
|
127
|
+
}
|
|
128
|
+
};
|
|
129
|
+
|
|
130
|
+
const checkEach = (schema, records, options) =>
|
|
131
|
+
each(schemaOf(schema), recordsOf(records, false), options, limitOf(options), CHECK);
|
|
132
|
+
|
|
133
|
+
const parseEach = (schema, records, options) =>
|
|
134
|
+
each(schemaOf(schema), recordsOf(records, false), options, limitOf(options), PARSE);
|
|
135
|
+
|
|
136
|
+
const checkEachAsync = (schema, records, options) => {
|
|
137
|
+
const limit = limitOf(options);
|
|
138
|
+
return eachAsync(
|
|
139
|
+
schemaOf(schema),
|
|
140
|
+
recordsOf(records, true),
|
|
141
|
+
options,
|
|
142
|
+
limit,
|
|
143
|
+
widthOf(options),
|
|
144
|
+
CHECK_ASYNC,
|
|
145
|
+
);
|
|
146
|
+
};
|
|
147
|
+
|
|
148
|
+
const parseEachAsync = (schema, records, options) => {
|
|
149
|
+
const limit = limitOf(options);
|
|
150
|
+
return eachAsync(
|
|
151
|
+
schemaOf(schema),
|
|
152
|
+
recordsOf(records, true),
|
|
153
|
+
options,
|
|
154
|
+
limit,
|
|
155
|
+
widthOf(options),
|
|
156
|
+
PARSE_ASYNC,
|
|
157
|
+
);
|
|
158
|
+
};
|
|
159
|
+
|
|
160
|
+
// The error a stream ends with: the record, its position and its result
|
|
161
|
+
// travel on it, since the stream carries them no further.
|
|
162
|
+
const invalidRecord = (index, record, result) => {
|
|
163
|
+
const error = new TypeError(`Record ${index} is invalid`);
|
|
164
|
+
error.code = 'ERR_INVALID_RECORD';
|
|
165
|
+
error.index = index;
|
|
166
|
+
error.record = record;
|
|
167
|
+
error.result = result;
|
|
168
|
+
return error;
|
|
169
|
+
};
|
|
170
|
+
|
|
171
|
+
// A TransformStream over the records of a stream: a valid record goes on as
|
|
172
|
+
// its parsed value (or as it is, in `check` mode); an invalid one is handed
|
|
173
|
+
// to `onInvalid` and dropped, or ends the stream with the error when there
|
|
174
|
+
// is no `onInvalid` or `maxInvalid` is reached. A stream that ends looks
|
|
175
|
+
// complete to what reads it, so it cannot stop quietly as a generator does.
|
|
176
|
+
// The stream handles one record at a time, so the async methods settle in
|
|
177
|
+
// order too.
|
|
178
|
+
const transform = (schema, options) => {
|
|
179
|
+
schemaOf(schema);
|
|
180
|
+
const limit = limitOf(options);
|
|
181
|
+
const mode = modeOf(options);
|
|
182
|
+
const async = asyncOf(options);
|
|
183
|
+
const onInvalid = callbackOf(options);
|
|
184
|
+
const parse = mode === 'parse';
|
|
185
|
+
const call = async ? (parse ? PARSE_ASYNC : CHECK_ASYNC) : parse ? PARSE : CHECK;
|
|
186
|
+
let failed = 0;
|
|
187
|
+
let index = 0;
|
|
188
|
+
const settle = (record, result, controller) => {
|
|
189
|
+
const at = index;
|
|
190
|
+
index += 1;
|
|
191
|
+
if (result.valid) {
|
|
192
|
+
controller.enqueue(parse ? result.value : record);
|
|
193
|
+
return;
|
|
194
|
+
}
|
|
195
|
+
failed += 1;
|
|
196
|
+
if (onInvalid !== undefined) onInvalid(record, result, at);
|
|
197
|
+
if (onInvalid === undefined || failed >= limit) {
|
|
198
|
+
controller.error(invalidRecord(at, record, result));
|
|
199
|
+
}
|
|
200
|
+
};
|
|
201
|
+
return new TransformStream({
|
|
202
|
+
transform(record, controller) {
|
|
203
|
+
const result = call(schema, record, options);
|
|
204
|
+
if (!async) return settle(record, result, controller);
|
|
205
|
+
return result.then((settled) => settle(record, settled, controller));
|
|
206
|
+
},
|
|
207
|
+
});
|
|
208
|
+
};
|
|
209
|
+
|
|
210
|
+
// Installed like class members: not enumerable. A copy per subpath rather
|
|
211
|
+
// than a helper of the core: the core is measured on its own, the subpaths
|
|
212
|
+
// on top of it.
|
|
213
|
+
const install = (target, members) => {
|
|
214
|
+
const descriptors = Object.getOwnPropertyDescriptors(members);
|
|
215
|
+
for (const name in descriptors) {
|
|
216
|
+
descriptors[name].enumerable = false;
|
|
217
|
+
Object.defineProperty(target, name, descriptors[name]);
|
|
218
|
+
}
|
|
219
|
+
};
|
|
220
|
+
|
|
221
|
+
let registered = false;
|
|
222
|
+
|
|
223
|
+
// Adds `schema.checkEach`, `schema.parseEach`, `schema.checkEachAsync`,
|
|
224
|
+
// `schema.parseEachAsync` and `schema.transform`. Once per process.
|
|
225
|
+
const register = () => {
|
|
226
|
+
if (registered) return;
|
|
227
|
+
registered = true;
|
|
228
|
+
install(Schema.prototype, {
|
|
229
|
+
checkEach(records, options) {
|
|
230
|
+
return checkEach(this, records, options);
|
|
231
|
+
},
|
|
232
|
+
parseEach(records, options) {
|
|
233
|
+
return parseEach(this, records, options);
|
|
234
|
+
},
|
|
235
|
+
checkEachAsync(records, options) {
|
|
236
|
+
return checkEachAsync(this, records, options);
|
|
237
|
+
},
|
|
238
|
+
parseEachAsync(records, options) {
|
|
239
|
+
return parseEachAsync(this, records, options);
|
|
240
|
+
},
|
|
241
|
+
transform(options) {
|
|
242
|
+
return transform(this, options);
|
|
243
|
+
},
|
|
244
|
+
});
|
|
245
|
+
};
|
|
246
|
+
|
|
247
|
+
module.exports = { checkEach, parseEach, checkEachAsync, parseEachAsync, transform, register };
|
package/src/struct.js
CHANGED
|
@@ -131,7 +131,7 @@ const createStruct = (defs, prep) => {
|
|
|
131
131
|
const calculated = [];
|
|
132
132
|
for (const key of Object.keys(defs)) {
|
|
133
133
|
const entry = defs[key];
|
|
134
|
-
const { field, required } = formatters.key(key
|
|
134
|
+
const { field, required } = formatters.key(key);
|
|
135
135
|
try {
|
|
136
136
|
const { Type, defs: typeDefs } = prep.parse(entry);
|
|
137
137
|
if (!Type) {
|
package/src/types.js
CHANGED
|
@@ -7,6 +7,7 @@ const { tuple } = require('./prototypes/tuple.js');
|
|
|
7
7
|
const { union } = require('./prototypes/union.js');
|
|
8
8
|
const { json } = require('./prototypes/json.js');
|
|
9
9
|
const { SchemaDefinitionError } = require('./errors.js');
|
|
10
|
+
const { firstKey, isFirstUpper } = require('./metautil.js');
|
|
10
11
|
|
|
11
12
|
const PROTOTYPES = {
|
|
12
13
|
...scalars,
|
|
@@ -58,6 +59,18 @@ const TYPES = createTypes(PROTOTYPES);
|
|
|
58
59
|
|
|
59
60
|
const createRegistry = () => createTypes(PROTOTYPES);
|
|
60
61
|
|
|
62
|
+
// Whether a dictionary of fields reads as a struct under the first-key rule
|
|
63
|
+
// (`preprocessor.js`): the first key that starts with a letter is a kind
|
|
64
|
+
// when capitalized, the long form when it is `type` and a shorthand when it
|
|
65
|
+
// is a type name of the registry; a struct that reads otherwise needs the
|
|
66
|
+
// `schema` shorthand around it. What generates a definition asks here.
|
|
67
|
+
const readsAsStruct = (fields, types = TYPES) => {
|
|
68
|
+
const first = firstKey(fields);
|
|
69
|
+
if (first === undefined) return true;
|
|
70
|
+
const key = first.endsWith('?') ? first.slice(0, -1) : first;
|
|
71
|
+
return key !== 'type' && types[key] === undefined && !isFirstUpper(key);
|
|
72
|
+
};
|
|
73
|
+
|
|
61
74
|
// The own keys of the metadata given, as data: the table has no prototype.
|
|
62
75
|
const updateTypeMetadata = (Type, metadata) => {
|
|
63
76
|
Object.assign(Type.metadata, metadata);
|
|
@@ -124,4 +137,23 @@ const typeFactory = (customTypes, registry = TYPES) => {
|
|
|
124
137
|
return registry;
|
|
125
138
|
};
|
|
126
139
|
|
|
127
|
-
|
|
140
|
+
// The type a field validates as: its own class, or the one its alias chain
|
|
141
|
+
// ends in (`{ money: { js: 'number' } }` validates as a number, and so does
|
|
142
|
+
// `{ cents: { js: 'money' } }`); a custom type with a check of its own ends
|
|
143
|
+
// the chain with `source.js` undefined, and a name the registry cannot
|
|
144
|
+
// resolve (a namespace detached since) ends it where it stands. Read by the
|
|
145
|
+
// generators, the samples and the mongodb subpath, which look at `.type` and
|
|
146
|
+
// `.source` of what comes back.
|
|
147
|
+
const baseTypeOf = (field) => {
|
|
148
|
+
const { types } = field.root;
|
|
149
|
+
let Type = field.constructor;
|
|
150
|
+
for (;;) {
|
|
151
|
+
const js = Type.source?.js;
|
|
152
|
+
if (js === undefined) return Type;
|
|
153
|
+
const next = types[js];
|
|
154
|
+
if (next === undefined) return Type;
|
|
155
|
+
Type = next;
|
|
156
|
+
}
|
|
157
|
+
};
|
|
158
|
+
|
|
159
|
+
module.exports = { TYPES, createRegistry, readsAsStruct, typeFactory, baseTypeOf };
|
package/src/util.js
CHANGED
|
@@ -9,6 +9,21 @@ const INSPECT = Symbol.for('nodejs.util.inspect.custom');
|
|
|
9
9
|
|
|
10
10
|
const hasBrand = (value, name) => value?.[BRAND] === name;
|
|
11
11
|
|
|
12
|
+
// A built-in is recognised by the name of its constructor, which nothing
|
|
13
|
+
// renames, and by its shape, which a value parsed from JSON cannot fake: an
|
|
14
|
+
// own key called `constructor` holding `{ name: 'Map' }` is data, and
|
|
15
|
+
// iterating such a value, or calling `getTime` on it, would throw out of a
|
|
16
|
+
// check. The name is tested first, since most values fail it at once.
|
|
17
|
+
const iterableNamed = (name) => (value) =>
|
|
18
|
+
value?.constructor?.name === name && typeof value[Symbol.iterator] === 'function';
|
|
19
|
+
|
|
20
|
+
const isMap = iterableNamed('Map');
|
|
21
|
+
|
|
22
|
+
const isSet = iterableNamed('Set');
|
|
23
|
+
|
|
24
|
+
const isDate = (value) =>
|
|
25
|
+
value?.constructor?.name === 'Date' && typeof value.getTime === 'function';
|
|
26
|
+
|
|
12
27
|
// A JavaScript identifier: what generated code may write as `object.key` and
|
|
13
28
|
// a TypeScript declaration as a bare name.
|
|
14
29
|
const IDENTIFIER = /^[A-Za-z_$][\w$]*$/;
|
|
@@ -77,6 +92,9 @@ module.exports = {
|
|
|
77
92
|
BRAND,
|
|
78
93
|
INSPECT,
|
|
79
94
|
hasBrand,
|
|
95
|
+
isMap,
|
|
96
|
+
isSet,
|
|
97
|
+
isDate,
|
|
80
98
|
IDENTIFIER,
|
|
81
99
|
define,
|
|
82
100
|
RUN,
|