l0bsterssg

node.js static responsive blog post generator
Log | Files | Refs | README

engines.js (1023B)


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