From 0778ea67563154f878be36fe7d093d86630f898f Mon Sep 17 00:00:00 2001 From: Jermolene Date: Mon, 15 Dec 2014 17:50:24 +0000 Subject: [PATCH] Some tests and style updates for #1229 @tobibeer I simplified the handling of matched strings a little. --- core/modules/utils/utils.js | 26 ++++++++----------- editions/test/tiddlers/tests/test-utils.js | 9 +++++++ .../tw5.com/tiddlers/features/DateFormat.tid | 2 +- 3 files changed, 21 insertions(+), 16 deletions(-) diff --git a/core/modules/utils/utils.js b/core/modules/utils/utils.js index dc15e3646..46c2d35ab 100644 --- a/core/modules/utils/utils.js +++ b/core/modules/utils/utils.js @@ -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); diff --git a/editions/test/tiddlers/tests/test-utils.js b/editions/test/tiddlers/tests/test-utils.js index c2c038c65..2aa3ea665 100644 --- a/editions/test/tiddlers/tests/test-utils.js +++ b/editions/test/tiddlers/tests/test-utils.js @@ -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( diff --git a/editions/tw5.com/tiddlers/features/DateFormat.tid b/editions/tw5.com/tiddlers/features/DateFormat.tid index 7f179e775..0fb417201 100644 --- a/editions/tw5.com/tiddlers/features/DateFormat.tid +++ b/editions/tw5.com/tiddlers/features/DateFormat.tid @@ -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") |