Comparing version 1.2.2 to 1.3.0-hf.1
@@ -26,5 +26,9 @@ var Promise = require('bluebird') | ||
function getOpalScriptWithERBTemplate (erbTemplate) { | ||
function getOpalScriptWithERBTemplate (erbTemplate, fields) { | ||
return replaceAll({ | ||
string: opalScript, | ||
string: replaceAll({ | ||
string: opalScript, | ||
target: '__FIELDS__', | ||
replacement: stringToBase64String(JSON.stringify(fields)) | ||
}), | ||
target: '__TEMPLATE__', | ||
@@ -31,0 +35,0 @@ replacement: stringToBase64String(erbTemplate) |
@@ -19,6 +19,3 @@ var Promise = require('bluebird') | ||
if (options.data === undefined) { | ||
options.data = { | ||
values: {}, | ||
functions: {} | ||
} | ||
options.data = {} | ||
} | ||
@@ -31,2 +28,5 @@ if (options.data.values === undefined) { | ||
} | ||
if (options.data.fields === undefined) { | ||
options.data.fields = {} | ||
} | ||
@@ -36,6 +36,21 @@ return options | ||
function earlyValidation (opts) { | ||
if (opts.data && typeof opts.data === 'object' && opts.data.fields && typeof opts.data.fields === 'object') { | ||
Object.keys(opts.data.fields).forEach(function (field) { | ||
if (opts.data.fields[field] === undefined) { | ||
throw new Error(field + ' field is undefined') | ||
} | ||
if (typeof opts.data.fields[field] === 'function') { | ||
throw new Error(field + ' field is a function') | ||
} | ||
}) | ||
} | ||
} | ||
function erb (opts) { | ||
var options = getNormalizedOptions(opts) | ||
var options | ||
return Promise | ||
.try(function () { | ||
earlyValidation(opts) | ||
options = getNormalizedOptions(opts) | ||
validateTemplate(options.template) | ||
@@ -46,7 +61,9 @@ validateData(options.data) | ||
.then(function (rubyCode) { | ||
return getOpalScriptWithERBTemplate('<%\n' + rubyCode + '\n%>' + options.template) | ||
return getOpalScriptWithERBTemplate('<%\n' + rubyCode + '\n%>' + options.template, options.data.fields) | ||
}) | ||
.then(runOpalScript.bind(null, options.timeout)) | ||
.then(function (script) { | ||
return runOpalScript(options.timeout, script) | ||
}) | ||
} | ||
module.exports = erb |
@@ -6,3 +6,12 @@ /* eslint-disable */ | ||
eval(global.Opal.Opal.ERB.$compile(new Buffer('__TEMPLATE__', 'base64').toString('utf8'), 'mytemplate')); | ||
output = global.Opal.Template['$[]']('mytemplate').$render(); | ||
var ctx = global.Opal.get('Object').$new(); | ||
var json = new Buffer('__FIELDS__', 'base64').toString('utf8'); | ||
var jsFields = JSON.parse(json); | ||
var rubyFields = global.Opal.get('JSON').$parse(json); | ||
Object.keys(jsFields).forEach(function (field) { | ||
ctx.$instance_variable_set("@" + field, rubyFields['$[]'](field)); | ||
}); | ||
output = global.Opal.Template['$[]']('mytemplate').$render(ctx); | ||
/* eslint-enable */ |
@@ -1,22 +0,27 @@ | ||
var maxArguments = 60056 | ||
var maxArguments = 39017 | ||
function validateData (data) { | ||
if (typeof data.values !== 'object') { | ||
throw new Error('data.values must be an object') | ||
function validateObject (data, key) { | ||
if (typeof data[key] !== 'object') { | ||
throw new Error('data.' + key + ' must be an object') | ||
} | ||
if (data.values === null) { | ||
throw new Error('data.values must not be null') | ||
if (data[key] === null) { | ||
throw new Error('data.' + key + ' must not be null') | ||
} | ||
if (typeof data.functions !== 'object') { | ||
throw new Error('data.functions must be an object') | ||
} | ||
if (data.functions === null) { | ||
throw new Error('data.functions must not be null') | ||
} | ||
Object.keys(data.values).forEach(function (name) { | ||
var type = typeof data.values[name] | ||
} | ||
function validatePrimitives (object, what) { | ||
Object.keys(object).forEach(function (name) { | ||
var type = typeof object[name] | ||
if (type !== 'number' && type !== 'string' && type !== 'boolean' && type !== 'object') { | ||
throw new Error('type of value ' + name + ' is ' + type + ', but only number, string, boolean and object types are supported') | ||
throw new Error('type of ' + what + ' ' + name + ' is ' + type + ', but only number, string, boolean and object types are supported') | ||
} | ||
}) | ||
} | ||
function validateData (data) { | ||
validateObject(data, 'values') | ||
validateObject(data, 'functions') | ||
validateObject(data, 'fields') | ||
validatePrimitives(data.values, 'value') | ||
validatePrimitives(data.fields, 'field') | ||
Object.keys(data.functions).forEach(function (name) { | ||
@@ -23,0 +28,0 @@ var definition = data.functions[name] |
{ | ||
"name": "erb", | ||
"version": "1.2.2", | ||
"version": "1.3.0-hf.1", | ||
"description": "Compile a given Embedded RuBy (ERB) template using variables and functions defined in given a JavaScript object", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -19,2 +19,6 @@ # erb [![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com) [![Build Status](https://travis-ci.org/wix/erb.svg?branch=master)](https://travis-ci.org/wix/erb) | ||
var data = { | ||
"fields": { | ||
"first": "Morty", | ||
"second": "Rick" | ||
}, | ||
"values": { | ||
@@ -41,3 +45,3 @@ "additions": "with pattie, breaded and fried" | ||
data: data, | ||
template: 'Morty had <%= title(1) %> <%= additions %>.\nRick had <%= title(2, 3) %> <%= additions %>.' | ||
template: '<%= @first %> had <%= title(1) %> <%= additions %>.\n<%= @second %> had <%= title(2, 3) %> <%= additions %>.' | ||
}).then(console.log, console.error); | ||
@@ -61,2 +65,3 @@ ``` | ||
* `data` (optional, object) - an object that contains these properties: | ||
* `fields` (optional, object) - the keys of this object are instance variable names to be used in the ERB template and values are the values of the instance variables | ||
* `values` (optional, object) - the keys of this object are variable names to be used in the ERB template and values are the values of the variables | ||
@@ -63,0 +68,0 @@ * `functions` (optional, object) - the keys of this object are function names to be used in the ERB template and values are special arrays - the items match the function call arguments with the last item being the value returned by the function when called with these arguments. |
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
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
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
1882462
35296
74
1