From: Tim Wood Date: Thu, 13 Oct 2011 05:40:00 +0000 (-0700) Subject: Adding convenience methods per #22 X-Git-Tag: 1.0.1~27 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=f5fe9e89c728c5ce6c3c6f41d369d652cdf54dfc;p=thirdparty%2Fmoment.git Adding convenience methods per #22 Adding `year month date day hour minute second` Changing internal _date native `Date` object name to `_d` instead of `date` to avoid collisions with the `_date.fn.date` method. --- diff --git a/test/date.js b/test/date.js index d24f8db0b..673c53c83 100755 --- a/test/date.js +++ b/test/date.js @@ -19,35 +19,42 @@ module("create"); test("array", 8, function() { - ok(_date([2010]).date instanceof Date, "[2010]"); - ok(_date([2010, 1]).date instanceof Date, "[2010, 1]"); - ok(_date([2010, 1, 12]).date instanceof Date, "[2010, 1, 12]"); - ok(_date([2010, 1, 12, 1]).date instanceof Date, "[2010, 1, 12, 1]"); - ok(_date([2010, 1, 12, 1, 1]).date instanceof Date, "[2010, 1, 12, 1, 1]"); - ok(_date([2010, 1, 12, 1, 1, 1]).date instanceof Date, "[2010, 1, 12, 1, 1, 1]"); - ok(_date([2010, 1, 12, 1, 1, 1, 1]).date instanceof Date, "[2010, 1, 12, 1, 1, 1, 1]"); + ok(_date([2010])._d instanceof Date, "[2010]"); + ok(_date([2010, 1])._d instanceof Date, "[2010, 1]"); + ok(_date([2010, 1, 12])._d instanceof Date, "[2010, 1, 12]"); + ok(_date([2010, 1, 12, 1])._d instanceof Date, "[2010, 1, 12, 1]"); + ok(_date([2010, 1, 12, 1, 1])._d instanceof Date, "[2010, 1, 12, 1, 1]"); + ok(_date([2010, 1, 12, 1, 1, 1])._d instanceof Date, "[2010, 1, 12, 1, 1, 1]"); + ok(_date([2010, 1, 12, 1, 1, 1, 1])._d instanceof Date, "[2010, 1, 12, 1, 1, 1, 1]"); deepEqual(_date(new Date(2010, 1, 14, 15, 25, 50, 125)), _date([2010, 1, 14, 15, 25, 50, 125]), "constructing with array === constructing with new Date()"); }); +test("number", 2, function() { + ok(_date(1000)._d instanceof Date, "1000"); + ok((_date(1000).valueOf() === 1000), "testing valueOf"); +}); + + test("date", 1, function() { - ok(_date(new Date()).date instanceof Date, "new Date()"); + ok(_date(new Date())._d instanceof Date, "new Date()"); }); +console.log(_date(1000).valueOf()) test("_date", 2, function() { - ok(_date(_date()).date instanceof Date, "_date(_date())"); - ok(_date(_date(_date())).date instanceof Date, "_date(_date(_date()))"); + ok(_date(_date())._d instanceof Date, "_date(_date())"); + ok(_date(_date(_date()))._d instanceof Date, "_date(_date(_date()))"); }); test("undefined", 1, function() { - ok(_date().date instanceof Date, "undefined"); + ok(_date()._d instanceof Date, "undefined"); }); test("string without format", 2, function() { - ok(_date("Aug 9, 1995").date instanceof Date, "Aug 9, 1995"); - ok(_date("Mon, 25 Dec 1995 13:30:00 GMT").date instanceof Date, "Mon, 25 Dec 1995 13:30:00 GMT"); + ok(_date("Aug 9, 1995")._d instanceof Date, "Aug 9, 1995"); + ok(_date("Mon, 25 Dec 1995 13:30:00 GMT")._d instanceof Date, "Mon, 25 Dec 1995 13:30:00 GMT"); }); @@ -201,8 +208,7 @@ test("diff", 5, function() { module("leap year"); -test("leap year", function() { - expect(4); +test("leap year", 4, function() { equal(_date([2010, 0, 1]).isLeapYear(), false, '2010'); equal(_date([2100, 0, 1]).isLeapYear(), false, '2100'); equal(_date([2008, 0, 1]).isLeapYear(), true, '2008'); @@ -210,15 +216,64 @@ test("leap year", function() { }); +module("getters and setters"); + + +test("getters", 7, function() { + var a = _date([2011, 9, 12, 6, 7, 8]); + equal(a.year(), 2011, 'year'); + equal(a.month(), 9, 'month'); + equal(a.date(), 12, 'date'); + equal(a.day(), 3, 'day'); + equal(a.hours(), 6, 'hour'); + equal(a.minutes(), 7, 'minute'); + equal(a.seconds(), 8, 'second'); +}); + + +test("setters", 7, function() { + var a = _date(); + a.year(2011); + a.month(9); + a.date(12); + a.hours(6); + a.minutes(7); + a.seconds(8); + equal(a.year(), 2011, 'year'); + equal(a.month(), 9, 'month'); + equal(a.date(), 12, 'date'); + equal(a.day(), 3, 'day'); + equal(a.hours(), 6, 'hour'); + equal(a.minutes(), 7, 'minute'); + equal(a.seconds(), 8, 'second'); +}); + +test("chaining setters", 7, function() { + var a = _date(); + a.year(2011) + .month(9) + .date(12) + .hours(6) + .minutes(7) + .seconds(8); + equal(a.year(), 2011, 'year'); + equal(a.month(), 9, 'month'); + equal(a.date(), 12, 'date'); + equal(a.day(), 3, 'day'); + equal(a.hours(), 6, 'hour'); + equal(a.minutes(), 7, 'minute'); + equal(a.seconds(), 8, 'second'); +}); + module("underscore mixin"); test("underscore mixin", 6, function() { - ok(_.date([2010, 1, 12]).date instanceof Date, "[2010, 1, 12]"); - ok(_.date([2010, 1, 12, 1]).date instanceof Date, "[2010, 1, 12, 1]"); - ok(_.date().date instanceof Date, "undefined"); - ok(_.date("Aug 9, 1995").date instanceof Date, "Aug 9, 1995"); - ok(_.date("Mon, 25 Dec 1995 13:30:00 GMT").date instanceof Date, "Mon, 25 Dec 1995 13:30:00 GMT"); + ok(_.date([2010, 1, 12])._d instanceof Date, "[2010, 1, 12]"); + ok(_.date([2010, 1, 12, 1])._d instanceof Date, "[2010, 1, 12, 1]"); + ok(_.date()._d instanceof Date, "undefined"); + ok(_.date("Aug 9, 1995")._d instanceof Date, "Aug 9, 1995"); + ok(_.date("Mon, 25 Dec 1995 13:30:00 GMT")._d instanceof Date, "Mon, 25 Dec 1995 13:30:00 GMT"); deepEqual(_.date(new Date(2010, 1, 14, 15, 25, 50, 125)), _.date([2010, 1, 14, 15, 25, 50, 125]), "constructing with array === constructing with new Date()"); }); diff --git a/underscore.date.js b/underscore.date.js index db5e76eb2..aeace760b 100644 --- a/underscore.date.js +++ b/underscore.date.js @@ -5,12 +5,14 @@ // // Version 0.6.1 -(function (undefined) { +(function (Date, undefined) { var _date, round = Math.round, languages = {}, - paramsToParse = 'months|monthsShort|weekdays|weekdaysShort|relativeTime|ordinal'.split('|'); + paramsToParse = 'months|monthsShort|weekdays|weekdaysShort|relativeTime|ordinal'.split('|'), + i, + shortcuts = 'Month|Date|Hours|Minutes|Seconds'.split('|'); // left zero fill a number // see http://jsperf.com/left-zero-filling for performance comparison @@ -284,18 +286,18 @@ // UnderscoreDate prototype object function UnderscoreDate(input, format) { // parse UnderscoreDate object - if (input && input.date instanceof Date) { - this.date = input.date; + if (input && input._d instanceof Date) { + this._d = input._d; // parse string and format } else if (format) { if (isArray(format)) { - this.date = makeDateFromStringAndArray(input, format); + this._d = makeDateFromStringAndArray(input, format); } else { - this.date = makeDateFromStringAndFormat(input, format); + this._d = makeDateFromStringAndFormat(input, format); } // parse everything else } else { - this.date = input === undefined ? new Date() : + this._d = input === undefined ? new Date() : input instanceof Date ? input : isArray(input) ? dateFromArray(input) : new Date(input); @@ -378,31 +380,31 @@ _date.fn = UnderscoreDate.prototype = { valueOf : function () { - return this.date.getTime(); + return +this._d; }, format : function (inputString) { - return formatDate(this.date, inputString); + return formatDate(this._d, inputString); }, add : function (input) { - this.date = dateAddRemove(this.date, input, 1); + this._d = dateAddRemove(this._d, input, 1); return this; }, subtract : function (input) { - this.date = dateAddRemove(this.date, input, -1); + this._d = dateAddRemove(this._d, input, -1); return this; }, diff : function (input, format) { - return this.date - _date(input, format).date; + return this._d - _date(input, format)._d; }, from : function (time, withoutSuffix) { var difference = this.diff(time), string = difference < 0 ? _date.relativeTime.past : _date.relativeTime.future, - output = relativeTime(difference) + output = relativeTime(difference); return withoutSuffix ? output : string.replace(/%s/i, output); }, @@ -411,11 +413,36 @@ }, isLeapYear : function () { - var year = this.date.getFullYear(); + var year = this._d.getFullYear(); return (year % 4 === 0 && year % 100 !== 0) || year % 400 === 0; } }; + // helper for adding shortcuts + function makeShortcut(name, key) { + _date.fn[name] = function (input) { + if (input) { + this._d['set' + key](input); + return this; + } else { + return this._d['get' + key](); + } + }; + } + + // loop through and add shortcuts + for (i = 0; i < shortcuts.length; i ++) { + makeShortcut(shortcuts[i].toLowerCase(), shortcuts[i]); + } + + // add shortcut for year (uses different syntax than the getter/setter 'year' == 'FullYear') + makeShortcut('year', 'FullYear'); + + // add shortcut for day (no setter) + _date.fn.day = function () { + return this._d.getDay(); + }; + // CommonJS module is defined if (typeof window === 'undefined' && typeof module !== 'undefined') { // Export module @@ -428,4 +455,4 @@ this._date = _date; } -}()); +})(Date); \ No newline at end of file diff --git a/underscore.date.min.js b/underscore.date.min.js index e12c73f7d..76e39f277 100755 --- a/underscore.date.min.js +++ b/underscore.date.min.js @@ -1,8 +1 @@ -// Underscore.date -// -// (c) 2011 Tim Wood -// Underscore.date is freely distributable under the terms of the MIT license. -// -// Version 0.6.1 - -(function(a){function f(a,b){var c=a+"";while(c.length11?"pm":"am";case"A":return i>11?"PM":"AM";case"H":return i;case"HH":return f(i,2);case"h":return i%12||12;case"hh":return f(i%12||12,2);case"m":return j;case"mm":return f(j,2);case"s":return k;case"ss":return f(k,2);case"zz":case"z":return(a.toString().match(n)||[""])[0].replace(m,"");default:return c.replace("\\","")}}var d=a.getMonth(),e=a.getDate(),g=a.getFullYear(),h=a.getDay(),i=a.getHours(),j=a.getMinutes(),k=a.getSeconds(),l=/(\\)?(Mo|MM?M?M?|Do|DDDo|DD?D?D?|dddd?|do?|w[o|w]?|YYYY|YY|a|A|hh?|HH?|mm?|ss?|zz?)/g,m=/[^A-Z]/g,n=/\([A-Za-z ]+\)|:[0-9]{2} [A-Z]{3} /g;return c.replace(l,o)}function k(a,b){function j(a,b){switch(a){case"M":case"MM":c[1]=~~b-1;break;case"D":case"DD":case"DDD":case"DDDD":c[2]=~~b;break;case"YY":b=~~b,c[0]=b+(b>70?1900:2e3);break;case"YYYY":c[0]=~~b;break;case"a":case"A":h=b.toLowerCase()==="pm";break;case"H":case"HH":case"h":case"hh":c[3]=~~b;break;case"m":case"mm":c[4]=~~b;break;case"s":case"ss":c[5]=~~b}}var c=[0],d=/[0-9a-zA-Z]+/g,e=a.match(d),f=b.match(d),g,h;for(g=0;g11?"pm":"am";case"A":return j>11?"PM":"AM";case"H":return j;case"HH":return i(j,2);case"h":return j%12||12;case"hh":return i(j%12||12,2);case"m":return k;case"mm":return i(k,2);case"s":return l;case"ss":return i(l,2);case"zz":case"z":return(b.toString().match(o)||[""])[0].replace(n,"");default:return d.replace("\\","")}}var e=b.getMonth(),f=b.getDate(),g=b.getFullYear(),h=b.getDay(),j=b.getHours(),k=b.getMinutes(),l=b.getSeconds(),m=/(\\)?(Mo|MM?M?M?|Do|DDDo|DD?D?D?|dddd?|do?|w[o|w]?|YYYY|YY|a|A|hh?|HH?|mm?|ss?|zz?)/g,n=/[^A-Z]/g,o=/\([A-Za-z ]+\)|:[0-9]{2} [A-Z]{3} /g;return d.replace(m,p)}function n(a,b){function i(a,b){switch(a){case"M":case"MM":c[1]=~~b-1;break;case"D":case"DD":case"DDD":case"DDDD":c[2]=~~b;break;case"YY":b=~~b,c[0]=b+(b>70?1900:2e3);break;case"YYYY":c[0]=~~b;break;case"a":case"A":h=b.toLowerCase()==="pm";break;case"H":case"HH":case"h":case"hh":c[3]=~~b;break;case"m":case"mm":c[4]=~~b;break;case"s":case"ss":c[5]=~~b}}var c=[0],d=/[0-9a-zA-Z]+/g,e=a.match(d),f=b.match(d),g,h;for(g=0;g