From fbcd24494601d8cabc016af46daeda344dc5d405 Mon Sep 17 00:00:00 2001 From: harttle Date: Mon, 24 Oct 2016 00:23:50 +0800 Subject: [PATCH] test for strftime --- filters.js | 2 +- src/{ => util}/strftime.js | 159 ------------------------------------- test/util/strftime.js | 14 ++++ 3 files changed, 15 insertions(+), 160 deletions(-) rename src/{ => util}/strftime.js (63%) create mode 100644 test/util/strftime.js diff --git a/filters.js b/filters.js index e95f7bae8..e0205c08d 100644 --- a/filters.js +++ b/filters.js @@ -1,4 +1,4 @@ -const strftime = require('./src/strftime.js'); +const strftime = require('./src/util/strftime.js'); const _ = require('./src/util/underscore.js'); var escapeMap = { diff --git a/src/strftime.js b/src/util/strftime.js similarity index 63% rename from src/strftime.js rename to src/util/strftime.js index 03aef0c10..051102269 100644 --- a/src/strftime.js +++ b/src/util/strftime.js @@ -88,18 +88,6 @@ var _date = { } }; -var _obj = { - values_of: function(obj) { - var values = []; - for(var k in obj){ - if(obj.hasOwnProperty(k)){ - values.push(obj[k]); - } - } - return values; - } -}; - var _number = { pad: function(value, size, ch) { if (!ch) ch = '0'; @@ -217,153 +205,6 @@ var format_codes = { format_codes.h = format_codes.b; format_codes.N = format_codes.L; - -// * r stands for regex, p stands for parser -// * all parseInt calls have to have the base supplied as the second -// parameter, otherwise they will default to octal when parsing numbers -// with leading zeros. This is most evident when parsing a date with 08 as -// the minutes / year as 08 is an invalid octal number, and so returns 0 -var parse_codes = { - a: { - r: "(?:" + dayNamesShort.join("|") + ")" - }, - A: { - r: "(?:" + dayNames.join("|") + ")" - }, - b: { - r: "(" + monthNamesShort.join("|") + ")", - p: function(data) { - this.month = $.inArray(data, monthNamesShort); - } - }, - B: { - r: "(" + monthNames.join("|") + ")", - p: function(data) { - this.month = $.inArray(data, monthNames); - } - }, - C: { - r: "(\\d{1,2})", - p: function(d) { - this.century = parseInt(d, 10); - } - }, - d: { - r: "(\\d{1,2})", - p: function(d) { - this.day = parseInt(d, 10); - } - }, - H: { - r: "(\\d{1,2})", - p: function(d) { - this.hour = parseInt(d, 10); - } - }, - // This gives only the day. Parsing of the month happens at the end because - // we also need the year - j: { - r: "(\\d{1,3})", - p: function(d) { - this.day = parseInt(d, 10); - } - }, - L: { - r: "(\\d{3})", - p: function(d) { - this.milliseconds = parseInt(d, 10); - } - }, - m: { - r: "(\\d{1,2})", - p: function(d) { - this.month = parseInt(d, 10) - 1; - } - }, - M: { - r: "(\\d{2})", - p: function(d) { - this.minute = parseInt(d, 10); - } - }, - p: { - r: "(AM|PM)", - p: function(d) { - if (d == 'AM') { - if (this.hour == 12) { - this.hour = 0; - } - } else { - if (this.hour < 12) { - this.hour += 12; - } - } - } - }, - P: { - r: "(am|pm)", - p: function(d) { - if (d == 'am') { - if (this.hour == 12) { - this.hour = 0; - } - } else { - if (this.hour < 12) { - this.hour += 12; - } - } - } - }, - q: { - r: "(?:" + _obj.values_of(suffixes).join('|') + ")" - }, - S: { - r: "(\\d{2})", - p: function(d) { - this.second = parseInt(d, 10); - } - }, - y: { - r: "(\\d{1,2})", - p: function(d) { - this.year = parseInt(d, 10); - } - }, - Y: { - r: "(\\d{4})", - p: function(d) { - this.century = Math.floor(parseInt(d, 10) / 100); - this.year = parseInt(d, 10) % 100; - } - }, - z: { // "Z", "+05:00", "+0500" all acceptable. - r: "(Z|[+-]\\d{2}:?\\d{2})", - p: function(d) { - // UTC, no offset. - if (d == "Z") { - this.zone = 0; - return; - } - - var seconds = parseInt(d[0] + d[1] + d[2], 10) * 3600; // e.g., "+05" or "-08" - if (d[3] == ":") { - // "+HH:MM" is preferred iso8601 format - seconds += parseInt(d[4] + d[5], 10) * 60; - } else { - // "+HHMM" is frequently used, though. - seconds += parseInt(d[3] + d[4], 10) * 60; - } - this.zone = seconds; - } - } -}; -parse_codes.e = parse_codes.d; -parse_codes.h = parse_codes.b; -parse_codes.I = parse_codes.H; -parse_codes.k = parse_codes.H; -parse_codes.l = parse_codes.H; - - var strftime = function(d, format) { // I used to use string split with a regex and a capturing block here, // which I thought was really clever, but apparently this exact feature is diff --git a/test/util/strftime.js b/test/util/strftime.js new file mode 100644 index 000000000..33fd84487 --- /dev/null +++ b/test/util/strftime.js @@ -0,0 +1,14 @@ +const chai = require("chai"); +const expect = chai.expect; + +var t = require('../../src/util/strftime.js'); + +describe('util/strftime', function() { + var now = new Date('2016-10-23T16:15:23'); + it('should format week by %U', function() { + expect(t(now, '%U')).to.equal('43'); + }); + it('should format GMT string', function() { + expect(t(now, '%Y-%m-%dT%H:%M:%S')).to.equal('2016-10-24T00:15:23'); + }); +});