UNPKG

2.15 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const nodes_1 = require("./graph/nodes");
4function computeSourceMap(map, data, parentUri) {
5 const uriMap = {};
6 const nodeTree = [];
7 switch (typeof data) {
8 case 'object':
9 for (const key in data) {
10 if (!Object.prototype.hasOwnProperty.call(data, key)) {
11 continue;
12 }
13 const sanitizedKey = key.replace(/~/g, '~0').replace(/\//g, '~1');
14 const match = findMapMatch(sanitizedKey, data[key], map);
15 if (match) {
16 const uri = `${parentUri || ''}/${sanitizedKey}`;
17 const sourceMapNode = {
18 category: nodes_1.NodeCategory.SourceMap,
19 type: match.type,
20 path: sanitizedKey,
21 };
22 uriMap[uri] = sourceMapNode;
23 if (match.subtype)
24 sourceMapNode.subtype = match.subtype;
25 const node = Object.assign({}, uriMap[uri], { children: [] });
26 nodeTree.push(node);
27 if (match.children) {
28 node.children = computeSourceMap(match.children, data[key]).nodeTree;
29 Object.assign(uriMap, computeSourceMap(match.children, data[key], uri).uriMap);
30 }
31 }
32 }
33 break;
34 }
35 return { uriMap, nodeTree };
36}
37exports.computeSourceMap = computeSourceMap;
38function findMapMatch(key, value, map) {
39 for (const entry of map) {
40 let match = true;
41 let target = key;
42 if (entry.field) {
43 target = value[entry.field];
44 }
45 if (entry.match) {
46 match = target && typeof target === 'string' ? !!target.match(entry.match) : false;
47 }
48 else if (entry.notMatch) {
49 match = target && typeof target === 'string' ? !!!target.match(entry.notMatch) : true;
50 }
51 if (match)
52 return entry;
53 }
54}
55//# sourceMappingURL=sourceMapper.js.map
\No newline at end of file