Skip to content

Commit

Permalink
Use categorical type by default when input values are strings
Browse files Browse the repository at this point in the history
fixes #19
  • Loading branch information
Lucas Wojciechowski committed Oct 14, 2016
1 parent 111a2b4 commit d6dc680
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ function createFunction(parameters, defaultType) {
var zoomAndFeatureDependent = parameters.stops && typeof parameters.stops[0][0] === 'object';
var featureDependent = zoomAndFeatureDependent || parameters.property !== undefined;
var zoomDependent = zoomAndFeatureDependent || !featureDependent;
var type = parameters.type || defaultType || 'exponential';
var inputType = parameters.stops && typeof (zoomAndFeatureDependent ? parameters.stops[0][0].property : parameters.stops[0][0]);
var type = parameters.type || defaultType || (inputType === 'string' ? 'categorical' : 'exponential');

var innerFun;
if (type === 'exponential') {
Expand Down
22 changes: 22 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,17 @@ test('function types', function(t) {

t.test('exponential', function(t) {

t.test('is the default type for numeric inputs', function(t) {
var f = MapboxGLFunction({
stops: [[1, 2], [3, 6]],
base: 2
});

t.equal(f(2), 30 / 9);

t.end();
});

t.test('base', function(t) {
var f = MapboxGLFunction({
type: 'exponential',
Expand Down Expand Up @@ -256,6 +267,17 @@ test('function types', function(t) {

t.test('categorical', function(t) {

t.test('is the default type for string inputs', function(t) {
var f = MapboxGLFunction({
type: 'categorical',
stops: [['umpteen', 42]]
});

t.equal(f('umpteen'), 42);

t.end();
});

t.test('one element', function(t) {
var f = MapboxGLFunction({
type: 'categorical',
Expand Down

0 comments on commit d6dc680

Please sign in to comment.