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

Fall back to spec default if property function's target property is undefined #24

Closed
wants to merge 6 commits into from
Closed
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
34 changes: 23 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

function createFunction(parameters, defaultType) {
function createFunction(parameters, defaultType, specDefault) {
var fun;

if (!isFunctionDefinition(parameters)) {
Expand Down Expand Up @@ -42,10 +42,10 @@ function createFunction(parameters, defaultType) {
}

for (var z in featureFunctions) {
featureFunctionStops.push([featureFunctions[z].zoom, createFunction(featureFunctions[z])]);
featureFunctionStops.push([featureFunctions[z].zoom, createFunction(featureFunctions[z], type, specDefault)]);
}
fun = function(zoom, feature) {
return evaluateExponentialFunction({ stops: featureFunctionStops, base: parameters.base }, zoom)(zoom, feature);
return evaluateExponentialFunction({ stops: featureFunctionStops, base: parameters.base }, zoom)(zoom, feature, specDefault);
};
fun.isFeatureConstant = false;
fun.isZoomConstant = false;
Expand All @@ -58,7 +58,7 @@ function createFunction(parameters, defaultType) {
fun.isZoomConstant = false;
} else {
fun = function(zoom, feature) {
return innerFun(parameters, feature[parameters.property]);
return innerFun(parameters, feature[parameters.property], specDefault);
};
fun.isFeatureConstant = false;
fun.isZoomConstant = true;
Expand All @@ -68,7 +68,11 @@ function createFunction(parameters, defaultType) {
return fun;
}

function evaluateCategoricalFunction(parameters, input) {
function evaluateCategoricalFunction(parameters, input, specDefault) {
if (input === undefined && typeof specDefault !== 'undefined') {
return specDefault;
}

for (var i = 0; i < parameters.stops.length; i++) {
if (input === parameters.stops[i][0]) {
return parameters.stops[i][1];
Expand All @@ -77,14 +81,22 @@ function evaluateCategoricalFunction(parameters, input) {
return parameters.stops[0][1];
}

function evaluateIntervalFunction(parameters, input) {
function evaluateIntervalFunction(parameters, input, specDefault) {
if (input === undefined && typeof specDefault !== 'undefined') {
return specDefault;
}

for (var i = 0; i < parameters.stops.length; i++) {
if (input < parameters.stops[i][0]) break;
}
return parameters.stops[Math.max(i - 1, 0)][1];
}

function evaluateExponentialFunction(parameters, input) {
function evaluateExponentialFunction(parameters, input, specDefault) {
if (input === undefined && typeof specDefault !== 'undefined') {
return specDefault;
}

var base = parameters.base !== undefined ? parameters.base : 1;

var i = 0;
Expand Down Expand Up @@ -156,10 +168,10 @@ function isFunctionDefinition(value) {

module.exports.isFunctionDefinition = isFunctionDefinition;

module.exports.interpolated = function(parameters) {
return createFunction(parameters, 'exponential');
module.exports.interpolated = function(parameters, specDefault) {
return createFunction(parameters, 'exponential', specDefault);
};

module.exports['piecewise-constant'] = function(parameters) {
return createFunction(parameters, 'interval');
module.exports['piecewise-constant'] = function(parameters, specDefault) {
return createFunction(parameters, 'interval', specDefault);
};
83 changes: 81 additions & 2 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ var MapboxGLFunction = require('../').interpolated;

test('function types', function(t) {

t.test('contant', function(t) {
t.test('constant', function(t) {

t.test('range types', function(t) {

Expand Down Expand Up @@ -107,6 +107,56 @@ test('function types', function(t) {
t.end();
});

t.test('property', function(t) {
var f = MapboxGLFunction({
type: 'exponential',
stops: [[0, 0], [1000, 3000]],
property: 'prop'
});

t.equal(f(10, { prop: 0 }), 0);
t.equal(f(10, { prop: 1 }), 3);
t.equal(f(10, { prop: 100 }), 300);
t.equal(f(10, { prop: 1000 }), 3000);
t.equal(f(10, { prop: 3000 }), 3000);
t.equal(f(10, { prop: undefined }), 3000);

t.end();
});

t.test('property with reference default', function(t) {
var f = MapboxGLFunction({
type: 'exponential',
stops: [[0, 0], [1000, 3000]],
property: 'prop'
}, 10);

t.equal(f(10, { prop: 0 }), 0);
t.equal(f(10, { prop: 1 }), 3);
t.equal(f(10, { prop: 100 }), 300);
t.equal(f(10, { prop: 1000 }), 3000);
t.equal(f(10, { prop: 3000 }), 3000);
t.equal(f(10, { prop: undefined }), 10);

t.end();
});

t.test('property with 0 reference default', function(t) {
var f = MapboxGLFunction({
type: 'exponential',
stops: [[0, 0], [1000, 3000]],
property: 'prop'
}, 0);

t.equal(f(10, { prop: 0 }), 0);
t.equal(f(10, { prop: 1 }), 3);
t.equal(f(10, { prop: 100 }), 300);
t.equal(f(10, { prop: 1000 }), 3000);
t.equal(f(10, { prop: 3000 }), 3000);
t.equal(f(10, { prop: undefined }), 0);

t.end();
});
});

t.test('zoom + data stops', function(t) {
Expand Down Expand Up @@ -177,6 +227,34 @@ test('function types', function(t) {
t.end();
});

t.test('default', function(t) {
var f = MapboxGLFunction({
type: 'exponential',
property: 'prop',
base: 1,
stops: [
[{ zoom: 15, value: 0}, 0],
[{ zoom: 15, value: 1000}, 0],
[{ zoom: 15.1, value: 0}, 0],
[{ zoom: 15.1, value: 10}, 30],
[{ zoom: 15.1, value: 1000}, 3000]]
}, 3);

t.equal(f(15, { prop: 0 }), 0);
t.equal(f(15, { prop: 10 }), 0);
t.equal(f(15, { prop: 300 }), 0);
t.equal(f(15.2, { prop: 0 }), 0);
t.equal(f(15.2, { prop: 10 }), 30);
t.equal(f(15.2, { prop: undefined }), 3);
t.equal(f(15.2, { prop: 299 }), 897);
t.equal(f(16, { prop: 0 }), 0);
t.equal(f(16, { prop: 10 }), 30);
t.equal(f(16, { prop: undefined }), 3);
t.equal(f(16, { prop: 299 }), 897);

t.end();
});

t.test('fractional zoom', function(t) {
var f = MapboxGLFunction({
type: 'exponential',
Expand Down Expand Up @@ -308,9 +386,10 @@ test('property', function(t) {
type: 'categorical',
stops: [['map', 'neat'], ['box', 'swell']],
property: 'mapbox'
});
}, 'cool');

t.equal(f({}, {mapbox: 'box'}), 'swell');
t.equal(f({}, {mapbox: undefined}), 'cool');

t.end();
});
Expand Down