-
-
Notifications
You must be signed in to change notification settings - Fork 133
/
engines.js
54 lines (45 loc) · 1023 Bytes
/
engines.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
'use strict';
const yaml = require('js-yaml');
/**
* Default engines
*/
const engines = exports = module.exports;
/**
* YAML
*/
engines.yaml = {
parse: yaml.safeLoad.bind(yaml),
stringify: yaml.safeDump.bind(yaml)
};
/**
* JSON
*/
engines.json = {
parse: JSON.parse.bind(JSON),
stringify: function(obj, options) {
const opts = Object.assign({replacer: null, space: 2}, options);
return JSON.stringify(obj, opts.replacer, opts.space);
}
};
/**
* JavaScript
*/
engines.javascript = {
parse: function parse(str, options, wrap) {
/* eslint no-eval: 0 */
try {
if (wrap !== false) {
str = '(function() {\nreturn ' + str.trim() + ';\n}());';
}
return eval(str) || {};
} catch (err) {
if (wrap !== false && /(unexpected|identifier)/i.test(err.message)) {
return parse(str, options, false);
}
throw new SyntaxError(err);
}
},
stringify: function() {
throw new Error('stringifying JavaScript is not supported');
}
};