Comparing version 0.2.9 to 0.2.10
23
jslt.js
@@ -324,2 +324,3 @@ | ||
$replace(input, args, global) { | ||
if (!args) return error("[arguments]", "missing / invalid"); | ||
if (!(typeof args.newSubstr == "string")) return error("[newSubstr]", "missing / invalid"); | ||
@@ -357,2 +358,24 @@ | ||
$blacklist(input, args, global) { | ||
if (!(args instanceof Array)) return error("[arguments]", "missing / invalid"); | ||
if (!(input instanceof Object) || input === null) return input; | ||
var retVal = {}; | ||
Object.keys(input).forEach(key => { | ||
if (!args.includes(key)) retVal[key] = input[key]; | ||
}); | ||
return retVal; | ||
}, | ||
$whitelist(input, args, global) { | ||
if (!(args instanceof Array)) return error("[arguments]", "missing / invalid"); | ||
if (!(input instanceof Object) || input === null) return input; | ||
var retVal = {}; | ||
Object.keys(input).forEach(key => { | ||
if (args.includes(key)) retVal[key] = input[key]; | ||
}); | ||
return retVal; | ||
}, | ||
// Array | ||
@@ -359,0 +382,0 @@ $map(input, args, global) { |
{ | ||
"name": "jslt", | ||
"version": "0.2.9", | ||
"version": "0.2.10", | ||
"description": "JSON transformer", | ||
@@ -5,0 +5,0 @@ "main": "jslt.js", |
13
test.js
@@ -164,2 +164,3 @@ | ||
test("$replace regexp string", "test 123", { $replace : { regexp : "\\d+", newSubstr : "" } }, "test "); | ||
test("$replace", "test str", { $replace : null }, "$replace.[arguments] - missing / invalid"); | ||
@@ -249,2 +250,14 @@ test("$catch - no error", { prop : "test" }, { $fetch : "{{prop}}", $catch : {} }, "test"); | ||
test("$blacklist", { a : 3, b : 2 }, { $blacklist : [ "a", "c" ] }, { b : 2 }); | ||
test("$blacklist", { a : 3 }, { $blacklist : [ "a" ] }, {}); | ||
test("$blacklist", 3, { $blacklist : [ "a", "c" ] }, 3); | ||
test("$blacklist", null, { $blacklist : [ "a", "c" ] }, null); | ||
test("$blacklist", null, { $blacklist : 3 }, "$blacklist.[arguments] - missing / invalid"); | ||
test("$whitelist", { a : 3, b : 2 }, { $whitelist : [ "a", "c" ] }, { a : 3 }); | ||
test("$whitelist", { a : 3 }, { $whitelist : [ "b" ] }, {}); | ||
test("$whitelist", 3, { $whitelist : [ "a", "c" ] }, 3); | ||
test("$whitelist", null, { $whitelist : [ "a", "c" ] }, null); | ||
test("$whitelist", null, { $whitelist : 3 }, "$whitelist.[arguments] - missing / invalid"); | ||
console.log(`\nPassed: ${passed}, Failed: ${failed}`); |
45024
680