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

opusonline-binary.js

v1.0.2

Published

Javascript Binary Reader to read and manipulate binary data.

Downloads

4

Readme

binary.js

Javascript Binary Reader to read and manipulate binary data.

This library supports:

  • little/big endian
  • seek and tell
  • strings and chars in different encodings
  • blobs and chunks
  • really fast, because it avoids copying underlaying buffer
  • takes care of byteOffset
  • chainable :D

Install

Install with yarn: yarn add opusonline-binary.js

Usage

Example

let binary = new Binary(16);
binary.setString('hello world');
binary.setByte(33);
binary.skip();
binary.setByte(32);
binary.setChar(':');
binary.setChar('D');
binary.tell();
// gives 16

binary.seek(0).getBytes(4);
// gives [104, 101, 108, 108]
binary.skip(10).getUint16();
// gives 14916

let blob = binary.getBlob('text/plain');
// gives Blob {type: "text/plain", size: 16, …}

Same, but chained:

new Binary(16)
	.setString('hello world')
	.setByte(33)
	.skip()
	.setByte(32)
	.setChar(':')
	.setChar('D')
	.seek(0)
	.getString();
// gives "hello world :D"

Methods

new Binary(input[, littleEndian = false])

input can be:

Binary Object has variables length, position, littleEndian.

Getter

getByte, getUint8, getInt8, getUint16, getInt16, getUint32, getInt32, getFloat32, getFloat64, getBytes, getChar, getString, getBlob, getChunk, getBuffer, toString

Setter

setByte, setUint8, setInt8, setUint16, setInt16, setUint32, setInt32, setFloat32, setFloat64, setBytes, setChar, setString, fill, copy, setLittleEndian, setBigEndian

Specials

seek, tell, skip

getByte() = getUint8()

Returns 1 unsigned byte.

setByte(value) = setUint8(value)

Sets value as 1 unsigned byte.

getUint16([littleEndian = Binary.littleEndian])

Reads 2 unsigned bytes and returns as 1 number.

If littleEndian is not provided, the property of the constructed binary object is taken.

Others

  • getInt8() / setInt8(value)
  • getInt16([littleEndian = Binary.littleEndian]) / setInt16(value)
  • getUint32([littleEndian = Binary.littleEndian]) / setUint32(value)
  • getInt32([littleEndian = Binary.littleEndian]) / setInt32(value)
  • getFloat32([littleEndian = Binary.littleEndian]) / setFloat32(value)
  • getFloat64([littleEndian = Binary.littleEndian]) / setFloat64(value)

getBytes([length][, copy = false])

Returns a new Uint8Array.

If length is not provided, the full length until the end is assumed.

If copy is true the Uint8Array holds a copy of the ArrayBuffer in the desired length.

getChar([encoding = 'binary'])

Returns a character value.

If TextDecoder is available you can choose every encoding. Otherwise encoding supports only utf-8 and binary (raw).

getString([length][, encoding = 'binary'])

Returns a string.

If length is not provided, the full length until the end is assumed.

If TextDecoder is available you can choose every encoding. Otherwise encoding supports only utf-8 and binary (raw).

toString([encoding = 'binary'][, from = 0][, length])

Returns a string.

If length is not provided, the full length until the end is assumed.

If TextDecoder is available you can choose every encoding. Otherwise encoding supports only utf-8 and binary (raw).

getBlob([type = 'application/octet'])

Returns a new Blob generated from the complete underlaying ArrayBuffer.

getChunk(from, to) or getChunk(length)

Returns a new UintArray with a copy of the ArrayBuffer in the desired length.

With from and to the offset is given. With length it starts from current position.

getBuffer()

Returns the complete underlaying ArrayBuffer.

setBytes(input)

input can be:

setChar(value[, encoding = 'binary'])

Sets a character value at current position.

If TextDecoder is available you can choose every encoding. Otherwise encoding supports only utf-8 and binary (raw).

setString(string[, encoding = 'binary'])

Sets a string.

If TextDecoder is available you can choose every encoding. Otherwise encoding supports only utf-8 and binary (raw).

fill(value[, length])

value can be

  • any number, but will be cut of as uint8
  • any character or string, but only first character will be used and cut of as uint8

Fills binary with provided value from current position for length bytes.

If length is not provided, the full length until the end is assumed.

copy(target[, targetFrom = 0][, sourceFrom = 0][, length]) {

Copies given bytes strictly (blind) to target binary.

target must be another binary.

length defaults to length of source bytes from sourceStart. If source length is higher than target size starting from targetFrom an Error is thrown.

setLittleEndian() / setBigEndian()

Changes binary object to desired mode.

seek(offset)

Sets position for next operations on binary object.

tell()

Returns current position.

skip([length = 1])

Shifts the current operation position by given length.