mirror of
https://github.com/Jermolene/TiddlyWiki5
synced 2024-11-27 03:57:21 +00:00
Add suffix and parameter to trim operator (#4811)
* Add suffix and parameter to trim operator Fixes #4809 * Unit tests for new trim operator parameters * Mention trim operator in 5.1.23 release notes * Address review comments * Move regex escaping into utils.js trim functions This way the trimPrefix and trimSuffix functions from utils.js are safe to call without regex-escaping their parameters, which should make them easier to use from other parts of the Javascript code.
This commit is contained in:
parent
ef29d05ea4
commit
5202441769
@ -34,9 +34,31 @@ exports.titlecase = makeStringBinaryOperator(
|
||||
function(a) {return [$tw.utils.toTitleCase(a)];}
|
||||
);
|
||||
|
||||
exports.trim = makeStringBinaryOperator(
|
||||
function(a) {return [$tw.utils.trim(a)];}
|
||||
);
|
||||
exports.trim = function(source,operator,options) {
|
||||
var result = [],
|
||||
suffix = operator.suffix || "",
|
||||
operand = (operator.operand || ""),
|
||||
fnCalc;
|
||||
if(suffix === "prefix") {
|
||||
fnCalc = function(a,b) {return [$tw.utils.trimPrefix(a,b)];}
|
||||
} else if(suffix === "suffix") {
|
||||
fnCalc = function(a,b) {return [$tw.utils.trimSuffix(a,b)];}
|
||||
} else {
|
||||
if(operand === "") {
|
||||
fnCalc = function(a) {return [$tw.utils.trim(a)];}
|
||||
} else {
|
||||
fnCalc = function(a,b) {return [$tw.utils.trimSuffix($tw.utils.trimPrefix(a,b),b)];}
|
||||
}
|
||||
}
|
||||
source(function(tiddler,title) {
|
||||
Array.prototype.push.apply(result,fnCalc(title,operand));
|
||||
});
|
||||
return result;
|
||||
};
|
||||
|
||||
// makeStringBinaryOperator(
|
||||
// function(a) {return [$tw.utils.trim(a)];}
|
||||
// );
|
||||
|
||||
exports.split = makeStringBinaryOperator(
|
||||
function(a,b) {return ("" + a).split(b);}
|
||||
|
@ -94,6 +94,36 @@ exports.trim = function(str) {
|
||||
}
|
||||
};
|
||||
|
||||
exports.trimPrefix = function(str,unwanted) {
|
||||
if(typeof str === "string" && typeof unwanted === "string") {
|
||||
if(unwanted === "") {
|
||||
return str.replace(/^\s\s*/, '');
|
||||
} else {
|
||||
// Safely regexp-escape the unwanted text
|
||||
unwanted = unwanted.replace(/[\\^$*+?.()|[\]{}]/g, '\\$&');
|
||||
var regex = new RegExp('^(' + unwanted + ')+');
|
||||
return str.replace(regex, '');
|
||||
}
|
||||
} else {
|
||||
return str;
|
||||
}
|
||||
};
|
||||
|
||||
exports.trimSuffix = function(str,unwanted) {
|
||||
if(typeof str === "string" && typeof unwanted === "string") {
|
||||
if(unwanted === "") {
|
||||
return str.replace(/\s\s*$/, '');
|
||||
} else {
|
||||
// Safely regexp-escape the unwanted text
|
||||
unwanted = unwanted.replace(/[\\^$*+?.()|[\]{}]/g, '\\$&');
|
||||
var regex = new RegExp('(' + unwanted + ')+$');
|
||||
return str.replace(regex, '');
|
||||
}
|
||||
} else {
|
||||
return str;
|
||||
}
|
||||
};
|
||||
|
||||
/*
|
||||
Convert a string to sentence case (ie capitalise first letter)
|
||||
*/
|
||||
|
@ -62,6 +62,7 @@ type: text/vnd.tiddlywiki
|
||||
* [[Extended|https://github.com/Jermolene/TiddlyWiki5/pull/4741]] [[WidgetMessage: tm-import-tiddlers]] to override the title $:/Import and to better control whether the import tiddler is opened automatically
|
||||
* [[Extended|https://github.com/Jermolene/TiddlyWiki5/pull/4740]] the EditWidget to pass all attributes through to the sub-widget
|
||||
* [[Extended|https://github.com/Jermolene/TiddlyWiki5/commit/bd2cf5c46498222a32ebda92da3ae50bde33decb]] the internal `<$element>` widget to add a hook so that plugins can intercept DOM node creation
|
||||
* [[Extended|https://github.com/Jermolene/TiddlyWiki5/pull/4811]] the [[trim Operator]] to optionally trim off a prefixing or suffixing string from input tiddler titles
|
||||
|
||||
! Bug Fixes
|
||||
|
||||
|
@ -467,6 +467,18 @@ function runTests(wiki) {
|
||||
// Ensure that join correctly handles empty strings in source
|
||||
expect(wiki.filterTiddlers("[[]] Paul +[join[-]]").join(",")).toBe("-Paul");
|
||||
expect(wiki.filterTiddlers("[[ John ]] [[Paul ]] [[ George]] Ringo +[trim[]join[-]]").join(",")).toBe("John-Paul-George-Ringo");
|
||||
expect(wiki.filterTiddlers("[[ abc ]] [[def ]] [[ ghi]] +[trim[]]").join(",")).toBe("abc,def,ghi");
|
||||
expect(wiki.filterTiddlers("[[ abc ]] [[def ]] [[ ghi]] +[trim:prefix[]]").join(",")).toBe("abc ,def ,ghi");
|
||||
expect(wiki.filterTiddlers("[[ abc ]] [[def ]] [[ ghi]] +[trim:suffix[]]").join(",")).toBe(" abc,def, ghi");
|
||||
expect(wiki.filterTiddlers("[[ abacus ]] [[ baobab ]] +[trim[ab]]").join(",")).toBe(" abacus , baobab ");
|
||||
expect(wiki.filterTiddlers("[[ abacus ]] [[ baobab ]] +[trim[a]]").join(",")).toBe(" abacus , baobab ");
|
||||
expect(wiki.filterTiddlers("abacus baobab +[trim[a]]").join(",")).toBe("bacus,baobab");
|
||||
expect(wiki.filterTiddlers("abacus baobab +[trim[ab]]").join(",")).toBe("acus,baob");
|
||||
expect(wiki.filterTiddlers("abacus baobab +[trim:prefix[ab]]").join(",")).toBe("acus,baobab");
|
||||
expect(wiki.filterTiddlers("abacus baobab +[trim:suffix[ab]]").join(",")).toBe("abacus,baob");
|
||||
// Trim doesn't hiccup on regexp special characters
|
||||
expect(wiki.filterTiddlers("[[.*abacus.*]] [[.+baobab.+]] +[trim[.*]]").join(",")).toBe("abacus,.+baobab.+");
|
||||
expect(wiki.filterTiddlers("[[.*abacus.*]] [[.+baobab.+]] +[trim[.+]]").join(",")).toBe(".*abacus.*,baobab");
|
||||
});
|
||||
|
||||
it("should handle the mathematics operators", function() {
|
||||
|
@ -5,3 +5,11 @@ title: trim Operator (Examples)
|
||||
type: text/vnd.tiddlywiki
|
||||
|
||||
<<.operator-example 1 "[[ a b ]trim[]addprefix[-]addsuffix[-]]">>
|
||||
|
||||
<<.operator-example 2 "[[ abc ]] [[def ]] [[ ghi]] +[trim[]addprefix[-]addsuffix[-]]">>
|
||||
<<.operator-example 3 "[[ abc ]] [[def ]] [[ ghi]] +[trim:prefix[]addprefix[-]addsuffix[-]]">>
|
||||
<<.operator-example 4 "[[ abc ]] [[def ]] [[ ghi]] +[trim:suffix[]addprefix[-]addsuffix[-]]">>
|
||||
|
||||
<<.operator-example 5 "abacus baobab +[trim[ab]]">>
|
||||
<<.operator-example 6 "abacus baobab +[trim:prefix[ab]]">>
|
||||
<<.operator-example 7 "abacus baobab +[trim:suffix[ab]]">>
|
||||
|
@ -12,4 +12,6 @@ op-output: those input titles that start with <<.place S>>, but with those chara
|
||||
|
||||
<<.s-matching-is-case-sensitive>>
|
||||
|
||||
<<.tip " This filters out input titles that do not start with S. For removing S without filtering out input titles that don't start with S, see [[trim|trim Operator]].">>
|
||||
|
||||
<<.operator-examples "removeprefix">>
|
||||
|
@ -12,4 +12,6 @@ op-output: those input titles that end with <<.place S>>, but with those charact
|
||||
|
||||
<<.s-matching-is-case-sensitive>>
|
||||
|
||||
<<.tip " This filters out input titles that do not end with S. For removing S without filtering out input titles that don't end with S, see [[trim|trim Operator]].">>
|
||||
|
||||
<<.operator-examples "removesuffix">>
|
||||
|
@ -1,13 +1,16 @@
|
||||
caption: trim
|
||||
created: 20190613153740241
|
||||
modified: 20190613153820282
|
||||
op-purpose: returns each item in the list with whitespace, or a given character string, trimmed from the start and/or end
|
||||
op-input: a [[selection of titles|Title Selection]]
|
||||
op-output: the input titles with whitespace trimmed from the start and end
|
||||
op-purpose: returns each item in the list with whitespace trimmed from the start and end
|
||||
op-parameter: <<.from-version "5.1.23">> a string of characters
|
||||
op-parameter-name: S
|
||||
op-output: the input titles with <<.place S>>, or whitespace if <<.place S>> is not specified, trimmed from the start and/or end
|
||||
tags: [[Filter Operators]]
|
||||
title: trim Operator
|
||||
type: text/vnd.tiddlywiki
|
||||
|
||||
<<.from-version "5.1.20">>
|
||||
<<.from-version "5.1.23">>The <<.op trim>> filter allows a parameter specifying a string to trim.
|
||||
|
||||
<<.operator-examples "trim">>
|
||||
|
Loading…
Reference in New Issue
Block a user