1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 | const { list, eq, contains } = require('ferrum');
|
14 | const { parent, pointer } = require('./symbols');
|
15 |
|
16 | function isSkippableKeyword(schema) {
|
17 | const skippableKeywords = ['examples', 'required', 'properties'];
|
18 | const containsCurrentSchema = contains((keyword) => eq(keyword, schema[pointer].split('/').pop()));
|
19 |
|
20 | return containsCurrentSchema(skippableKeywords);
|
21 | }
|
22 |
|
23 | function reducer({ seen, ids }, schema) {
|
24 | if (schema
|
25 | && schema[parent]
|
26 | && (seen.has(schema) || (schema.$id && ids.indexOf(schema.$id) >= 0))) {
|
27 | return { seen, ids };
|
28 | } else if (Array.isArray(schema)) {
|
29 | if (isSkippableKeyword(schema)) {
|
30 | return { seen, ids };
|
31 | }
|
32 | return schema.reduce(reducer, { seen, ids });
|
33 | } else if (schema && typeof schema === 'object') {
|
34 | if (isSkippableKeyword(schema)) {
|
35 | return [...Object.values(schema)].reduce(reducer, { seen, ids });
|
36 | }
|
37 | seen.add(schema);
|
38 | if (schema.$id) {
|
39 | ids.push(schema.$id);
|
40 | }
|
41 | return [...Object.values(schema)].reduce(reducer, { seen, ids });
|
42 | }
|
43 | return { seen, ids };
|
44 | }
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 | function traverse(schemas) {
|
52 | return Array.from(list(schemas).reduce(reducer, { seen: new Set(), ids: [] }).seen);
|
53 | }
|
54 |
|
55 | module.exports = traverse;
|