Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Less verbose function -> expression conversion #7337

Merged
merged 2 commits into from
Sep 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions src/style-spec/function/convert.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import type {StylePropertySpecification} from '../style-spec';

export default convertFunction;

function convertLiteral(value) {
return typeof value === 'object' ? ['literal', value] : value;
}

function convertFunction(parameters: any, propertySpec: StylePropertySpecification) {
let stops = parameters.stops;
if (!stops) {
Expand All @@ -20,7 +24,7 @@ function convertFunction(parameters: any, propertySpec: StylePropertySpecificati
if (!featureDependent && propertySpec.tokens && typeof stop[1] === 'string') {
return [stop[0], convertTokenString(stop[1])];
}
return [stop[0], ['literal', stop[1]]];
return [stop[0], convertLiteral(stop[1])];
});

if (zoomAndFeatureDependent) {
Expand Down Expand Up @@ -48,7 +52,7 @@ function convertIdentityFunction(parameters, propertySpec): Array<mixed> {
parameters.default
];
} else {
const expression = [propertySpec.type === 'color' ? 'to-color' : propertySpec.type, get, ['literal', parameters.default]];
const expression = [propertySpec.type === 'color' ? 'to-color' : propertySpec.type, get, convertLiteral(parameters.default)];
if (propertySpec.type === 'array') {
expression.splice(1, 0, propertySpec.value, propertySpec.length || null);
}
Expand Down Expand Up @@ -126,14 +130,14 @@ function convertPropertyFunction(parameters, propertySpec, stops) {
for (const stop of stops) {
expression.push(['==', get, stop[0]], stop[1]);
}
expression.push(['literal', coalesce(parameters.default, propertySpec.default)]);
expression.push(convertLiteral(coalesce(parameters.default, propertySpec.default)));
return expression;
} else if (type === 'categorical') {
const expression = ['match', get];
for (const stop of stops) {
appendStopPair(expression, stop[0], stop[1], false);
}
expression.push(['literal', coalesce(parameters.default, propertySpec.default)]);
expression.push(convertLiteral(coalesce(parameters.default, propertySpec.default)));
return expression;
} else if (type === 'interval') {
const expression = ['step', ['number', get]];
Expand All @@ -145,7 +149,7 @@ function convertPropertyFunction(parameters, propertySpec, stops) {
'case',
['==', ['typeof', get], 'number'],
expression,
['literal', parameters.default]
convertLiteral(parameters.default)
];
} else if (type === 'exponential') {
const base = parameters.base !== undefined ? parameters.base : 1;
Expand All @@ -157,7 +161,7 @@ function convertPropertyFunction(parameters, propertySpec, stops) {
'case',
['==', ['typeof', get], 'number'],
expression,
['literal', parameters.default]
convertLiteral(parameters.default)
];
} else {
throw new Error(`Unknown property function type ${type}`);
Expand Down
44 changes: 44 additions & 0 deletions test/unit/style-spec/migrate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,50 @@ t('converts token strings to expressions', (t) => {
t.end();
});

t('converts stop functions to expressions', (t) => {
const migrated = migrate({
version: 8,
layers: [{
id: '1',
type: 'background',
paint: {
'background-opacity': {
base: 1.0,
stops: [[0, 1], [10, 0.72]]
}
}
}, {
id: '2',
type: 'background',
paint: {
'background-opacity': {
base: 1.0,
stops: [[0, [1, 2]], [10, [0.72, 0.98]]]
}
}
}]
}, spec.latest.$version);
t.deepEqual(migrated.layers[0].paint['background-opacity'], [
'interpolate',
['exponential', 1],
['zoom'],
0,
1,
10,
0.72
]);
t.deepEqual(migrated.layers[1].paint['background-opacity'], [
'interpolate',
['exponential', 1],
['zoom'],
0,
['literal', [1, 2]],
10,
['literal', [0.72, 0.98]]
]);
t.end();
});

glob.sync(`${__dirname}/fixture/v7-migrate/*.input.json`).forEach((file) => {
t(path.basename(file), (t) => {
const outputfile = file.replace('.input', '.output');
Expand Down