renamed getvariablejson to jsonvariable and updated the docs

This commit is contained in:
pmario 2024-05-06 17:13:08 +02:00
parent bd7ff4b539
commit a895787851
7 changed files with 126 additions and 80 deletions

View File

@ -1,31 +0,0 @@
/*\
title: $:/core/modules/filters/getvariablejson.js
type: application/javascript
module-type: filteroperator
Filter operator for replacing input values by the value of the variable with the same name, or blank if the variable is missing
\*/
(function(){
/*jslint node: true, browser: true */
/*global $tw: false */
"use strict";
/*
Export our filter function
*/
exports.getvariablejson = function(source,operator,options) {
var results = [],
space = operator.operands[1] || null,
replacerList = (operator.operands[2]) ? operator.operands[2].split(" ") : null,
widget = options.widget;
source(function(tiddler,title) {
var variable = widget.getVariableInfo(title, {}),
text = JSON.stringify(variable,replacerList,space);
results.push(text || "");
});
return results;
};
})();

View File

@ -0,0 +1,33 @@
/*\
title: $:/core/modules/filters/jsonvariable.js
type: application/javascript
module-type: filteroperator
Filter operator to get widget variable info and
Display as JSON with basic formatting
\*/
(function(){
/*jslint node: true, browser: true */
/*global $tw: false */
"use strict";
/*
Export our filter function
*/
exports.jsonvariable = function(source,operator,options) {
var results = [],
widget = options.widget;
// "replacer" must be defined, otherwise JSON.stringify will throw a circular reference error RSOD
// "replacer" does not contain: isCacheable
var replacer= "params name value default resultList srcVariable text isFunctionDefinition isProcedureDefinition isWidgetDefinition isMacroDefinition".split(" ");
source(function(tiddler,title) {
var variable = widget.getVariableInfo(title, {}),
text = JSON.stringify(variable,replacer);
results.push(text || "");
});
return results;
};
})();

View File

@ -0,0 +1,42 @@
created: 20240506132850677
modified: 20240506150652759
tags: [[Operator Examples]] [[jsonvariable Operator]]
title: jsonvariable Operator (Examples)
type: text/vnd.tiddlywiki
The following example shows the "internal" structure of the `fn.test` function defined in <<.olink jsonvariable>> operator
<<.example 1 """\procedure testProc() [[aa aa]] bb
\function fn.test(splitChar:" ") [enlist<testProc>format:titlelist[]]
<pre><code><$text text={{{[[fn.test]jsonvariable[]]}}}/></code></pre>
""">>
The following example shows the same structure ''pretty printed'' using the <<.olink format>> filter operator
<<.example 2 """\procedure testProc() [[aa aa]] bb
\function fn.test(splitChar:" ") [enlist<testProc>format:titlelist[]]
\function tab() [charcode[9]]
<pre><code><$text text={{{[[fn.test]jsonvariable[]format:json<tab>]}}}/></code></pre>
""">>
The following example extracts the ''function definition'' using the <<.olink jsonextract>> filter operator
<<.example 3 """\procedure testProc() [[aa aa]] bb
\function fn.test(splitChar:" ") [enlist<testProc>format:titlelist[]]
<pre><code><$text text={{{[[fn.test]jsonvariable[]jsonextract[srcVariable],[value]]}}}/></code></pre>
""">>
The following example extracts the `srcVariables.params` without pretty printing using <<.olink jsonextract>>
<<.example 4 """\procedure testProc() [[aa aa]] bb
\function fn.test(splitChar:" ") [enlist<testProc>format:titlelist[]]
\function tab() [charcode[9]]
<pre><code><$text text={{{[[fn.test]jsonvariable[]jsonextract[srcVariable],[params]format:json<tab>]}}}/></code></pre>""">>

View File

@ -0,0 +1,45 @@
caption: jsonvariable
created: 20240506131935424
modified: 20240506145944709
op-input: a selection variable names
op-output: the JSON string values of each of the retrieved properties
op-parameter: one or more indexes of the property to retrieve
op-parameter-name: R
op-purpose: retrieve the JSON string from a ~TiddlyWiki variable
tags: [[Filter Operators]] [[JSON Operators]]
title: jsonvariable Operator
type: text/vnd.tiddlywiki
\procedure testProc() [[aa aa]] bb
\function fn.test(splitChar:" ") [enlist<testProc>format:titlelist[]]
\function tab() [charcode[9]]
<<.from-version "5.3.4">>
The <<.op jsonvariable>> operator is used to retrieve variables as JSON substrings. See also the following related operators:
Properties within a JSON object are identified by a sequence of indexes. In the following example, we use a procedure: `testProc` and a function: `fn.test` to show the possibilities of <<.op jsonvariable>>. The `tab`-function is needed for the <<.olink format>> ooperator, which is used to "pretty-print" the output.
```
\procedure testProc() [[aa aa]] bb
\function fn.test(splitChar:" ") [enlist<testProc>format:titlelist[]]
\function tab() [charcode[9]]
<pre><code><$text text={{{[[fn.test]jsonvariable[]format:json<tab>]}}}/></code></pre>
```
Which results to:
<pre><code><$text text={{{[[fn.test]jsonvariable[]format:json<tab>]}}}/></code></pre>
Also see:
* <<.olink jsonextract>> to retrieve a JSON value as a string of JSON
* <<.olink jsonget>> to retrieve the values of a property in JSON data
* <<.olink jsontype>> to retrieve the type of a JSON value
* <<.olink jsonindexes>> to retrieve the names of the fields of a JSON object, or the indexes of a JSON array
Every TW variable has this internal structure, which we can use to extract the info we need.
!! <<.operator-examples "jsonvariable">>

View File

@ -1,14 +0,0 @@
created: 20240505165315301
modified: 20240505170435200
tags:
title: test-getvariable
type: text/vnd.tiddlywiki
\procedure proc() some text
\function fn() [<proc>]
\function xx() [[proc]] [[fn]] :map[getvariable[value]] +[join[ ]]
<<xx>>

View File

@ -1,29 +0,0 @@
created: 20240506064659892
modified: 20240506101142810
tags:
title: test-getvariablejson
type: text/vnd.tiddlywiki
\define test-macro(a:10) test macro
\procedure test-proc(a:10) test proc
\function my.test(x:3, abc:def) [<test-proc>split[ ]]
\function tab() [charcode[9]]
\define elements() srcVariable params name default
\define openBracket() [
\define closeBracket() ]
<<.example 1 """\procedure test-proc(a:10) test proc
\function my.test(x:3, abc:def) [<test-proc>split[ ]]
\function tab() [charcode[9]]
<pre><$text text={{{[[my.test]getvariablejson[],<tab>]}}}/></pre>
""">>
<<.example 2 """<pre><$text text={{{[[my.test]getvariablejson[],<tab>] +[jsonextract[srcVariable],[params]] +[format:json<tab>] }}}/></pre>""">>

View File

@ -1,35 +1,35 @@
created: 20240506101435113
modified: 20240506105325655
modified: 20240506140610448
tags:
title: test-getvariablejson-conat
title: test-jsonvariable-conat
type: text/vnd.tiddlywiki
''Nice to have''
<<.example 1 """\function tab() [charcode[9]]
<pre><$text text={{{[[.concat]getvariablejson[]]}}}/></pre>
<pre><$text text={{{[[.concat]jsonvariable[]]}}}/></pre>
""">>
''Human readable version''
<<.example 2 """\function tab() [charcode[9]]
<pre><$text text={{{[[.concat]getvariablejson[]format:json<tab>]}}}/></pre>
<pre><$text text={{{[[.concat]jsonvariable[]format:json<tab>]}}}/></pre>
""">>
''Function definition''
<<.example 3 """\function tab() [charcode[9]]
<pre><$text text={{{[[.concat]getvariablejson[]jsonextract[srcVariable],[value]format:json<tab>]}}}/></pre>
<pre><$text text={{{[[.concat]jsonvariable[]jsonextract[srcVariable],[value]format:json<tab>]}}}/></pre>
""">>
''Get parameters''
<<.example 4 """\function tab() [charcode[9]]
<pre><$text text={{{[[.concat]getvariablejson[]jsonextract[srcVariable],[params]format:json<tab>]}}}/></pre>
<pre><$text text={{{[[.concat]jsonvariable[]jsonextract[srcVariable],[params]format:json<tab>]}}}/></pre>
""">>
''Expected''