From: Iskren Chernev Date: Sun, 14 Jul 2013 07:14:05 +0000 (-0700) Subject: Added invalidAt function to get invalid unit index X-Git-Tag: 2.2.0~40^2~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=92f852f9f84973fc4452e4f8a7eabfeec14d7d7f;p=thirdparty%2Fmoment.git Added invalidAt function to get invalid unit index isValid is boolean, so its not very helpful in determining which section of a date is wrong. inavlidAt returns the index of the broken token, where 0 is year and 6 is millisecond. --- diff --git a/moment.js b/moment.js index c1673c127..4e29df475 100644 --- a/moment.js +++ b/moment.js @@ -1180,6 +1180,13 @@ return !!this._isValid; }, + invalidAt: function () { + var i, arr1 = this._a, arr2 = (this._isUTC ? moment.utc(this._a) : moment(this._a)).toArray(); + for (i = 6; i >= 0 && arr1[i] === arr2[i]; --i) + ; + return i; + }, + utc : function () { return this.zone(0); }, diff --git a/test/moment/is_valid.js b/test/moment/is_valid.js index a0e2b6028..ea1c4e1ca 100644 --- a/test/moment/is_valid.js +++ b/test/moment/is_valid.js @@ -160,5 +160,18 @@ exports.is_valid = { test.equal(moment.utc(tests[i]).isValid(), true, tests[i] + ' should be valid'); } test.done(); + }, + + "invalidAt" : function (test) { + test.expect(7); + test.equal(moment([2000, 12]).invalidAt(), 1, 'month 12 is invalid: 0-11'); + test.equal(moment([2000, 1, 30]).invalidAt(), 2, '30 is not a valid february day'); + test.equal(moment([2000, 1, 29, 24]).invalidAt(), 3, '24 is invalid hour'); + test.equal(moment([2000, 1, 29, 23, 60]).invalidAt(), 4, '60 is invalid minute'); + test.equal(moment([2000, 1, 29, 23, 59, 60]).invalidAt(), 5, '60 is invalid second'); + test.equal(moment([2000, 1, 29, 23, 59, 59, 1000]).invalidAt(), 6, '1000 is invalid millisecond'); + test.equal(moment([2000, 1, 29, 23, 59, 59, 999]).invalidAt(), -1, '-1 if everything is fine'); + test.done(); } + };