mirror of
https://github.com/Jermolene/TiddlyWiki5
synced 2025-02-09 15:40:03 +00:00
Initial Commit
This commit is contained in:
parent
a51191d334
commit
5f4dc2a5fe
167
core/modules/filters/json-ops.js
Normal file
167
core/modules/filters/json-ops.js
Normal file
@ -0,0 +1,167 @@
|
||||
/*\
|
||||
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["getjson"] = function(source,operator,options) {
|
||||
var results = [];
|
||||
source(function(tiddler,title) {
|
||||
var data = options.wiki.getTiddlerDataCached(title);
|
||||
if(data) {
|
||||
var item = getDataItemValueAsStrings(data,operator.operands);
|
||||
if(item !== undefined) {
|
||||
results.push.apply(results,item);
|
||||
}
|
||||
}
|
||||
});
|
||||
return results;
|
||||
};
|
||||
|
||||
exports["indexesjson"] = function(source,operator,options) {
|
||||
var results = [];
|
||||
source(function(tiddler,title) {
|
||||
var data = options.wiki.getTiddlerDataCached(title);
|
||||
if(data) {
|
||||
var item = getDataItemKeysAsStrings(data,operator.operands);
|
||||
if(item !== undefined) {
|
||||
results.push.apply(results,item);
|
||||
}
|
||||
}
|
||||
});
|
||||
return results;
|
||||
};
|
||||
|
||||
exports["typejson"] = function(source,operator,options) {
|
||||
var results = [];
|
||||
source(function(tiddler,title) {
|
||||
var data = options.wiki.getTiddlerDataCached(title);
|
||||
if(data) {
|
||||
var item = getDataItemType(data,operator.operands);
|
||||
if(item !== undefined) {
|
||||
results.push(item);
|
||||
}
|
||||
}
|
||||
});
|
||||
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
|
||||
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 item;
|
||||
}
|
||||
if(typeof item === "object") {
|
||||
if(item === null) {
|
||||
return ["null"];
|
||||
}
|
||||
var results = [];
|
||||
if($tw.utils.isArray(item)) {
|
||||
$tw.utils.each(item,function(value) {
|
||||
results.push.apply(results,convertDataItemValueToStrings(value));
|
||||
});
|
||||
return results;
|
||||
} else {
|
||||
$tw.utils.each(Object.keys(item).sort(),function(key) {
|
||||
results.push.apply(results,convertDataItemValueToStrings(item[key]));
|
||||
});
|
||||
return results;
|
||||
}
|
||||
}
|
||||
return [item.toString()];
|
||||
}
|
||||
|
||||
/*
|
||||
Return an array of the string representation of the keys of a data item, or "undefined" if the item is undefined
|
||||
*/
|
||||
function convertDataItemKeysToStrings(item) {
|
||||
// Return the item as a string
|
||||
if(item === undefined) {
|
||||
return item;
|
||||
} else if(typeof item === "object") {
|
||||
if(item === null) {
|
||||
return [];
|
||||
}
|
||||
var results = [];
|
||||
if($tw.utils.isArray(item)) {
|
||||
for(var i=0; i<item.length; i++) {
|
||||
results.push(i.toString());
|
||||
}
|
||||
return results;
|
||||
} else {
|
||||
$tw.utils.each(Object.keys(item).sort(),function(key) {
|
||||
results.push(key);
|
||||
});
|
||||
return results;
|
||||
}
|
||||
}
|
||||
return [];
|
||||
}
|
||||
|
||||
function getDataItemType(data,indexes) {
|
||||
// Get the item
|
||||
var item = getDataItem(data,indexes);
|
||||
// Return the item type
|
||||
if(item === undefined) {
|
||||
return item;
|
||||
} else if(item === null) {
|
||||
return "null";
|
||||
} else if($tw.utils.isArray(item)) {
|
||||
return "array";
|
||||
} else if(typeof item === "object") {
|
||||
return "object";
|
||||
} else {
|
||||
return typeof item;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
Given a JSON data structure and an array of index strings, return the value at the end of the index chain, or "undefined" if any of the index strings are invalid
|
||||
*/
|
||||
function getDataItem(data,indexes) {
|
||||
if(indexes.length === 0 || (indexes.length === 1 && indexes[0] === "")) {
|
||||
return data;
|
||||
}
|
||||
// Get the item
|
||||
var item = data;
|
||||
for(var i=0; i<indexes.length; i++) {
|
||||
if(item !== undefined) {
|
||||
item = item[indexes[i]];
|
||||
}
|
||||
}
|
||||
return item;
|
||||
}
|
||||
|
||||
})();
|
||||
|
82
editions/test/tiddlers/tests/test-json-filters.js
Normal file
82
editions/test/tiddlers/tests/test-json-filters.js
Normal file
@ -0,0 +1,82 @@
|
||||
/*\
|
||||
title: test-json-filters.js
|
||||
type: application/javascript
|
||||
tags: [[$:/tags/test-spec]]
|
||||
|
||||
Tests the JSON filters.
|
||||
|
||||
\*/
|
||||
(function(){
|
||||
|
||||
/* jslint node: true, browser: true */
|
||||
/* eslint-env node, browser, jasmine */
|
||||
/* eslint no-mixed-spaces-and-tabs: ["error", "smart-tabs"]*/
|
||||
/* global $tw, require */
|
||||
"use strict";
|
||||
|
||||
describe("json filter tests", function() {
|
||||
|
||||
var wiki = new $tw.Wiki();
|
||||
var tiddlers = [{
|
||||
title: "First",
|
||||
text: '{"a":"one","b":"","c":1.618,"d": {"e": "four","f": ["five","six",true,false,null]}}',
|
||||
type: "application/json"
|
||||
},{
|
||||
title: "Second",
|
||||
text: '["une","deux","trois"]',
|
||||
type: "application/json"
|
||||
}];
|
||||
wiki.addTiddlers(tiddlers);
|
||||
|
||||
it("should support the getindex operator", function() {
|
||||
expect(wiki.filterTiddlers("[[First]getindex[b]]")).toEqual([]);
|
||||
});
|
||||
|
||||
it("should support the getjson operator", function() {
|
||||
expect(wiki.filterTiddlers("[[First]getjson[]]")).toEqual(["one","","1.618","four","five","six","true","false","null"]);
|
||||
expect(wiki.filterTiddlers("[[First]getjson[a]]")).toEqual(["one"]);
|
||||
expect(wiki.filterTiddlers("[[First]getjson[b]]")).toEqual([""]);
|
||||
expect(wiki.filterTiddlers("[[First]getjson[d]]")).toEqual(["four","five","six","true","false","null"]);
|
||||
expect(wiki.filterTiddlers("[[First]getjson[d],[e]]")).toEqual(["four"]);
|
||||
expect(wiki.filterTiddlers("[[First]getjson[d],[f]]")).toEqual(["five","six","true","false","null"]);
|
||||
expect(wiki.filterTiddlers("[[First]getjson[d],[f],[0]]")).toEqual(["five"]);
|
||||
expect(wiki.filterTiddlers("[[First]getjson[d],[f],[1]]")).toEqual(["six"]);
|
||||
expect(wiki.filterTiddlers("[[First]getjson[d],[f],[2]]")).toEqual(["true"]);
|
||||
expect(wiki.filterTiddlers("[[First]getjson[d],[f],[3]]")).toEqual(["false"]);
|
||||
expect(wiki.filterTiddlers("[[First]getjson[d],[f],[4]]")).toEqual(["null"]);
|
||||
});
|
||||
|
||||
it("should support the indexesjson operator", function() {
|
||||
expect(wiki.filterTiddlers("[[Second]indexesjson[]]")).toEqual(["0","1","2"]);
|
||||
expect(wiki.filterTiddlers("[[First]indexesjson[]]")).toEqual(["a","b","c","d"]);
|
||||
expect(wiki.filterTiddlers("[[First]indexesjson[a]]")).toEqual([]);
|
||||
expect(wiki.filterTiddlers("[[First]indexesjson[b]]")).toEqual([]);
|
||||
expect(wiki.filterTiddlers("[[First]indexesjson[d]]")).toEqual(["e","f"]);
|
||||
expect(wiki.filterTiddlers("[[First]indexesjson[d],[e]]")).toEqual([]);
|
||||
expect(wiki.filterTiddlers("[[First]indexesjson[d],[f]]")).toEqual(["0","1","2","3","4"]);
|
||||
expect(wiki.filterTiddlers("[[First]indexesjson[d],[f],[0]]")).toEqual([]);
|
||||
expect(wiki.filterTiddlers("[[First]indexesjson[d],[f],[1]]")).toEqual([]);
|
||||
expect(wiki.filterTiddlers("[[First]indexesjson[d],[f],[2]]")).toEqual([]);
|
||||
expect(wiki.filterTiddlers("[[First]indexesjson[d],[f],[3]]")).toEqual([]);
|
||||
expect(wiki.filterTiddlers("[[First]indexesjson[d],[f],[4]]")).toEqual([]);
|
||||
});
|
||||
|
||||
it("should support the typejson operator", function() {
|
||||
expect(wiki.filterTiddlers("[[First]typejson[]]")).toEqual(["object"]);
|
||||
expect(wiki.filterTiddlers("[[First]typejson[a]]")).toEqual(["string"]);
|
||||
expect(wiki.filterTiddlers("[[First]typejson[b]]")).toEqual(["string"]);
|
||||
expect(wiki.filterTiddlers("[[First]typejson[c]]")).toEqual(["number"]);
|
||||
expect(wiki.filterTiddlers("[[First]typejson[d]]")).toEqual(["object"]);
|
||||
expect(wiki.filterTiddlers("[[First]typejson[d],[e]]")).toEqual(["string"]);
|
||||
expect(wiki.filterTiddlers("[[First]typejson[d],[f]]")).toEqual(["array"]);
|
||||
expect(wiki.filterTiddlers("[[First]typejson[d],[f],[0]]")).toEqual(["string"]);
|
||||
expect(wiki.filterTiddlers("[[First]typejson[d],[f],[1]]")).toEqual(["string"]);
|
||||
expect(wiki.filterTiddlers("[[First]typejson[d],[f],[2]]")).toEqual(["boolean"]);
|
||||
expect(wiki.filterTiddlers("[[First]typejson[d],[f],[3]]")).toEqual(["boolean"]);
|
||||
expect(wiki.filterTiddlers("[[First]typejson[d],[f],[4]]")).toEqual(["null"]);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
})();
|
||||
|
Loading…
x
Reference in New Issue
Block a user