Comparing version 1.0.4 to 1.0.5
{ | ||
"name": "jeyson", | ||
"version": "1.0.4", | ||
"version": "1.0.5", | ||
"description": "Json template engine", | ||
@@ -5,0 +5,0 @@ "main": "./src/index.js", |
@@ -18,17 +18,4 @@ var linker = require("./linker"), | ||
return JSON.parse(config.getTemplate(path)); | ||
}, | ||
compileNode = function(scope, nodeValue){ | ||
if(Array.isArray(nodeValue)){ | ||
return nodeValue.map(function (element){ | ||
return compile(scope, element); | ||
}); | ||
} | ||
if(typeof nodeValue == "object"){ | ||
return compile(scope, nodeValue); | ||
} | ||
return linker.link(scope, nodeValue); | ||
}; | ||
if(templates.isDirective(template)) { | ||
@@ -38,7 +25,17 @@ return directives.link(scope, template, compile, getTemplate); | ||
for(var node in template){ | ||
result[node] = compileNode(scope, template[node]); | ||
if(templates.isList(template)) { | ||
return template.map(function(element){ | ||
return compile(scope, element, config); | ||
}); | ||
} | ||
return result; | ||
if(templates.isSubtree(template)){ | ||
for(var node in template){ | ||
result[node] = compile(scope, template[node], config); | ||
} | ||
return result; | ||
} | ||
return linker.link(scope, template); | ||
} | ||
}; |
@@ -1,4 +0,4 @@ | ||
var repeater = require("./directives/repeat"), | ||
compileD = require("./directives/compile"), | ||
include = require("./directives/include"), | ||
var repeater = require("./directives/repeat"), | ||
compileIt = require("./directives/compile"), | ||
include = require("./directives/include"), | ||
templates = require("./templates"), | ||
@@ -15,7 +15,8 @@ all = {}; | ||
}, | ||
link: function (scope, template, compile, getTemplate) { | ||
link: function (scope, body, compile, getTemplate) { | ||
var directive, | ||
param; | ||
param, | ||
replaceBody; | ||
for(var field in template){ | ||
for(var field in body){ | ||
if(field.startsWith("@")){ | ||
@@ -29,13 +30,13 @@ directive = { | ||
param = template[directive.name]; | ||
templates.deleteDirective(template, directive.name) | ||
param = body[directive.name]; | ||
templates.deleteDirective(body, directive.name) | ||
var replace = directive.directive.link(scope, template, param, compile, getTemplate); | ||
template = replace || compile(scope, template); // replace if directive returns valid value, else compile the template after directoryis done | ||
return template; | ||
// If directive returns body, replace template with returned body | ||
// Else compile the updated body and return | ||
return directive.directive.link(scope, body, param, compile, getTemplate) || compile(scope, body); | ||
} | ||
}; | ||
Directives.add("@repeat", {link: repeater.link}); | ||
Directives.add("@compile", {link: compileD.link}); | ||
Directives.add("@compile", {link: compileIt.link}); | ||
Directives.add("@include", {link: include.link}); | ||
module.exports = Directives; |
module.exports = { | ||
link: function(scope, template, params, compile){ | ||
link: function(scope, body, params, compile){ | ||
return compile(scope, scope.execute(params)); | ||
} | ||
}; |
var extend = require("extend"); | ||
module.exports = { | ||
link: function(scope, template, params, compile, getTemplate){ | ||
var includes = compile(scope, getTemplate(params)); | ||
extend(true, | ||
template, | ||
includes); | ||
link: function(scope, body, params, compile, getTemplate){ | ||
extend( | ||
true, | ||
body, | ||
compile(scope, getTemplate(params)) | ||
); | ||
} | ||
}; |
module.exports = { | ||
link: function(scope, template, params, compile){ | ||
link: function(scope, body, params, compile){ | ||
var varName = params.split(" in ")[0].trim(), | ||
@@ -13,3 +13,3 @@ listName = params.split(" in ")[1].trim(), | ||
var newScope = scope.createChild(params); | ||
parsed[index] = compile(newScope, template) | ||
parsed[index] = compile(newScope, body) | ||
} | ||
@@ -16,0 +16,0 @@ |
@@ -6,3 +6,3 @@ var error = require("./scope-error"); | ||
for (var field in scope) { | ||
contextScript += ("var <field> = this.<field>;".replace("<field>", field).replace("<field>", field)); | ||
contextScript += ("var <field> = scope.<field>;".replace("<field>", field).replace("<field>", field)); | ||
} | ||
@@ -14,3 +14,3 @@ scope.execute = function () { | ||
}catch(err){ | ||
return error.create({scope: this, expression: expression, error: err}).message; | ||
return error.create({scope: scope, expression: expression, error: err}).message; | ||
} | ||
@@ -17,0 +17,0 @@ }; |
@@ -85,12 +85,22 @@ var extend = function() { | ||
module.exports = { | ||
copy : function (template) { | ||
var Templates = { | ||
copy: function (template) { | ||
if (Templates.isList(template)){ | ||
return template.map(function(element){ | ||
return Templates.copy(element); | ||
}); | ||
} | ||
var result = {}; | ||
extend(true, result, template); | ||
if(Templates.isSubtree(template)){ | ||
extend(true, result, template); | ||
} else{ | ||
return result = template; | ||
} | ||
return result; | ||
}, | ||
deleteDirective : function (template, name) { | ||
deleteDirective: function (template, name) { | ||
delete template[name]; | ||
}, | ||
isDirective : function (template) { | ||
isDirective: function (template) { | ||
for (var field in template) { | ||
@@ -103,2 +113,9 @@ if (field.startsWith("@")) { | ||
}, | ||
}; | ||
isSubtree: function (template) { | ||
return (typeof template == "object") && !Array.isArray(template); | ||
}, | ||
isList: function (template) { | ||
return Array.isArray(template); | ||
} | ||
}; | ||
module.exports = Templates; |
@@ -31,2 +31,10 @@ var expect = require('chai').expect, | ||
it('should compile expressions inside arrays', function () { | ||
var scope = {"id" : "some-id"}, | ||
template = {array: ["my-{{id}}"]}, | ||
parsed = jeyson.compile(scope, template); | ||
expect(parsed.array[0]).to.equal("my-some-id"); | ||
}); | ||
it('should parse sub trees', function () { | ||
@@ -33,0 +41,0 @@ var scope = {"id" : "some-id"}, |
{ | ||
"scope" : {}, | ||
"scope" : {"userName": "Billu"}, | ||
"config" : {}, | ||
"template" : { | ||
"message" : "hello", | ||
"message" : "Hello {{userName}}", | ||
"float" : "{{0 + 1.0 }}", | ||
@@ -15,3 +15,3 @@ "sum" : "{{0 + 1 }}", | ||
"result" : { | ||
"message" : "hello", | ||
"message" : "Hello Billu", | ||
"sum" : 1, | ||
@@ -18,0 +18,0 @@ "float" : 1.0, |
@@ -13,3 +13,6 @@ var fs = require("fs"), | ||
}).map(function(specFile){ | ||
return JSON.parse(fs.readFileSync(specsPath +"/" + specFile)); | ||
var filename = specsPath + "/" + specFile, | ||
spec = JSON.parse(fs.readFileSync(filename)); | ||
spec.filename = filename; | ||
return spec; | ||
}); | ||
@@ -16,0 +19,0 @@ }, |
@@ -22,2 +22,9 @@ var expect = require('chai').expect, | ||
}); | ||
it('template.copy should copy a list type', function () { | ||
var template = ["one", "two"], | ||
copy = templates.copy(template); | ||
expect(copy).to.deep.equal(template); | ||
}); | ||
}); |
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
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
2
1
40475
29
1046