1 | 'use strict';
|
2 |
|
3 | var extend = require('extend-shallow');
|
4 | var yaml = require('js-yaml');
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 | var engines = exports = module.exports;
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 | engines.yaml = {
|
17 | parse: yaml.safeLoad.bind(yaml),
|
18 | stringify: yaml.safeDump.bind(yaml)
|
19 | };
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 | engines.json = {
|
26 | parse: JSON.parse.bind(JSON),
|
27 | stringify: function(obj, options) {
|
28 | var opts = extend({replacer: null, space: 2}, options);
|
29 | return JSON.stringify(obj, opts.replacer, opts.space);
|
30 | }
|
31 | };
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 | engines.javascript = {
|
38 | parse: function parse(str, options, wrap) {
|
39 |
|
40 | try {
|
41 | if (wrap !== false) {
|
42 | str = '(function() {\nreturn ' + str.trim() + ';\n}());';
|
43 | }
|
44 | return eval(str) || {};
|
45 | } catch (err) {
|
46 | if (wrap !== false && /(unexpected|identifier)/i.test(err.message)) {
|
47 | return parse(str, options, false);
|
48 | }
|
49 | throw new SyntaxError(err);
|
50 | }
|
51 | },
|
52 | stringify: function() {
|
53 | throw new Error('stringifying JavaScript is not supported');
|
54 | }
|
55 | };
|