-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
152 lines (137 loc) · 3.97 KB
/
index.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
export enum FieldMaskType {
Exclude = 'exclude',
Include = 'include',
}
export type FieldMaskConvertible = Iterable<string>
| Record<string, any>
| FieldMask;
export class FieldMask {
private _entries: Set<string> = new Set;
constructor(public type = FieldMaskType.Include) {
}
static exclude(excluded: Iterable<string>): FieldMask {
const mask = new FieldMask(FieldMaskType.Exclude);
mask.add(...excluded);
return mask;
}
static include(included: Iterable<string>): FieldMask {
const mask = new FieldMask(FieldMaskType.Include);
mask.add(...included);
return mask;
}
static from(from: FieldMaskConvertible): FieldMask {
if (from instanceof FieldMask) {
return from;
} else if (Array.isArray(from)) {
return FieldMask.include(from);
} else {
let maskType = undefined;
for (const v of Object.values(from)) {
if (maskType === undefined) {
maskType = v ? FieldMaskType.Include : FieldMaskType.Exclude;
} else if ((maskType === FieldMaskType.Exclude && v !== 0) ||
(maskType === FieldMaskType.Include && v !== 1)) {
throw new Error('Invalid field mask object');
}
}
maskType = maskType || FieldMaskType.Exclude;
const mask = new FieldMask(maskType);
mask.add(...Object.keys(from));
return mask;
}
}
add(...fields: string[]): void {
fields.forEach(f => this._entries.add(f));
}
get(): Record<string, 0 | 1> {
const result = {} as Record<string, 0 | 1>;
this._entries.forEach(f => {
switch (this.type) {
case FieldMaskType.Exclude: result[f] = 0; break;
case FieldMaskType.Include: result[f] = 1; break;
}
});
return result;
}
includes(field: string): boolean {
switch (this.type) {
case FieldMaskType.Exclude:
return !this._entries.has(field);
case FieldMaskType.Include:
return this._entries.has(field);
}
}
excludes(field: string): boolean {
switch (this.type) {
case FieldMaskType.Exclude:
return this._entries.has(field);
case FieldMaskType.Include:
return !this._entries.has(field);
}
}
equals(other: FieldMaskConvertible): boolean {
const otherMask = FieldMask.from(other);
if (this.type !== otherMask.type) return false;
for (const field of this._entries) {
if (!otherMask._entries.has(field))
return false;
}
for (const field of otherMask._entries) {
if (!this._entries.has(field))
return false;
}
return true;
}
negate(): FieldMask {
switch (this.type) {
case FieldMaskType.Exclude:
return FieldMask.include(this._entries);
case FieldMaskType.Include:
return FieldMask.exclude(this._entries);
}
}
join(otherFrom: FieldMaskConvertible): FieldMask {
const entries = [...this._entries];
const other = FieldMask.from(otherFrom);
switch (this.type) {
case FieldMaskType.Exclude:
return FieldMask.exclude(entries.filter(f => !other.includes(f)));
case FieldMaskType.Include:
const includeMask = FieldMask.include(entries);
if (other.type === FieldMaskType.Include)
includeMask.add(...other._entries);
return includeMask;
}
}
intersect(otherFrom: FieldMaskConvertible): FieldMask {
const entries = [...this._entries];
const other = FieldMask.from(otherFrom);
switch (this.type) {
case FieldMaskType.Exclude:
const excludeMask = FieldMask.exclude(entries);
if (other.type === FieldMaskType.Exclude)
excludeMask.add(...other._entries);
return excludeMask;
case FieldMaskType.Include:
return FieldMask.include(entries.filter(f => other.includes(f)));
}
}
apply<T extends Record<string, any>>(o: T): Record<string, any> {
const result: Record<string, any> = {};
switch (this.type) {
case FieldMaskType.Exclude:
for (const [k, v] of Object.entries(o)) {
if (!this._entries.has(k))
result[k] = v;
}
break;
case FieldMaskType.Include:
for (const [k, v] of Object.entries(o)) {
if (this._entries.has(k))
result[k] = v;
}
break;
}
return result;
}
}