Mutar
MutableTypedArray - /mu'taɾ/, spanish for to change, mutate
Mutar is a toolkit to interact with typed arrays and modify them (or let's say a kit to emulate modification) and a constructor for a special object. It is a very convenient way to handle binary data. If constructed, the array behaves pretty much as a regular array. You can concatenate, pop, shift, unshift... On top of that the type can be changed from - let's say - Uint8 to Float64.
Mutability
In reality TypedArrays are not mutable in terms of growing and shrinking. To emulate mutability a new array with the desired properties is created. This comes to a price of course. Every time the array length "changes", a new TypedArray is allocated in memory. Keep that in mind when using it, if this is critical to you.
Endianness
Mutar objects are designed to be aware of endianness. If not specified, the the endianness of the system is used, which is most likely little endian. Despite this fact, sometimes data (e.g. network related) differ in its endianness. It is possible to store them in a Mutar object, interact with it but keep the given byte order. (Values which are added or got are converted to the according endianness).
Installation
GitHub
git clone https://github.com/UmamiAppearance/MutarJS.git
npm
Builds
The GitHub repository has ready to use builds included. You can find them in dist. The npm package comes without pre build files.
For building you have to run:
Either way you have two builds available (esm and iife), plus a minified version of each.
Mutar.esm.js
Mutar.esm.min.js
Mutar.iife.js
Mutar.iife.min.js
Importing
Node
// common js
const Mutar = require("mutar");
// ES6 import
import Mutar from "mutar";
Browser
<!-- iife / classic script tag -->
<script src="Mutar.iife(.min).js"></script>
// ES6
import Mutar from "./path/Mutar.esm(.min).js"
Usage
Toolkit
If you want to work with an existing TypedArray, you can use Mutar to analyse and modify it. Let's for example take a Uint32Array which holds the integer 400. A little remark: If you plan to do a lot of manipulation to a single array, you should strongly consider to use the Mutar Object.
const Uint32 = new Uint32Array([400]);
Mutar comes with some functions to analyse the given object. (Let us forget for a moment that we exactly know what kind of object it is).
Analysis
const Uint32 = new Uint32Array([400]);
const regularArray = [400];
// Test if is a TypedArray //
Mutar.isTypedArray(Uint32); // -> true
Mutar.isTypedArray(regularArray); // -> false
// Get the type //
Mutar.getType(Uint32); // -> "Uint32Array"
// Test if array is of type foo //
// test for Uint32Array
Mutar.isTypeOf(Uint32, "Uint32Array"); // -> true
// you can also use the Uint32 constructor as input
Mutar.isTypeOf(Uint32, Uint32Array); // -> true
// test for Int8Array (this time a shortcut for the type)
Mutar.isTypeOf(Uint32, "Int8"); // -> false
// test the "wrong" type
Mutar.isTypeOf(regularArray, Uint32Array); // -> false
Let's now take a look at the fun part: the modification.
Modification
One major focus of Mutar is to be aware of endianness. For simplicity, there is only a small section at the beginning of this chapter, that is going into this feature. The other here introduced functions are not using the opportunity to manipulate endianness, but that doesn't mean that it is not there. Every function, that manipulates the bytes has the possibility to set the integers in either little or big endian. If nothing is specified, the endianness of the system is getting used.
// Again we are taking the Uint32Array of the Integer 400
// The individual bytes for this number are:
// [ 144, 1, 0, 0 ] (little endian byte order)
const Uint32 = new Uint32Array([400]); // -> Uint32Array(1) [ 400 ]
// Easy start: create a copy (clone) of a TypedArray
// (changes on the clone will not affect the original)
const clone = Mutar.clone(Uint32); // -> Uint32Array(1) [ 400 ]
// Change endianness
Mutar.flipEndianness(clone); // -> Uint32Array(1) [ 2415984640 ]
// Determine the endianness of the System
const littleEndian = Mutar.SYS_LITTLE_ENDIAN; // -> true (most likely)
const bigEndian = !littleEndian; // -> false
// Get the integer at index 0 (little or big endian is getting passed here,
// which is only done for universal correctness. Just pass true for little
// and false for big endian)
Mutar.at(Uint32, 0, littleEndian); // -> 400
Mutar.at(clone, 0, littleEndian); // -> 2415984640
Mutar.at(clone, 0, bigEndian); // -> 400
// Set single integers (Uint32 has the systems endianness, so no need to specify)
Mutar.setAt(Uint32, 0, 500);
Mutar.setAt(clone, 0, 500, bigEndian);
// Now let's set back the endianness of the clone and test if it is equal to
// the original Uint32
Uint32[0] === clone[0] // -> false (500 === 4093706240)
Mutar.flipEndianness(clone); // -> Uint32Array(1) [ 500 ]
Uint32[0] === clone[0] // -> true
// As already mentioned, you have the ability to specify the endianness
// for almost every function. From now on it is no longer used, everything
// is done with the systems default endianness. Before we continue, let us
// set back clone and original to 400.
Uint32[0] = 400;
clone[0] = 400;
// Concatenation of arrays (let's join the original, the clone and a fresh array)
const fresh = new Uint16Array([500, 600]); // -> Uint16Array(2) [500, 600]
Mutar.concat(Uint32, clone, fresh); // -> TypeError
// Seems like the objects needs to be converted before they fit together.
let freshUint32 = Mutar.convert(fresh, "Uint32"); // -> int32Array(1) [ 39322100 ]
// Two Uint16 values fit in one Uint32 (or to be more clear:
// the underlying buffer has 4 integers [ 244, 1, 88, 2 ],
// which converts into one Uint32 integer), but this is not
// what we want in this case.
// Luckily we can convert in another mode -> "intMode"
freshUint32 = Mutar.convert(fresh, "Uint32", true); // -> Uint32Array(2) [500, 600]
// Now we can concat.
let concat = Mutar.concat(Uint32, clone, freshUint32); // -> Uint32Array(4) [ 400, 400, 500, 600 ]
// If the conversions are that simple as in this case,
// we can tell the concat function to force conversion
// on the fly. By literally passing the string "force".
// Also the "intMode" can be activated by passing the
// string (by using "intMode", you don't have to pass
// "force" anymore, as the intention is clear, but it
// doesn't hurt either).
concat = Mutar.concat(concat, new Uint16Array([700]), "intMode");
// -> Uint32Array(4) [ 400, 400, 500, 600, 700 ]
// Let's take a closer look at the type conversion.
// The focus is now on the regular mode
// conversion (e.g. BigInt64)
const bigInt = Mutar.convert(concat, "BigInt"); // -> BigInt64Array(2) [ 1717986918800n, 2576980378100n, 700n ]
// The original "concat" array has a byte length of 20, BingIntArrays
// byte lengths must be devisable by 8. Mutar is using zero padding to
// make this fit (zeros are added to the end of the ArrayBuffer or at
// the beginning for big endian). So when you convert the BigInt back
// to Uint32 it looks like this.
let Uint32fromBigInt = Mutar.convert(bigInt, "Uint32"); // -> Uint32Array(5) [ 400, 400, 500, 600, 700, 0 ]
// To remove this padded zero we can use the "trim" function
Uint32fromBigInt = Mutar.trim(Uint32fromBigInt); // -> Uint32Array(5) [ 400, 400, 500, 600, 700 ]
// We can get rid of this zero also during conversion by setting
// the third param "trim" to be true.
Uint32fromBigInt = Mutar.convert(bigInt, "Uint32", false, true);
// -> Uint32Array(5) [ 400, 400, 500, 600, 700 ]
// Let us now remove the repetitive 400 and position 1
// (the function returns an array with the new typed array
// at position 0 and the detached value an position 1)
let cleanedUint32, detached;
[cleanedUint32, detached] = Mutar.detach