Socket
Socket
Sign inDemoInstall

odesza

Package Overview
Dependencies
Maintainers
1
Versions
29
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

odesza - npm Package Compare versions

Comparing version 0.0.4 to 0.0.5

.npmignore

149

index.js

@@ -9,56 +9,119 @@ /**

var extend = require('extend');
var fs = require('fs');
var vm = require('vm');
var __import = require('./lib/import');
var odesza = {};
module.exports = odesza;
/**
* Renders a template with the given variables.
* Creates an odesza object.
*
* @param {string} template The template to render.
* @param {object} vars An object of key-value pairs representing the
* variables to be used in the template.
* @return {string} The rendered template.
* @param {Object} context Initial context
* @return {Object}
*/
odesza.render = function(template, vars) {
try {
return vm.runInNewContext('`' + template + '`', vars);
} catch (e) {
throw new Error(e);
}
};
module.exports = function createOdesza(context) {
/**
* Compiles a template file.
*
* @param {string} path The path to the template file.
* @param {object} options Options passed in to render the template.
* @return {string} The rendered template.
*/
// create scope for this instance
var scope = extend(true, {}, context || {});
odesza.compile = function(path, options) {
try {
var template = fs.readFileSync(path).toString();
} catch (e) {
throw new Error(e);
}
return odesza.render(template, options);
};
/**
* odesza object.
*/
/**
* Adds support for express.
*
* @param {string} path
* @param {object} options
* @param {function} fn
*/
var odesza = {};
odesza.__express = function(path, options, fn) {
try {
return fn(null, odesza.compile(path, options));
} catch (e) {
return fn(e);
}
/**
* odesza middleware.
*/
var middleware = [];
/**
* Renders a template with the given variables.
*
* @param {string} template The template to render.
* @param {object} vars An object of key-value pairs representing the
* variables to be used in the template.
* @return {string} The rendered template.
*/
odesza.render = function(template, vars) {
vars = vars && 'object' == typeof vars ? vars : {};
var renderScope = extend(true, scope, vars);
var ctx = {
template: template,
context: renderScope
};
middleware.forEach(ware => ware.call(odesza, ctx));
try {
return vm.runInNewContext('`' + (ctx.template || template) + '`', renderScope);
} catch (e) {
throw new Error(e);
}
};
/**
* Compiles a template file.
*
* @param {string} path The path to the template file.
* @param {object} options Options passed in to render the template.
* @return {string} The rendered template.
*/
odesza.compile = function(path, options) {
try {
var template = fs.readFileSync(path).toString().trim();
} catch (e) {
throw new Error(e);
}
return odesza.render(template, options);
};
/**
* Install plugin.
*
* @param {Function} fn Middlware
* @return {Object} odesza
*/
odesza.use = function(fn) {
if ('function' == typeof fn) {
middleware.push(fn);
} else {
throw new TypeError('fn is not Function type.')
}
return this;
};
/**
* Adds support for import('') syntax.
*/
odesza.use(__import);
/**
* Adds support for express.
*
* @param {string} path
* @param {object} options
* @param {function} fn
*/
odesza.__express = function(path, options, fn) {
try {
return fn(null, odesza.compile(path, options));
} catch (e) {
return fn(e);
}
};
/**
* odesza instance.
*/
return odesza;
};
{
"name": "odesza",
"version": "0.0.4",
"version": "0.0.5",
"description": "Flexible templates powered by ES6 template strings.",
"main": "index.js",
"author": "Wells Johnston",
"license": "MIT"
"license": "MIT",
"scripts": {
"test": "node test"
},
"dependencies": {
"extend": "^3.0.0"
},
"devDependencies": {
"tape": "^4.2.2"
}
}
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc