Skip to content

Commit

Permalink
Split Collator/Formatted types into separate files
Browse files Browse the repository at this point in the history
This avoids a troublesome circular import.
  • Loading branch information
jfirebaugh committed Sep 14, 2018
1 parent 77a1d50 commit 9680322
Show file tree
Hide file tree
Showing 24 changed files with 131 additions and 133 deletions.
2 changes: 1 addition & 1 deletion src/data/bucket/symbol_bucket.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import Anchor from '../../symbol/anchor';
import { getSizeData } from '../../symbol/symbol_size';
import { register } from '../../util/web_worker_transfer';
import EvaluationParameters from '../../style/evaluation_parameters';
import {Formatted} from '../../style-spec/expression/definitions/formatted';
import Formatted from '../../style-spec/expression/types/formatted';


import type {
Expand Down
10 changes: 0 additions & 10 deletions src/style-spec/expression/definitions/coercion.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import type ParsingContext from '../parsing_context';
import type EvaluationContext from '../evaluation_context';
import type { Value } from '../values';
import type { Type } from '../types';
import { Formatted, FormattedSection } from './formatted';

const types = {
'to-boolean': BooleanType,
Expand Down Expand Up @@ -84,15 +83,6 @@ class Coercion implements Expression {
}
}
throw new RuntimeError(error || `Could not parse color from value '${typeof input === 'string' ? input : JSON.stringify(input)}'`);
} else if (this.type.kind === 'formatted') {
let input;
for (const arg of this.args) {
input = arg.evaluate(ctx);
if (typeof input === 'string') {
return new Formatted([new FormattedSection(input, null, null)]);
}
}
throw new RuntimeError(`Could not parse formatted text from value '${typeof input === 'string' ? input : JSON.stringify(input)}'`);
} else if (this.type.kind === 'number') {
let value = null;
for (const arg of this.args) {
Expand Down
63 changes: 2 additions & 61 deletions src/style-spec/expression/definitions/collator.js
Original file line number Diff line number Diff line change
@@ -1,73 +1,14 @@
// @flow

import { StringType, BooleanType, CollatorType } from '../types';
import Collator from '../types/collator';

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;
}
}

export class CollatorExpression implements Expression {
export default class CollatorExpression implements Expression {
type: Type;
caseSensitive: Expression;
diacriticSensitive: Expression;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,56 +1,21 @@
// @flow

import { NumberType, ValueType, FormattedType, array, StringType } from '../types';
import Formatted, { FormattedSection } from '../types/formatted';
import { toString } from '../values';

import type { Expression } from '../expression';
import type EvaluationContext from '../evaluation_context';
import type ParsingContext from '../parsing_context';
import type { Type } from '../types';

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 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;
}
}

type FormattedSectionExpression = {
text: Expression,
scale: Expression | null;
font: Expression | null;
}

export class FormatExpression implements Expression {
export default class FormatExpression implements Expression {
type: Type;
sections: Array<FormattedSectionExpression>;

Expand Down
4 changes: 2 additions & 2 deletions src/style-spec/expression/definitions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ import {
LessThanOrEqual,
GreaterThanOrEqual
} from './comparison';
import { CollatorExpression } from './collator';
import { FormatExpression } from './formatted';
import CollatorExpression from './collator';
import FormatExpression from './format';
import Length from './length';

import type { Varargs } from '../compound_expression';
Expand Down
2 changes: 1 addition & 1 deletion src/style-spec/expression/definitions/literal.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import assert from 'assert';
import { isValue, typeOf, Color } from '../values';
import { Formatted } from './formatted';
import Formatted from '../types/formatted';

import type { Type } from '../types';
import type { Value } from '../values';
Expand Down
4 changes: 2 additions & 2 deletions src/style-spec/expression/parsing_context.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ import Assertion from './definitions/assertion';
import Coercion from './definitions/coercion';
import EvaluationContext from './evaluation_context';
import CompoundExpression from './compound_expression';
import { CollatorExpression } from './definitions/collator';
import CollatorExpression from './definitions/collator';
import FormatExpression from './definitions/format';
import {isGlobalPropertyConstant, isFeatureConstant} from './is_constant';
import Var from './definitions/var';


import type {Expression, ExpressionRegistry} from './expression';
import type {Type} from './types';
import {FormatExpression} from './definitions/formatted';

/**
* State associated parsing at a given point in an expression tree.
Expand Down
61 changes: 61 additions & 0 deletions src/style-spec/expression/types/collator.js
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;
}
}
37 changes: 37 additions & 0 deletions src/style-spec/expression/types/formatted.js
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;
}
}
12 changes: 8 additions & 4 deletions src/style-spec/expression/values.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
import assert from 'assert';

import Color from '../util/color';
import { Collator } from './definitions/collator';
import { Formatted } from './definitions/formatted';
import { NullType, NumberType, StringType, BooleanType, ColorType, ObjectType, ValueType, CollatorType, array } from './types';
import Collator from './types/collator';
import Formatted from './types/formatted';
import { NullType, NumberType, StringType, BooleanType, ColorType, ObjectType, ValueType, CollatorType, FormattedType, array } from './types';

import type { Type } from './types';

Expand All @@ -28,7 +28,7 @@ export function validateRGBA(r: mixed, g: mixed, b: mixed, a?: mixed): ?string {
return null;
}

export type Value = null | string | boolean | number | Color | Collator | $ReadOnlyArray<Value> | { +[string]: Value }
export type Value = null | string | boolean | number | Color | Collator | Formatted | $ReadOnlyArray<Value> | { +[string]: Value }

export function isValue(mixed: mixed): boolean {
if (mixed === null) {
Expand All @@ -43,6 +43,8 @@ export function isValue(mixed: mixed): boolean {
return true;
} else if (mixed instanceof Collator) {
return true;
} else if (mixed instanceof Formatted) {
return true;
} else if (Array.isArray(mixed)) {
for (const item of mixed) {
if (!isValue(item)) {
Expand Down Expand Up @@ -75,6 +77,8 @@ export function typeOf(value: Value): Type {
return ColorType;
} else if (value instanceof Collator) {
return CollatorType;
} else if (value instanceof Formatted) {
return FormattedType;
} else if (Array.isArray(value)) {
const length = value.length;
let itemType: ?Type;
Expand Down
2 changes: 1 addition & 1 deletion src/style/style_layer/background_style_layer_properties.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {

import type Color from '../../style-spec/util/color';

import type {Formatted} from '../../style-spec/expression/definitions/formatted';
import type Formatted from '../../style-spec/expression/types/formatted';


export type PaintProps = {|
Expand Down
2 changes: 1 addition & 1 deletion src/style/style_layer/circle_style_layer_properties.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {

import type Color from '../../style-spec/util/color';

import type {Formatted} from '../../style-spec/expression/definitions/formatted';
import type Formatted from '../../style-spec/expression/types/formatted';


export type PaintProps = {|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {

import type Color from '../../style-spec/util/color';

import type {Formatted} from '../../style-spec/expression/definitions/formatted';
import type Formatted from '../../style-spec/expression/types/formatted';


export type PaintProps = {|
Expand Down
2 changes: 1 addition & 1 deletion src/style/style_layer/fill_style_layer_properties.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {

import type Color from '../../style-spec/util/color';

import type {Formatted} from '../../style-spec/expression/definitions/formatted';
import type Formatted from '../../style-spec/expression/types/formatted';


export type PaintProps = {|
Expand Down
2 changes: 1 addition & 1 deletion src/style/style_layer/heatmap_style_layer_properties.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {

import type Color from '../../style-spec/util/color';

import type {Formatted} from '../../style-spec/expression/definitions/formatted';
import type Formatted from '../../style-spec/expression/types/formatted';


export type PaintProps = {|
Expand Down
2 changes: 1 addition & 1 deletion src/style/style_layer/hillshade_style_layer_properties.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {

import type Color from '../../style-spec/util/color';

import type {Formatted} from '../../style-spec/expression/definitions/formatted';
import type Formatted from '../../style-spec/expression/types/formatted';


export type PaintProps = {|
Expand Down
Loading

0 comments on commit 9680322

Please sign in to comment.