1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-12-26 01:50:28 +00:00

jsonset: add support for assigning JSON strings

This commit is contained in:
jeremy@jermolene.com 2023-04-05 12:26:40 +01:00
parent 2e922976c4
commit a71150ae81
3 changed files with 9 additions and 2 deletions

View File

@ -75,7 +75,7 @@ exports["jsonset"] = function(source,operator,options) {
value = operator.operands[operator.operands.length - 1], value = operator.operands[operator.operands.length - 1],
results = []; results = [];
if(operator.operands.length === 1 && operator.operands[0] === "") { if(operator.operands.length === 1 && operator.operands[0] === "") {
value = undefined; value = undefined; // Prevents the value from being assigned
} }
switch(type) { switch(type) {
case "string": case "string":
@ -99,6 +99,9 @@ exports["jsonset"] = function(source,operator,options) {
indexes = operator.operands; indexes = operator.operands;
value = null; value = null;
break; break;
case "json":
value = $tw.utils.parseJSONSafe(value,function() {return undefined;});
break;
default: default:
// Use value unchanged // Use value unchanged
break; break;

View File

@ -116,6 +116,9 @@ describe("json filter tests", function() {
expect(wiki.filterTiddlers("[{First}jsonset:array[d],[f],[5]]")).toEqual(['{"a":"one","b":"","c":1.618,"d":{"e":"four","f":["five","six",true,false,null,[]]}}']); expect(wiki.filterTiddlers("[{First}jsonset:array[d],[f],[5]]")).toEqual(['{"a":"one","b":"","c":1.618,"d":{"e":"four","f":["five","six",true,false,null,[]]}}']);
expect(wiki.filterTiddlers("[{First}jsonset:object[d],[f],[5]]")).toEqual(['{"a":"one","b":"","c":1.618,"d":{"e":"four","f":["five","six",true,false,null,{}]}}']); expect(wiki.filterTiddlers("[{First}jsonset:object[d],[f],[5]]")).toEqual(['{"a":"one","b":"","c":1.618,"d":{"e":"four","f":["five","six",true,false,null,{}]}}']);
expect(wiki.filterTiddlers("[{First}jsonset[missing],[id],[Antelope]]")).toEqual(['{"a":"one","b":"","c":1.618,"d":{"e":"four","f":["five","six",true,false,null]}}']); expect(wiki.filterTiddlers("[{First}jsonset[missing],[id],[Antelope]]")).toEqual(['{"a":"one","b":"","c":1.618,"d":{"e":"four","f":["five","six",true,false,null]}}']);
expect(wiki.filterTiddlers("[{First}jsonset:json[\"Antelope\"]]")).toEqual(['"Antelope"']);
expect(wiki.filterTiddlers("[{First}jsonset:json[id],[{\"a\":313}]]")).toEqual(['{"a":"one","b":"","c":1.618,"d":{"e":"four","f":["five","six",true,false,null]},"id":{"a":313}}']);
expect(wiki.filterTiddlers("[{First}jsonset:json[notjson]]")).toEqual(['{"a":"one","b":"","c":1.618,"d":{"e":"four","f":["five","six",true,false,null]}}']);
}); });
it("should support the format:json operator", function() { it("should support the format:json operator", function() {

View File

@ -61,8 +61,9 @@ The data type of the value to be assigned to the property can be specified with
|!Suffix |!Description | |!Suffix |!Description |
|''string'' |The string is specified as the final operand | |''string'' |The string is specified as the final operand |
|''boolean'' |The boolean value is true if the final operand is the string "true" and false if the final operand is the string "false". Any other value for the final string results in the property not being assigned | |''boolean'' |The boolean value is true if the final operand is the string "true" and false if the final operand is the string "false". Any other value for the final string results prevents the property from being assigned |
|''number'' |The numeric value is taken from the final operand. Invalid numbers are interpreted as zero | |''number'' |The numeric value is taken from the final operand. Invalid numbers are interpreted as zero |
|''json'' |The JSON string value is taken from the final operand. Invalid JSON prevents the property from being assigned |
|''object'' |An empty object is assigned to the property. The final operand is not used as a value | |''object'' |An empty object is assigned to the property. The final operand is not used as a value |
|''array'' |An empty array is assigned to the property. The final operand is not used as a value | |''array'' |An empty array is assigned to the property. The final operand is not used as a value |
|''null'' |The special value null is assigned to the property. The final operand is not used as a value | |''null'' |The special value null is assigned to the property. The final operand is not used as a value |