UNPKG

1.05 kBJavaScriptView Raw
1'use strict';
2
3var extend = require('extend-shallow');
4var yaml = require('js-yaml');
5
6/**
7 * Default engines
8 */
9
10var engines = exports = module.exports;
11
12/**
13 * YAML
14 */
15
16engines.yaml = {
17 parse: yaml.safeLoad.bind(yaml),
18 stringify: yaml.safeDump.bind(yaml)
19};
20
21/**
22 * JSON
23 */
24
25engines.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 * JavaScript
35 */
36
37engines.javascript = {
38 parse: function parse(str, options, wrap) {
39 /* eslint no-eval: 0 */
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};