Skip to content

Commit

Permalink
Fix identity functions for 'text-field' (i.e. 'formatted' type)
Browse files Browse the repository at this point in the history
Fixes issue #7311.
  • Loading branch information
ChrisLoer committed Oct 1, 2018
1 parent beae7b7 commit 0e58a16
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/style-spec/function/index.js
Expand Up @@ -5,6 +5,7 @@ import extend from '../util/extend';
import getType from '../util/get_type';
import * as interpolate from '../util/interpolate';
import Interpolate from '../expression/definitions/interpolate';
import Formatted from '../expression/types/formatted';
import { supportsInterpolation } from '../util/properties';

export function isFunction(value) {
Expand Down Expand Up @@ -194,6 +195,8 @@ function evaluateExponentialFunction(parameters, propertySpec, input) {
function evaluateIdentityFunction(parameters, propertySpec, input) {
if (propertySpec.type === 'color') {
input = Color.parse(input);
} else if (propertySpec.type === 'formatted') {
input = Formatted.fromString(input.toString());
} else if (getType(input) !== propertySpec.type && (propertySpec.type !== 'enum' || !propertySpec.values[input])) {
input = undefined;
}
Expand Down
17 changes: 17 additions & 0 deletions test/unit/style-spec/function.test.js
@@ -1,6 +1,7 @@
import { test } from 'mapbox-gl-js-test';
import { createFunction } from '../../../src/style-spec/function';
import Color from '../../../src/style-spec/util/color';
import Formatted from '../../../src/style-spec/expression/types/formatted';

test('binary search', (t) => {
t.test('will eventually terminate.', (t) => {
Expand Down Expand Up @@ -989,6 +990,22 @@ test('identity function', (t) => {
t.end();
});

t.test('formatted', (t) => {
const f = createFunction({
property: 'foo',
type: 'identity'
}, {
type: 'formatted'
}).evaluate;

t.deepEqual(f({zoom: 0}, {properties: {foo: 'foo'}}), Formatted.fromString('foo'));
t.deepEqual(f({zoom: 1}, {properties: {foo: 'bar'}}), Formatted.fromString('bar'));
t.deepEqual(f({zoom: 2}, {properties: {foo: 2}}), Formatted.fromString('2'));
t.deepEqual(f({zoom: 3}, {properties: {foo: true}}), Formatted.fromString('true'));

t.end();
});

t.end();
});

Expand Down

0 comments on commit 0e58a16

Please sign in to comment.