-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathbundle.ts
More file actions
45 lines (39 loc) · 1.37 KB
/
bundle.ts
File metadata and controls
45 lines (39 loc) · 1.37 KB
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
import {Pattern} from "../../syntax/parser/ast.js";
import {ScopeError} from "./error.js";
import {Message} from "./message.js";
import {Resource} from "./resource.js";
import {Scope} from "./scope.js";
import {Value} from "./value.js";
export interface FormatResult {
readonly value: string;
readonly errors: Array<ScopeError>;
}
export class Bundle {
public readonly messages: Map<string, Message> = new Map();
addResource(resource: Resource) {
for (let message of resource.body) {
if (message.type === "Message") {
let attributes: Record<string, Pattern> = {};
for (let attribute of message.attributes) {
attributes[attribute.id.name] = attribute.value;
}
this.messages.set(message.id.name, <Message>{
id: message.id.name,
value: message.value,
attributes,
});
}
}
}
hasMessage(id: string) {
return this.messages.has(id);
}
getMessage(id: string) {
return this.messages.get(id);
}
formatPattern(pattern: Pattern, variables: Map<string, Value>): FormatResult {
let scope = new Scope(this.messages, variables);
let value = scope.resolvePattern(pattern).format(scope);
return {value, errors: scope.errors};
}
}