forked from compat-table/compat-table
-
Notifications
You must be signed in to change notification settings - Fork 33
/
utils.js
57 lines (51 loc) · 1.5 KB
/
utils.js
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
// tries to traverse an object to a given path
// example: $(myObj, ['foo', 'bar', 'baz'])
function $get (obj, path, ...more) {
return more.length && obj[path] ? $get(obj[path], ...more) : obj[path]
}
// sets a nested value at a given path that is delimited by '›'
// example: $set({}, 'foo›bar›baz', 123)
// output: { foo: { bar: { baz: 123}}}
function $set (target, path, value) {
var parts = path.split('›')
if (parts.length === 2) parts.splice(1, 0, '')
var obj = target
var last = parts.pop()
parts.forEach(function (prop) {
if (!obj[prop]) obj[prop] = {}
obj = obj[prop]
})
obj[last] = value
}
const replacements = {
'&': '&',
'<': '<',
'>': '>',
'"': '"',
"'": ''',
'/': '/'
}
module.exports = {
$get,
$set,
// The structure of the testers.json data focused on keeping it simple
// if looking at the raw file, but it isn't too useful programmatically.
// This utility opens it and transforms it in to an object tree.
objectifiedTesters: function () {
var testers = {}
var _testers = require('./testers.json')
Object.keys(_testers).forEach((esVersion) => {
testers[esVersion] = {}
Object.keys(_testers[esVersion]).forEach((path) => {
var item = _testers[esVersion][path]
$set(testers[esVersion], path, {
path: path,
spec: item.spec,
code: item.code
})
})
})
return testers
},
$escape: (str) => str.replace(/[&<>"'\/]/g, (x) => replacements[x])
}