Skip to content

Commit

Permalink
Fix when an array contains ArrayBuffer arrays as elements
Browse files Browse the repository at this point in the history
  • Loading branch information
twojtasz committed May 8, 2019
1 parent 511f241 commit 3d6ee1e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
6 changes: 5 additions & 1 deletion modules/builder/src/utils/flatten.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@
// limitations under the License.

function isNestedDimension(array) {
return array.length && Array.isArray(array) && Array.isArray(array[0]);
return (
array.length &&
Array.isArray(array) &&
(Array.isArray(array[0]) || ArrayBuffer.isView(array[0]))
);
}

export function flattenToTypedArray(nestedArray, dimensions = 3, ArrayType = Float32Array) {
Expand Down
19 changes: 18 additions & 1 deletion test/modules/builder/utils/flatten.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@
import test from 'tape-catch';
import {flattenToTypedArray} from '@xviz/builder/utils';

const typedArray = Float32Array.from([1, 1, 1, 2, 2, 2, 3, 3, 3]);
const nestedTypedArray = new Array(3);
nestedTypedArray[0] = typedArray.subarray(0, 3);
nestedTypedArray[1] = typedArray.subarray(3, 6);
nestedTypedArray[2] = typedArray.subarray(6);

const FLATTEN_VERTICES_TEST_CASES = [
{
title: 'empty array',
Expand All @@ -30,7 +36,18 @@ const FLATTEN_VERTICES_TEST_CASES = [
title: 'nested one level',
argument: [[1, 2], [1, 2, 3]],
result: [1, 2, 0, 1, 2, 3]
},
{
title: 'typed array',
argument: typedArray,
result: [1, 1, 1, 2, 2, 2, 3, 3, 3]
},
{
title: 'nested typed array',
argument: nestedTypedArray,
result: [1, 1, 1, 2, 2, 2, 3, 3, 3]
}

// {
// title: 'nested empty',
// argument: [1, [1, 2, 3], 3],
Expand All @@ -43,7 +60,7 @@ test('flatten#import', t => {
t.end();
});

test('flatten#flattenToTypedArray', t => {
test.only('flatten#flattenToTypedArray', t => {
for (const tc of FLATTEN_VERTICES_TEST_CASES) {
const result = flattenToTypedArray(tc.argument);
t.deepEqual(result, tc.result, `flattenToTypedArray ${tc.title} returned expected result`);
Expand Down

0 comments on commit 3d6ee1e

Please sign in to comment.