-
Notifications
You must be signed in to change notification settings - Fork 514
/
Copy pathutils.ts
212 lines (181 loc) · 5.79 KB
/
utils.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import { compareVersions } from "compare-versions";
import type BCD from "@mdn/browser-compat-data/types";
// Extended for the fields, beyond the bcd types, that are extra-added
// exclusively in Yari.
interface SimpleSupportStatementExtended extends BCD.SimpleSupportStatement {
// Known for some support statements where the browser *version* is known,
// as opposed to just "true" and if the version release date is known.
release_date?: string;
}
export type SupportStatementExtended =
| SimpleSupportStatementExtended
| SimpleSupportStatementExtended[];
export function getFirst<T>(a: T | T[]): T;
export function getFirst<T>(a: T | T[] | undefined): T | undefined {
return Array.isArray(a) ? a[0] : a;
}
export function asList<T>(a: T | T[]): T[] {
return Array.isArray(a) ? a : [a];
}
export function isTruthy<T>(t: T | false | undefined | null): t is T {
return Boolean(t);
}
interface Feature {
name: string;
compat: BCD.CompatStatement;
depth: number;
}
function findFirstCompatDepth(identifier: BCD.Identifier) {
const entries = [["", identifier]];
while (entries.length) {
const [path, value] = entries.shift() as [string, BCD.Identifier];
if (value.__compat) {
// Following entries have at least this depth.
return path.split(".").length;
}
for (const key of Object.keys(value)) {
const subpath = path ? `${path}.${key}` : key;
entries.push([subpath, value[key]]);
}
}
// Fallback.
return 0;
}
export function listFeatures(
identifier: BCD.Identifier,
parentName: string = "",
rootName: string = "",
depth: number = 0,
firstCompatDepth: number = 0
): Feature[] {
const features: Feature[] = [];
if (rootName && identifier.__compat) {
features.push({
name: rootName,
compat: identifier.__compat,
depth,
});
}
if (rootName) {
firstCompatDepth = findFirstCompatDepth(identifier);
}
for (const subName of Object.keys(identifier)) {
if (subName === "__compat") {
continue;
}
const subIdentifier = identifier[subName];
if (subIdentifier.__compat) {
features.push({
name: parentName ? `${parentName}.${subName}` : subName,
compat: subIdentifier.__compat,
depth: depth + 1,
});
}
if (subIdentifier.__compat || depth + 1 < firstCompatDepth) {
features.push(
...listFeatures(subIdentifier, subName, "", depth + 1, firstCompatDepth)
);
}
}
return features;
}
export function hasMore(support: BCD.SupportStatement | undefined) {
return Array.isArray(support) && support.length > 1;
}
export function versionIsPreview(
version: BCD.VersionValue | string | undefined,
browser: BCD.BrowserStatement
): boolean {
if (version === "preview") {
return true;
}
if (browser && typeof version === "string" && browser.releases[version]) {
return ["beta", "nightly", "planned"].includes(
browser.releases[version].status
);
}
return false;
}
export function hasNoteworthyNotes(support: BCD.SimpleSupportStatement) {
return (
support.notes?.length &&
!support.version_removed &&
!support.partial_implementation
);
}
function hasLimitation(support: BCD.SimpleSupportStatement) {
return hasMajorLimitation(support) || support.notes;
}
function hasMajorLimitation(support: BCD.SimpleSupportStatement) {
return (
support.partial_implementation ||
support.alternative_name ||
support.flags ||
support.prefix ||
support.version_removed
);
}
export function isFullySupportedWithoutLimitation(
support: BCD.SimpleSupportStatement
) {
return support.version_added && !hasLimitation(support);
}
export function isNotSupportedAtAll(support: BCD.SimpleSupportStatement) {
return !support.version_added && !hasLimitation(support);
}
function isFullySupportedWithoutMajorLimitation(
support: BCD.SimpleSupportStatement
) {
return support.version_added && !hasMajorLimitation(support);
}
// Prioritizes support items
export function getCurrentSupport(
support: SupportStatementExtended | undefined
): SimpleSupportStatementExtended | undefined {
if (!support) return undefined;
// Full support without limitation
const noLimitationSupportItem = asList(support).find((item) =>
isFullySupportedWithoutLimitation(item)
);
if (noLimitationSupportItem) return noLimitationSupportItem;
// Full support with only notes and version_added
const minorLimitationSupportItem = asList(support).find((item) =>
isFullySupportedWithoutMajorLimitation(item)
);
if (minorLimitationSupportItem) return minorLimitationSupportItem;
// Full support with altname/prefix
const altnamePrefixSupportItem = asList(support).find(
(item) => !item.version_removed && (item.prefix || item.alternative_name)
);
if (altnamePrefixSupportItem) return altnamePrefixSupportItem;
// Partial support
const partialSupportItem = asList(support).find(
(item) => !item.version_removed && item.partial_implementation
);
if (partialSupportItem) return partialSupportItem;
// Support with flags only
const flagSupportItem = asList(support).find(
(item) => !item.version_removed && item.flags
);
if (flagSupportItem) return flagSupportItem;
// No/Inactive support
const noSupportItem = asList(support).find((item) => item.version_removed);
if (noSupportItem) return noSupportItem;
// Default (likely never reached)
return getFirst(support);
}
export function getPreviousVersion(
version: BCD.VersionValue,
browser: BCD.BrowserStatement
): BCD.VersionValue {
if (browser && typeof version === "string") {
const browserVersions = Object.keys(browser["releases"]).sort(
compareVersions
);
const currentVersionIndex = browserVersions.indexOf(version);
if (currentVersionIndex > 0) {
return browserVersions[currentVersionIndex - 1];
}
}
return version;
}