]> git.ipfire.org Git - thirdparty/moment.git/commitdiff
[bugfix] Ignore enumerable Object.prototype extensions (#4953)
authorAsh <ash@hexmen.com>
Fri, 24 Apr 2020 11:40:00 +0000 (12:40 +0100)
committerGitHub <noreply@github.com>
Fri, 24 Apr 2020 11:40:00 +0000 (14:40 +0300)
* [bugfix] Ignore enumerable Object.prototype extensions

* Waste time with jshint

* Waste time with jscs

Co-authored-by: Iskren Ivov Chernev <iskren.chernev@gmail.com>
src/lib/duration/valid.js
src/lib/locale/set.js
src/lib/units/priorities.js
src/lib/utils/deprecate.js
src/test/helpers/common-locale.js
src/test/helpers/each-own-prop.js [new file with mode: 0644]
src/test/moment/create.js
src/test/moment/format.js
src/test/moment/is_valid.js

index 033fd5b0f04491f3ecaf36afea22564f527eac43..7c57c64c20fe9d583f9ddd8e1eac4964f6ade492 100644 (file)
@@ -1,3 +1,4 @@
+import hasOwnProp from '../utils/has-own-prop';
 import toInt from '../utils/to-int';
 import indexOf from '../utils/index-of';
 import {Duration} from './constructor';
@@ -7,7 +8,7 @@ var ordering = ['year', 'quarter', 'month', 'week', 'day', 'hour', 'minute', 'se
 
 export default function isDurationValid(m) {
     for (var key in m) {
-        if (!(indexOf.call(ordering, key) !== -1 && (m[key] == null || !isNaN(m[key])))) {
+        if (hasOwnProp(m, key) && !(indexOf.call(ordering, key) !== -1 && (m[key] == null || !isNaN(m[key])))) {
             return false;
         }
     }
index c63d5adec10e340b35738c183d1df32e7b7863f8..f0e48ca3b4ddd15ca174c5050aa5dab647e46e19 100644 (file)
@@ -6,11 +6,13 @@ import hasOwnProp from '../utils/has-own-prop';
 export function set (config) {
     var prop, i;
     for (i in config) {
-        prop = config[i];
-        if (isFunction(prop)) {
-            this[i] = prop;
-        } else {
-            this['_' + i] = prop;
+        if (hasOwnProp(config, i)) {
+            prop = config[i];
+            if (isFunction(prop)) {
+                this[i] = prop;
+            } else {
+                this['_' + i] = prop;
+            }
         }
     }
     this._config = config;
index 699017cb80f22b5df9f98ed2f0a16609888e0a6a..af227d3618878b3bbf45bb3fe1c4b78db8262d6f 100644 (file)
@@ -1,3 +1,5 @@
+import hasOwnProp from '../utils/has-own-prop';
+
 var priorities = {};
 
 export function addUnitPriority(unit, priority) {
@@ -7,7 +9,9 @@ export function addUnitPriority(unit, priority) {
 export function getPrioritizedUnits(unitsObj) {
     var units = [];
     for (var u in unitsObj) {
-        units.push({unit: u, priority: priorities[u]});
+        if (hasOwnProp(unitsObj, u)) {
+            units.push({unit: u, priority: priorities[u]});
+        }
     }
     units.sort(function (a, b) {
         return a.priority - b.priority;
index d1a6dc65fb99d2563386d7dcfddc4fdb58814ca7..8fbd127cf3b21d6ca38eae28d4e7462ab5bea8c7 100644 (file)
@@ -1,5 +1,6 @@
 import extend from './extend';
 import { hooks } from './hooks';
+import hasOwnProp from './has-own-prop';
 
 function warn(msg) {
     if (hooks.suppressDeprecationWarnings === false &&
@@ -23,7 +24,9 @@ export function deprecate(msg, fn) {
                 if (typeof arguments[i] === 'object') {
                     arg += '\n[' + i + '] ';
                     for (var key in arguments[0]) {
-                        arg += key + ': ' + arguments[0][key] + ', ';
+                        if (hasOwnProp(arguments[0], key)) {
+                            arg += key + ': ' + arguments[0][key] + ', ';
+                        }
                     }
                     arg = arg.slice(0, -2); // Remove trailing comma and space
                 } else {
index 08709d5dfabeb8d9b8f986ac921c26b0af79f246..c17a875e615977e9f0b4a45cc965912349529cd1 100644 (file)
@@ -1,8 +1,6 @@
 import { test } from '../qunit';
-import each from './each';
-import objectKeys from './object-keys';
+import eachOwnProp from './each-own-prop';
 import moment from '../../moment';
-import defaults from '../../lib/utils/defaults';
 
 export function defineCommonLocaleTests(locale, options) {
     test('lenient day of month ordinal parsing', function (assert) {
@@ -54,13 +52,11 @@ export function defineCommonLocaleTests(locale, options) {
     });
 
     test('date format correctness', function (assert) {
-        var data, tokens;
-        data = moment.localeData()._longDateFormat;
-        tokens = objectKeys(data);
-        each(tokens, function (srchToken) {
+        var data = moment.localeData()._longDateFormat;
+        eachOwnProp(data, function (srchToken) {
             // Check each format string to make sure it does not contain any
             // tokens that need to be expanded.
-            each(tokens, function (baseToken) {
+            eachOwnProp(data, function (baseToken) {
                 // strip escaped sequences
                 var format = data[baseToken].replace(/(\[[^\]]*\])/g, '');
                 assert.equal(false, !!~format.indexOf(srchToken),
diff --git a/src/test/helpers/each-own-prop.js b/src/test/helpers/each-own-prop.js
new file mode 100644 (file)
index 0000000..f6b4693
--- /dev/null
@@ -0,0 +1,8 @@
+import each from './each';
+import objectKeys from './object-keys';
+
+function eachOwnProp(object, callback) {
+    each(objectKeys(object), callback);
+}
+
+export default eachOwnProp;
index 85321d41faa4d8471cbde10d941c75f3106eda9b..956e6d1a33d0050736a038cb7b8efddce06dffb0 100644 (file)
@@ -1,4 +1,5 @@
 import { module, test } from '../qunit';
+import eachOwnProp from '../helpers/each-own-prop';
 import moment from '../../moment';
 
 module('create');
@@ -488,17 +489,15 @@ test('parsing RFC 2822', function (assert) {
         'Mon, 02 Jan 2017 06:00:00 EDT': [2017, 0, 2, 6, 0, 0, -4 * 60]
     };
 
-    var inp, tokens, parseResult, expResult;
-
-    for (inp in testCases) {
-        tokens = testCases[inp];
-        parseResult = moment(inp, moment.RFC_2822, true).parseZone();
-        expResult = moment.utc(tokens.slice(0, 6)).utcOffset(tokens[6], true);
+    eachOwnProp(testCases, function (inp) {
+        var tokens = testCases[inp],
+            parseResult = moment(inp, moment.RFC_2822, true).parseZone(),
+            expResult = moment.utc(tokens.slice(0, 6)).utcOffset(tokens[6], true);
         assert.ok(parseResult.isValid(), inp);
         assert.ok(parseResult.parsingFlags().rfc2822, inp + ' - rfc2822 parsingFlag');
         assert.equal(parseResult.utcOffset(), expResult.utcOffset(), inp + ' - zone');
         assert.equal(parseResult.valueOf(), expResult.valueOf(), inp + ' - correctness');
-    }
+    });
 });
 
 test('non RFC 2822 strings', function (assert) {
@@ -506,13 +505,12 @@ test('non RFC 2822 strings', function (assert) {
         'RFC2822 datetime with all options but invalid day delimiter': 'Tue. 01 Nov 2016 01:23:45 GMT',
         'RFC2822 datetime with mismatching Day (weekday v date)': 'Mon, 01 Nov 2016 01:23:45 GMT'
     };
-    var testCase;
 
-    for (testCase in testCases) {
+    eachOwnProp(testCases, function (testCase) {
         var testResult = moment(testCases[testCase], moment.RFC_2822, true);
         assert.ok(!testResult.isValid(), testCase + ': ' + testResult + ' - is invalid rfc2822');
         assert.ok(!testResult.parsingFlags().rfc2822, testCase + ': ' + testResult + ' - rfc2822 parsingFlag');
-    }
+    });
 });
 
 test('parsing RFC 2822 in a different locale', function (assert) {
@@ -525,15 +523,14 @@ test('parsing RFC 2822 in a different locale', function (assert) {
         'clean RFC2822 datetime with single-digit day-of-month': 'Tue, 1 Nov 2016 06:23:45 GMT',
         'RFC2822 datetime with CFWSs': '(Init Comment) Tue,\n 1 Nov              2016 (Split\n Comment)  07:23:45 +0000 (GMT)'
     };
-    var testCase;
 
     try {
         moment.locale('ru');
-        for (testCase in testCases) {
+        eachOwnProp(testCases, function (testCase) {
             var testResult = moment(testCases[testCase], moment.RFC_2822, true);
             assert.ok(testResult.isValid(), testResult);
             assert.ok(testResult.parsingFlags().rfc2822, testResult + ' - rfc2822 parsingFlag');
-        }
+        });
     }
     finally {
         moment.locale('en');
@@ -545,15 +542,14 @@ test('non RFC 2822 strings in a different locale', function (assert) {
         'RFC2822 datetime with all options but invalid day delimiter': 'Tue. 01 Nov 2016 01:23:45 GMT',
         'RFC2822 datetime with mismatching Day (week v date)': 'Mon, 01 Nov 2016 01:23:45 GMT'
     };
-    var testCase;
 
     try {
         moment.locale('ru');
-        for (testCase in testCases) {
+        eachOwnProp(testCases, function (testCase) {
             var testResult = moment(testCases[testCase], moment.RFC_2822, true);
             assert.ok(!testResult.isValid(), testResult);
             assert.ok(!testResult.parsingFlags().rfc2822, testResult + ' - rfc2822 parsingFlag');
-        }
+        });
     }
     finally {
         moment.locale('en');
index c2a507d79c52270aa24f983c11b66cee0bafbb4c..c3ca663d95a0dc90800f008243465ce245f0020e 100644 (file)
@@ -1,5 +1,6 @@
 import { module, test } from '../qunit';
 import each from '../helpers/each';
+import eachOwnProp from '../helpers/each-own-prop';
 import moment from '../../moment';
 
 module('format');
@@ -275,16 +276,17 @@ test('iso week formats', function (assert) {
         '2010-01-03': '2009-53',
         '404-12-31': '0404-53',
         '405-12-31': '0405-52'
-    }, i, isoWeek, formatted2, formatted1;
+    };
 
-    for (i in cases) {
+    eachOwnProp(cases, function (i) {
+        var isoWeek, formatted2, formatted1;
         isoWeek = cases[i].split('-').pop();
         formatted2 = moment(i, 'YYYY-MM-DD').format('WW');
         assert.equal(isoWeek, formatted2, i + ': WW should be ' + isoWeek + ', but ' + formatted2);
         isoWeek = isoWeek.replace(/^0+/, '');
         formatted1 = moment(i, 'YYYY-MM-DD').format('W');
         assert.equal(isoWeek, formatted1, i + ': W should be ' + isoWeek + ', but ' + formatted1);
-    }
+    });
 });
 
 test('iso week year formats', function (assert) {
@@ -307,9 +309,10 @@ test('iso week year formats', function (assert) {
         '2010-01-03': '2009-53',
         '404-12-31': '0404-53',
         '405-12-31': '0405-52'
-    }, i, isoWeekYear, formatted5, formatted4, formatted2;
+    };
 
-    for (i in cases) {
+    eachOwnProp(cases, function (i) {
+        var isoWeekYear, formatted5, formatted4, formatted2;
         isoWeekYear = cases[i].split('-')[0];
         formatted5 = moment(i, 'YYYY-MM-DD').format('GGGGG');
         assert.equal('0' + isoWeekYear, formatted5, i + ': GGGGG should be ' + isoWeekYear + ', but ' + formatted5);
@@ -317,7 +320,7 @@ test('iso week year formats', function (assert) {
         assert.equal(isoWeekYear, formatted4, i + ': GGGG should be ' + isoWeekYear + ', but ' + formatted4);
         formatted2 = moment(i, 'YYYY-MM-DD').format('GG');
         assert.equal(isoWeekYear.slice(2, 4), formatted2, i + ': GG should be ' + isoWeekYear + ', but ' + formatted2);
-    }
+    });
 });
 
 test('week year formats', function (assert) {
@@ -340,11 +343,12 @@ test('week year formats', function (assert) {
         '2010-01-03': '2009-53',
         '404-12-31': '0404-53',
         '405-12-31': '0405-52'
-    }, i, isoWeekYear, formatted5, formatted4, formatted2;
+    };
 
     moment.defineLocale('dow:1,doy:4', {week: {dow: 1, doy: 4}});
 
-    for (i in cases) {
+    eachOwnProp(cases, function (i) {
+        var isoWeekYear, formatted5, formatted4, formatted2;
         isoWeekYear = cases[i].split('-')[0];
         formatted5 = moment(i, 'YYYY-MM-DD').format('ggggg');
         assert.equal('0' + isoWeekYear, formatted5, i + ': ggggg should be ' + isoWeekYear + ', but ' + formatted5);
@@ -352,7 +356,7 @@ test('week year formats', function (assert) {
         assert.equal(isoWeekYear, formatted4, i + ': gggg should be ' + isoWeekYear + ', but ' + formatted4);
         formatted2 = moment(i, 'YYYY-MM-DD').format('gg');
         assert.equal(isoWeekYear.slice(2, 4), formatted2, i + ': gg should be ' + isoWeekYear + ', but ' + formatted2);
-    }
+    });
     moment.defineLocale('dow:1,doy:4', null);
 });
 
@@ -445,20 +449,6 @@ test('quarter ordinal formats', function (assert) {
 });
 
 // test('full expanded format is returned from abbreviated formats', function (assert) {
-//     function objectKeys(obj) {
-//         if (Object.keys) {
-//             return Object.keys(obj);
-//         } else {
-//             // IE8
-//             var res = [], i;
-//             for (i in obj) {
-//                 if (obj.hasOwnProperty(i)) {
-//                     res.push(i);
-//                 }
-//             }
-//             return res;
-//         }
-//     }
 
 //     var locales =
 //         'ar-sa ar-tn ar az be bg bn bo br bs ca cs cv cy da de-at de dv el ' +
@@ -468,10 +458,8 @@ test('quarter ordinal formats', function (assert) {
 //         'sr sv sw ta te th tl-ph tlh tr tzl tzm-latn tzm uk uz vi zh-cn zh-tw';
 
 //     each(locales.split(' '), function (locale) {
-//         var data, tokens;
-//         data = moment().locale(locale).localeData()._longDateFormat;
-//         tokens = objectKeys(data);
-//         each(tokens, function (token) {
+//         var data = moment().locale(locale).localeData()._longDateFormat;
+//         eachOwnProp(data, function (token) {
 //             // Check each format string to make sure it does not contain any
 //             // tokens that need to be expanded.
 //             each(tokens, function (i) {
index 85bbd0c3a8cc07f0cd0ae062b0247b09b2c22e61..5288b4f1a0814e307bdbc4e82524d07742f091a0 100644 (file)
@@ -1,4 +1,5 @@
 import { module, test } from '../qunit';
+import each from '../helpers/each';
 import moment from '../../moment';
 
 module('is valid');
@@ -27,13 +28,11 @@ test('array bad date', function (assert) {
         moment([2100, 0, 32]),
         moment.utc([2010, 0, 0]),
         moment.utc([2100, 0, 32])
-    ],
-    i, m;
+    ];
 
-    for (i in tests) {
-        m = tests[i];
+    each(tests, function (m) {
         assert.equal(m.isValid(), false);
-    }
+    });
 });
 
 test('h/hh with hour > 12', function (assert) {