1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-11-27 03:57:21 +00:00

Fixes to split operator, plus instructions for using it to do search and replace

This commit is contained in:
Jeremy Ruston 2019-06-21 10:21:52 +01:00
parent 90684f9f52
commit 6582b106ee
3 changed files with 66 additions and 6 deletions

View File

@ -39,13 +39,17 @@ exports.trim = makeStringBinaryOperator(
);
exports.split = makeStringBinaryOperator(
function(a,b) {return ("" + a).split(b).filter(function(str) {return !!str;});}
function(a,b) {return ("" + a).split(b);}
);
exports.join = makeStringReducingOperator(
function(accumulator,value,operand) {
return "" + (accumulator ? accumulator + (operand || "") + value : value);
}
if(accumulator === null) {
return value;
} else {
return accumulator + operand + value;
}
},null
);
function makeStringBinaryOperator(fnCalc) {
@ -59,7 +63,6 @@ function makeStringBinaryOperator(fnCalc) {
}
function makeStringReducingOperator(fnCalc,initialValue) {
initialValue = initialValue || "";
return function(source,operator,options) {
var result = [];
source(function(tiddler,title) {
@ -75,7 +78,12 @@ exports.splitregexp = function(source,operator,options) {
var result = [],
suffix = operator.suffix || "",
flags = (suffix.indexOf("m") !== -1 ? "m" : "") + (suffix.indexOf("i") !== -1 ? "i" : ""),
regExp;
try {
regExp = new RegExp(operator.operand || "",flags);
} catch(ex) {
return ["RegExp error: " + ex];
}
source(function(tiddler,title) {
Array.prototype.push.apply(result,title.split(regExp));
});

View File

@ -12,4 +12,28 @@ type: text/vnd.tiddlywiki
<<.from-version "5.1.20">>
Note that in some circumstances the <<.op split>> operator will include blank items in the list of results. For example,
```
[[the band thethe are the best the]split[the]]
```
The following results are returned:
```
["", " band ", "", " are ", " best ", ""]
```
Where it might be expected that the results would be:
```
[" band ", " are ", " best "]
```
The blank items mark the boundaries between matches. If they are not required they can be removed with the ''blank'' category of the [[is Operator]]: `[[the band thethe are the best the]split[the]!is[blank]]`.
The reason that the blank items can be useful is that they allow search and replace operations to be constructed from a combination of the [[split Operator]] or [[splitregexp Operator]] and the [[join Operator]]. For example:
<<.operator-example 1 "[[the band thethe are the best the]split[the]join[every]]">>
<<.operator-examples "split">>

View File

@ -13,4 +13,32 @@ type: text/vnd.tiddlywiki
<<.from-version "5.1.20">>
Note that in some circumstances the <<.op splitregexp>> operator will include blank items in the list of results. For example,
```
[[the band thethe are the best the]splitregexp[the]]
```
The following results are returned:
```
["", " band ", "", " are ", " best ", ""]
```
Where it might be expected that the results would be:
```
[" band ", " are ", " best "]
```
The blank items mark the boundaries between matches. If they are not required they can be removed with the ''blank'' category of the [[is Operator]]: `[[the band thethe are the best the]splitregexp[the]!is[blank]]`.
The reason that the blank items can be useful is that they allow search and replace operations to be constructed from a combination of the [[split Operator]] or [[splitregexp Operator]] and the [[join Operator]]. For example:
<<.operator-example 1 "[[nobody, really; wants; to see -- all this \punctuation]splitregexp[,|;|-|\\]join[...]]">>
Syntax errors in the regular expression will cause the filter to return an error message. For example:
<<.operator-example 2 "[[the cat sat on the mat]splitregexp[\]]">>
<<.operator-examples "splitregexp">>