Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- interpolates typed arrays (Float64Array, etc) and standard arrays that contain only numbers with a simple for… loop. - autodetects both situations (we could optionally unplug the number array detection) - not compatible with BigInt typed arrays (#75)
- Loading branch information
Showing
7 changed files
with
83 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
export default function(a, b) { | ||
if (!b) b = []; | ||
var na = a ? Math.min(b.length, a.length) : 0, | ||
c = b.slice(); | ||
let i; | ||
return function(t) { | ||
for (i = 0; i < na; ++i) c[i] = a[i] + (b[i] - a[i]) * t; | ||
return c; | ||
}; | ||
} | ||
|
||
export function isNumberArray(x) { | ||
if (ArrayBuffer.isView(x)) return !(x instanceof DataView); | ||
return Array.isArray(x) && x.every(i => typeof i == "number"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
var tape = require("tape"), | ||
interpolate = require("../"); | ||
|
||
tape("interpolateNumberArray(a, b) interpolates defined elements in a and b", function(test) { | ||
test.deepEqual(interpolate.interpolateNumberArray(Float64Array.from([2, 12]), Float64Array.from([4, 24]))(0.5), Float64Array.from([3, 18])); | ||
test.end(); | ||
}); | ||
|
||
tape("interpolateNumberArray(a, b) ignores elements in a that are not in b", function(test) { | ||
test.deepEqual(interpolate.interpolateNumberArray(Float64Array.from([2, 12, 12]), Float64Array.from([4, 24]))(0.5), Float64Array.from([3, 18])); | ||
test.end(); | ||
}); | ||
|
||
tape("interpolateNumberArray(a, b) uses constant elements in b that are not in a", function(test) { | ||
test.deepEqual(interpolate.interpolateNumberArray(Float64Array.from([2, 12]), Float64Array.from([4, 24, 12]))(0.5), Float64Array.from([3, 18, 12])); | ||
test.end(); | ||
}); | ||
|
||
tape("interpolateNumberArray(a, b) treats undefined as an empty array", function(test) { | ||
test.deepEqual(interpolate.interpolateNumberArray(undefined, [2, 12])(0.5), [2, 12]); | ||
test.deepEqual(interpolate.interpolateNumberArray([2, 12], undefined)(0.5), []); | ||
test.deepEqual(interpolate.interpolateNumberArray(undefined, undefined)(0.5), []); | ||
test.end(); | ||
}); | ||
|
||
tape("interpolateNumberArray(a, b) uses b’s array type", function(test) { | ||
test.ok(interpolate.interpolateNumberArray(Float64Array.from([2, 12]), Float64Array.from([4, 24, 12]))(0.5) instanceof Float64Array); | ||
test.ok(interpolate.interpolateNumberArray(Float64Array.from([2, 12]), Float32Array.from([4, 24, 12]))(0.5) instanceof Float32Array); | ||
test.ok(interpolate.interpolateNumberArray(Float64Array.from([2, 12]), Uint8Array.from([4, 24, 12]))(0.5) instanceof Uint8Array); | ||
test.ok(interpolate.interpolateNumberArray(Float64Array.from([2, 12]), Uint16Array.from([4, 24, 12]))(0.5) instanceof Uint16Array); | ||
test.end(); | ||
}); | ||
|
||
tape("interpolateNumberArray(a, b) works with unsigned data", function(test) { | ||
test.deepEqual(interpolate.interpolateNumberArray(Uint8Array.from([1, 12]), Uint8Array.from([255, 0]))(0.5), Uint8Array.from([128, 6])); | ||
test.end(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters