2019-02-07 11:18:53 +00:00
|
|
|
/*\
|
|
|
|
title: $:/core/modules/filters/strings.js
|
|
|
|
type: application/javascript
|
|
|
|
module-type: filteroperator
|
|
|
|
|
|
|
|
Filter operators for strings. Unary/binary operators work on each item in turn, and return a new item list.
|
|
|
|
|
|
|
|
Sum/product/maxall/minall operate on the entire list, returning a single item.
|
|
|
|
|
|
|
|
\*/
|
|
|
|
(function(){
|
|
|
|
|
|
|
|
/*jslint node: true, browser: true */
|
|
|
|
/*global $tw: false */
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
exports.length = makeStringBinaryOperator(
|
2019-02-07 12:56:12 +00:00
|
|
|
function(a) {return ["" + ("" + a).length];}
|
2019-02-07 11:18:53 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
exports.uppercase = makeStringBinaryOperator(
|
|
|
|
function(a) {return [("" + a).toUpperCase()];}
|
|
|
|
);
|
|
|
|
|
|
|
|
exports.lowercase = makeStringBinaryOperator(
|
|
|
|
function(a) {return [("" + a).toLowerCase()];}
|
|
|
|
);
|
|
|
|
|
2019-06-19 11:11:02 +00:00
|
|
|
exports.sentencecase = makeStringBinaryOperator(
|
|
|
|
function(a) {return [$tw.utils.toSentenceCase(a)];}
|
|
|
|
);
|
|
|
|
|
2019-06-21 07:24:02 +00:00
|
|
|
exports.titlecase = makeStringBinaryOperator(
|
|
|
|
function(a) {return [$tw.utils.toTitleCase(a)];}
|
|
|
|
);
|
|
|
|
|
2020-08-29 11:27:58 +00:00
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
2019-02-07 11:18:53 +00:00
|
|
|
exports.split = makeStringBinaryOperator(
|
2019-06-21 09:21:52 +00:00
|
|
|
function(a,b) {return ("" + a).split(b);}
|
2019-02-07 11:18:53 +00:00
|
|
|
);
|
|
|
|
|
2020-11-01 17:11:46 +00:00
|
|
|
exports["enlist-input"] = makeStringBinaryOperator(
|
2020-11-30 17:52:46 +00:00
|
|
|
function(a,o,s) {return $tw.utils.parseStringArray("" + a,(s === "raw"));}
|
2020-11-01 17:11:46 +00:00
|
|
|
);
|
|
|
|
|
2019-06-13 15:52:19 +00:00
|
|
|
exports.join = makeStringReducingOperator(
|
2019-02-07 11:18:53 +00:00
|
|
|
function(accumulator,value,operand) {
|
2019-06-21 09:21:52 +00:00
|
|
|
if(accumulator === null) {
|
|
|
|
return value;
|
|
|
|
} else {
|
|
|
|
return accumulator + operand + value;
|
|
|
|
}
|
|
|
|
},null
|
2019-02-07 11:18:53 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
function makeStringBinaryOperator(fnCalc) {
|
|
|
|
return function(source,operator,options) {
|
|
|
|
var result = [];
|
|
|
|
source(function(tiddler,title) {
|
2020-11-30 17:52:46 +00:00
|
|
|
Array.prototype.push.apply(result,fnCalc(title,operator.operand || "",operator.suffix || ""));
|
2019-02-07 11:18:53 +00:00
|
|
|
});
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-06-13 15:52:19 +00:00
|
|
|
function makeStringReducingOperator(fnCalc,initialValue) {
|
2019-02-07 11:18:53 +00:00
|
|
|
return function(source,operator,options) {
|
|
|
|
var result = [];
|
|
|
|
source(function(tiddler,title) {
|
|
|
|
result.push(title);
|
|
|
|
});
|
2020-09-24 17:37:51 +00:00
|
|
|
if(result.length === 0) {
|
|
|
|
return [];
|
|
|
|
}
|
2019-02-07 11:18:53 +00:00
|
|
|
return [result.reduce(function(accumulator,currentValue) {
|
|
|
|
return fnCalc(accumulator,currentValue,operator.operand || "");
|
2020-04-14 16:49:38 +00:00
|
|
|
},initialValue) || ""];
|
2019-02-07 11:18:53 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-06-13 15:52:19 +00:00
|
|
|
exports.splitregexp = function(source,operator,options) {
|
|
|
|
var result = [],
|
|
|
|
suffix = operator.suffix || "",
|
|
|
|
flags = (suffix.indexOf("m") !== -1 ? "m" : "") + (suffix.indexOf("i") !== -1 ? "i" : ""),
|
2019-06-21 09:21:52 +00:00
|
|
|
regExp;
|
|
|
|
try {
|
|
|
|
regExp = new RegExp(operator.operand || "",flags);
|
|
|
|
} catch(ex) {
|
|
|
|
return ["RegExp error: " + ex];
|
|
|
|
}
|
2019-06-13 15:52:19 +00:00
|
|
|
source(function(tiddler,title) {
|
|
|
|
Array.prototype.push.apply(result,title.split(regExp));
|
2019-06-21 09:21:52 +00:00
|
|
|
});
|
2019-06-13 15:52:19 +00:00
|
|
|
return result;
|
|
|
|
};
|
|
|
|
|
2020-11-07 10:09:11 +00:00
|
|
|
exports["search-replace"] = function(source,operator,options) {
|
|
|
|
var results = [],
|
|
|
|
suffixes = operator.suffixes || [],
|
2020-11-08 09:34:05 +00:00
|
|
|
flagSuffix = (suffixes[0] ? (suffixes[0][0] || "") : ""),
|
2020-11-07 10:09:11 +00:00
|
|
|
flags = (flagSuffix.indexOf("g") !== -1 ? "g" : "") + (flagSuffix.indexOf("i") !== -1 ? "i" : ""),
|
|
|
|
isRegExp = (suffixes[1] && suffixes[1][0] === "regexp") ? true : false,
|
|
|
|
searchTerm,
|
|
|
|
regExp;
|
|
|
|
|
|
|
|
source(function(tiddler,title) {
|
|
|
|
if(title && (operator.operands.length > 1)) {
|
|
|
|
//Escape regexp characters if the operand is not a regular expression
|
|
|
|
searchTerm = isRegExp ? operator.operand : $tw.utils.escapeRegExp(operator.operand);
|
|
|
|
try {
|
|
|
|
regExp = new RegExp(searchTerm,flags);
|
|
|
|
} catch(ex) {
|
|
|
|
return ["RegExp error: " + ex];
|
|
|
|
}
|
|
|
|
results.push(
|
|
|
|
title.replace(regExp,operator.operands[1])
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
results.push(title);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return results;
|
|
|
|
};
|
|
|
|
|
2020-11-30 17:43:50 +00:00
|
|
|
exports.pad = function(source,operator,options) {
|
|
|
|
var results = [],
|
|
|
|
targetLength = operator.operand ? parseInt(operator.operand) : 0,
|
|
|
|
fill = operator.operands[1] || "0";
|
|
|
|
|
|
|
|
source(function(tiddler,title) {
|
|
|
|
if(title && title.length) {
|
|
|
|
if(title.length >= targetLength) {
|
|
|
|
results.push(title);
|
|
|
|
} else {
|
|
|
|
var padString = "",
|
|
|
|
padStringLength = targetLength - title.length;
|
|
|
|
while (padStringLength > padString.length) {
|
|
|
|
padString += fill;
|
|
|
|
}
|
|
|
|
//make sure we do not exceed the specified length
|
|
|
|
padString = padString.slice(0,padStringLength);
|
|
|
|
if(operator.suffix && (operator.suffix === "suffix")) {
|
|
|
|
title = title + padString;
|
|
|
|
} else {
|
|
|
|
title = padString + title;
|
|
|
|
}
|
|
|
|
results.push(title);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return results;
|
|
|
|
}
|
|
|
|
|
2019-02-07 11:18:53 +00:00
|
|
|
})();
|