From: Tim Wood Date: Mon, 21 Mar 2011 16:38:56 +0000 (-0700) Subject: Trying to clean up the _ namespace. X-Git-Tag: 0.3.1~4 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=281b21258e49192d6b10d65269ebe46aa38ffcff;p=thirdparty%2Fmoment.git Trying to clean up the _ namespace. _ now only has 3 date methods, _date, _now, and _isLeapYear() Everything else is moved to a _.date() wrapper prototype --- diff --git a/lib/underscore.date.js b/lib/underscore.date.js index 2d873a969..82f270907 100644 --- a/lib/underscore.date.js +++ b/lib/underscore.date.js @@ -49,8 +49,9 @@ } // helper function for _.addTime and _.subtractTime - function dateAddRemove(input, self, adding){ - var ms = (input.ms || input[6] || 0) + + function dateAddRemove(_this, input, adding){ + var self = _this.date, + ms = (input.ms || input[6] || 0) + (input.s || input[5] || 0) * 1e3 + // 1000 (input.m || input[4] || 0) * 6e4 + // 1000 * 60 (input.h || input[3] || 0) * 36e5 + // 1000 * 60 * 60 @@ -68,7 +69,7 @@ self.setMonth(self.getMonth() + M * adding); self.setDate(Math.min(new Date(self.getFullYear(), self.getMonth() + 1, 0).getDate(), currentDate)); } - return self; + return _this; } // convert an array to a date. @@ -113,181 +114,203 @@ return wordsTimeAgo[string].replace(/%d/i, number || 1); } + function _Date(input) { + this.date = makeInputDate(input); + return this; + } + + // cache prototype for minification + _Date.prototype = new Date; + + _Date.prototype.format = function(inputString) { + // shortcuts to this and getting time functions + // done to save bytes in minification + var date = this.date, + currentMonth = date.getMonth(), + currentDate = date.getDate(), + currentYear = date.getFullYear(), + currentDay = date.getDay(), + currentHours = date.getHours(), + currentMinutes = date.getMinutes(), + currentSeconds = date.getSeconds(), + charactersToReplace = /(\\)?(Mo|MM?M?M?|Do|DDDo|DD?D?D?|dddd?|do?|w[o|w]?|YYYY|YY|a|A|hh?|HH?|mm?|ss?)/g; + // check if the character is a format + // return formatted string or non string. + // + // uses switch/case instead of an object of named functions (like http://phpjs.org/functions/date:380) + // for minification and performance + // see http://jsperf.com/object-of-functions-vs-switch for performance comparison + function replaceFunction(input) { + // create a couple variables to be used later inside one of the cases. + var a, b; + switch (input) { + // MONTH + case 'M' : + return currentMonth + 1; + case 'Mo' : + return (currentMonth + 1) + createOrdinal(currentMonth + 1); + case 'MM' : + return leftZeroFill(currentMonth + 1, 2); + case 'MMM' : + return wordsMonthsShort[currentMonth]; + case 'MMMM' : + return wordsMonths[currentMonth]; + // DAY OF MONTH + case 'D' : + return currentDate; + case 'Do' : + return currentDate + createOrdinal(currentDate); + case 'DD' : + return leftZeroFill(currentDate, 2); + // DAY OF YEAR + case 'DDD' : + a = new Date(currentYear, currentMonth, currentDate); + b = new Date(currentYear, 0, 1); + return ((a - b) / 864e5) + 1.5 | 0; + case 'DDDo' : + a = replaceFunction('DDD'); + return a + createOrdinal(a); + case 'DDDD' : + return leftZeroFill(replaceFunction('DDD'), 3); + // WEEKDAY + case 'd' : + return currentDay; + case 'do' : + return currentDay + createOrdinal(currentDay); + case 'ddd' : + return wordsWeekdaysShort[currentDay]; + case 'dddd' : + return wordsWeekdays[currentDay]; + // WEEK OF YEAR + case 'w' : + a = new Date(currentYear, currentMonth, currentDate - currentDay + 5); + b = new Date(a.getFullYear(), 0, 4); + return (a - b) / 864e5 / 7 + 1.5 | 0; + case 'wo' : + a = replaceFunction('w'); + return a + createOrdinal(a); + case 'ww' : + return leftZeroFill(replaceFunction('w'), 2); + // YEAR + case 'YY' : + return (currentYear + '').slice(-2); + case 'YYYY' : + return currentYear; + // AM / PM + case 'a' : + return currentHours > 11 ? 'pm' : 'am'; + case 'A' : + return currentHours > 11 ? 'PM' : 'AM'; + // 24 HOUR + case 'H' : + return currentHours; + case 'HH' : + return leftZeroFill(currentHours, 2); + // 12 HOUR + case 'h' : + return currentHours % 12 || 12; + case 'hh' : + return leftZeroFill(currentHours % 12 || 12, 2); + // MINUTE + case 'm' : + return currentMinutes; + case 'mm' : + return leftZeroFill(currentMinutes, 2); + // SECOND + case 's' : + return currentSeconds; + case 'ss' : + return leftZeroFill(currentSeconds, 2); + // DEFAULT + default : + return input.replace("\\", ""); + } + } + return inputString.replace(charactersToReplace, replaceFunction); + } + + _Date.prototype.add = function(input) { + return dateAddRemove(this, input, 1); + } + + _Date.prototype.subtract = function(input) { + return dateAddRemove(this, input, -1); + } + + _Date.prototype.customize = function(input) { + var inputWeekdays = input.weekdays, + inputWeekdaysShort = input.weekdaysShort, + inputMonths = input.months, + inputMonthsShort = input.monthsShort, + inputRelativeTime = input.relativeTime, + inputOrdinal = input.ordinal; + if (inputWeekdays && _.isArray(inputWeekdays) && inputWeekdays.length === 7) { + wordsWeekdays = inputWeekdays; + } + if (inputWeekdaysShort && _.isArray(inputWeekdaysShort) && inputWeekdaysShort.length === 7) { + wordsWeekdaysShort = inputWeekdaysShort; + } + if (inputMonths && _.isArray(inputMonths) && inputMonths.length === 12) { + wordsMonths = inputMonths; + } + if (inputMonthsShort && _.isArray(inputMonthsShort) && inputMonthsShort.length === 12) { + wordsMonthsShort = inputMonthsShort; + } + if (inputRelativeTime) { + _.extend(wordsTimeAgo, inputRelativeTime); + } + if (inputOrdinal && _.isFunction(inputOrdinal)) { + createOrdinal = inputOrdinal; + } + } + + function msApart(time, now) { + return makeInputMilliseconds(time) - makeInputMilliseconds(now); + } + + _Date.prototype.from = function(time, asMilliseconds) { + var difference = msApart(this.date, time), + string = difference < 0 ? wordsTimeAgo.past : wordsTimeAgo.future; + return asMilliseconds ? difference : string.replace(/%s/i, this.relative(difference)); + } + + _Date.prototype.fromNow = function(asMilliseconds) { + return this.from(_.now, asMilliseconds); + } + + _Date.prototype.isLeapYear = function() { + return _.isLeapYear(this.date.getFullYear()); + } + + _Date.prototype.relative = function(milliseconds) { + var seconds = Math.abs(makeInputMilliseconds(milliseconds)) / 1000, + minutes = seconds / 60, + hours = minutes / 60, + days = hours / 24, + years = days / 365; + return seconds < 45 && substituteTimeAgo('s', seconds | 0) || + seconds < 90 && substituteTimeAgo('m') || + minutes < 45 && substituteTimeAgo('mm', minutes | 0) || + minutes < 90 && substituteTimeAgo('h') || + hours < 24 && substituteTimeAgo('hh', hours | 0) || + hours < 48 && substituteTimeAgo('d') || + days < 30 && substituteTimeAgo('dd', days | 0) || + days < 60 && substituteTimeAgo('M') || + days < 350 && substituteTimeAgo('MM', days / 30 | 0) || + years < 2 && substituteTimeAgo('y') || + substituteTimeAgo('yy', years | 0); + } + // underscore mixins _d = { - addTime : function(date, input) { - return dateAddRemove(input, makeInputDate(date), 1); - }, - customizeDate : function(input) { - var inputWeekdays = input.weekdays, - inputWeekdaysShort = input.weekdaysShort, - inputMonths = input.months, - inputMonthsShort = input.monthsShort, - inputRelativeTime = input.relativeTime, - inputOrdinal = input.ordinal; - if (inputWeekdays && _.isArray(inputWeekdays) && inputWeekdays.length === 7) { - wordsWeekdays = inputWeekdays; - } - if (inputWeekdaysShort && _.isArray(inputWeekdaysShort) && inputWeekdaysShort.length === 7) { - wordsWeekdaysShort = inputWeekdaysShort; - } - if (inputMonths && _.isArray(inputMonths) && inputMonths.length === 12) { - wordsMonths = inputMonths; - } - if (inputMonthsShort && _.isArray(inputMonthsShort) && inputMonthsShort.length === 12) { - wordsMonthsShort = inputMonthsShort; - } - if (inputRelativeTime) { - _.extend(wordsTimeAgo, inputRelativeTime); - } - if (inputOrdinal && _.isFunction(inputOrdinal)) { - createOrdinal = inputOrdinal; - } - }, date : function(input) { - return makeInputDate(input); - }, - formatDate : function(_date, inputString) { - // shortcuts to this and getting time functions - // done to save bytes in minification - var date = makeInputDate(_date), - currentMonth = date.getMonth(), - currentDate = date.getDate(), - currentYear = date.getFullYear(), - currentDay = date.getDay(), - currentHours = date.getHours(), - currentMinutes = date.getMinutes(), - currentSeconds = date.getSeconds(), - charactersToReplace = /(\\)?(Mo|MM?M?M?|Do|DDDo|DD?D?D?|dddd?|do?|w[o|w]?|YYYY|YY|a|A|hh?|HH?|mm?|ss?)/g; - // check if the character is a format - // return formatted string or non string. - // - // uses switch/case instead of an object of named functions (like http://phpjs.org/functions/date:380) - // for minification and performance - // see http://jsperf.com/object-of-functions-vs-switch for performance comparison - function replaceFunction(input) { - // create a couple variables to be used later inside one of the cases. - var a, b; - switch (input) { - // MONTH - case 'M' : - return currentMonth + 1; - case 'Mo' : - return (currentMonth + 1) + createOrdinal(currentMonth + 1); - case 'MM' : - return leftZeroFill(currentMonth + 1, 2); - case 'MMM' : - return wordsMonthsShort[currentMonth]; - case 'MMMM' : - return wordsMonths[currentMonth]; - // DAY OF MONTH - case 'D' : - return currentDate; - case 'Do' : - return currentDate + createOrdinal(currentDate); - case 'DD' : - return leftZeroFill(currentDate, 2); - // DAY OF YEAR - case 'DDD' : - a = new Date(currentYear, currentMonth, currentDate); - b = new Date(currentYear, 0, 1); - return ((a - b) / 864e5) + 1.5 | 0; - case 'DDDo' : - a = replaceFunction('DDD'); - return a + createOrdinal(a); - case 'DDDD' : - return leftZeroFill(replaceFunction('DDD'), 3); - // WEEKDAY - case 'd' : - return currentDay; - case 'do' : - return currentDay + createOrdinal(currentDay); - case 'ddd' : - return wordsWeekdaysShort[currentDay]; - case 'dddd' : - return wordsWeekdays[currentDay]; - // WEEK OF YEAR - case 'w' : - a = new Date(currentYear, currentMonth, currentDate - currentDay + 5); - b = new Date(a.getFullYear(), 0, 4); - return (a - b) / 864e5 / 7 + 1.5 | 0; - case 'wo' : - a = replaceFunction('w'); - return a + createOrdinal(a); - case 'ww' : - return leftZeroFill(replaceFunction('w'), 2); - // YEAR - case 'YY' : - return (currentYear + '').slice(-2); - case 'YYYY' : - return currentYear; - // AM / PM - case 'a' : - return currentHours > 11 ? 'pm' : 'am'; - case 'A' : - return currentHours > 11 ? 'PM' : 'AM'; - // 24 HOUR - case 'H' : - return currentHours; - case 'HH' : - return leftZeroFill(currentHours, 2); - // 12 HOUR - case 'h' : - return currentHours % 12 || 12; - case 'hh' : - return leftZeroFill(currentHours % 12 || 12, 2); - // MINUTE - case 'm' : - return currentMinutes; - case 'mm' : - return leftZeroFill(currentMinutes, 2); - // SECOND - case 's' : - return currentSeconds; - case 'ss' : - return leftZeroFill(currentSeconds, 2); - // DEFAULT - default : - return input.replace("\\", ""); - } - } - return inputString.replace(charactersToReplace, replaceFunction); - }, - fromNow : function(time, now) { - var difference = _d.msApart(time, now), - string = difference < 0 ? wordsTimeAgo.past : wordsTimeAgo.future; - return string.replace(/%s/i, _d.relativeTime(difference)); - }, - isLeapYear : function(input) { - var year = !isNaN(input) && input < 1e4 ? input : makeInputDate(input).getFullYear(); - return (year % 4 === 0 && year % 100 !== 0) || year % 400 === 0; - }, - msApart : function(time, now) { - return makeInputMilliseconds(time) - makeInputMilliseconds(now); + return new _Date(input); }, now : function(asTimestamp) { return asTimestamp ? new Date().getTime() : new Date(); }, - relativeTime : function(milliseconds) { - var seconds = Math.abs(makeInputMilliseconds(milliseconds)) / 1000, - minutes = seconds / 60, - hours = minutes / 60, - days = hours / 24, - years = days / 365; - return seconds < 45 && substituteTimeAgo('s', seconds | 0) || - seconds < 90 && substituteTimeAgo('m') || - minutes < 45 && substituteTimeAgo('mm', minutes | 0) || - minutes < 90 && substituteTimeAgo('h') || - hours < 24 && substituteTimeAgo('hh', hours | 0) || - hours < 48 && substituteTimeAgo('d') || - days < 30 && substituteTimeAgo('dd', days | 0) || - days < 60 && substituteTimeAgo('M') || - days < 350 && substituteTimeAgo('MM', days / 30 | 0) || - years < 2 && substituteTimeAgo('y') || - substituteTimeAgo('yy', years | 0); - }, - subtractTime : function(date, input) { - return dateAddRemove(input, makeInputDate(date), -1); + isLeapYear : function(year) { + return (year % 4 === 0 && year % 100 !== 0) || year % 400 === 0; } }; @@ -295,4 +318,5 @@ if (_ && _.mixin) { _.mixin(_d); } + }(Date, _)); diff --git a/test/customizeDate.js b/test/customizeDate.js index 20d93c30a..1bdbb9dcf 100644 --- a/test/customizeDate.js +++ b/test/customizeDate.js @@ -2,7 +2,7 @@ $(function() { module("Customize"); - _.customizeDate({ + _.date().customize({ months : ["enero", "febrero", "marzo", "abril", "mayo", "junio", "julio", "agosto", "septiembre", "octubre", "noviembre", "diciembre"], monthsShort : ["ene", "feb", "mar", "abr", "may", "jun", "jul", "ago", "sep", "oct", "nov", "dic"], weekdays : ["domingo", "lunes", "martes", "miércoles", "jueves", "viernes", "sábado"], @@ -27,41 +27,41 @@ $(function() { } }); - test("_.formatDate()", 14, function() { - var dateTest = new Date(2010, 1, 14, 15, 25, 50, 125); - equal(_.formatDate([2010, 1, 14, 15, 25, 50, 125], "dddd, MMMM Do YYYY, h:mm:ss a"), "domingo, febrero 14o 2010, 3:25:50 pm"); - equal(_.formatDate([2010, 1, 14, 15, 25, 50, 125], "ddd, hA"), "dom, 3PM"); - equal(_.formatDate(dateTest, "M Mo MM MMMM MMM"), "2 2o 02 febrero feb"); - equal(_.formatDate(dateTest, "YYYY YY"), "2010 10"); - equal(_.formatDate(dateTest, "D Do DD"), "14 14o 14"); - equal(_.formatDate(dateTest, "d do dddd ddd"), "0 0o domingo dom"); - equal(_.formatDate(dateTest, "DDD DDDo DDDD"), "45 45o 045"); - equal(_.formatDate(dateTest, "w wo ww"), "8 8o 08"); - equal(_.formatDate(dateTest, "h hh"), "3 03"); - equal(_.formatDate(dateTest, "H HH"), "15 15"); - equal(_.formatDate(dateTest, "m mm"), "25 25"); - equal(_.formatDate(dateTest, "s ss"), "50 50"); - equal(_.formatDate(dateTest, "a A"), "pm PM"); - equal(_.formatDate(dateTest, "t\\he DDDo \\d\\ay of t\\he ye\\ar"), "the 45o day of the year"); + test("_date.format()", 14, function() { + var _date = _.date(new Date(2010, 1, 14, 15, 25, 50, 125)); + equal(_date.format("dddd, MMMM Do YYYY, h:mm:ss a"), "domingo, febrero 14o 2010, 3:25:50 pm"); + equal(_date.format("ddd, hA"), "dom, 3PM"); + equal(_date.format("M Mo MM MMMM MMM"), "2 2o 02 febrero feb"); + equal(_date.format("YYYY YY"), "2010 10"); + equal(_date.format("D Do DD"), "14 14o 14"); + equal(_date.format("d do dddd ddd"), "0 0o domingo dom"); + equal(_date.format("DDD DDDo DDDD"), "45 45o 045"); + equal(_date.format("w wo ww"), "8 8o 08"); + equal(_date.format("h hh"), "3 03"); + equal(_date.format("H HH"), "15 15"); + equal(_date.format("m mm"), "25 25"); + equal(_date.format("s ss"), "50 50"); + equal(_date.format("a A"), "pm PM"); + equal(_date.format("t\\he DDDo \\d\\ay of t\\he ye\\ar"), "the 45o day of the year"); }); - test("_.relativeTime()", 11, function() { - equal(_.relativeTime(1000 * 30), "seconds"); - equal(_.relativeTime(1000 * 60), "a minute"); - equal(_.relativeTime(1000 * 60 * 5), "5 minutes"); - equal(_.relativeTime(1000 * 60 * 60), "an hour"); - equal(_.relativeTime(1000 * 60 * 60 * 5), "5 hours"); - equal(_.relativeTime(1000 * 60 * 60 * 24), "a day"); - equal(_.relativeTime(1000 * 60 * 60 * 24 * 5), "5 days"); - equal(_.relativeTime(1000 * 60 * 60 * 24 * 30), "a month"); - equal(_.relativeTime(1000 * 60 * 60 * 24 * 30 * 5), "5 months"); - equal(_.relativeTime(1000 * 60 * 60 * 24 * 30 * 12), "a year"); - equal(_.relativeTime(1000 * 60 * 60 * 24 * 365 * 5), "5 years"); + test("_.date().relative()", 11, function() { + equal(_.date().relative(1000 * 30), "seconds"); + equal(_.date().relative(1000 * 60), "a minute"); + equal(_.date().relative(1000 * 60 * 5), "5 minutes"); + equal(_.date().relative(1000 * 60 * 60), "an hour"); + equal(_.date().relative(1000 * 60 * 60 * 5), "5 hours"); + equal(_.date().relative(1000 * 60 * 60 * 24), "a day"); + equal(_.date().relative(1000 * 60 * 60 * 24 * 5), "5 days"); + equal(_.date().relative(1000 * 60 * 60 * 24 * 30), "a month"); + equal(_.date().relative(1000 * 60 * 60 * 24 * 30 * 5), "5 months"); + equal(_.date().relative(1000 * 60 * 60 * 24 * 30 * 12), "a year"); + equal(_.date().relative(1000 * 60 * 60 * 24 * 365 * 5), "5 years"); }); test("_.fromNow()", 2, function() { - equal(_.fromNow(30000, 0), "seconds from now"); - equal(_.fromNow(0, 30000), "seconds ago"); + equal(_.date(30000).from(0), "seconds from now"); + equal(_.date(0).from(30000), "seconds ago"); }); }); diff --git a/test/date.js b/test/date.js index 8794e70ef..ab69fde78 100644 --- a/test/date.js +++ b/test/date.js @@ -4,23 +4,24 @@ $(function() { test("_.formatDate()", 14, function() { var dateTest = new Date(2010, 1, 14, 15, 25, 50, 125); + var _date = _.date(dateTest); - equal(_.formatDate([2010, 1, 14, 15, 25, 50, 125], "dddd, MMMM Do YYYY, h:mm:ss a"), "Sunday, February 14th 2010, 3:25:50 pm"); - equal(_.formatDate([2010, 1, 14, 15, 25, 50, 125], "ddd, hA"), "Sun, 3PM"); - equal(_.formatDate(dateTest, "M Mo MM MMMM MMM"), "2 2nd 02 February Feb"); - equal(_.formatDate(dateTest, "YYYY YY"), "2010 10"); - equal(_.formatDate(dateTest, "D Do DD"), "14 14th 14"); - equal(_.formatDate(dateTest, "d do dddd ddd"), "0 0th Sunday Sun"); - equal(_.formatDate(dateTest, "DDD DDDo DDDD"), "45 45th 045"); - equal(_.formatDate(dateTest, "w wo ww"), "8 8th 08"); - equal(_.formatDate(dateTest, "h hh"), "3 03"); - equal(_.formatDate(dateTest, "H HH"), "15 15"); - equal(_.formatDate(dateTest, "m mm"), "25 25"); - equal(_.formatDate(dateTest, "s ss"), "50 50"); - equal(_.formatDate(dateTest, "a A"), "pm PM"); - equal(_.formatDate(dateTest, "t\\he DDDo \\d\\ay of t\\he ye\\ar"), "the 45th day of the year"); + equal(_.date([2010, 1, 14, 15, 25, 50, 125]).format("dddd, MMMM Do YYYY, h:mm:ss a"), "Sunday, February 14th 2010, 3:25:50 pm"); + equal(_date.format("ddd, hA"), "Sun, 3PM"); + equal(_date.format("M Mo MM MMMM MMM"), "2 2nd 02 February Feb"); + equal(_date.format("YYYY YY"), "2010 10"); + equal(_date.format("D Do DD"), "14 14th 14"); + equal(_date.format("d do dddd ddd"), "0 0th Sunday Sun"); + equal(_date.format("DDD DDDo DDDD"), "45 45th 045"); + equal(_date.format("w wo ww"), "8 8th 08"); + equal(_date.format("h hh"), "3 03"); + equal(_date.format("H HH"), "15 15"); + equal(_date.format("m mm"), "25 25"); + equal(_date.format("s ss"), "50 50"); + equal(_date.format("a A"), "pm PM"); + equal(_date.format("t\\he DDDo \\d\\ay of t\\he ye\\ar"), "the 45th day of the year"); }); - + test("_.date()", 6, function() { ok(_.isDate(_.date([2010, 1, 12])), "[2010, 1, 12]"); ok(_.isDate(_.date([2010, 1, 12, 1])), "[2010, 1, 12, 1]"); @@ -30,44 +31,43 @@ $(function() { deepEqual(_.date(new Date(2010, 1, 14, 15, 25, 50, 125)), _.date([2010, 1, 14, 15, 25, 50, 125]), "native date constructor === _.date([]);"); }); - test("_.addTime() + _.subtractTime()", 5, function() { - equal(_([2010, 1, 14, 15, 25, 50, 125]).chain().date().addTime({ms:200,s:10,m:10,h:2,d:3,M:2,y:3}).formatDate("MMMM Do YYYY, h:mm:ss a").value(), "April 17th 2013, 5:36:00 pm"); - equal(_([2010, 0, 31]).chain().date().formatDate("MMMM Do YYYY").value(), "January 31st 2010"); - equal(_([2010, 0, 31]).chain().date().addTime({M:1}).formatDate("MMMM Do YYYY").value(), "February 28th 2010"); - equal(_([2007, 1, 28]).chain().date().formatDate("MMMM Do YYYY").value(), "February 28th 2007"); - equal(_([2007, 1, 28]).chain().date().subtractTime({M:1}).formatDate("MMMM Do YYYY").value(), "January 28th 2007"); + equal(_([2010, 1, 14, 15, 25, 50, 125]).date().add({ms:200,s:10,m:10,h:2,d:3,M:2,y:3}).format("MMMM Do YYYY, h:mm:ss a"), "April 17th 2013, 5:36:00 pm"); + equal(_([2010, 0, 31]).date().format("MMMM Do YYYY"), "January 31st 2010"); + equal(_([2010, 0, 31]).date().add({M:1}).format("MMMM Do YYYY"), "February 28th 2010"); + equal(_([2007, 1, 28]).date().format("MMMM Do YYYY"), "February 28th 2007"); + equal(_([2007, 1, 28]).date().subtract({M:1}).format("MMMM Do YYYY"), "January 28th 2007"); }); test("_.relativeTime()", 11, function() { - equal(_.relativeTime(1000 * 30), "less than a minute"); - equal(_.relativeTime(1000 * 60), "about a minute"); - equal(_.relativeTime(1000 * 60 * 5), "5 minutes"); - equal(_.relativeTime(1000 * 60 * 60), "about an hour"); - equal(_.relativeTime(1000 * 60 * 60 * 5), "about 5 hours"); - equal(_.relativeTime(1000 * 60 * 60 * 24), "a day"); - equal(_.relativeTime(1000 * 60 * 60 * 24 * 5), "5 days"); - equal(_.relativeTime(1000 * 60 * 60 * 24 * 30), "about a month"); - equal(_.relativeTime(1000 * 60 * 60 * 24 * 30 * 5), "5 months"); - equal(_.relativeTime(1000 * 60 * 60 * 24 * 30 * 12), "about a year"); - equal(_.relativeTime(1000 * 60 * 60 * 24 * 365 * 5), "5 years"); + equal(_.date().relative(1000 * 30), "less than a minute"); + equal(_.date().relative(1000 * 60), "about a minute"); + equal(_.date().relative(1000 * 60 * 5), "5 minutes"); + equal(_.date().relative(1000 * 60 * 60), "about an hour"); + equal(_.date().relative(1000 * 60 * 60 * 5), "about 5 hours"); + equal(_.date().relative(1000 * 60 * 60 * 24), "a day"); + equal(_.date().relative(1000 * 60 * 60 * 24 * 5), "5 days"); + equal(_.date().relative(1000 * 60 * 60 * 24 * 30), "about a month"); + equal(_.date().relative(1000 * 60 * 60 * 24 * 30 * 5), "5 months"); + equal(_.date().relative(1000 * 60 * 60 * 24 * 30 * 12), "about a year"); + equal(_.date().relative(1000 * 60 * 60 * 24 * 365 * 5), "5 years"); }); test("_.msApart()", 5, function() { - equal(_.msApart(1000, 0), 1000, "1 second (ms) - 0 = 1000"); - equal(_.msApart(1000, 500), 500, "1 second (ms) - .5 second (ms) = -500"); - equal(_.msApart(0, 1000), -1000, "0 - 1 second (ms) = -1000"); - equal(_.msApart(new Date(1000), 1000), 0, "1 second (date) - 1 second (ms) = 0"); + equal(_.date(1000).from(0, true), 1000, "1 second (ms) - 0 = 1000"); + equal(_.date(1000).from(500, true), 500, "1 second (ms) - .5 second (ms) = -500"); + equal(_.date(0).from(1000, true), -1000, "0 - 1 second (ms) = -1000"); + equal(_.date(new Date(1000)).from(1000, true), 0, "1 second (date) - 1 second (ms) = 0"); var oneHourDate = new Date(), nowDate = new Date(); oneHourDate.setHours(oneHourDate.getHours() + 1); - equal(_.msApart(oneHourDate, nowDate), 60 * 60 * 1000, "1 hour from now = 360000"); + equal(_.date(oneHourDate).from(nowDate, true), 60 * 60 * 1000, "1 hour from now = 360000"); }); test("_.fromNow()", 2, function() { - equal(_.fromNow(30000, 0), "in less than a minute"); - equal(_.fromNow(0, 30000), "less than a minute ago"); + equal(_.date(30000).from(0), "in less than a minute"); + equal(_.date(0).from(30000), "less than a minute ago"); }); test("_.isLeapYear()", function() { @@ -76,9 +76,8 @@ $(function() { equal(_.isLeapYear(2100), false); equal(_.isLeapYear(2008), true); equal(_.isLeapYear(2000), true); - equal(_.isLeapYear([2100, 0, 1]), false); - equal(_.isLeapYear(new Date(2008, 0, 1)), true); - equal(_.isLeapYear(new Date(2000, 0, 1)), true); + equal(_.date([2100, 0, 1]).isLeapYear(), false); + equal(_.date(new Date(2008, 0, 1)).isLeapYear(), true); + equal(_.date(new Date(2000, 0, 1)).isLeapYear(), true); }); - }); \ No newline at end of file diff --git a/test/speed.js b/test/speed.js index 802f08e3f..671e693f9 100644 --- a/test/speed.js +++ b/test/speed.js @@ -5,30 +5,14 @@ rt1 = 1000 * 60 * 60 * 24 * 365 * 5, rt2 = 1000 * 30; - JSLitmus.test('_.relativeTime(1000 * 30)', function() { - return _.relativeTime(rt2); + JSLitmus.test('_.date().relative(1000 * 30)', function() { + return _.date().relative(rt2); }); - JSLitmus.test('_.relativeTime(1000 * 60 * 60 * 24 * 365 * 5)', function() { - return _.relativeTime(rt1); + JSLitmus.test('_.date().relative(1000 * 60 * 60 * 24 * 365 * 5)', function() { + return _.date().relative(rt1); }); - JSLitmus.test('_.msApart(1000)', function() { - return _.msApart(1000); - }); - - JSLitmus.test('_.msApart(date1, date2)', function() { - return _.msApart(date1, date2); - }); - - JSLitmus.test('_.msApart(date1, 1000)', function() { - return _.msApart(date1, 1000); - }); - - JSLitmus.test('_.msApart(1000, 1000)', function() { - return _.msApart(1000, 1000); - }); - JSLitmus.test('_.date(date1)', function() { return _.date(date1); });