npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

structs.js

v1.0.2

Published

A javascript data type for creating and manipulating bytearrays

Downloads

4

Readme

structs.js

A Javascript data type for creating and manipulating bytearrays

Example usage

import Struct from 'structs.js'

var bottle = new Struct();

var label = bottle.push(new Struct());
var content = bottle.push("B", [119, 0x41, 116, 101, 114]); //push array of unsigned 8bit integers
label.push("c", "WATER"); //push a string, strings are pushed as arrays of uint8 charcodes.

//structs have some getter-methods defined:
var bytes = bottle.array; //[87, 65, 84, 69, 82,  119, 0x41, 116, 101, 114]
var text = bottle.string; //WATERwAter
var length = bottle.byteLength; //10
var buffer = bottle.buffer; //ArrayBuffer {}

//set the byte at offset 1 of the bottle's content to be a char of value "a"
content.set("c", 1, "a"); 
console.log(bottle.string); //WATERwater

label.set("L", 1, 0); //write an unsigned long with value 0 at offset 1 of the label
console.log(bottle.array); //[87, 0, 0, 0, 0,  119, 97, 116, 101, 114]

//create a blob from our struct.
var blob = new Blob([bottle.buffer], {type: "plain/text"});

Endians

struct.push and struct.get take a third boolean argument: littleEndian

//big endian
var struct = new Struct();
struct.push("H", 10000);
console.log(struct.array); // --> [39, 16]

//little endian
var struct = new Struct();
struct.push("H", 10000, true);
console.log(struct.array); // --> [16, 39]
console.log(struct.get("H", 0)); // --> 4135
console.log(struct.get("H", 0, true)); // --> 10000

Format characters

struct.js uses format characters like the Python struct module to define types of values.

var struct = new Struct();
struct.push("B", 3);	//push an unsigned byte with value 3
struct.push("l", 300)	//push a signed long with value 300
struct.push("c", "a")	//push the charcode of "a"

character | size | type ----------|------|------ c | 1 | char (string) b | 1 | signed char B | 1 | unsigned char ? | 1 | bool h | 2 | signed short H | 2 | unsigned short l | 4 | signed long L | 4 | unsigned long f | 4 | float d | 8 | double float

Types are defined in Struct.prototype.TYPES

Reading and writing at offsets

When one pushes a new member, a getter and setter for reading and writing as the different types at specific offsets of the member's bytearray are bound to the member object.

var struct = new Struct();
var bytes = struct.push("B", [65, 66, 67, 68]);
bytes.get("B", 0)			// read as unsigned chars from offset 0: --> 65
bytes.get("H", 0)			// read as unsigned short from offset 0: --> 16706
bytes.get("H", 0, true)     // read as little endian unsigned short from offset 0: --> 16961
bytes.get("c", 1)			// read as char from offset 1: --> "B"

bytes.set("H", 0, 5)		// write unsigned short of value 5 at offset 0
console.log(bytes.array)	// --> [0, 5, 67, 68]

The getter and setter work by applying the prototype-methods of the native DataView type. so someBytes.get("H", 0) will apply DataView.prototype.getUint16(0) to a DataView created from the member's bytearray.

the getters and setters are defined in Struct.prototype.createMember, and you can see which DataView.prototype-method is applied by looking up the format-character of your type in Struct.prototype.TYPES.

Struct.prototype.get and Struct.prototype.set do the same thing as a member's getters and setters, however the changes are applied to the total array of the Struct