1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-09-29 07:20:47 +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) { exports.formatDateString = function(date,template) {
var match, var result = "",
result="",
t = template, t = template,
matches = [ matches = [
[/^0hh12/, function() { [/^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); return (tz < 0 ? '+' : '-') + $tw.utils.pad(Math.floor(atz / 60)) + ':' + $tw.utils.pad(atz % 60);
}], }],
[/^wYY/, function() { [/^wYY/, function() {
return $tw.utils.pad($tw.utils.getYearForWeekNo(date)-2000); return $tw.utils.pad($tw.utils.getYearForWeekNo(date) - 2000);
}], }],
[/^[ap]m/, function() { [/^[ap]m/, function() {
return $tw.utils.getAmPm(date).toLowerCase(); return $tw.utils.getAmPm(date).toLowerCase();
@ -258,25 +257,22 @@ exports.formatDateString = function(date,template) {
return $tw.utils.getWeek(date); return $tw.utils.getWeek(date);
}], }],
[/^YY/, function() { [/^YY/, function() {
return $tw.utils.pad(date.getFullYear()-2000); return $tw.utils.pad(date.getFullYear() - 2000);
}] }]
]; ];
while(t.length){ while(t.length){
match = ""; var matchString = "";
$tw.utils.each(matches, function(m) { $tw.utils.each(matches, function(m) {
var r,l; var r,l,
if(t.match(m[0])) { match = m[0].exec(t);
match = m[1].call(); if(match) {
r = m[0].toString(), matchString = m[1].call();
l = r.length - 3; t = t.substr(match[0].length);
l = r.substr(2,1) == "[" ? l - 3 : l;
t = t.substr(l);
return false; return false;
} }
}); });
if(match) { if(matchString) {
result += match; result += matchString;
} else { } else {
result += t.charAt(0); result += t.charAt(0);
t = t.substr(1); t = t.substr(1);

View File

@ -25,6 +25,15 @@ describe("Utility tests", function() {
expect(psa(" [[Tidd\u00a0ler8]] two ")).toEqual(["Tidd\u00a0ler8","two"]); 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() { it("should parse text references", function() {
var ptr = $tw.utils.parseTextReference; var ptr = $tw.utils.parseTextReference;
expect(ptr("title")).toEqual( 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 | |`DD` |Day of month |
|`0DD` |Adds a leading zero | |`0DD` |Adds a leading zero |
|`DDth` |Adds a suffix | |`DDth` |Adds a suffix |
|`WW` |~ISO-8601 week number of year | |`WW` |ISO-8601 week number of year |
|`0WW` |Adds a leading zero | |`0WW` |Adds a leading zero |
|`MMM` |Month in full (eg, "July") | |`MMM` |Month in full (eg, "July") |
|`mmm` |Short month (eg, "Jul") | |`mmm` |Short month (eg, "Jul") |