1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-06-23 13:53:15 +00:00

Some tests and style updates for #1229

@tobibeer I simplified the handling of matched strings a little.
This commit is contained in:
Jermolene 2014-12-15 17:50:24 +00:00
parent d031a93c6d
commit 0778ea6756
3 changed files with 21 additions and 16 deletions

View File

@ -176,8 +176,7 @@ exports.slowInSlowOut = function(t) {
};
exports.formatDateString = function(date,template) {
var match,
result="",
var result = "",
t = template,
matches = [
[/^0hh12/, function() {
@ -231,7 +230,7 @@ exports.formatDateString = function(date,template) {
return (tz < 0 ? '+' : '-') + $tw.utils.pad(Math.floor(atz / 60)) + ':' + $tw.utils.pad(atz % 60);
}],
[/^wYY/, function() {
return $tw.utils.pad($tw.utils.getYearForWeekNo(date)-2000);
return $tw.utils.pad($tw.utils.getYearForWeekNo(date) - 2000);
}],
[/^[ap]m/, function() {
return $tw.utils.getAmPm(date).toLowerCase();
@ -258,25 +257,22 @@ exports.formatDateString = function(date,template) {
return $tw.utils.getWeek(date);
}],
[/^YY/, function() {
return $tw.utils.pad(date.getFullYear()-2000);
return $tw.utils.pad(date.getFullYear() - 2000);
}]
];
while(t.length){
match = "";
var matchString = "";
$tw.utils.each(matches, function(m) {
var r,l;
if(t.match(m[0])) {
match = m[1].call();
r = m[0].toString(),
l = r.length - 3;
l = r.substr(2,1) == "[" ? l - 3 : l;
t = t.substr(l);
var r,l,
match = m[0].exec(t);
if(match) {
matchString = m[1].call();
t = t.substr(match[0].length);
return false;
}
});
if(match) {
result += match;
if(matchString) {
result += matchString;
} else {
result += t.charAt(0);
t = t.substr(1);

View File

@ -25,6 +25,15 @@ describe("Utility tests", function() {
expect(psa(" [[Tidd\u00a0ler8]] two ")).toEqual(["Tidd\u00a0ler8","two"]);
});
it("should handle formatting a date string", function() {
var fds = $tw.utils.formatDateString,
d = new Date(2014,10,9,17,41,28,542);
expect(fds(d,"DDD DD MMM YYYY")).toBe("Sunday 9 November 2014");
expect(fds(d,"ddd hh mm ssss")).toBe("Sun 17 41 2828");
expect(fds(d,"MM0DD")).toBe("1109");
expect(fds(d,"MM0\\D\\D")).toBe("110DD");
});
it("should parse text references", function() {
var ptr = $tw.utils.parseTextReference;
expect(ptr("title")).toEqual(

View File

@ -12,7 +12,7 @@ The ViewWidget accepts a `template` attribute that allows the format of date val
|`DD` |Day of month |
|`0DD` |Adds a leading zero |
|`DDth` |Adds a suffix |
|`WW` |~ISO-8601 week number of year |
|`WW` |ISO-8601 week number of year |
|`0WW` |Adds a leading zero |
|`MMM` |Month in full (eg, "July") |
|`mmm` |Short month (eg, "Jul") |