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

Allow for arbitrary properties in functions [do not merge] #1

Closed
wants to merge 3 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
42 changes: 31 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,39 +18,59 @@ function interpolateArray(a, b, t) {
return result;
}

function zip(a, b) {
var result = [];
for (var i = 0; i < Math.min(a.length, b.length); i++) {
result.push([a[i], b[i]]);
}
return result;
}

exports['is'] = function(f) {
return (f.domain && f.range) || !!f.stops;
}

exports['interpolated'] = function(f) {
if (!f.stops) {
var stops;
if (f.domain && f.range) {
stops = zip(f.domain, f.range);
} else if (f.stops) {
stops = f.stops
} else {
return constant(f);
}

var stops = f.stops,
base = f.base || 1,
var base = f.base || 1,
interpolate = Array.isArray(stops[0][1]) ? interpolateArray : interpolateNumber;

return function(z) {
// find the two stops which the current z is between
return function(z, properties) {
// If this is a property scale, get the property value; otherwise get
// the zoom value.
var value = f.property ? properties[f.property] : z;

// find the two stops which the current value is between
var low, high;

for (var i = 0; i < stops.length; i++) {
var stop = stops[i];

if (stop[0] <= z) {
if (stop[0] <= value) {
low = stop;
}

if (stop[0] > z) {
if (stop[0] > value) {
high = stop;
break;
}
}

if (low && high) {
var zoomDiff = high[0] - low[0],
zoomProgress = z - low[0],
var valueDiff = high[0] - low[0],
valueProgress = value - low[0],

t = base === 1 ?
zoomProgress / zoomDiff :
(Math.pow(base, zoomProgress) - 1) / (Math.pow(base, zoomDiff) - 1);
valueProgress / valueDiff :
(Math.pow(base, valueProgress) - 1) / (Math.pow(base, valueDiff) - 1);

return interpolate(low[1], high[1], t);

Expand Down
40 changes: 40 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,36 @@ test('interpolated, single stop', function(t) {
t.end();
});

test('interpolated, single range element', function(t) {
var f = func.interpolated({domain: [1], range: [1]});
t.equal(f(0), 1);
t.equal(f(1), 1);
t.equal(f(3), 1);
t.end();
});

test('interpolated, multiple range elements', function(t) {
var f = func.interpolated({domain: [1,2], range: [1,2]});
t.equal(f(1), 1);
t.equal(f(1.5), 1.5);
t.equal(f(2), 2);
t.end();
});

test('interpolated, different number of domain and range elements', function(t) {
var f = func.interpolated({domain: [1,2,-1000], range: [1,2]});
t.equal(f(1), 1);
t.equal(f(1.5), 1.5);
t.equal(f(2), 2);

f = func.interpolated({domain: [1,2], range: [1,2,-1000]});
t.equal(f(1), 1);
t.equal(f(1.5), 1.5);
t.equal(f(2), 2);

t.end();
});

test('interpolated, default base', function(t) {
var f = func.interpolated({stops: [[1, 1], [5, 10]]});
t.equal(f(0), 1);
Expand All @@ -45,6 +75,16 @@ test('interpolated, specified base', function(t) {
t.end();
});

test('interpolated, specified property', function(t) {
var f = func.interpolated({stops: [[1, 1], [5, 10]], base: 2, property: 'baz'});
t.equal(f(0, {baz: 0}), 1);
t.equal(f(0, {baz: 1}), 1);
t.equal(f(0, {baz: 3}), 2.8);
t.equal(f(0, {baz: 5}), 10);
t.equal(f(0, {baz: 11}), 10);
t.end();
});

test('interpolated, array', function(t) {
var f = func.interpolated({stops: [[1, [1, 2]], [5, [5, 10]]]});
t.deepEqual(f(0), [1, 2]);
Expand Down