forked from mdn/browser-compat-data
-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
73 lines (63 loc) · 1.7 KB
/
index.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
'use strict';
const fs = require('fs');
const path = require('path');
function load() {
// Recursively load one or more directories passed as arguments.
let dir,
result = {};
function processFilename(fn) {
const fp = path.join(dir, fn);
let extra;
if (fs.statSync(fp).isDirectory()) {
// If the given filename is a directory, recursively load it.
extra = load(fp);
} else if (path.extname(fp) === '.json') {
try {
extra = JSON.parse(fs.readFileSync(fp));
} catch (e) {
// Skip invalid JSON. Tests will flag the problem separately.
return;
}
} else {
// Skip anything else, such as *~ backup files or similar.
return;
}
// The JSON data is independent of the actual file
// hierarchy, so it is essential to extend "deeply".
extend(result, extra);
}
for (dir of arguments) {
dir = path.resolve(__dirname, dir);
fs.readdirSync(dir).forEach(processFilename);
}
return result;
}
function isPlainObject(v) {
return typeof v === 'object' && v !== null && !Array.isArray(v);
}
function extend(target, source) {
if (!isPlainObject(target) || !isPlainObject(source)) {
throw new Error('Both target and source must be plain objects');
}
// iterate over own enumerable properties
for (const [key, value] of Object.entries(source)) {
// recursively extend if target has the same key, otherwise just assign
if (Object.prototype.hasOwnProperty.call(target, key)) {
extend(target[key], value);
} else {
target[key] = value;
}
}
}
module.exports = load(
'api',
'browsers',
'css',
'html',
'http',
'javascript',
'mathml',
'svg',
'webdriver',
'webextensions',
);