Last active
October 14, 2021 03:28
-
-
Save yoya/0755190bc800e4e8eba327ddcd395fbe to your computer and use it in GitHub Desktop.
typedarray concat polyfill
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
(function() { | |
for (typedarr of [Int8Array, Uint8Array, Uint8ClampedArray, | |
Int16Array, Uint16Array, Int32Array, Uint32Array, | |
Float32Array, Float64Array]) { | |
if (!typedarr.prototype.concat) { | |
Object.defineProperty(typedarr.prototype, 'concat', { | |
value: function(b) { | |
const a = this; | |
if (!b || !b.length) return a.slice(0); | |
const c = new a.constructor(a.length + b.length); | |
c.set(a); c.set(b, a.length); | |
return c; | |
} | |
}) | |
} | |
} | |
})(); | |
const a = Uint8Array.from([11]); | |
const b = Int16Array.from([22, 33]); | |
const c = Float64Array.from([44, 55]); | |
const d = a.concat(c); | |
const e = b.concat(c); | |
console.log(a, b, c); | |
console.log(d, e); | |
% node typedarray_concat.js
Uint8Array(1) [ 11 ] Int16Array(2) [ 22, 33 ] Float64Array(2) [ 44, 55 ]
Uint8Array(3) [ 11, 44, 55 ] Int16Array(4) [ 22, 33, 44, 55 ]
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
% node typedarray_concat.js
Uint8Array(2) [ 11, 22 ] Uint16Array(3) [ 33, 44, 55 ]
Uint8Array(5) [ 11, 22, 33, 44, 55 ]