You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
79 lines
2.9 KiB
79 lines
2.9 KiB
// @flow
|
|
|
|
import {StringType, BooleanType, CollatorType} from '../types.js';
|
|
import Collator from '../types/collator.js';
|
|
|
|
import type {Expression, SerializedExpression} from '../expression.js';
|
|
import type EvaluationContext from '../evaluation_context.js';
|
|
import type ParsingContext from '../parsing_context.js';
|
|
import type {Type} from '../types.js';
|
|
|
|
export default 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: $ReadOnlyArray<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['case-sensitive'] === undefined ? false : options['case-sensitive'], 1, BooleanType);
|
|
if (!caseSensitive) return null;
|
|
|
|
const diacriticSensitive = context.parse(
|
|
options['diacritic-sensitive'] === undefined ? false : options['diacritic-sensitive'], 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): Collator {
|
|
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);
|
|
}
|
|
}
|
|
|
|
outputDefined(): boolean {
|
|
// Technically the set of possible outputs is the combinatoric set of Collators produced
|
|
// by all possible outputs of locale/caseSensitive/diacriticSensitive
|
|
// But for the primary use of Collators in comparison operators, we ignore the Collator's
|
|
// possible outputs anyway, so we can get away with leaving this false for now.
|
|
return false;
|
|
}
|
|
|
|
serialize(): SerializedExpression {
|
|
const options = {};
|
|
options['case-sensitive'] = this.caseSensitive.serialize();
|
|
options['diacritic-sensitive'] = this.diacriticSensitive.serialize();
|
|
if (this.locale) {
|
|
options['locale'] = this.locale.serialize();
|
|
}
|
|
return ["collator", options];
|
|
}
|
|
}
|