Comparing version 0.2.7 to 0.2.9
14
jslt.js
@@ -1,2 +0,2 @@ | ||
class JSLT { | ||
@@ -115,3 +115,4 @@ constructor() { | ||
if (type) { | ||
if (scope === undefined) return maybe ? undefined : error(`{{${nameWithoutType}}}`, "Missing required value"); | ||
if (scope === undefined || scope === null) | ||
return maybe ? scope : error(`{{${nameWithoutType}}}`, "Missing required value"); | ||
return verifyType(nameWithoutType, type, scope); | ||
@@ -214,3 +215,3 @@ } | ||
val.path = prefix.concat(i); | ||
value[i] = null; | ||
value[i] = "[JSLT ERROR]"; | ||
} else visit(val, prefix.concat(i)); | ||
@@ -222,3 +223,3 @@ }); | ||
val.path = prefix.concat(key); | ||
value[key] = null; | ||
value[key] = "[JSLT ERROR]"; | ||
} else visit(val, prefix.concat(key)); | ||
@@ -273,2 +274,4 @@ }); | ||
$join(input, args, global) { | ||
if (input instanceof Array) | ||
return input.join(compileTemplate(global, args)); | ||
return args.map(item => compileTemplate(global, item)).join(""); | ||
@@ -278,3 +281,4 @@ }, | ||
$concat(input, args, global) { | ||
return [].concat(...args.map(item => compileTemplate(global, item))); | ||
var target = input instanceof Array ? input : []; | ||
return target.concat(...args.map(item => compileTemplate(global, item))); | ||
}, | ||
@@ -281,0 +285,0 @@ |
{ | ||
"name": "jslt", | ||
"version": "0.2.7", | ||
"version": "0.2.9", | ||
"description": "JSON transformer", | ||
@@ -5,0 +5,0 @@ "main": "jslt.js", |
@@ -200,3 +200,3 @@ # jslt | ||
); | ||
console.log(res); // => true | ||
console.log(res); // => "Yes" | ||
``` | ||
@@ -203,0 +203,0 @@ With query operators: |
16
test.js
@@ -79,7 +79,10 @@ | ||
test("type - object - fail", { prop : "zz" }, { p : "{{prop:object}}" }, "p.{{prop}} - Expected object, but received string"); | ||
test("type - object/null - fail", { prop : null }, { p : "{{prop:object}}" }, "p.{{prop}} - Missing required value"); | ||
test("type - date - pass", { prop : "2015-07-15T22:00:00.000Z" }, { p : "{{prop:date}}" }, { p : "2015-07-15T22:00:00.000Z" }); | ||
test("type - date obj - pass", { prop : new Date("2015-07-15T22:00:00.000Z") }, { p : "{{prop:date}}" }, { p : "2015-07-15T22:00:00.000Z" }); | ||
test("type - date - fail", { prop : "zz" }, { p : "{{prop:date}}" }, "p.{{prop}} - Expected date, but received string"); | ||
test("type - required - fail", { prop1 : "test" }, { p : "{{prop:number}}" }, "p.{{prop}} - Missing required value"); | ||
test("type - optional - pass", { prop1 : "test" }, { p : "{{prop:?number}}", p2 : 3 }, { p2 : 3 }); | ||
test("type - required/undefined - fail", { prop1 : "test" }, { p : "{{prop:number}}" }, "p.{{prop}} - Missing required value"); | ||
test("type - required/null - fail", { prop : null }, { p : "{{prop:number}}" }, "p.{{prop}} - Missing required value"); | ||
test("type - optional/undefined - pass", { prop1 : "test" }, { p : "{{prop:?number}}", p2 : 3 }, { p2 : 3 }); | ||
test("type - optional/null - pass", { prop : null }, { p : "{{prop:?number}}", p2 : 3 }, { p : null, p2 : 3 }); | ||
test("type - in-string - fail", { prop : "aa" }, { p : "test {{prop:number}}" }, "p.{{prop}} - Expected number, but received string"); | ||
@@ -101,3 +104,8 @@ | ||
test("$join - transform", { $join : [ "Hello ", "{{stringField}}" ] }, "Hello testString"); | ||
test("$join (method)", { $fetch: [ "Hello", "world" ], $join : "+" }, "Hello+world"); | ||
test("$join (method) - transform", { $fetch: [ "Hello", "world" ], $join : "{{numberField}}" }, "Hello12world"); | ||
test("$concat", { $fetch : [ "Hello", "world" ], $concat : [ "world2" ] }, [ "Hello", "world", "world2" ]); | ||
test("$concat", { $concat : [ [ "Hello", "world" ], [ "world2" ] ] }, [ "Hello", "world", "world2" ]); | ||
test("$concat", { $fetch : [ "Hello", "world" ], $concat : [ "{{stringField}}" ] }, [ "Hello", "world", "testString" ]); | ||
@@ -213,3 +221,3 @@ test("$push", [ "aaa", "bbb", "ccc" ], { $push : "ddd" }, [ "aaa", "bbb", "ccc", "ddd" ]); | ||
test("continueOnError", { p1 : "{{prop1:number}}", p2 : "{{prop1:number}}" }, { | ||
result : { p1 : null, p2 : null }, | ||
result : { p1 : "[JSLT ERROR]", p2 : "[JSLT ERROR]" }, | ||
errors : [ | ||
@@ -222,3 +230,3 @@ { message : "Missing required value", stack : [ "{{prop1}}" ], path : [ "p1" ] }, | ||
test("continueOnError - array", { a : [ 1,"z",3 ]}, { p : { $fetch : "{{a}}", $map : { b : "{{this:number}}" } } }, { | ||
result : { p : [ { b : 1 }, { b : null },{ b : 3 } ] }, | ||
result : { p : [ { b : 1 }, { b : "[JSLT ERROR]" },{ b : 3 } ] }, | ||
errors : [ { message : "Expected number, but received string", stack : [ "{{this}}" ], path : [ "p", 1, "b" ] } ] | ||
@@ -225,0 +233,0 @@ }); |
43494
650