mirror of
https://github.com/Jermolene/TiddlyWiki5
synced 2024-06-24 06:13:17 +00:00
Fixes to split operator, plus instructions for using it to do search and replace
This commit is contained in:
parent
90684f9f52
commit
6582b106ee
|
@ -39,13 +39,17 @@ exports.trim = makeStringBinaryOperator(
|
||||||
);
|
);
|
||||||
|
|
||||||
exports.split = 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(
|
exports.join = makeStringReducingOperator(
|
||||||
function(accumulator,value,operand) {
|
function(accumulator,value,operand) {
|
||||||
return "" + (accumulator ? accumulator + (operand || "") + value : value);
|
if(accumulator === null) {
|
||||||
}
|
return value;
|
||||||
|
} else {
|
||||||
|
return accumulator + operand + value;
|
||||||
|
}
|
||||||
|
},null
|
||||||
);
|
);
|
||||||
|
|
||||||
function makeStringBinaryOperator(fnCalc) {
|
function makeStringBinaryOperator(fnCalc) {
|
||||||
|
@ -59,7 +63,6 @@ function makeStringBinaryOperator(fnCalc) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function makeStringReducingOperator(fnCalc,initialValue) {
|
function makeStringReducingOperator(fnCalc,initialValue) {
|
||||||
initialValue = initialValue || "";
|
|
||||||
return function(source,operator,options) {
|
return function(source,operator,options) {
|
||||||
var result = [];
|
var result = [];
|
||||||
source(function(tiddler,title) {
|
source(function(tiddler,title) {
|
||||||
|
@ -75,10 +78,15 @@ exports.splitregexp = function(source,operator,options) {
|
||||||
var result = [],
|
var result = [],
|
||||||
suffix = operator.suffix || "",
|
suffix = operator.suffix || "",
|
||||||
flags = (suffix.indexOf("m") !== -1 ? "m" : "") + (suffix.indexOf("i") !== -1 ? "i" : ""),
|
flags = (suffix.indexOf("m") !== -1 ? "m" : "") + (suffix.indexOf("i") !== -1 ? "i" : ""),
|
||||||
regExp = new RegExp(operator.operand || "",flags);
|
regExp;
|
||||||
|
try {
|
||||||
|
regExp = new RegExp(operator.operand || "",flags);
|
||||||
|
} catch(ex) {
|
||||||
|
return ["RegExp error: " + ex];
|
||||||
|
}
|
||||||
source(function(tiddler,title) {
|
source(function(tiddler,title) {
|
||||||
Array.prototype.push.apply(result,title.split(regExp));
|
Array.prototype.push.apply(result,title.split(regExp));
|
||||||
});
|
});
|
||||||
return result;
|
return result;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -12,4 +12,28 @@ type: text/vnd.tiddlywiki
|
||||||
|
|
||||||
<<.from-version "5.1.20">>
|
<<.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">>
|
<<.operator-examples "split">>
|
||||||
|
|
|
@ -13,4 +13,32 @@ type: text/vnd.tiddlywiki
|
||||||
|
|
||||||
<<.from-version "5.1.20">>
|
<<.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">>
|
<<.operator-examples "splitregexp">>
|
||||||
|
|
Loading…
Reference in New Issue
Block a user