1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | Object.defineProperty(exports, "__esModule", { value: true });
|
7 | exports.ArrayType = void 0;
|
8 | const tslib_1 = require("tslib");
|
9 | const util_1 = tslib_1.__importDefault(require("util"));
|
10 |
|
11 |
|
12 |
|
13 |
|
14 | class ArrayType {
|
15 | constructor(itemType) {
|
16 | this.itemType = itemType;
|
17 | this.name = 'array';
|
18 | }
|
19 | isInstance(value) {
|
20 | if (value == null)
|
21 | return true;
|
22 | if (!Array.isArray(value)) {
|
23 | return false;
|
24 | }
|
25 | const list = value;
|
26 | return list.every(i => this.itemType.isInstance(i));
|
27 | }
|
28 | isCoercible(value) {
|
29 | if (value == null)
|
30 | return true;
|
31 | if (!Array.isArray(value)) {
|
32 | return false;
|
33 | }
|
34 | return value.every(i => this.itemType.isCoercible(i));
|
35 | }
|
36 | defaultValue() {
|
37 | return [];
|
38 | }
|
39 | coerce(value) {
|
40 | if (value == null)
|
41 | return value;
|
42 | if (!Array.isArray(value)) {
|
43 | const msg = util_1.default.format('Invalid %s: %j', this.name, value);
|
44 | throw new TypeError(msg);
|
45 | }
|
46 | return value.map(i => this.itemType.coerce(i));
|
47 | }
|
48 | serialize(value) {
|
49 | if (value == null)
|
50 | return value;
|
51 | return value.map(i => this.itemType.serialize(i));
|
52 | }
|
53 | }
|
54 | exports.ArrayType = ArrayType;
|
55 |
|
\ | No newline at end of file |