Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feature] Ignore NaN values in setters #3735

Merged
3 changes: 1 addition & 2 deletions src/lib/moment/get-set.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { getPrioritizedUnits } from '../units/priorities';
import { hooks } from '../utils/hooks';
import isFunction from '../utils/is-function';


export function makeGetSet (unit, keepTime) {
return function (value) {
if (value != null) {
Expand All @@ -22,7 +21,7 @@ export function get (mom, unit) {
}

export function set (mom, unit, value) {
if (mom.isValid()) {
if (mom.isValid() && !isNaN(value)) {
mom._d['set' + (mom._isUTC ? 'UTC' : '') + unit](value);
}
}
Expand Down
6 changes: 6 additions & 0 deletions src/lib/utils/is-numeric.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export default function isNumeric(val) {
var _val = +val;
return (val !== val + 1) && //infinity check
(_val === +val) && //Cute coercion check
(typeof val !== 'object'); //Array/object check
}
29 changes: 29 additions & 0 deletions src/test/moment/getters_setters.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,35 @@ test('setters', function (assert) {
assert.equal(a.month(), 3, 'month edge case');
});

test('setters should handle garbage input', function (assert) {
var a = moment();
a.set('year', 2011);
a.set('month', 9);
a.set('date', 12);
a.set('hours', 6);
a.set('minutes', 7);
a.set('seconds', 8);
a.set('milliseconds', 9);

a.year(undefined);
a.month('foo');
a.date(null);
a.day({a:2,b:3});
a.hours('[1]');
a.minutes(undefined);
a.seconds(null);
a.milliseconds(NaN);

assert.equal(a.year(), 2011, 'year - provided undefined');
assert.equal(a.month(), 9, 'month - provided null');
assert.equal(a.date(), 12, 'date - provided [1]');
assert.equal(a.day(), 3, 'day - provided Infinity');
assert.equal(a.hours(), 6, 'hour - provided new Date');
assert.equal(a.minutes(), 7, 'minute - provided {a:1,b:2}');
assert.equal(a.seconds(), 8, 'second - provided foo');
assert.equal(a.milliseconds(), 9, 'milliseconds - provided Infinity');
});

test('setter programmatic', function (assert) {
var a = moment();
a.set('year', 2011);
Expand Down
26 changes: 26 additions & 0 deletions src/test/moment/is_numeric.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { module, test } from '../qunit';
import isNumeric from '../../lib/utils/is-numeric.js';

test('isNumeric recognizes numeric things', function (assert) {
assert.ok(isNumeric(1), 'simple integer');
assert.ok(isNumeric(0), 'simple number');
assert.ok(isNumeric(-0), 'silly number');
assert.ok(isNumeric(1010010293029), 'large number');
assert.ok(isNumeric(1.100393830000), 'decimal numbers');
assert.ok(isNumeric(Math.LN2), 'natural log of two');
assert.ok(isNumeric(Math.PI), 'delicious number');
assert.ok(isNumeric(5e10), 'scientifically notated number');
});

test('isNumeric rejects non-numeric things', function (assert) {
assert.ok(!isNumeric(NaN), 'not number');
assert.ok(!isNumeric(Infinity), 'largest number');
assert.ok(!isNumeric(-Infinity), 'smallest number');
assert.ok(!isNumeric(), 'nothing');
assert.ok(!isNumeric(undefined), 'undefined');
assert.ok(!isNumeric(null), 'null');
assert.ok(!isNumeric([1]), 'array');
assert.ok(!isNumeric('[1,2,3]'), 'string');
assert.ok(!isNumeric(new Date()), 'date');
assert.ok(!isNumeric({a:1,b:2}), 'object');
});