-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
'Collator' expression for controlling case and diacritic sensitivity in string comparisons #6270
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
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 |
---|---|---|
@@ -0,0 +1,147 @@ | ||
// @flow | ||
|
||
import { StringType, BooleanType, CollatorType } from '../types'; | ||
|
||
import type { Expression } from '../expression'; | ||
import type EvaluationContext from '../evaluation_context'; | ||
import type ParsingContext from '../parsing_context'; | ||
import type { Type } from '../types'; | ||
|
||
// Flow type declarations for Intl cribbed from | ||
// https://github.com/facebook/flow/issues/1270 | ||
|
||
declare var Intl: { | ||
Collator: Class<Intl$Collator> | ||
} | ||
|
||
declare class Intl$Collator { | ||
constructor ( | ||
locales?: string | string[], | ||
options?: CollatorOptions | ||
): Intl$Collator; | ||
|
||
static ( | ||
locales?: string | string[], | ||
options?: CollatorOptions | ||
): Intl$Collator; | ||
|
||
compare (a: string, b: string): number; | ||
|
||
resolvedOptions(): any; | ||
} | ||
|
||
type CollatorOptions = { | ||
localeMatcher?: 'lookup' | 'best fit', | ||
usage?: 'sort' | 'search', | ||
sensitivity?: 'base' | 'accent' | 'case' | 'variant', | ||
ignorePunctuation?: boolean, | ||
numeric?: boolean, | ||
caseFirst?: 'upper' | 'lower' | 'false' | ||
} | ||
|
||
export class Collator { | ||
locale: string | null; | ||
sensitivity: 'base' | 'accent' | 'case' | 'variant'; | ||
collator: Intl$Collator; | ||
|
||
constructor(caseSensitive: boolean, diacriticSensitive: boolean, locale: string | null) { | ||
if (caseSensitive) | ||
this.sensitivity = diacriticSensitive ? 'variant' : 'case'; | ||
else | ||
this.sensitivity = diacriticSensitive ? 'accent' : 'base'; | ||
|
||
this.locale = locale; | ||
this.collator = new Intl.Collator(this.locale ? this.locale : [], | ||
{ sensitivity: this.sensitivity, usage: 'search' }); | ||
} | ||
|
||
compare(lhs: string, rhs: string): number { | ||
return this.collator.compare(lhs, rhs); | ||
} | ||
|
||
resolvedLocale(): string { | ||
// We create a Collator without "usage: search" because we don't want | ||
// the search options encoded in our result (e.g. "en-u-co-search") | ||
return new Intl.Collator(this.locale ? this.locale : []) | ||
.resolvedOptions().locale; | ||
} | ||
|
||
serialize() { | ||
const options = {}; | ||
options['caseSensitive'] = this.sensitivity === 'variant' || this.sensitivity === 'case'; | ||
options['diacriticSensitive'] = this.sensitivity === 'variant' || this.sensitivity === 'accent'; | ||
if (this.locale) { | ||
options['locale'] = this.locale; | ||
} | ||
return ["collator", options]; | ||
} | ||
} | ||
|
||
export class CollatorExpression implements Expression { | ||
type: Type; | ||
caseSensitive: Expression; | ||
diacriticSensitive: Expression; | ||
locale: Expression | null; | ||
|
||
constructor(caseSensitive: Expression, diacriticSensitive: Expression, locale: Expression | null) { | ||
this.type = CollatorType; | ||
this.locale = locale; | ||
this.caseSensitive = caseSensitive; | ||
this.diacriticSensitive = diacriticSensitive; | ||
} | ||
|
||
static parse(args: Array<mixed>, context: ParsingContext): ?Expression { | ||
if (args.length !== 2) | ||
return context.error(`Expected one argument.`); | ||
|
||
const options = (args[1]: any); | ||
if (typeof options !== "object" || Array.isArray(options)) | ||
return context.error(`Collator options argument must be an object.`); | ||
|
||
const caseSensitive = context.parse( | ||
options['caseSensitive'] === undefined ? false : options['caseSensitive'], 1, BooleanType); | ||
if (!caseSensitive) return null; | ||
|
||
const diacriticSensitive = context.parse( | ||
options['diacriticSensitive'] === undefined ? false : options['diacriticSensitive'], 1, BooleanType); | ||
if (!diacriticSensitive) return null; | ||
|
||
let locale = null; | ||
if (options['locale']) { | ||
locale = context.parse(options['locale'], 1, StringType); | ||
if (!locale) return null; | ||
} | ||
|
||
return new CollatorExpression(caseSensitive, diacriticSensitive, locale); | ||
} | ||
|
||
evaluate(ctx: EvaluationContext) { | ||
return new Collator(this.caseSensitive.evaluate(ctx), this.diacriticSensitive.evaluate(ctx), this.locale ? this.locale.evaluate(ctx) : null); | ||
} | ||
|
||
eachChild(fn: (Expression) => void) { | ||
fn(this.caseSensitive); | ||
fn(this.diacriticSensitive); | ||
if (this.locale) { | ||
fn(this.locale); | ||
} | ||
} | ||
|
||
possibleOutputs() { | ||
// Technically the set of possible outputs is the combinatoric set of Collators produced | ||
// by all possibleOutputs of locale/caseSensitive/diacriticSensitive | ||
// But for the primary use of Collators in comparison operators, we ignore the Collator's | ||
// possibleOutputs anyway, so we can get away with leaving this undefined for now. | ||
return [undefined]; | ||
} | ||
|
||
serialize() { | ||
const options = {}; | ||
options['caseSensitive'] = this.caseSensitive; | ||
options['diacriticSensitive'] = this.diacriticSensitive; | ||
if (this.locale) { | ||
options['locale'] = this.locale; | ||
} | ||
return ["collator", options]; | ||
} | ||
} |
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.
Let's leave a comment referencing facebook/flow#1270, which tracks adding these to flow's library definitions