From: Ash Date: Fri, 24 Apr 2020 11:40:00 +0000 (+0100) Subject: [bugfix] Ignore enumerable Object.prototype extensions (#4953) X-Git-Tag: 2.25.0~70 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=f3d4e1e00d2d147b95ea3682306c19e5182b793f;p=thirdparty%2Fmoment.git [bugfix] Ignore enumerable Object.prototype extensions (#4953) * [bugfix] Ignore enumerable Object.prototype extensions * Waste time with jshint * Waste time with jscs Co-authored-by: Iskren Ivov Chernev --- diff --git a/src/lib/duration/valid.js b/src/lib/duration/valid.js index 033fd5b0f..7c57c64c2 100644 --- a/src/lib/duration/valid.js +++ b/src/lib/duration/valid.js @@ -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; } } diff --git a/src/lib/locale/set.js b/src/lib/locale/set.js index c63d5adec..f0e48ca3b 100644 --- a/src/lib/locale/set.js +++ b/src/lib/locale/set.js @@ -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; diff --git a/src/lib/units/priorities.js b/src/lib/units/priorities.js index 699017cb8..af227d361 100644 --- a/src/lib/units/priorities.js +++ b/src/lib/units/priorities.js @@ -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; diff --git a/src/lib/utils/deprecate.js b/src/lib/utils/deprecate.js index d1a6dc65f..8fbd127cf 100644 --- a/src/lib/utils/deprecate.js +++ b/src/lib/utils/deprecate.js @@ -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 { diff --git a/src/test/helpers/common-locale.js b/src/test/helpers/common-locale.js index 08709d5df..c17a875e6 100644 --- a/src/test/helpers/common-locale.js +++ b/src/test/helpers/common-locale.js @@ -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 index 000000000..f6b4693fb --- /dev/null +++ b/src/test/helpers/each-own-prop.js @@ -0,0 +1,8 @@ +import each from './each'; +import objectKeys from './object-keys'; + +function eachOwnProp(object, callback) { + each(objectKeys(object), callback); +} + +export default eachOwnProp; diff --git a/src/test/moment/create.js b/src/test/moment/create.js index 85321d41f..956e6d1a3 100644 --- a/src/test/moment/create.js +++ b/src/test/moment/create.js @@ -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'); diff --git a/src/test/moment/format.js b/src/test/moment/format.js index c2a507d79..c3ca663d9 100644 --- a/src/test/moment/format.js +++ b/src/test/moment/format.js @@ -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) { diff --git a/src/test/moment/is_valid.js b/src/test/moment/is_valid.js index 85bbd0c3a..5288b4f1a 100644 --- a/src/test/moment/is_valid.js +++ b/src/test/moment/is_valid.js @@ -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) {