/*\ title: $:/core/modules/filters/json-ops.js type: application/javascript module-type: filteroperator Filter operators for JSON operations \*/ (function(){ /*jslint node: true, browser: true */ /*global $tw: false */ "use strict"; exports["jsonget"] = function(source,operator,options) { var results = []; source(function(tiddler,title) { var data = $tw.utils.parseJSONSafe(title,title); if(data) { var items = getDataItemValueAsStrings(data,operator.operands); if(items !== undefined) { results.push.apply(results,items); } } }); return results; }; exports["jsonextract"] = function(source,operator,options) { var results = []; source(function(tiddler,title) { var data = $tw.utils.parseJSONSafe(title,title); if(data) { var item = getDataItem(data,operator.operands); if(item !== undefined) { results.push(JSON.stringify(item)); } } }); return results; }; exports["jsonindexes"] = function(source,operator,options) { var results = []; source(function(tiddler,title) { var data = $tw.utils.parseJSONSafe(title,title); if(data) { var items = getDataItemKeysAsStrings(data,operator.operands); if(items !== undefined) { results.push.apply(results,items); } } }); return results; }; exports["jsontype"] = function(source,operator,options) { var results = []; source(function(tiddler,title) { var data = $tw.utils.parseJSONSafe(title,title); if(data) { var item = getDataItemType(data,operator.operands); if(item !== undefined) { results.push(item); } } }); return results; }; exports["jsonset"] = function(source,operator,options) { var suffixes = operator.suffixes || [], type = suffixes[0] && suffixes[0][0], indexes = operator.operands.slice(0,-1), value = operator.operands[operator.operands.length - 1], results = []; if(operator.operands.length === 1 && operator.operands[0] === "") { value = undefined; // Prevents the value from being assigned } switch(type) { case "string": // Use value unchanged break; case "boolean": value = (value === "true" ? true : (value === "false" ? false : undefined)); break; case "number": value = $tw.utils.parseNumber(value); break; case "array": indexes = operator.operands; value = []; break; case "object": indexes = operator.operands; value = {}; break; case "null": indexes = operator.operands; value = null; break; case "json": value = $tw.utils.parseJSONSafe(value,function() {return undefined;}); break; default: // Use value unchanged break; } source(function(tiddler,title) { var data = $tw.utils.parseJSONSafe(title,title); if(data) { data = setDataItem(data,indexes,value); results.push(JSON.stringify(data)); } }); return results; }; /* Given a JSON data structure and an array of index strings, return an array of the string representation of the values at the end of the index chain, or "undefined" if any of the index strings are invalid */ function getDataItemValueAsStrings(data,indexes) { // Get the item var item = getDataItem(data,indexes); // Return the item as a string list return convertDataItemValueToStrings(item); } /* Given a JSON data structure and an array of index strings, return an array of the string representation of the keys of the item at the end of the index chain, or "undefined" if any of the index strings are invalid */ function getDataItemKeysAsStrings(data,indexes) { // Get the item var item = getDataItem(data,indexes); // Return the item keys as a string return convertDataItemKeysToStrings(item); } /* Return an array of the string representation of the values of a data item, or "undefined" if the item is undefined */ function convertDataItemValueToStrings(item) { // Return the item as a string if(item === undefined) { return undefined; } else if(item === null) { return ["null"] } else if(typeof item === "object") { var results = [],i,t; if($tw.utils.isArray(item)) { // Return all the items in arrays recursively for(i=0; i