Comparing version 0.0.4 to 0.0.5
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" | ||
} | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
4491
7
146
1
1
4
+ Addedextend@^3.0.0
+ Addedextend@3.0.2(transitive)