Added string operator pad[] along with tests and docs (#5146)

This commit is contained in:
saqimtiaz
2020-11-30 17:43:50 +00:00
committed by GitHub
parent 4623c45d29
commit 8fc6910c03
4 changed files with 76 additions and 0 deletions
+29
View File
@@ -143,4 +143,33 @@ exports["search-replace"] = function(source,operator,options) {
return results;
};
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;
}
})();