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

Expose parsed date parts. Invalid date if 'a' token parsed but no date parts parsed. #3045

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/lib/create/from-string-and-format.js
Expand Up @@ -70,6 +70,9 @@ export function configFromStringAndFormat(config) {
config._a[HOUR] > 0) {
getParsingFlags(config).bigHour = undefined;
}

getParsingFlags(config).parsedDateParts = config._a.slice(0);
getParsingFlags(config).meridiem = config._meridiem;
// handle meridiem
config._a[HOUR] = meridiemFixWrap(config._locale, config._a[HOUR], config._meridiem);

Expand Down
4 changes: 3 additions & 1 deletion src/lib/create/parsing-flags.js
Expand Up @@ -10,7 +10,9 @@ function defaultParsingFlags() {
invalidMonth : null,
invalidFormat : false,
userInvalidated : false,
iso : false
iso : false,
parsedDateParts : [],
meridiem : null
};
}

Expand Down
7 changes: 6 additions & 1 deletion src/lib/create/valid.js
@@ -1,18 +1,23 @@
import extend from '../utils/extend';
import { createUTC } from './utc';
import getParsingFlags from '../create/parsing-flags';
import some from '../utils/some';

export function isValid(m) {
if (m._isValid == null) {
var flags = getParsingFlags(m);
var parsedParts = some.call(flags.parsedDateParts, function (i) {
return i != null;
});
m._isValid = !isNaN(m._d.getTime()) &&
flags.overflow < 0 &&
!flags.empty &&
!flags.invalidMonth &&
!flags.invalidWeekday &&
!flags.nullInput &&
!flags.invalidFormat &&
!flags.userInvalidated;
!flags.userInvalidated &&
(!flags.meridiem || (flags.meridiem && parsedParts));

if (m._strict) {
m._isValid = m._isValid &&
Expand Down
19 changes: 19 additions & 0 deletions src/lib/utils/some.js
@@ -0,0 +1,19 @@
var some;
if (Array.prototype.some) {
some = Array.prototype.some;
} else {
some = function (fun) {
var t = Object(this);
var len = t.length >>> 0;

for (var i = 0; i < len; i++) {
if (i in t && fun.call(this, t[i], i, t)) {
return true;
}
}

return false;
};
}

export { some as default };
16 changes: 16 additions & 0 deletions src/test/moment/create.js
Expand Up @@ -1041,3 +1041,19 @@ test('hmm', function (assert) {
test('Y token', function (assert) {
assert.equal(moment('1-1-2010', 'M-D-Y', true).year(), 2010, 'parsing Y');
});

test('parsing flags retain parsed date parts', function (assert) {
var a = moment('10 p', 'hh:mm a');
assert.equal(a.parsingFlags().parsedDateParts[3], 10, 'parsed 10 as the hour');
assert.equal(a.parsingFlags().parsedDateParts[0], undefined, 'year was not parsed');
assert.equal(a.parsingFlags().meridiem, 'p', 'meridiem flag was added');
var b = moment('10:30', ['MMDDYY', 'HH:mm']);
assert.equal(b.parsingFlags().parsedDateParts[3], 10, 'multiple format parshing matched hour');
assert.equal(b.parsingFlags().parsedDateParts[0], undefined, 'array is properly copied, no residual data from first token parse');
});

test('parsing only meridiem results in invalid date', function (assert) {
assert.ok(!moment('alkj', 'hh:mm a').isValid(), 'because an a token is used, a meridiem will be parsed but nothing else was so invalid');
assert.ok(moment('02:30 p more extra stuff', 'hh:mm a').isValid(), 'because other tokens were parsed, date is valid');
assert.ok(moment('1/1/2016 extra data', ['a', 'M/D/YYYY']).isValid(), 'took second format, does not pick up on meridiem parsed from first format (good copy)');
});