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
Fix parsing for months/weekdays with weird characters #3078
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,12 @@ | ||
import { addFormatToken } from '../format/format'; | ||
import { addUnitAlias } from './aliases'; | ||
import { addRegexToken, match1to2, matchWord } from '../parse/regex'; | ||
import { addRegexToken, match1to2, matchWord, regexEscape } from '../parse/regex'; | ||
import { addWeekParseToken } from '../parse/token'; | ||
import toInt from '../utils/to-int'; | ||
import isArray from '../utils/is-array'; | ||
import { createLocal } from '../create/local'; | ||
import indexOf from '../utils/index-of'; | ||
import hasOwnProp from '../utils/has-own-prop'; | ||
import { createUTC } from '../create/utc'; | ||
import getParsingFlags from '../create/parsing-flags'; | ||
|
||
// FORMATTING | ||
|
@@ -37,9 +39,15 @@ addUnitAlias('isoWeekday', 'E'); | |
addRegexToken('d', match1to2); | ||
addRegexToken('e', match1to2); | ||
addRegexToken('E', match1to2); | ||
addRegexToken('dd', matchWord); | ||
addRegexToken('ddd', matchWord); | ||
addRegexToken('dddd', matchWord); | ||
addRegexToken('dd', function (isStrict, locale) { | ||
return locale.weekdaysMinRegex(isStrict); | ||
}); | ||
addRegexToken('ddd', function (isStrict, locale) { | ||
return locale.weekdaysShortRegex(isStrict); | ||
}); | ||
addRegexToken('dddd', function (isStrict, locale) { | ||
return locale.weekdaysRegex(isStrict); | ||
}); | ||
|
||
addWeekParseToken(['dd', 'ddd', 'dddd'], function (input, week, config, token) { | ||
var weekday = config._locale.weekdaysParse(input, token, config._strict); | ||
|
@@ -92,9 +100,77 @@ export function localeWeekdaysMin (m) { | |
return this._weekdaysMin[m.day()]; | ||
} | ||
|
||
function handleStrictParse(weekdayName, format, strict) { | ||
var i, ii, mom, llc = weekdayName.toLocaleLowerCase(); | ||
if (!this._weekdaysParse) { | ||
this._weekdaysParse = []; | ||
this._shortWeekdaysParse = []; | ||
this._minWeekdaysParse = []; | ||
|
||
for (i = 0; i < 7; ++i) { | ||
mom = createUTC([2000, 1]).day(i); | ||
this._minWeekdaysParse[i] = this.weekdaysMin(mom, '').toLocaleLowerCase(); | ||
this._shortWeekdaysParse[i] = this.weekdaysShort(mom, '').toLocaleLowerCase(); | ||
this._weekdaysParse[i] = this.weekdays(mom, '').toLocaleLowerCase(); | ||
} | ||
} | ||
|
||
if (strict) { | ||
if (format === 'dddd') { | ||
ii = indexOf.call(this._weekdaysParse, llc); | ||
return ii !== -1 ? ii : null; | ||
} else if (format === 'ddd') { | ||
ii = indexOf.call(this._shortWeekdaysParse, llc); | ||
return ii !== -1 ? ii : null; | ||
} else { | ||
ii = indexOf.call(this._minWeekdaysParse, llc); | ||
return ii !== -1 ? ii : null; | ||
} | ||
} else { | ||
if (format === 'dddd') { | ||
ii = indexOf.call(this._weekdaysParse, llc); | ||
if (ii !== -1) { | ||
return ii; | ||
} | ||
ii = indexOf.call(this._shortWeekdaysParse, llc); | ||
if (ii !== -1) { | ||
return ii; | ||
} | ||
ii = indexOf.call(this._minWeekdaysParse, llc); | ||
return ii !== -1 ? ii : null; | ||
} else if (format === 'ddd') { | ||
ii = indexOf.call(this._shortWeekdaysParse, llc); | ||
if (ii !== -1) { | ||
return ii; | ||
} | ||
ii = indexOf.call(this._weekdaysParse, llc); | ||
if (ii !== -1) { | ||
return ii; | ||
} | ||
ii = indexOf.call(this._minWeekdaysParse, llc); | ||
return ii !== -1 ? ii : null; | ||
} else { | ||
ii = indexOf.call(this._minWeekdaysParse, llc); | ||
if (ii !== -1) { | ||
return ii; | ||
} | ||
ii = indexOf.call(this._weekdaysParse, llc); | ||
if (ii !== -1) { | ||
return ii; | ||
} | ||
ii = indexOf.call(this._shortWeekdaysParse, llc); | ||
return ii !== -1 ? ii : null; | ||
} | ||
} | ||
} | ||
|
||
export function localeWeekdaysParse (weekdayName, format, strict) { | ||
var i, mom, regex; | ||
|
||
if (this._weekdaysParseExact) { | ||
return handleStrictParse.call(this, weekdayName, format, strict); | ||
} | ||
|
||
if (!this._weekdaysParse) { | ||
this._weekdaysParse = []; | ||
this._minWeekdaysParse = []; | ||
|
@@ -105,7 +181,7 @@ export function localeWeekdaysParse (weekdayName, format, strict) { | |
for (i = 0; i < 7; i++) { | ||
// make the regex if we don't have it already | ||
|
||
mom = createLocal([2000, 1]).day(i); | ||
mom = createUTC([2000, 1]).day(i); | ||
if (strict && !this._fullWeekdaysParse[i]) { | ||
this._fullWeekdaysParse[i] = new RegExp('^' + this.weekdays(mom, '').replace('.', '\.?') + '$', 'i'); | ||
this._shortWeekdaysParse[i] = new RegExp('^' + this.weekdaysShort(mom, '').replace('.', '\.?') + '$', 'i'); | ||
|
@@ -160,3 +236,96 @@ export function getSetISODayOfWeek (input) { | |
// as a setter, sunday should belong to the previous week. | ||
return input == null ? this.day() || 7 : this.day(this.day() % 7 ? input : input - 7); | ||
} | ||
|
||
export var defaultWeekdaysRegex = matchWord; | ||
export function weekdaysRegex (isStrict) { | ||
if (this._weekdaysParseExact) { | ||
if (!hasOwnProp(this, '_weekdaysRegex')) { | ||
computeWeekdaysParse.call(this); | ||
} | ||
if (isStrict) { | ||
return this._weekdaysStrictRegex; | ||
} else { | ||
return this._weekdaysRegex; | ||
} | ||
} else { | ||
return this._weekdaysStrictRegex && isStrict ? | ||
this._weekdaysStrictRegex : this._weekdaysRegex; | ||
} | ||
} | ||
|
||
export var defaultWeekdaysShortRegex = matchWord; | ||
export function weekdaysShortRegex (isStrict) { | ||
if (this._weekdaysParseExact) { | ||
if (!hasOwnProp(this, '_weekdaysRegex')) { | ||
computeWeekdaysParse.call(this); | ||
} | ||
if (isStrict) { | ||
return this._weekdaysShortStrictRegex; | ||
} else { | ||
return this._weekdaysShortRegex; | ||
} | ||
} else { | ||
return this._weekdaysShortStrictRegex && isStrict ? | ||
this._weekdaysShortStrictRegex : this._weekdaysShortRegex; | ||
} | ||
} | ||
|
||
export var defaultWeekdaysMinRegex = matchWord; | ||
export function weekdaysMinRegex (isStrict) { | ||
if (this._weekdaysParseExact) { | ||
if (!hasOwnProp(this, '_weekdaysRegex')) { | ||
computeWeekdaysParse.call(this); | ||
} | ||
if (isStrict) { | ||
return this._weekdaysMinStrictRegex; | ||
} else { | ||
return this._weekdaysMinRegex; | ||
} | ||
} else { | ||
return this._weekdaysMinStrictRegex && isStrict ? | ||
this._weekdaysMinStrictRegex : this._weekdaysMinRegex; | ||
} | ||
} | ||
|
||
|
||
function computeWeekdaysParse () { | ||
function cmpLenRev(a, b) { | ||
return b.length - a.length; | ||
} | ||
|
||
var minPieces = [], shortPieces = [], longPieces = [], mixedPieces = [], | ||
i, mom, minp, shortp, longp; | ||
for (i = 0; i < 7; i++) { | ||
// make the regex if we don't have it already | ||
mom = createUTC([2000, 1]).day(i); | ||
minp = this.weekdaysMin(mom, ''); | ||
shortp = this.weekdaysShort(mom, ''); | ||
longp = this.weekdays(mom, ''); | ||
minPieces.push(minp); | ||
shortPieces.push(shortp); | ||
longPieces.push(longp); | ||
mixedPieces.push(minp); | ||
mixedPieces.push(shortp); | ||
mixedPieces.push(longp); | ||
} | ||
// Sorting makes sure if one weekday (or abbr) is a prefix of another it | ||
// will match the longer piece. | ||
minPieces.sort(cmpLenRev); | ||
shortPieces.sort(cmpLenRev); | ||
longPieces.sort(cmpLenRev); | ||
mixedPieces.sort(cmpLenRev); | ||
for (i = 0; i < 7; i++) { | ||
shortPieces[i] = regexEscape(shortPieces[i]); | ||
longPieces[i] = regexEscape(longPieces[i]); | ||
mixedPieces[i] = regexEscape(mixedPieces[i]); | ||
} | ||
|
||
this._weekdaysRegex = new RegExp('^(' + mixedPieces.join('|') + ')', 'i'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should each piece be escaped ? Note, there is RegExp.escape function in es6 |
||
this._weekdaysShortRegex = this._weekdaysRegex; | ||
this._weekdaysMinRegex = this._weekdaysRegex; | ||
|
||
this._weekdaysStrictRegex = new RegExp('^(' + longPieces.join('|') + ')', 'i'); | ||
this._weekdaysShortStrictRegex = new RegExp('^(' + shortPieces.join('|') + ')', 'i'); | ||
this._weekdaysMinStrictRegex = new RegExp('^(' + minPieces.join('|') + ')', 'i'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
var indexOf; | ||
|
||
if (Array.prototype.indexOf) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Es6 must provide it, please dont reinvent wheel. |
||
indexOf = Array.prototype.indexOf; | ||
} else { | ||
indexOf = function (o) { | ||
// I know | ||
var i; | ||
for (i = 0; i < this.length; ++i) { | ||
if (this[i] === o) { | ||
return i; | ||
} | ||
} | ||
return -1; | ||
}; | ||
} | ||
|
||
export { indexOf as default }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Es6: https://babeljs.io/repl/#?evaluate=true&presets=es2015%2Ces2015-loose%2Creact%2Cstage-0%2Cstage-1%2Cstage-2%2Cstage-3&code=const%20qwe%3D'e'%20in%20%5B'qwe'%5D%3B%0Aconsole.log(qwe)
Please avoid indexof in this PR