Comparing version 1.0.3 to 1.0.4
{ | ||
"name": "jeyson", | ||
"version": "1.0.3", | ||
"version": "1.0.4", | ||
"description": "Json template engine", | ||
@@ -18,3 +18,3 @@ "main": "./src/index.js", | ||
"json", | ||
"jsong", | ||
"jeyson", | ||
"jso-ng", | ||
@@ -21,0 +21,0 @@ "template", |
@@ -13,4 +13,4 @@ ## Jeyson Templates | ||
scope = {message: 'Hello!'}, | ||
templateJson = '{"message": "{{message}}"}', | ||
compiled = jeyson.parse(scope, templateJson); | ||
templateJson = {"message": "{{message}}"}, | ||
compiled = jeyson.compile(scope, templateJson); | ||
``` | ||
@@ -17,0 +17,0 @@ |
@@ -8,3 +8,3 @@ var linker = require("./linker"), | ||
$compile: function (scope, template, config) { | ||
return this.compile(scopes.create(scope), template, config); | ||
return this.compile(scopes.create(scope), template, config ? config : {}); | ||
}, | ||
@@ -15,25 +15,30 @@ compile: function (scope, template, config) { | ||
compile = function(scope, template){ | ||
return self.compile(scope, template, config); | ||
return self.compile(scope, templates.copy(template), config); | ||
}, | ||
getTemplate = function(path){ | ||
return templates.create(JSON.parse(config.getTemplate(path))); | ||
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); | ||
}; | ||
config = config ? config : {}; | ||
//TODO invoke compile through $comiple (always) | ||
template.__ || (template = templates.create(template)); | ||
if(template.isDirective()) { | ||
if(templates.isDirective(template)) { | ||
return directives.link(scope, template, compile, getTemplate); | ||
} | ||
for (var node in template) { | ||
var value = template[node], | ||
isSubtree = (typeof value == "object") && !(value instanceof Array); | ||
result[node] = isSubtree ? this.compile(scope, value, config) : linker.link(scope, value); | ||
for(var node in template){ | ||
result[node] = compileNode(scope, template[node]); | ||
} | ||
return result.render(); | ||
return result; | ||
} | ||
}; |
var repeater = require("./directives/repeat"), | ||
compileD = require("./directives/compile"), | ||
include = require("./directives/include"), | ||
templates = require("./templates"), | ||
all = {}; | ||
@@ -18,10 +19,13 @@ | ||
for (var field in template) { | ||
field.startsWith("@") && (directive = { | ||
name: field, | ||
directive: all[field] | ||
}); | ||
} | ||
for(var field in template){ | ||
if(field.startsWith("@")){ | ||
directive = { | ||
name: field, | ||
directive: all[field] | ||
} | ||
} | ||
}; | ||
param = template[directive.name]; | ||
template.deleteDirective(directive.name) | ||
templates.deleteDirective(template, directive.name) | ||
@@ -28,0 +32,0 @@ var replace = directive.directive.link(scope, template, param, compile, getTemplate); |
@@ -13,3 +13,3 @@ module.exports = { | ||
var newScope = scope.createChild(params); | ||
parsed[index] = compile(newScope, template.copy()) | ||
parsed[index] = compile(newScope, template) | ||
} | ||
@@ -16,0 +16,0 @@ |
@@ -1,36 +0,102 @@ | ||
var extend = require("extend"); | ||
var create = function(template){ | ||
template.__ = true; //TODO for trnsitioning to template model | ||
template.deleteDirective = function(name){ | ||
delete this[name]; | ||
var extend = function() { | ||
var hasOwn = Object.prototype.hasOwnProperty; | ||
var toStr = Object.prototype.toString; | ||
var isArray = function isArray(arr) { | ||
if (typeof Array.isArray === 'function') { | ||
return Array.isArray(arr); | ||
} | ||
return toStr.call(arr) === '[object Array]'; | ||
}; | ||
template.isDirective = function(){ | ||
for(var field in this){ | ||
if(field.startsWith("@")) {return true;} | ||
var isPlainObject = function isPlainObject(obj) { | ||
if (!obj || toStr.call(obj) !== '[object Object]') { | ||
return false; | ||
} | ||
return false; | ||
var hasOwnConstructor = hasOwn.call(obj, 'constructor'); | ||
var hasIsPrototypeOf = obj.constructor && obj.constructor.prototype && hasOwn.call(obj.constructor.prototype, 'isPrototypeOf'); | ||
// Not own constructor property must be Object | ||
if (obj.constructor && !hasOwnConstructor && !hasIsPrototypeOf) { | ||
return false; | ||
} | ||
// Own properties are enumerated firstly, so to speed up, | ||
// if last one is own, then all properties are own. | ||
var key; | ||
for (key in obj) {/**/} | ||
return typeof key === 'undefined' || hasOwn.call(obj, key); | ||
}; | ||
var options, name, src, copy, copyIsArray, clone, | ||
target = arguments[0], | ||
i = 1, | ||
length = arguments.length, | ||
deep = false; | ||
template.render = function(){ | ||
delete this.isDirective; | ||
delete this.render; | ||
delete this.deleteDirective; | ||
delete this.__; | ||
delete this.copy; | ||
return this; | ||
// Handle a deep copy situation | ||
if (typeof target === 'boolean') { | ||
deep = target; | ||
target = arguments[1] || {}; | ||
// skip the boolean and the target | ||
i = 2; | ||
} else if ((typeof target !== 'object' && typeof target !== 'function') || target == null) { | ||
target = {}; | ||
} | ||
for (; i < length; ++i) { | ||
options = arguments[i]; | ||
// Only deal with non-null/undefined values | ||
if (options != null) { | ||
// Extend the base object | ||
for (name in options) { | ||
src = target[name]; | ||
copy = options[name]; | ||
// Prevent never-ending loop | ||
if (target !== copy) { | ||
// Recurse if we're merging plain objects or arrays | ||
if (deep && copy && (isPlainObject(copy) || (copyIsArray = isArray(copy)))) { | ||
if (copyIsArray) { | ||
copyIsArray = false; | ||
clone = src && isArray(src) ? src : []; | ||
} else { | ||
clone = src && isPlainObject(src) ? src : {}; | ||
} | ||
// Never move original objects, clone them | ||
target[name] = extend(deep, clone, copy); | ||
// Don't bring in undefined values | ||
} else if (typeof copy !== 'undefined') { | ||
target[name] = copy; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
// Return the modified object | ||
return target; | ||
}; | ||
template.copy = function(){ | ||
module.exports = { | ||
copy : function (template) { | ||
var result = {}; | ||
extend(true, result, this); | ||
extend(true, result, template); | ||
return result; | ||
}; | ||
return template | ||
}; | ||
module.exports = { | ||
create: function(template){ | ||
return create(template); | ||
} | ||
}, | ||
deleteDirective : function (template, name) { | ||
delete template[name]; | ||
}, | ||
isDirective : function (template) { | ||
for (var field in template) { | ||
if (field.startsWith("@")) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
}, | ||
}; |
var expect = require('chai').expect, | ||
jsong = require("../src/index.js"); | ||
jeyson = require("../src/index.js"); | ||
@@ -9,3 +9,3 @@ describe('Compile jso-ng', function() { | ||
template = {id: "my-{{id}}"}, | ||
parsed = jsong.compile(scope, template); | ||
parsed = jeyson.compile(scope, template); | ||
@@ -18,3 +18,3 @@ expect(parsed.id).to.equal("my-some-id"); | ||
template = {crazy: "{{crazy}}", sober: "{{sober}}"}, | ||
parsed = jsong.compile(scope, template); | ||
parsed = jeyson.compile(scope, template); | ||
@@ -25,6 +25,14 @@ expect(parsed.sober).to.equal(true); | ||
it('should compile expressions in objects inside array', function () { | ||
var scope = {"id" : "some-id"}, | ||
template = {item: [{id: "my-{{id}}"}]}, | ||
parsed = jeyson.compile(scope, template); | ||
expect(parsed.item[0].id).to.equal("my-some-id"); | ||
}); | ||
it('should parse sub trees', function () { | ||
var scope = {"id" : "some-id"}, | ||
template = {item: {id: "my-{{id}}"}}, | ||
parsed = jsong.compile(scope, template); | ||
parsed = jeyson.compile(scope, template); | ||
@@ -44,3 +52,3 @@ expect(parsed.item.id).to.equal("my-some-id"); | ||
}}, | ||
parsed = jsong.compile(scope, template); | ||
parsed = jeyson.compile(scope, template); | ||
@@ -47,0 +55,0 @@ expect(parsed.item.id).to.equal("id is one"); |
var expect = require('chai').expect, | ||
jsong = require('../src/index'); | ||
jeyson = require('../src/index'); | ||
describe('Directive Definitions', function() { | ||
it('should replace directory body with parsed result', function () { | ||
var app = jsong.create(), | ||
var app = jeyson.create(), | ||
template = { | ||
@@ -23,3 +23,3 @@ "data" : { | ||
result = jsong.compile({}, template); | ||
result = jeyson.compile({}, template); | ||
expect(result.data.fooTarget).to.equal("bar"); | ||
@@ -29,3 +29,3 @@ }); | ||
it('should replace directory body with a subtree', function () { | ||
var app = jsong.create(), | ||
var app = jeyson.create(), | ||
template = { | ||
@@ -46,3 +46,3 @@ "data" : { | ||
result = jsong.compile({}, template); | ||
result = jeyson.compile({}, template); | ||
expect(result.data.fooTarget.child).to.equal("bar"); | ||
@@ -52,3 +52,3 @@ }); | ||
it('should apply nested directives', function () { | ||
var app = jsong.create(), | ||
var app = jeyson.create(), | ||
template = { | ||
@@ -72,3 +72,3 @@ "data" : { | ||
result = jsong.compile({}, template); | ||
result = jeyson.compile({}, template); | ||
expect(result.data.fooTarget.child).to.equal("foodified"); | ||
@@ -80,3 +80,3 @@ expect(result.data.fooTarget.fooTarget.child).to.equal("foodified"); | ||
var scope = {param: "replaced by expression"}, | ||
app = jsong.create(), | ||
app = jeyson.create(), | ||
template = { | ||
@@ -97,3 +97,3 @@ "data" : { | ||
result = jsong.compile(scope, template); | ||
result = jeyson.compile(scope, template); | ||
expect(result.data.fooTarget).to.equal("replaced by expression"); | ||
@@ -104,3 +104,3 @@ }); | ||
var scope = {param: "found"}, | ||
app = jsong.create(), | ||
app = jeyson.create(), | ||
template = { | ||
@@ -131,3 +131,3 @@ "data" : { | ||
result = jsong.compile(scope, template); | ||
result = jeyson.compile(scope, template); | ||
expect(result.data.fooTarget.child).to.equal("found"); | ||
@@ -134,0 +134,0 @@ expect(result.data.fooTarget.otherChild).to.equal("bar"); |
@@ -94,5 +94,5 @@ var expect = require('chai').expect, | ||
expect(result).to.eql(expected); | ||
expect(result).to.deep.equal(expected); | ||
}); | ||
}); |
var expect = require('chai').expect, | ||
temlpates = require('../src/templates'); | ||
templates = require('../src/templates'); | ||
describe('TemplateTest', function() { | ||
it('should clear template before rendering', function () { | ||
var temlpate = temlpates.create({}); | ||
expect(temlpate.render()).to.eql({}); | ||
}); | ||
it('template is a directive if one of its field starts with "@"', function () { | ||
var notHas = temlpates.create({"id": 1, "name": "My Name"}), | ||
has = temlpates.create({"id": 1, "@name": "My Name"}); | ||
expect(notHas.isDirective()).to.equal(false); | ||
expect(has.isDirective()).to.equal(true); | ||
var notHas = {"id": 1, "name": "My Name"}, | ||
has = {"id": 1, "@name": "My Name"}; | ||
expect(templates.isDirective(notHas)).to.equal(false); | ||
expect(templates.isDirective(has)).to.equal(true); | ||
}); | ||
it('template.copy should return deep copy of current template', function () { | ||
var template = temlpates.create({"id": 1, "name": "My Name"}), | ||
templateOne = template.copy(); | ||
var template = {"id": 1, "name": "My Name"}, | ||
templateOne = templates.copy(template); | ||
expect(template).to.deep.equal(templateOne); | ||
expect(templateOne.id).to.equal(1); | ||
expect(templateOne.name).to.equal("My Name"); | ||
templateOne.id = "replaced"; | ||
expect(template.id).to.deep.equal(1); | ||
expect(template.id).to.equal(1); | ||
}); | ||
}); |
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
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
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
49683
30
1289
4
2