/*eslint new-cap:0*/ var dtype = require('dtype') module.exports = flattenVertexData function flattenVertexData (data, output, offset) { if (!data) throw new TypeError('must specify data as first parameter') offset = +(offset || 0) | 0 if (Array.isArray(data) && (data[0] && typeof data[0][0] === 'number')) { var dim = data[0].length var length = data.length * dim var i, j, k, l // no output specified, create a new typed array if (!output || typeof output === 'string') { output = new (dtype(output || 'float32'))(length + offset) } var dstLength = output.length - offset if (length !== dstLength) { throw new Error('source length ' + length + ' (' + dim + 'x' + data.length + ')' + ' does not match destination length ' + dstLength) } for (i = 0, k = offset; i < data.length; i++) { for (j = 0; j < dim; j++) { output[k++] = data[i][j] === null ? NaN : data[i][j] } } } else { if (!output || typeof output === 'string') { // no output, create a new one var Ctor = dtype(output || 'float32') // handle arrays separately due to possible nulls if (Array.isArray(data) || output === 'array') { output = new Ctor(data.length + offset) for (i = 0, k = offset, l = output.length; k < l; k++, i++) { output[k] = data[i] === null ? NaN : data[i] } } else { if (offset === 0) { output = new Ctor(data) } else { output = new Ctor(data.length + offset) output.set(data, offset) } } } else { // store output in existing array output.set(data, offset) } } return output }