Comparing version 1.0.0 to 1.0.1
22
index.js
@@ -0,1 +1,3 @@ | ||
'use strict'; | ||
const escapeStringRegexp = require('escape-string-regexp'); | ||
@@ -15,3 +17,3 @@ | ||
*/ | ||
module.exports.renderString = function(str, data, delimiters) { | ||
module.exports.renderString = function renderString(str, data, delimiters) { | ||
let renderedStr = str; | ||
@@ -29,12 +31,18 @@ | ||
while((entries = extractVarPattern.exec(renderedStr)) !== null) { | ||
if(!data.hasOwnProperty(entries[1])) { | ||
throw entries[1] +' is not defined'; | ||
const variables = renderedStr.match(extractVarPattern); | ||
if (variables === null) { | ||
return str; | ||
} | ||
for(let i = 0; i < variables.length; i++) { | ||
entries = extractVarPattern.exec(str); | ||
const attr = entries[1]; | ||
if(!data.hasOwnProperty(attr)) { | ||
throw attr +' is not defined'; | ||
} | ||
const itemPattern = new RegExp(d[0] + '\\s*' + entries[1] + '\\s*' + d[1], 'gm'); | ||
renderedStr = renderedStr.replace(itemPattern, data[entries[1]]); | ||
const itemPattern = new RegExp(d[0] + '\\s*' + attr + '\\s*' + d[1], 'gm'); | ||
renderedStr = renderedStr.replace(itemPattern, data[attr]); | ||
} | ||
return renderedStr; | ||
}; | ||
}; |
{ | ||
"name": "templyte", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "Simple Javascript string template. Useful for simple string translations and text replacing.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -0,0 +0,0 @@ # templyte |
@@ -107,2 +107,9 @@ 'use strict'; | ||
}); | ||
it('should render a url query string', () => { | ||
params = {lang: 'es', q: 'google'}; | ||
const output = templyte.renderString('https://encrypted.google.com/search?hl={{lang}}&q={{q}}', params); | ||
const expectedString = 'https://encrypted.google.com/search?hl=es&q=google'; | ||
expect(output).toEqual(expectedString); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
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
9743
134