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

jpstruct

v0.0.6

Published

WebAPI port of Python's struct

Downloads

3

Readme

jpstruct (Js-Python Struct)

Vanilla JavaScript port of Python's struct. Implemented as an ESM module for simple usage into the browser.

Requires support for BigInt and BigUint64Array from ES2020 for Q and q format characters.

Input and Output

Uint8Arrays are used for byte-level representations.

Unpacking operations return an Array containing the the unpacked data in the same order as the format string.

Differences from Python's struct

In Python, the empty array [] is truthy, while in JavaScript it is falsy. This means that struct.pack('?',[]) in Python has a different result to jpstruct.pack('?',[]); in JavaScript.

Values that return true from Number.isInteger() will be coerced into integers for integer format characters. This breaks with Python behaviour where attempting to pack a float would raise an error.

The native size format characters n and N are interpreted the as i and I, i.e. the platform is assumed to use 32-bit ssize_t and size_t types. Similarly, the pointer format character P is also assumed to be 32-bit and is interpreted as I.

The half-precision floating point format character e is not supported. It will be treated as an unsigned short (H format character).

The perl-string format character p is not supported.

Format String

The format string format is the same as Python's implementation with a few caveats detailed below.

Byteorder Marker

The leading character of a format string can be used to indicate the byte order of the underlying data. In Python's implementation, this can also determine additional size and alignment variations. In this implementation, no alignment padding is added to any of the variants. Additionally, standard sizes apply for all cases.

Character | Endianness ----------|--------------------------------- @ | (Platform determined) = | (Platform determined) < | Little-endian > | Big-endian ! | Big-endian (network byte order)

As in Python, if the leading character is not one of the options in the table, the functions will behave as if the leading character was @.

Format Characters

The remainder of the format string is made up of format characters and an optional count preceding each format character. The count is a base-10 integer that effectively repeats the following format character. For strings (s), the count is used to specify the length of the string.

Format Character | C Type | Javascript Type -----------------|----------------------------|------------------------------------ x | - | Used to add padding byte to format c | char | string (length 1) b | int8_t | number B | uint8_t | number ? | bool (encoded as 1 byte) | boolean h | int16_t | number H | uint16_t | number i | int32_t | number I | uint32_t | number l | int32_t | number L | uint32_t | number q | int64_t | bigint Q | uint64_t | bigint n | int32_t | number N | uint32_t | number e | binary16 | number f | float | number d | double | number s | char[] | Uint8Array P | void* | number

To convert the Uint8Array returned from a s format string:

let unpacked = jpstruct.unpack('4s',Uint8Array.from([0x20,0x20,0x20,0x20]));
let result = String.fromCharCode(...unpacked[0]);

Examples

Pack a string

import * as jpstruct from '../jpstruct.js';

let packed = jpstruct.pack('10s','helloworld');

Pack into a supplied buffer

const orderedfields =  [253,13,0,0,40,11,10,33280,0];
const formatter = new jpstruct.Struct('<BBBBBBBHB');

let writable_buf = new Uint8Array(100);

formatter.pack_into(writable_buf,0,...orderedfields);

Pack a BigInt

const packed = jpstruct.pack('<Q',72340172838076673n);

Tests

The testsuite attempts to replicate all of the tests present in the Python testsuite. There are still a few missing at this point but coverage is 100% when big-endian platform detection is excluded.

There are also some additional tests that are a hangover from MAVLink porting efforts.

npm test