odesza
Advanced tools
Comparing version 0.0.5 to 0.0.6
175
index.js
@@ -9,6 +9,4 @@ /** | ||
var extend = require('extend'); | ||
var fs = require('fs'); | ||
var vm = require('vm'); | ||
var __import = require('./lib/import'); | ||
@@ -22,107 +20,96 @@ /** | ||
module.exports = function createOdesza(context) { | ||
var odesza = {}; | ||
module.exports = odesza; | ||
// create scope for this instance | ||
var scope = extend(true, {}, context || {}); | ||
/** | ||
* Renders a template with the given variables. | ||
* | ||
* @param {string} template The template to render. | ||
* @param {object} options An object of key-value pairs representing the | ||
* variables to be used in the template. | ||
* @param {string} [basePath] Optional. The base path to use for import and | ||
* require statements. | ||
* @return {string} The rendered template. | ||
*/ | ||
/** | ||
* odesza object. | ||
*/ | ||
odesza.render = function(template, options, basePath) { | ||
var odesza = {}; | ||
options = options && 'object' == typeof options ? options : {}; | ||
/** | ||
* odesza middleware. | ||
*/ | ||
// matches import(''), import(""), require(''), require("") | ||
var rgx = /(import|require)\([\'\"]([\w.\/]+)[\'\"]\)/g; | ||
var matches = template.match(rgx) || []; | ||
var middleware = []; | ||
// make matches array values unique | ||
var imports = matches.filter((m, i) => matches.indexOf(m) == i); | ||
/** | ||
* 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. | ||
*/ | ||
// recursively replace each import statement with its compiled template | ||
imports.forEach(statement => { | ||
let path = basePath + statement.split('\'')[1]; | ||
template = template | ||
.split(statement) | ||
.join(odesza.compile(path, options)); | ||
}); | ||
odesza.render = function(template, vars) { | ||
try { | ||
return vm.runInNewContext('`' + template + '`', options); | ||
} catch (e) { | ||
throw new Error(e); | ||
} | ||
}; | ||
vars = vars && 'object' == typeof vars ? vars : {}; | ||
/** | ||
* 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. | ||
*/ | ||
var renderScope = extend(true, scope, vars); | ||
var ctx = { | ||
template: template, | ||
context: renderScope | ||
}; | ||
odesza.compile = function(path, options) { | ||
if (typeof path != 'string') { | ||
throw new TypeError('path must be a string'); | ||
} | ||
if (path.indexOf('.') == -1) { | ||
path += '.odesza'; | ||
} | ||
try { | ||
var basePath = path.substr(0, path.lastIndexOf('/') + 1); | ||
var template = fs.readFileSync(path).toString().trim(); | ||
} catch (e) { | ||
throw new Error(e); | ||
} | ||
return odesza.render(template, options, basePath); | ||
}; | ||
middleware.forEach(ware => ware.call(odesza, ctx)); | ||
/** | ||
* Install plugin. | ||
* | ||
* @param {Function} fn Middlware | ||
* @return {Object} odesza | ||
*/ | ||
try { | ||
return vm.runInNewContext('`' + (ctx.template || template) + '`', renderScope); | ||
} catch (e) { | ||
throw new Error(e); | ||
} | ||
}; | ||
odesza.use = function(fn) { | ||
if ('function' == typeof fn) { | ||
odesza.middleware.push(fn); | ||
} else { | ||
throw new TypeError('Middleware must provide a function.') | ||
} | ||
return this; | ||
}; | ||
/** | ||
* 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. | ||
*/ | ||
/** | ||
* Adds support for express. | ||
* | ||
* @param {string} path | ||
* @param {object} options | ||
* @param {function} fn | ||
*/ | ||
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; | ||
odesza.__express = function(path, options, fn) { | ||
try { | ||
return fn(null, odesza.compile(path, options)); | ||
} catch (e) { | ||
return fn(e); | ||
} | ||
}; |
{ | ||
"name": "odesza", | ||
"version": "0.0.5", | ||
"description": "Flexible templates powered by ES6 template strings.", | ||
"version": "0.0.6", | ||
"description": "The most straightforward template engine for JavaScript, using ES6 template string syntax.", | ||
"main": "index.js", | ||
@@ -11,5 +11,2 @@ "author": "Wells Johnston", | ||
}, | ||
"dependencies": { | ||
"extend": "^3.0.0" | ||
}, | ||
"devDependencies": { | ||
@@ -16,0 +13,0 @@ "tape": "^4.2.2" |
@@ -12,5 +12,12 @@ 'use strict'; | ||
test('simple', t => { | ||
let context = odesza({value: 'world'}); | ||
let string = context.compile(fixture('simple.txt')); | ||
test('render variable', t => { | ||
let vars = { value: 'world' }; | ||
let string = odesza.render('hello ${value}', vars); | ||
t.ok(string == 'hello world', 'template renders') | ||
t.end(); | ||
}); | ||
test('compile file', t => { | ||
let vars = { value: 'world' }; | ||
let string = odesza.compile(fixture('simple.txt'), vars); | ||
t.ok(string == 'hello world', 'template compiles'); | ||
@@ -20,16 +27,21 @@ t.end(); | ||
test('simple middleware', t => { | ||
let context = odesza({value: 'odesza'}); | ||
let ware = ctx => { ctx.template = ctx.template.replace('hello', 'HELLO') }; | ||
let string = context.use(ware).compile(fixture('simple.txt')); | ||
t.ok(string == 'HELLO odesza', 'template compiles with middleware changes'); | ||
test('import', t => { | ||
let vars = { name : 'yo' }; | ||
let string = odesza.compile(fixture('message1'), vars); | ||
t.ok(string == 'yo1', 'import statement works'); | ||
t.end(); | ||
}); | ||
test('function middleware', t => { | ||
let context = odesza(); | ||
let injectHello = ctx => { ctx.context.hello = () => 'hello'; }; | ||
let string = context.use(injectHello).compile(fixture('with-function-call.txt')); | ||
t.ok(string == 'hello', 'template compiles with middleware function call'); | ||
test('recursive imports', t => { | ||
let vars = { name: 'yo' }; | ||
let string = odesza.compile(fixture('message3.txt'), vars); | ||
t.ok(string == 'yo3yo1', 'recursive import statements'); | ||
t.end(); | ||
}); | ||
test('multiple import statements', t => { | ||
let vars = { name: 'yo' }; | ||
let string = odesza.compile(fixture('messages.txt'), vars); | ||
t.ok(string == 'yo1yo2yo3yo1', 'multiple import statements'); | ||
t.end(); | ||
}); |
No README
QualityPackage does not have a README. This may indicate a failed publish or a low quality package.
Found 1 instance in 1 package
6064
0
13
1
24
136
- Removedextend@^3.0.0
- Removedextend@3.0.2(transitive)