import { getPrioritizedUnits } from '../units/priorities';
import { hooks } from '../utils/hooks';
import isFunction from '../utils/is-function';
-
+import isNumeric from '../utils/is-numeric';
export function makeGetSet (unit, keepTime) {
return function (value) {
}
export function set (mom, unit, value) {
- if (mom.isValid()) {
+ if (mom.isValid() && isNumeric(value)) {
mom._d['set' + (mom._isUTC ? 'UTC' : '') + unit](value);
}
}
--- /dev/null
+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
+}
\ No newline at end of file
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(null);
+ a.date([1]);
+ a.day(-Infinity);
+ a.hours(new Date());
+ a.minutes({a:1,b:2});
+ a.seconds("foo");
+ a.milliseconds(Infinity);
+
+ assert.equal(a.year(), 2011, 'year');
+ assert.equal(a.month(), 9, 'month');
+ assert.equal(a.date(), 12, 'date');
+ assert.equal(a.day(), 3, 'day');
+ assert.equal(a.hours(), 6, 'hour');
+ assert.equal(a.minutes(), 7, 'minute');
+ assert.equal(a.seconds(), 8, 'second');
+ assert.equal(a.milliseconds(), 9, 'milliseconds');
+});
+
test('setter programmatic', function (assert) {
var a = moment();
a.set('year', 2011);
--- /dev/null
+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');
+});