-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split Collator/Formatted types into separate files
This avoids a troublesome circular import.
- Loading branch information
1 parent
85fce01
commit 11b6a19
Showing
23 changed files
with
131 additions
and
123 deletions.
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
39 changes: 2 additions & 37 deletions
39
...-spec/expression/definitions/formatted.js → ...yle-spec/expression/definitions/format.js
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// @flow | ||
|
||
// 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 default 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; | ||
} | ||
} |
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,37 @@ | ||
// @flow | ||
|
||
export class FormattedSection { | ||
text: string; | ||
scale: number | null; | ||
fontStack: string | null; | ||
|
||
constructor(text: string, scale: number | null, fontStack: string | null) { | ||
this.text = text; | ||
this.scale = scale; | ||
this.fontStack = fontStack; | ||
} | ||
} | ||
|
||
export default class Formatted { | ||
sections: Array<FormattedSection>; | ||
|
||
constructor(sections: Array<FormattedSection>) { | ||
this.sections = sections; | ||
} | ||
|
||
toString(): string { | ||
return this.sections.map(section => section.text).join(''); | ||
} | ||
|
||
serialize() { | ||
const serialized = ["format"]; | ||
for (const section of this.sections) { | ||
serialized.push(section.text); | ||
const fontStack = section.fontStack ? | ||
["literal", section.fontStack.split(',')] : | ||
null; | ||
serialized.push({ "text-font": fontStack, "font-scale": section.scale }); | ||
} | ||
return serialized; | ||
} | ||
} |
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
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.