]> git.ipfire.org Git - thirdparty/moment.git/commitdiff
Fix #2300: Default to current week.
authorBrian Schemp <bschemp@gmail.com>
Tue, 30 Aug 2016 04:09:52 +0000 (18:09 -1000)
committerIskren Chernev <iskren.chernev@gmail.com>
Sun, 6 Nov 2016 09:35:19 +0000 (01:35 -0800)
This update to Brian Schemp's patch calculates the current week once
instead of twice for a small performance boost.

src/lib/create/from-array.js
src/test/moment/create.js

index 25dbc28fc6759fad58ec2d6b192741488560ad2b..3ae35a0bdd2eabe1ea0e555642f1741d868d5304 100644 (file)
@@ -104,8 +104,12 @@ function dayOfYearFromWeekInfo(config) {
         dow = config._locale._week.dow;
         doy = config._locale._week.doy;
 
-        weekYear = defaults(w.gg, config._a[YEAR], weekOfYear(createLocal(), dow, doy).year);
-        week = defaults(w.w, 1);
+        var curWeek = weekOfYear(createLocal(), dow, doy);
+
+        weekYear = defaults(w.gg, config._a[YEAR], curWeek.year);
+
+        // Default to current week.
+        week = defaults(w.w, curWeek.week);
 
         if (w.d != null) {
             // weekday -- low day numbers are considered next week
index f5f2e6fc0c44b084115feac0644f03bbf1526bf9..9c509f0fd63d5230cc9d38ac544611f59e2c6d39 100644 (file)
@@ -359,6 +359,10 @@ test('string with timezone around start of year', function (assert) {
 });
 
 test('string with array of formats', function (assert) {
+    var thursdayForCurrentWeek = moment()
+      .day(4)
+      .format('YYYY MM DD');
+
     assert.equal(moment('11-02-1999', ['MM-DD-YYYY', 'DD-MM-YYYY']).format('MM DD YYYY'), '11 02 1999', 'switching month and day');
     assert.equal(moment('02-11-1999', ['MM/DD/YYYY', 'YYYY MM DD', 'MM-DD-YYYY']).format('MM DD YYYY'), '02 11 1999', 'year last');
     assert.equal(moment('1999-02-11', ['MM/DD/YYYY', 'YYYY MM DD', 'MM-DD-YYYY']).format('MM DD YYYY'), '02 11 1999', 'year first');
@@ -393,6 +397,8 @@ test('string with array of formats', function (assert) {
     assert.equal(moment('13-10-1998', ['DD MM YY', 'DD MM YYYY'])._f, 'DD MM YYYY', 'use four digit year');
 
     assert.equal(moment('01', ['MM', 'DD'])._f, 'MM', 'Should use first valid format');
+
+    assert.equal(moment('Thursday 8:30pm', ['dddd h:mma']).format('YYYY MM DD dddd h:mma'), thursdayForCurrentWeek + ' Thursday 8:30pm', 'Default to current week');
 });
 
 test('string with array of formats + ISO', function (assert) {
@@ -897,13 +903,21 @@ function getVerifier(test) {
 
 test('parsing week and weekday information', function (assert) {
     var ver = getVerifier(assert);
+    var currentWeekOfYear = moment().weeks();
+    var expectedDate2012 = moment([2012, 0, 1])
+      .day(0)
+      .add((currentWeekOfYear - 1), 'weeks')
+      .format('YYYY MM DD');
+    var expectedDate1999 = moment([1999, 0, 1])
+      .day(0)
+      .add((currentWeekOfYear - 1), 'weeks')
+      .format('YYYY MM DD');
 
     // year
-    ver('12', 'gg', '2012 01 01', 'week-year two digits');
-    ver('2012', 'gggg', '2012 01 01', 'week-year four digits');
-
-    ver('99', 'gg', '1998 12 27', 'week-year two digits previous year');
-    ver('1999', 'gggg', '1998 12 27', 'week-year four digits previous year');
+    ver('12', 'gg', expectedDate2012, 'week-year two digits');
+    ver('2012', 'gggg', expectedDate2012, 'week-year four digits');
+    ver('99', 'gg', expectedDate1999, 'week-year two digits previous year');
+    ver('1999', 'gggg', expectedDate1999, 'week-year four digits previous year');
 
     ver('99', 'GG', '1999 01 04', 'iso week-year two digits');
     ver('1999', 'GGGG', '1999 01 04', 'iso week-year four digits');