This repository was archived by the owner on Feb 22, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsearch.ts
233 lines (197 loc) · 6.22 KB
/
search.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
import { QUERY_CACHE_DURATION, IDL_TYPES, CONCEPT_TYPES } from './constants.js';
import { store } from './store.js';
import { objectHash, pickFields, textVariations, Cache } from './utils.js';
type Type =
| 'attribute'
| 'dfn'
| 'dict-member'
| 'dictionary'
| 'element'
| 'enum-value'
| 'enum'
| 'event'
| 'interface'
| 'method'
| 'typedef';
export interface DataEntry {
type: Type;
spec: string;
shortname: string;
status: 'snapshot' | 'current';
uri: string;
normative: boolean;
for?: string[];
}
type SpecType = DataEntry['status'] | 'draft' | 'official';
export interface Options {
fields: (keyof DataEntry)[];
spec_type: SpecType[];
types: (Type | '_IDL_' | '_CONCEPT_')[];
query?: boolean;
id?: string;
all?: boolean;
}
export interface Query {
term: string;
id: string;
types?: (Type | '_IDL_' | '_CONCEPT_')[];
specs?: string[][];
for?: string;
}
interface Response {
result: [string, Partial<DataEntry>[]][];
query?: Query[];
}
const specStatusAlias = new Map([
['draft', 'current'],
['official', 'snapshot'],
]);
export const defaultOptions: Options = {
fields: ['shortname', 'spec', 'type', 'for', 'normative', 'uri'],
spec_type: ['draft', 'official'],
types: [],
};
export const cache = new Cache<string, DataEntry[]>(QUERY_CACHE_DURATION);
export function search(queries: Query[] = [], opts: Partial<Options> = {}) {
const options = { ...defaultOptions, ...opts };
const response: Response = { result: [] };
if (options.query) response.query = [];
for (const query of queries) {
const result = searchOne(query, options);
response.result.push([query.id, result]);
if (options.query) {
response.query!.push(query);
}
}
return response;
}
export function searchOne(query: Query, options = defaultOptions) {
normalizeQuery(query, options);
const filtered = filter(query, options);
let prefereredData = filterBySpecType(filtered, options.spec_type);
prefereredData = filterPreferLatestVersion(prefereredData);
const result = prefereredData.map(item => pickFields(item, options.fields));
return result;
}
function normalizeQuery(query: Query, options: Options) {
if (Array.isArray(query.specs) && !Array.isArray(query.specs[0])) {
// @ts-ignore
query.specs = [query.specs]; // for backward compatibility
}
if (!Array.isArray(query.types) || !query.types.length) {
query.types = options.types;
}
if (!query.id) {
query.id = objectHash(query);
}
}
function filter(query: Query, options: Options) {
const { id } = query;
const cachedValue = cache.get(id);
if (cachedValue) return cachedValue;
const byTerm = filterByTerm(query);
const bySpec = filterBySpec(byTerm, query);
const byType = filterByType(bySpec, query);
const result = filterByForContext(byType, query, options);
cache.set(id, result);
return result;
}
function filterByTerm(query: Query) {
const { term: inputTerm, types = [] } = query;
const isConcept = types.some(t => CONCEPT_TYPES.has(t));
const isIDL = types.some(t => IDL_TYPES.has(t));
const shouldTreatAsConcept = isConcept && !isIDL && !!types.length;
let term = shouldTreatAsConcept ? inputTerm.toLowerCase() : inputTerm;
if (inputTerm === '""') term = '';
let termData = store.byTerm[term] || [];
if (!termData.length && shouldTreatAsConcept) {
for (const altTerm of textVariations(term)) {
if (altTerm in store.byTerm) {
termData = store.byTerm[altTerm];
break;
}
}
}
return termData;
}
function filterBySpec(data: DataEntry[], query: Query) {
const { specs: specsLists } = query;
if (!Array.isArray(specsLists) || !specsLists.length) return data;
for (const specs of specsLists) {
const filteredBySpec = data.filter(
item => specs.includes(item.spec) || specs.includes(item.shortname),
);
if (filteredBySpec.length) return filteredBySpec;
}
return [];
}
function filterByType(data: DataEntry[], query: Query) {
const types = query.types!;
if (!types.length) return data;
const isIDL = types.includes('_IDL_');
const isConcept = types.includes('_CONCEPT_');
return data.filter(({ type }) => {
return (
types.includes(type) ||
(isIDL && IDL_TYPES.has(type)) ||
(isConcept && CONCEPT_TYPES.has(type))
);
});
}
function filterByForContext(data: DataEntry[], query: Query, options: Options) {
const { for: forContext } = query;
const shouldFilter = options.all ? typeof forContext === 'string' : true;
if (!shouldFilter) return data;
return data.filter(item => {
if (!forContext) return !item.for;
if (!!item.for && item.for.includes(forContext)) return true;
if (CONCEPT_TYPES.has(item.type)) {
return !!item.for && item.for.includes(forContext.toLowerCase());
}
return false;
});
}
function filterBySpecType(data: DataEntry[], specTypes: SpecType[]) {
if (!specTypes.length) return data;
const preferredType = specStatusAlias.get(specTypes[0]) || specTypes[0];
const sorted = [...data].sort((a, b) =>
a.status === preferredType ? -1 : b.status === preferredType ? 1 : 0,
);
const preferredData: DataEntry[] = [];
for (const item of sorted) {
if (
item.status === preferredType ||
!preferredData.find(it => item.spec === it.spec && item.type === it.type)
) {
preferredData.push(item);
}
}
const hasPreferredData = specTypes.length === 2 && preferredData.length;
return specTypes.length === 1 || hasPreferredData ? preferredData : data;
}
function filterPreferLatestVersion(data: DataEntry[]) {
if (data.length <= 1) {
return data;
}
const differingByVersion: Record<string, DataEntry[]> = {};
for (const entry of data) {
const key = `${entry.shortname}/${entry.uri}`;
if (!differingByVersion[key]) {
differingByVersion[key] = [];
}
differingByVersion[key].push(entry);
}
const result: DataEntry[] = [];
for (const entries of Object.values(differingByVersion)) {
if (entries.length > 1) {
// sorted as largest version number (latest) first
entries.sort((a, b) => getVersion(b.spec) - getVersion(a.spec));
}
result.push(entries[0]);
}
return result;
}
function getVersion(s: string) {
const match = s.match(/(\d+)?$/);
return match ? Number(match[1]) : 0;
}