-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
[fields] Support format without separator #12489
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -202,56 +202,48 @@ const buildSections = <TDate extends PickerValidDate>( | |
const sections: FieldSection[] = []; | ||
let startSeparator: string = ''; | ||
|
||
// This RegExp test if the beginning of a string corresponds to a supported token | ||
const isTokenStartRegExp = new RegExp( | ||
`^(${Object.keys(utils.formatTokenMap) | ||
.sort((a, b) => b.length - a.length) // Sort to put longest word first | ||
.join('|')})`, | ||
'g', // used to get access to lastIndex state | ||
); | ||
// This RegExp tests if the beginning of a string corresponds to a supported token | ||
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. I reworked this method because it was duplicating the 1st character of the 2nd token (once inside the token and once as a separator). Since we catch the whole token in one passage in the loop, we can replace the Now we also support glued tokens (e.g: |
||
const isTokenStartRegExpStr = `^(${Object.keys(utils.formatTokenMap) | ||
.sort((a, b) => b.length - a.length) // Sort to put longest word first | ||
.join('|')})`; | ||
|
||
const getEscapedPartOfCurrentChar = (i: number) => | ||
escapedParts.find((escapeIndex) => escapeIndex.start <= i && escapeIndex.end >= i); | ||
|
||
let currentTokenValue = ''; | ||
let i = 0; | ||
while (i < expandedFormat.length) { | ||
const escapedPartOfCurrentChar = getEscapedPartOfCurrentChar(i); | ||
|
||
for (let i = 0; i < expandedFormat.length; i += 1) { | ||
const escapedPartOfCurrentChar = escapedParts.find( | ||
(escapeIndex) => escapeIndex.start <= i && escapeIndex.end >= i, | ||
const isTokenStartRegExp = new RegExp( | ||
isTokenStartRegExpStr, | ||
'g', // used to get access to lastIndex state | ||
); | ||
|
||
const char = expandedFormat[i]; | ||
const isEscapedChar = escapedPartOfCurrentChar != null; | ||
const potentialToken = `${currentTokenValue}${expandedFormat.slice(i)}`; | ||
const regExpMatch = isTokenStartRegExp.test(potentialToken); | ||
|
||
if (!isEscapedChar && char.match(/([A-Za-z]+)/) && regExpMatch) { | ||
currentTokenValue = potentialToken.slice(0, isTokenStartRegExp.lastIndex); | ||
i += isTokenStartRegExp.lastIndex - 1; | ||
if (!isEscapedChar && isTokenStartRegExp.test(expandedFormat.slice(i))) { | ||
const currentTokenValue = expandedFormat.slice(i, i + isTokenStartRegExp.lastIndex); | ||
sections.push(createSection({ ...params, now, token: currentTokenValue, startSeparator })); | ||
i += isTokenStartRegExp.lastIndex; | ||
} else { | ||
const char = expandedFormat[i]; | ||
|
||
// If we are on the opening or closing character of an escaped part of the format, | ||
// Then we ignore this character. | ||
const isEscapeBoundary = | ||
(isEscapedChar && escapedPartOfCurrentChar?.start === i) || | ||
escapedPartOfCurrentChar?.end === i; | ||
flaviendelangle marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if (!isEscapeBoundary) { | ||
if (currentTokenValue !== '') { | ||
sections.push( | ||
createSection({ ...params, now, token: currentTokenValue, startSeparator }), | ||
); | ||
currentTokenValue = ''; | ||
} | ||
|
||
if (sections.length === 0) { | ||
startSeparator += char; | ||
} else { | ||
startSeparator = ''; | ||
sections[sections.length - 1].endSeparator += char; | ||
} | ||
} | ||
} | ||
} | ||
|
||
if (currentTokenValue !== '') { | ||
sections.push(createSection({ ...params, now, token: currentTokenValue, startSeparator })); | ||
i += 1; | ||
} | ||
} | ||
|
||
if (sections.length === 0 && startSeparator.length > 0) { | ||
|
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.
This method was escaping tokens when they were not separated by any character.