Skip to content

Commit

Permalink
Move 'collator' from three arguments to a single "options" argument.
Browse files Browse the repository at this point in the history
Support/test default behavior when case/diacriticSensitivity isn't specified.
Remove 'collator','resolve-locale' from feature non-constant list.
  • Loading branch information
ChrisLoer committed Apr 7, 2018
1 parent be70589 commit 2e107ba
Show file tree
Hide file tree
Showing 21 changed files with 208 additions and 58 deletions.
12 changes: 1 addition & 11 deletions docs/components/expression-metadata.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,17 +204,7 @@ const types = {
}],
collator: [{
type: 'collator',
parameters: [
'caseSensitive: boolean',
'diacriticSensitive: boolean',
]
}, {
type: 'collator',
parameters: [
'caseSensitive: boolean',
'diacriticSensitive: boolean',
'locale: string'
]
parameters: [ '{ "caseSensitive": boolean, "diacriticSensitive": boolean, "locale": string }' ]
}]
};

Expand Down
39 changes: 25 additions & 14 deletions src/style-spec/expression/definitions/collator.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,13 @@ export class CollatorInstantiation {
}

serialize() {
const serialized = ["collator"];
serialized.push(this.sensitivity === 'variant' || this.sensitivity === 'case');
serialized.push(this.sensitivity === 'variant' || this.sensitivity === 'accent');
const options = {};
options['caseSensitive'] = this.sensitivity === 'variant' || this.sensitivity === 'case';
options['diacriticSensitive'] = this.sensitivity === 'variant' || this.sensitivity === 'accent';
if (this.locale) {
serialized.push(this.locale);
options['locale'] = this.locale;
}
return serialized;
return ["collator", options];
}
}

Expand All @@ -85,17 +85,24 @@ export class CollatorExpression implements Expression {
}

static parse(args: Array<mixed>, context: ParsingContext): ?Expression {
if (args.length !== 3 && args.length !== 4)
return context.error(`Expected two or three arguments.`);
if (args.length !== 2)
return context.error(`Expected one argument.`);

const caseSensitive = context.parse(args[1], 1, BooleanType);
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(args[2], 2, BooleanType);

const diacriticSensitive = context.parse(
options['diacriticSensitive'] === undefined ? false : options['diacriticSensitive'], 2, BooleanType);
if (!diacriticSensitive) return null;

let locale = null;
if (args.length === 4) {
locale = context.parse(args[3], 3, StringType);
if (options['locale']) {
locale = context.parse(options['locale'], 3, StringType);
if (!locale) return null;
}

Expand Down Expand Up @@ -123,8 +130,12 @@ export class CollatorExpression implements Expression {
}

serialize() {
const serialized = ["collator"];
this.eachChild(child => { serialized.push(child.serialize()); });
return serialized;
const options = {};
options['caseSensitive'] = this.caseSensitive;
options['diacriticSensitive'] = this.diacriticSensitive;
if (this.locale) {
options['locale'] = this.locale;
}
return ["collator", options];
}
}
1 change: 0 additions & 1 deletion src/style-spec/expression/definitions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,6 @@ CompoundExpression.register(expressions, {
(ctx, args) => args.map(arg => arg.evaluate(ctx)).join('')
],
'resolved-locale': [
// Must be added to non-featureConstant list in parsing_context.js
StringType,
[CollatorType],
(ctx, [collator]) => collator.evaluate(ctx).resolvedLocale()
Expand Down
2 changes: 1 addition & 1 deletion src/style-spec/expression/parsing_context.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,5 +201,5 @@ function isConstant(expression: Expression) {
}

return isFeatureConstant(expression) &&
isGlobalPropertyConstant(expression, ['zoom', 'heatmap-density', 'resolved-locale', 'collator']);
isGlobalPropertyConstant(expression, ['zoom', 'heatmap-density']);
}
2 changes: 1 addition & 1 deletion src/style-spec/reference/v8.json
Original file line number Diff line number Diff line change
Expand Up @@ -2194,7 +2194,7 @@
}
},
"collator": {
"doc": "Returns a `collator` for use in locale dependent comparison operations. The first two arguments control case and diacritic sensitivity, and the optional third argument specifies the IETF language tag of the locale to use. If no locale is provided, the default locale is used.",
"doc": "Returns a `collator` for use in locale dependent comparison operations. The `caseSensitive` and `diacriticSensitive` options default to `false`. The `locale` argument specifies the IETF language tag of the locale to use. If none is provided, the default locale is used. If the requested locale is not available, the `collator` will use a system-defined fallback locale. Use `resolved-locale` to test the results of locale fallback behavior.",
"group": "Types",
"sdk-support": {
"basic functionality": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,25 @@
{
"expression": [
"case",
["==", ["resolved-locale", ["collator", true, false, "de"]], "de"],
[
"==",
[
"resolved-locale",
[
"collator",
{"caseSensitive": true, "diacriticSensitive": false, "locale": "de"}
]
],
"de"
],
[
"==",
["string", ["get", "lhs"]],
["get", "rhs"],
["collator", true, false, "de"]
[
"collator",
{"caseSensitive": true, "diacriticSensitive": false, "locale": "de"}
]
],
["case", ["==", ["get", "rhs"], "ue"], true, false]
],
Expand All @@ -24,12 +37,15 @@
"outputs": [true, false],
"serialized": [
"case",
["==", ["resolved-locale", ["collator", true, false, "de"]], "de"],
false,
[
"==",
["string", ["get", "lhs"]],
["get", "rhs"],
["collator", true, false, "de"]
[
"collator",
{"caseSensitive": true, "diacriticSensitive": false, "locale": "de"}
]
],
["case", ["==", ["get", "rhs"], "ue"], true, false]
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
"<",
["string", ["get", "lhs"]],
["get", "rhs"],
["collator", true, false, "en"]
[
"collator",
{"caseSensitive": true, "diacriticSensitive": false, "locale": "en"}
]
],
"inputs": [
[{}, {"properties": {"lhs": "a", "rhs": "ä"}}],
Expand All @@ -22,7 +25,10 @@
"<",
["string", ["get", "lhs"]],
["string", ["get", "rhs"]],
["collator", true, false, "en"]
[
"collator",
{"caseSensitive": true, "diacriticSensitive": false, "locale": "en"}
]
]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
"!=",
["string", ["get", "lhs"]],
["get", "rhs"],
["collator", true, false, "en"]
[
"collator",
{"caseSensitive": true, "diacriticSensitive": false, "locale": "en"}
]
]
],
"inputs": [
Expand All @@ -27,7 +30,10 @@
"!=",
["string", ["get", "lhs"]],
["get", "rhs"],
["collator", true, false, "en"]
[
"collator",
{"caseSensitive": true, "diacriticSensitive": false, "locale": "en"}
]
]
]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"==",
["string", ["get", "lhs"]],
["get", "rhs"],
["collator", false, false]
["collator", {"caseSensitive": false, "diacriticSensitive": false}]
],
"inputs": [
[{}, {"properties": {"lhs": "a", "rhs": "a"}}],
Expand All @@ -22,7 +22,7 @@
"==",
["string", ["get", "lhs"]],
["get", "rhs"],
["collator", false, false]
["collator", {"caseSensitive": false, "diacriticSensitive": false}]
]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
"==",
["string", ["get", "lhs"]],
["get", "rhs"],
["collator", false, false, "en"]
[
"collator",
{"caseSensitive": false, "diacriticSensitive": false, "locale": "en"}
]
],
"inputs": [
[{}, {"properties": {"lhs": "a", "rhs": "ä"}}],
Expand All @@ -22,7 +25,10 @@
"==",
["string", ["get", "lhs"]],
["get", "rhs"],
["collator", false, false, "en"]
[
"collator",
{"caseSensitive": false, "diacriticSensitive": false, "locale": "en"}
]
]
}
}
10 changes: 8 additions & 2 deletions test/integration/expression-tests/collator/base-gt-en/test.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
">",
["string", ["get", "lhs"]],
["get", "rhs"],
["collator", false, false, "en"]
[
"collator",
{"caseSensitive": false, "diacriticSensitive": false, "locale": "en"}
]
],
"inputs": [
[{}, {"properties": {"lhs": "a", "rhs": "ä"}}],
Expand All @@ -22,7 +25,10 @@
">",
["string", ["get", "lhs"]],
["string", ["get", "rhs"]],
["collator", false, false, "en"]
[
"collator",
{"caseSensitive": false, "diacriticSensitive": false, "locale": "en"}
]
]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
"<=",
["string", ["get", "lhs"]],
["get", "rhs"],
["collator", false, true, "en"]
[
"collator",
{"caseSensitive": false, "diacriticSensitive": true, "locale": "en"}
]
],
"inputs": [
[{}, {"properties": {"lhs": "ä", "rhs": "a"}}],
Expand All @@ -23,7 +26,10 @@
"<=",
["string", ["get", "lhs"]],
["string", ["get", "rhs"]],
["collator", false, true, "en"]
[
"collator",
{"caseSensitive": false, "diacriticSensitive": true, "locale": "en"}
]
]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
"!=",
["string", ["get", "lhs"]],
["get", "rhs"],
["collator", false, true, "en"]
[
"collator",
{"caseSensitive": false, "diacriticSensitive": true, "locale": "en"}
]
]
],
"inputs": [
Expand All @@ -27,7 +30,10 @@
"!=",
["string", ["get", "lhs"]],
["get", "rhs"],
["collator", false, true, "en"]
[
"collator",
{"caseSensitive": false, "diacriticSensitive": true, "locale": "en"}
]
]
]
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"expression": [
"==",
["string", ["get", "lhs"]],
["get", "rhs"],
["collator", {"diacriticSensitive": true, "locale": "en"}]
],
"inputs": [
[{}, {"properties": {"lhs": "ä", "rhs": "a"}}],
[{}, {"properties": {"lhs": "A", "rhs": "a"}}],
[{}, {"properties": {"lhs": "a", "rhs": "a"}}],
[{}, {"properties": {"lhs": "ä", "rhs": "b"}}]
],
"expected": {
"compiled": {
"result": "success",
"isFeatureConstant": false,
"isZoomConstant": true,
"type": "boolean"
},
"outputs": [false, true, true, false],
"serialized": [
"==",
["string", ["get", "lhs"]],
["get", "rhs"],
[
"collator",
{"caseSensitive": false, "diacriticSensitive": true, "locale": "en"}
]
]
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
{
"expression": ["<", 1, 2, ["collator", false, false]],
"expression": [
"<",
1,
2,
["collator", {"caseSensitive": false, "diacriticSensitive": false}]
],
"expected": {
"compiled": {
"result": "error",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"expression": [
"<",
["string", ["get", "lhs"]],
["get", "rhs"],
["collator", {"caseSensitive": ["==", 1, 1], "locale": "en"}]
],
"inputs": [
[{}, {"properties": {"lhs": "a", "rhs": "ä"}}],
[{}, {"properties": {"lhs": "a", "rhs": "A"}}],
[{}, {"properties": {"lhs": "ä", "rhs": "b"}}]
],
"expected": {
"compiled": {
"result": "success",
"isFeatureConstant": false,
"isZoomConstant": true,
"type": "boolean"
},
"outputs": [false, true, true],
"serialized": [
"<",
["string", ["get", "lhs"]],
["string", ["get", "rhs"]],
[
"collator",
{"caseSensitive": true, "diacriticSensitive": false, "locale": "en"}
]
]
}
}
Loading

0 comments on commit 2e107ba

Please sign in to comment.