Comparing version 1.0.5 to 1.0.6
15
dev.md
### Running unit tests | ||
mocha | ||
### Running all tests | ||
mocha test/ --recursive | ||
### Running all unit tests | ||
mocha test/unit --recursive | ||
### Running single unit test file | ||
mocha test/unit/compiler-test.js | ||
### Running end to end tests | ||
mocha test/e2e | ||
### publish gem | ||
npm publish | ||
npm publish |
{ | ||
"name": "jeyson", | ||
"version": "1.0.5", | ||
"version": "1.0.6", | ||
"description": "Json template engine", | ||
@@ -30,9 +30,9 @@ "main": "./src/index.js", | ||
"homepage": "https://github.com/nishants/jso-ng#readme", | ||
"dependencies": { | ||
"extend": "^3.0.0" | ||
}, | ||
"dependencies": {}, | ||
"devDependencies": { | ||
"chai": "^3.5.0", | ||
"mocha": "^2.4.5" | ||
"mocha": "^2.4.5", | ||
"wrench": "^1.5.9" | ||
} | ||
} |
103
README.md
@@ -120,3 +120,106 @@ ## Jeyson Templates | ||
# Conditional Blocks & System Directives | ||
Following are the conditional directives | ||
1. switch | ||
2. if | ||
3. ignore-if (a system directive) | ||
### @switch | ||
#### Will replace the body with "@[value]", "@default" or null based on choice. | ||
```javascript | ||
scope = {role : {name: "admin"}} | ||
template = { | ||
"user" : { | ||
"role" : { | ||
"@switch" : "role.name", | ||
"@admin" : {"name" : "admin"}, | ||
"@visitor" : {"name" : "visitor"}, | ||
"@default" : {"name" : "guest"} | ||
} | ||
} | ||
``` | ||
Will compile to | ||
```javascript | ||
template = { | ||
"user" : { | ||
"role" : {"name" : "admin"} | ||
} | ||
``` | ||
### @if, @then, @else | ||
#### Will set field value to null, "@then" or "@else", by condition | ||
```javascript | ||
{ | ||
"author" : "ABC", | ||
"book" : { | ||
"@if" : "1 == 2", | ||
"@then" : {"title" : "Harry Potter"} | ||
"@else" : "unknown", | ||
} | ||
} | ||
``` | ||
will result in | ||
```javascript | ||
{ | ||
"author" : "ABC", | ||
"book" : "unknown" | ||
} | ||
``` | ||
```javascript | ||
{ | ||
"author" : "ABC", | ||
"book" : { | ||
"@if" : "1 == 1", | ||
"@then" : {"title" : "Harry Potter"} | ||
} | ||
``` | ||
will result in | ||
```javascript | ||
{ | ||
"author" : "ABC", | ||
"book" : {"title" : "Harry Potter"} | ||
} | ||
``` | ||
```javascript | ||
{ | ||
"author" : "ABC", | ||
"book" : { | ||
"@if" : "1 == 2", | ||
"@then" : {"title" : "Harry Potter"} | ||
} | ||
``` | ||
will result in | ||
```javascript | ||
{ | ||
"author" : "ABC", | ||
"book" : null | ||
} | ||
``` | ||
### @ignore-if | ||
#### Will not render the field itself, if the condition is false. | ||
e.g | ||
```javascript | ||
{ | ||
"author" : "ABC", | ||
"book" : { | ||
"@ignore-if": "1 == 1", | ||
"title" : "Harry Potter" | ||
} | ||
} | ||
``` | ||
will result in | ||
```javascript | ||
{ | ||
"author" : "ABC" | ||
} | ||
``` | ||
### Inbuilt Directives | ||
@@ -123,0 +226,0 @@ Inbuilt directives for repeating, including other json etc. |
@@ -1,39 +0,47 @@ | ||
var linker = require("./linker"), | ||
scopes = require("./scope"), | ||
templates = require("./templates"), | ||
directives = require("./directives"); | ||
var linker = require("./linker"), | ||
scopes = require("./scope"), | ||
templates = require("./templates"); | ||
module.exports = { | ||
$compile: function (scope, template, config) { | ||
return this.compile(scopes.create(scope), template, config ? config : {}); | ||
}, | ||
compile: function (scope, template, config) { | ||
var result = {}, | ||
self = this, | ||
compile = function(scope, template){ | ||
return self.compile(scope, templates.copy(template), config); | ||
}, | ||
getTemplate = function(path){ | ||
return JSON.parse(config.getTemplate(path)); | ||
}; | ||
create: function(directives){ | ||
var Compiler = { | ||
$compile: function (scope, template, config) { | ||
return this.compile(scopes.create(scope), template, config ? config : {}); | ||
}, | ||
compile: function (scope, template, config) { | ||
var self = this, | ||
compile = function (scope, template) { | ||
return self.compile(scope, templates.copy(template), config); | ||
}, | ||
getTemplate = function (path) { | ||
return JSON.parse(config.getTemplate(path)); | ||
}; | ||
if(templates.isDirective(template)) { | ||
return directives.link(scope, template, compile, getTemplate); | ||
} | ||
if (templates.hasDirective(template)) { | ||
return directives.link(scope, template, compile, getTemplate); | ||
} | ||
if(templates.isList(template)) { | ||
return template.map(function(element){ | ||
return compile(scope, element, config); | ||
}); | ||
} | ||
if (templates.isList(template)) { | ||
return template.map(function (element) { | ||
return compile(scope, element, config); | ||
}); | ||
} | ||
if(templates.isSubtree(template)){ | ||
for(var node in template){ | ||
result[node] = compile(scope, template[node], config); | ||
if (templates.isSubtree(template)) { | ||
var result = {}; | ||
for (var node in template) { | ||
var exclude = templates.isIgnored(scope, template[node]); | ||
if (!exclude) { | ||
result[node] = compile(scope, templates.cleanup(template[node]), config); | ||
} | ||
} | ||
return result; | ||
} | ||
return linker.link(scope, template); | ||
} | ||
return result; | ||
} | ||
}; | ||
return linker.link(scope, template); | ||
return Compiler; | ||
} | ||
}; |
@@ -1,40 +0,41 @@ | ||
var repeater = require("./directives/repeat"), | ||
compileIt = require("./directives/compile"), | ||
include = require("./directives/include"), | ||
templates = require("./templates"), | ||
all = {}; | ||
var templates = require("./templates"); | ||
var Directives = { | ||
all: all, | ||
get: function (name) { | ||
return all[name]; | ||
}, | ||
add: function (name, definition) { | ||
all[name] = {link: definition.link}; | ||
}, | ||
link: function (scope, body, compile, getTemplate) { | ||
var directive, | ||
param, | ||
replaceBody; | ||
module.exports = { | ||
create: function(){ | ||
var Directives = { | ||
linkers: {}, | ||
add: function (name, definition) { | ||
Directives.linkers[name] = {link: definition.link}; | ||
}, | ||
link: function (scope, body, compile, getTemplate) { | ||
var directive, | ||
param; | ||
for(var field in body){ | ||
if(field.startsWith("@")){ | ||
directive = { | ||
name: field, | ||
directive: all[field] | ||
for(var field in body){ | ||
if(field.startsWith("@")){ | ||
directive = directive || { | ||
name: field, | ||
linker: Directives.linkers[field] | ||
} | ||
} | ||
}; | ||
//ignore an undefined directive | ||
if(!directive.linker){ | ||
return body; | ||
} | ||
param = body[directive.name]; | ||
templates.deleteDirective(body, directive.name) | ||
// If directive returns body, replace template with returned body | ||
// Else compile the updated body and return | ||
var linked = directive.linker.link(scope, body, param, compile, getTemplate); | ||
//Avoid undefined, allow null | ||
return linked === undefined ? compile(scope, body) : linked ; | ||
} | ||
}; | ||
param = body[directive.name]; | ||
templates.deleteDirective(body, directive.name) | ||
// 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); | ||
return Directives; | ||
} | ||
}; | ||
Directives.add("@repeat", {link: repeater.link}); | ||
Directives.add("@compile", {link: compileIt.link}); | ||
Directives.add("@include", {link: include.link}); | ||
module.exports = Directives; |
@@ -1,2 +0,2 @@ | ||
var extend = require("extend"); | ||
var extend = require("../support/extend"); | ||
@@ -3,0 +3,0 @@ module.exports = { |
@@ -1,15 +0,27 @@ | ||
var compiler = require("./compiler"), | ||
Directives = require("./directives"); | ||
var Directives = require("./directives"), | ||
Compiler = require("./compiler"), | ||
Repeater = require("./directives/repeat"), | ||
CompileDirective = require("./directives/compile").link, | ||
IncludeDirective = require("./directives/include"), | ||
IfElseThenDirective = require("./directives/if-else-then"); | ||
module.exports = { | ||
compile: function(scope, template, config){ | ||
return compiler.$compile(scope, template, config) ; | ||
}, | ||
directive: function(name, definition){ | ||
return Directives.add(name, definition); | ||
}, | ||
create: function(){ | ||
var directives = Directives.create(), | ||
compiler = Compiler.create(directives); | ||
create: function(){ | ||
return this; | ||
directives.add("@repeat" , {link: Repeater.link}); | ||
directives.add("@compile" , {link: CompileDirective}); | ||
directives.add("@include" , {link: IncludeDirective.link}); | ||
directives.add("@if" , {link: IfElseThenDirective.link}); | ||
return { | ||
compile: function(scope, template, config){ | ||
return compiler.$compile(scope, template, config) ; | ||
}, | ||
directive: function(name, definition){ | ||
return directives.add(name, definition); | ||
} | ||
}; | ||
} | ||
}; |
@@ -1,86 +0,29 @@ | ||
var extend = function() { | ||
var extend = require("./support/extend"); | ||
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]'; | ||
}; | ||
var isPlainObject = function isPlainObject(obj) { | ||
if (!obj || toStr.call(obj) !== '[object Object]') { | ||
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; | ||
// 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 = {}; | ||
var Templates = { | ||
isList: function (template) { | ||
return Array.isArray(template); | ||
}, | ||
isSubtree: function (template) { | ||
return (typeof template == "object") && !Templates.isList(template); | ||
}, | ||
hasDirective: function (template) { | ||
for (var field in template) { | ||
if (field.startsWith("@")) { | ||
return true; | ||
} | ||
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; | ||
}; | ||
var Templates = { | ||
} | ||
return false; | ||
}, | ||
deleteDirective: function (template, name) { | ||
delete template[name]; | ||
}, | ||
isIgnored : function(scope, temlpate){ | ||
return !! scope.execute(temlpate["@ignore-if"] || ""); | ||
}, | ||
cleanup : function(template){ | ||
var clean = Templates.copy(template); | ||
delete clean["@ignore-if"]; | ||
return clean; | ||
}, | ||
copy: function (template) { | ||
@@ -92,29 +35,12 @@ if (Templates.isList(template)){ | ||
} | ||
var result = {}; | ||
if(Templates.isSubtree(template)){ | ||
var result = {}; | ||
extend(true, result, template); | ||
} else{ | ||
return result = template; | ||
return result; | ||
} | ||
return result; | ||
}, | ||
deleteDirective: function (template, name) { | ||
delete template[name]; | ||
}, | ||
isDirective: function (template) { | ||
for (var field in template) { | ||
if (field.startsWith("@")) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
}, | ||
isSubtree: function (template) { | ||
return (typeof template == "object") && !Array.isArray(template); | ||
}, | ||
isList: function (template) { | ||
return Array.isArray(template); | ||
return template; | ||
} | ||
}; | ||
module.exports = Templates; |
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
50188
0
37
1253
245
3
2
- Removedextend@^3.0.0
- Removedextend@3.0.2(transitive)