array-encode-decode
v1.0.2
Published
The array-encode-decode library is a versatile JavaScript tool designed for creating 1D, 2D, or 3D arrays, as well as encoding or decoding them along with optional metadata. The library employs an efficient Run-Length Encoding algorithm for encoding and d
Downloads
1
Readme
Array Encode / Decode
A versatile package for creating, encoding, and decoding arrays of various dimensions.
Installation
npm install array-encode-decode
Available Methods
Method | Description ------------- | ------------- create1D | Creates a 1-Dimensional array along the x-axis create2D | Creates a 2-Dimensional array along the x and y axes create3D | Creates a 3-Dimensional array along the x, y, and z axes encode | Encodes an array and returns its run-length encoded representation along with its metadata (optional) decode | Decodes a run-length encoded array and its metadata to its original form
Usage
Import the package as follows:
const ArrayEncoderDecoder = require("array-encode-decode");
Creating a 1D array
The create1D
method creates a 1-dimensional array of a specified size along the x-axis. The values field is optional. If not provided, the array will be populated with the default value - 0.
let x = 10; // size along the x-axis
let value = 2; // example value (optional)
let array1D = ArrayEncoderDecoder.create1D(x, value);
Creating a 2D array
The create2D
method creates a 2-dimensional array of a specified size along the x and y axes. The values field is optional. If not provided, the array will be populated with the default value - 0.
let x = 5; // size along the x-axis
let y = 4; // size along the y-axis
let value = 3; // example value (optional)
let array2D = ArrayEncoderDecoder.create2D(x, y, value);
Creating a 3D array
The create3D
method creates a 3-dimensional array of a specified size along the x, y, and z axes. The values field is optional. If not provided, the array will be populated with the default value - 0.
let x = 3; // size along the x-axis
let y = 2; // size along the y-axis
let z = 1; // size along the z-axis
let value = 4; // example value (optional)
let array3D = ArrayEncoderDecoder.create3D(x, y, z, value);
Encoding an Array
let inputArray = ArrayEncoderDecoder.create3D(3, 2, 1);
let encodedArray = ArrayEncoderDecoder.encode(inputArray);
console.log("Encoded Array Size:", ArrayEncoderDecoder.displayBytes(JSON.stringify(encodedArray).length));
Decoding an Array
let decodedArray = ArrayEncoderDecoder.decode(encodedArray);
console.log("Decoded Array:", decodedArray);