-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimport_map.ts
166 lines (132 loc) · 4.61 KB
/
import_map.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
export type Imports = { [name: string]: string };
export type Scopes = { [scope: string]: Imports };
export interface ImportMap {
imports?: Imports;
scopes?: Scopes;
}
export class ImportMap implements ImportMap {
public imports?: Imports;
public scopes?: Scopes;
constructor({
imports,
scopes
}: { imports?: Imports; scopes?: Scopes } = {}) {
if (imports) this.imports = imports;
if (scopes) this.scopes = scopes;
}
public hasImport(name: string): boolean {
if (this.imports) return !!this.imports[name];
}
public addImport(name: string, url: string): void {
if (!this.imports) {
this.imports = {};
}
this.imports[name] = url;
}
public removeImport(name: string): void {
if (!this.imports) return;
if (this.hasImport(name)) {
delete this.imports[name];
}
}
public hasScope(scope: string): boolean {
if (this.scopes) return !!this.scopes[scope];
}
public addScope(scope: string, imports: Imports = {}): void {
if (!this.scopes) {
this.scopes = {};
}
this.scopes[scope] = imports;
}
public removeScope(scope: string): void {
if (!this.scopes) return;
if (this.hasScope(scope)) {
delete this.scopes[scope];
}
}
public toString(): string {
return JSON.stringify(this);
}
static parse(source: object | string, strict: boolean = false): ImportMap {
const parsed = typeof source === "string" ? JSON.parse(source) : source;
let imports: Imports | undefined = undefined;
let scopes: Scopes | undefined = undefined;
if (!valid(parsed)) {
throw new TypeError("Import map JSON must be of type object.");
}
if ("imports" in parsed) {
if (!valid(parsed.imports)) {
throw new TypeError(
"Import map's imports must be of type object."
);
}
imports = {};
for (const [name, url] of Object.entries(parsed.imports)) {
if (typeof url !== "string") {
throw new TypeError(
`Invalid imports key-value pair "${name}: ${url}". Element must be of type string.`
);
}
imports[name] = url;
}
}
if ("scopes" in parsed) {
if (!valid(parsed.scopes)) {
throw new TypeError(
"Import map's scopes must be of type object."
);
}
scopes = {};
for (const [scope, imports] of Object.entries(parsed.scopes)) {
if (!valid(imports)) {
throw new TypeError(
`Invalid scope imports in scope "${scope}". Scope imports must be of type object.`
);
}
scopes[scope] = {};
for (const [name, url] of Object.entries(imports)) {
if (typeof url !== "string") {
throw new TypeError(
`Invalid scope imports key-value pair "${name}: ${url}" in scope "${scope}". Element must be of type string.`
);
}
scopes[scope][name] = url;
}
}
}
if (strict) {
const badKeys = new Set(Object.keys(parsed));
if (badKeys.has("imports")) {
badKeys.delete("imports");
}
if (badKeys.has("scopes")) {
badKeys.delete("scopes");
}
for (const badKey of badKeys) {
throw new TypeError(
`Invalid top-level key "${badKey}". Only "imports" and "scopes" can be present.`
);
}
}
if (imports) {
if (scopes) {
return new ImportMap({
imports: imports,
scopes: scopes
});
}
return new ImportMap({
imports: imports
});
}
if (scopes) {
return new ImportMap({
scopes: scopes
});
}
return new ImportMap();
}
}
function valid(value): boolean {
return typeof value === "object" && value !== null && !Array.isArray(value);
}