Skip to content

Commit

Permalink
Resolve regressions re: invalid inputs -> default values
Browse files Browse the repository at this point in the history
  • Loading branch information
Anand Thakker committed Jun 22, 2017
1 parent 3f1ef06 commit d2692a0
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 4 deletions.
12 changes: 11 additions & 1 deletion src/style-spec/function/convert.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,22 @@ function convertPropertyFunction(parameters, propertySpec) {
firstStopType === 'boolean'
);

expression.push([firstStopType, ['get', parameters.property]]);
const expectedTypeName = firstStopType.slice(0, 1).toUpperCase() + firstStopType.slice(1);
const checkType = ['==', expectedTypeName, ['typeof', ['get', parameters.property]]];
expression.push([
'case',
checkType, [firstStopType, ['get', parameters.property]],
null
]);

for (const stop of parameters.stops) {
expression.push(stop[0], stop[1]);
}

if (expression[0] === 'match') {
expression.push(parameters.default);
}

return expression;
}

Expand Down
4 changes: 4 additions & 0 deletions src/style-spec/function/evaluation_context.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ function assert(condition, message) {
}

module.exports = () => ({
assert: assert,
error: (msg) => assert(false, msg),

at: function (index, arrayOrVector) {
Expand Down Expand Up @@ -112,7 +113,10 @@ module.exports = () => ({
},

evaluateCurve(input, stopInputs, stopOutputs, interpolation, resultType) {
assert(input !== null, 'Cannot evaluate function on null input value.');

const stopCount = stopInputs.length;
if (stopInputs.length === 1) return stopOutputs[0]();
if (input <= stopInputs[0]) return stopOutputs[0]();
if (input >= stopInputs[stopCount - 1]) return stopOutputs[stopCount - 1]();

Expand Down
2 changes: 0 additions & 2 deletions src/style-spec/function/expressions.js
Original file line number Diff line number Diff line change
Expand Up @@ -327,8 +327,6 @@ const expressions: { [string]: Definition } = {
outputs.push(output.js);
}

if (stops.length === 1) return {js: `${outputs[0]}`};

const interpolationOptions: Object = {
name: interpolation.name
};
Expand Down
2 changes: 1 addition & 1 deletion src/style-spec/function/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ function createFunction(parameters, propertySpec) {
if (compiled.result === 'success') {
const f = function (zoom, properties) {
const val = compiled.function({zoom}, {properties});
return val;
return val === null ? undefined : val;
};
f.isFeatureConstant = compiled.isFeatureConstant;
f.isZoomConstant = compiled.isZoomConstant;
Expand Down

0 comments on commit d2692a0

Please sign in to comment.