Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

beard

Package Overview
Dependencies
Maintainers
1
Versions
21
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

beard - npm Package Compare versions

Comparing version 0.2.0 to 0.3.0

203

beard.js

@@ -1,18 +0,25 @@

(function(context){
(function(){
var Beard = function(cache){
this._cache = cache;
}
var iterator = 0;
var onIgnore = false;
var skipIgnore = false;
var ignores = {};
var ignoreId = 0;
var exps = {
_block: (/{block\s+'(.[^}]*)'}([^]*?){endblock}/g),
_include: (/include\s(\S+?)$/),
_statement: (/\{\s*([^}]+?)\s*\}/g),
_operators: (/\s+(and|or|eq|neq|is|isnt|not)\s+/g),
_if: (/^if\s+([^]*)$/),
_elseif: (/^else\s+if\s+([^]*)$/),
_else: (/^else$/),
_for: (/^for\s+([$A-Za-z_][0-9A-Za-z_]*)(?:\s*,\s*([$A-Za-z_][0-9A-Za-z_]*))?\s+in\s+(.*)$/),
_each: (/^each\s+([$A-Za-z_][0-9A-Za-z_]*)(?:\s*,\s*([$A-Za-z_][0-9A-Za-z_]*))?\s+in\s(.*)$/),
_end: (/^end$/)
extend: (/{extend\s(.*?)}/),
include: (/{include\s(.*?)}/g),
block: (/{block\s+(.[^}]*)}([^]*?){endblock}/g),
ignore: (/{ignore}([^]*?){endignore}/g),
ignoreTemp: (/_ignore_([^]*?)_endignore_/g),
statement: (/\{\s*([^}]+?)\s*\}/g),
operators: (/\s+(and|or|eq|neq|is|isnt|not)\s+/g),
if: (/^if\s+([^]*)$/),
elseIf: (/^else\s+if\s+([^]*)$/),
else: (/^else$/),
for: (/^for\s+([$A-Za-z_][0-9A-Za-z_]*)(?:\s*,\s*([$A-Za-z_][0-9A-Za-z_]*))?\s+in\s+(.*)$/),
each: (/^each\s+([$A-Za-z_][0-9A-Za-z_]*)(?:\s*,\s*([$A-Za-z_][0-9A-Za-z_]*))?\s+in\s(.*)$/),
end: (/^end$/)
};

@@ -32,23 +39,29 @@

_include: function(_, name){
return '_buffer += (_data_["' + name + '"] || "")';
ignore: function(_, contents) {
ignoreId += 1;
ignores[ignoreId] = contents;
return '_ignore_' + ignoreId + '_endignore_';
},
_operators: function(_, op){
ignoreTemp: function(_, id) {
return ignores[id];
},
operators: function(_, op) {
return operators[op];
},
_if: function(_, statement){
if: function(_, statement) {
return 'if (' + statement + ') {';
},
_elseif: function(_, statement){
elseIf: function(_, statement) {
return '} else if (' + statement +') {';
},
_else: function(){
else: function() {
return '} else {';
},
_for: function(_, key, value, object){
for: function(_, key, value, object) {
if (!value) key = (value = key, 'iterator' + iterator++);

@@ -58,3 +71,3 @@ return 'for (var ' + key + ' in ' + object + '){' + 'var ' + value + ' = ' + object + '[' + key +'];';

_each: function(_, iter, value, array){
each: function(_, iter, value, array) {
if (!value) iter = (value = iter, 'iterator' + iterator++);

@@ -65,3 +78,3 @@ var length = 'length' + iterator++;

_end: function(){
end: function() {
return '}';

@@ -71,63 +84,45 @@ }

var parser = function(match, inner){
var prev = inner;
Beard.prototype = {
switch (true) {
case (match == '{ignore}'):
onIgnore = true;
return skipIgnore ? match : '';
case (onIgnore && match == '{endignore}'):
onIgnore = false;
return skipIgnore ? match : '';
case (onIgnore):
return match;
}
parser: function(match, inner) {
var prev = inner;
inner = inner
.replace(exps.operators, parse.operators)
.replace(exps.end, parse.end)
.replace(exps.else, parse.else)
.replace(exps.elseIf, parse.elseIf)
.replace(exps.if, parse.if)
.replace(exps.each, parse.each)
.replace(exps.for, parse.for);
inner = inner
.replace(exps._include, parse._include)
.replace(exps._operators, parse._operators)
.replace(exps._end, parse._end)
.replace(exps._else, parse._else)
.replace(exps._elseif, parse._elseif)
.replace(exps._if, parse._if)
.replace(exps._each, parse._each)
.replace(exps._for, parse._for);
return '";' + (inner === prev ? ' _buffer += ' : '') + inner.replace(/\t|\n|\r/, '') + '; _buffer += "';
},
return '";' + (inner == prev ? ' _buffer += ' : '') + inner.replace(/\t|\n|\r/, '') + '; _buffer += "';
};
parseExtend: function(template, data) {
var matches;
template = template.replace(exps.extend, function(){
matches = ([]).slice.call(arguments, 0);
return '';
});
var compiler = function(str){
str = str.replace(new RegExp('\\\\', 'g'), '\\\\').replace(/"/g, '\\"');
if (matches && matches.length) {
var path = matches[1];
var view = this._cache[path];
view = view.replace(exps.ignore, parse.ignore);
view += "{block view}" + this.preRender(template, data) + "{endblock}";
return view;
} else {
return template;
}
},
var fn = ('var _buffer = ""; with (_data_){ _buffer += "' + str.replace(exps._statement, parser) + '"; return _buffer; }')
// var fn = ('var _buffer = ""; for (var prop in _data_) { if (_data_.hasOwnProperty(prop)) this[prop] = _data_[prop] } _buffer += "' + str.replace(exps._statement, parser) + '"; return _buffer;')
.replace(/_buffer\s\+\=\s"";/, '')
.replace(/(\{|\});/g, '$1')
.replace('_buffer += "";', '')
.replace(/\n/g, '\\n')
.replace(/\t/g, '\\t')
.replace(/\r/g, '\\r');
parseInclude: function(template, data) {
return template.replace(exps.include, function(_, path){
return this.preRender(this._cache[path], data);
}.bind(this));
},
try {
return new Function('_data_', fn);
} catch (e) {
throw new Error('Cant compile template:' + fn);
}
};
// String.prototype.matchAll = function(regexp) {
// var matches = [];
// this.replace(regexp, function(){
// var arr = ([]).slice.call(arguments, 0);
// matches.push(arr);
// });
// return matches.length ? matches : null;
// };
var Beard = {
render: function(template, view){
parseBlock: function(template, data) {
var matches = [];
template = template.replace(exps._block, function(){
template = template.replace(exps.block, function(){
var arr = ([]).slice.call(arguments, 0);

@@ -138,11 +133,51 @@ matches.push(arr);

skipIgnore = true;
matches.forEach(function(set){
// set[1] is the var name;
// set[2] is the var content;
view[set[1]] = compiler(set[2])(view);
});
// set[2] is the var value;
data[set[1]] = this.compile(set[2])(data);
}.bind(this));
skipIgnore = false;
return compiler(template)(view);
return this.compile(template)(data);
},
preRender: function(template, data){
template = template.replace(exps.ignore, parse.ignore);
template = this.parseExtend(template, data);
template = this.parseInclude(template, data);
template = this.parseBlock(template, data);
return this.compile(template)(data);
},
render: function(template, data){
template = this.preRender(template, data);
template = template.replace(exps.ignoreTemp, parse.ignoreTemp);
ignoreId = 0;
ignores = {};
return template;
},
compile: function(str) {
str = str
.replace(new RegExp('\\\\', 'g'), '\\\\').replace(/"/g, '\\"')
.replace(exps.statement, this.parser)
.replace(/_buffer_\s\+=\s"";/g, '')
.replace(/(\{|\});/g, '$1')
.replace(/\n/g, '\\n')
.replace(/\t/g, '\\t')
.replace(/\r/g, '\\r');
var fn = (
'var _buffer = ""; \
for (var prop in _data_) { \
if (_data_.hasOwnProperty(prop)) this[prop] = _data_[prop]; \
} \
_buffer += "' + str + '"; \
return _buffer;'
);
try {
return new Function('_data_', fn);
} catch (e) {
throw new Error('Cant compile template:' + fn);
}
}

@@ -155,5 +190,5 @@ };

} else {
context.Beard = Beard;
window.Beard = Beard;
}
})(typeof module != 'undefined' ? module.exports : this);
})();
{
"name": "beard",
"version": "0.2.0",
"version": "0.3.0",
"description": "More than a mustache.",

@@ -5,0 +5,0 @@ "keywords": ["template engine", "node", "browser"],

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc