Comparing version 0.0.5 to 0.1.0
62
index.js
@@ -1,3 +0,1 @@ | ||
var regex = /(\{\{\s*\w+\s*\}\})/ | ||
module.exports = Block | ||
@@ -9,33 +7,19 @@ | ||
var blocks = this.blocks = string.split(regex) | ||
var names = this.names = {} | ||
var block | ||
for (var i = 0, l = blocks.length; i < l; i++) | ||
if (regex.test(block = blocks[i])) | ||
names[i] = block.match(/(\w+)/)[0] | ||
this.string = string | ||
} | ||
Block.prototype.locals = | ||
Block.prototype.local = function (name, value) { | ||
var names = this.names | ||
var j | ||
if (typeof name === 'object') | ||
Object.keys(name).forEach(function (key) { | ||
this.replace(key, name[key]) | ||
}, this) | ||
else | ||
this.replace(name, value) | ||
for (var i in names) | ||
if (names[i] === name && names.hasOwnProperty(i)) | ||
j = i | ||
if (j == null) | ||
throw new Error('Name ' + name + ' is not defined.') | ||
delete names[j] | ||
this.blocks[j] = value || '' | ||
return this | ||
} | ||
Block.prototype.locals = function (object) { | ||
for (var name in object) | ||
if (object.hasOwnProperty(name)) | ||
this.local(name, object[name]) | ||
Block.prototype.replace = function (key, value) { | ||
this.string = replace(this.string, key, value) | ||
@@ -46,14 +30,11 @@ return this | ||
Block.prototype.render = function (locals) { | ||
locals = locals || {} | ||
if (typeof locals === 'object') { | ||
var string = this.string | ||
Object.keys(locals).forEach(function (key) { | ||
string = replace(string, key, locals[key]) | ||
}) | ||
return string | ||
} | ||
var blocks = this.blocks | ||
var names = this.names | ||
var html = '' | ||
var name | ||
for (var i = 0, l = blocks.length; i < l; i++) | ||
html += (name = names[i]) ? locals[name] : blocks[i] | ||
return html | ||
return this.string | ||
} | ||
@@ -70,1 +51,8 @@ | ||
} catch (err) {} | ||
function replace(string, key, value) { | ||
return string.replace( | ||
new RegExp('\\{\\{\\s*' + key + '\\s*\\}\\}', 'g'), | ||
value | ||
) | ||
} |
{ | ||
"name": "block", | ||
"description": "Block-based HTML templating", | ||
"version": "0.0.5", | ||
"version": "0.1.0", | ||
"devDependencies": { | ||
@@ -27,2 +27,2 @@ "mocha": "*", | ||
"license": "MIT" | ||
} | ||
} |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
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
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
1
4195
6
43
1