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

stringview

v3.0.0

Published

DataView helper for reading/writing strings

Downloads

498

Readme

StringView

JavaScript DataView helper for reading/writing strings.

API

Provides the following methods for reading and writing strings from a native JS DataView object.

import StringView from "StringView"
StringView.getString(dataView, byteOffset, byteLength = <remaining length>, encoding = "UTF-8")
StringView.getStringData(dataView, byteOffset, byteLength = <remaining length>, encoding = "UTF-8")
StringView.getStringNT(dataView, byteOffset, encoding = "UTF-8", terminator = '\0')
StringView.getStringDataNT(dataView, byteOffset, encoding = "UTF-8", terminator = '\0')
StringView.setString(dataView, byteOffset, value, encoding = "UTF-8")
StringView.setStringNT(dataView, byteOffset, value, encoding = "UTF-8")
StringView.stringByteLength(dataView, str, encoding = "UTF-8")
StringView.addStringCodec(encoding, readerFunc, writerFunc)

All methods assume a default encoding of UTF-8 unless an encoding is specified.

Methods

StringView.getString(dataView, byteOffset, byteLength = <remaining length>, encoding = "UTF-8")

Returns the string represented by this DataView's buffer starting at byteOffset. The string will be made from byteLength bytes (defaulting to the length of the buffer minus byteOffset if not specified) interpreted using the specified encoding.

This method will throw an Error if the provided byteOffset and byteLength would cause access past the end of the buffer.

If encoding is provided to getString then byteLength must also be provided. The byteLength defaults to the length of the buffer minus the byteOffset if not provided.

StringView.getStringData(dataView, byteOffset, byteLength = <remaining length>, encoding - "UTF-8")

Functionally identical to the method getString, but returns an object with two properties: str, and byteLength - the str property is the read string, and the byteLength property indicates the number of bytes that were consumed while reading it. Note that if decoding issues are encountered this byte length value may differ from a subsequently calculated byte length for the returned string.

StringView.getStringNT(dataView, byteOffset, encoding = "UTF-8", terminator = '\0')

Returns the string represented by this DataView's buffer starting at byteOffset and reading until a null byte (or the numeric char code specified as terminator) or the end of the buffer is encountered, interpreted using the specified encoding.

StringView.getStringDataNT(dataView, byteOffset, encoding = "UTF-8", terminator = '\0')

Functionally identical to the method getStringNT, but returns an object with two properties: str, and byteLength - the str property is the read string (not including null byte), and the byteLength property indicates the number of bytes that were consumed while reading it (including the null byte). Note that if decoding issues are encountered this byte length value may differ from a subsequently calculated byte length for the returned string.

StringView.setString(dataView, byteOffset, value, encoding = "UTF-8")

Writes the provided value into this DataView's buffer starting at byteOffset. The string will be encoded using the specified encoding. This function will return the number of bytes written to the string, which may be less than the number required to completely represent the string if byteOffset is too close to the end of the buffer. Note that this function may write a partial character at the end of the string in the case of truncation.

StringView.setStringNT(dataView, byteOffset, value, encoding = "UTF-8")

Writes the provided value into this DataView's buffer starting at byteOffset. The string will be encoded using the specified encoding and terminated with a null byte. This function will return the number of bytes written to the string, which may be less than the number required to completely represent the string if byteOffset is too close to the end of the buffer. If the string was truncated it will still be terminated by a null byte. The null byte will be included the the return value. Note that this function may write a partial character at the end of the string in the case of truncation. Note that unlike getStringNT this method does not accept a custom terminator argument - if a custom terminator is required then use setString with the desired terminator appended to the string.

StringView.stringByteLength(str, encoding = "UTF-8")

Calculates and returns the number of bytes required to completely represent the provided string using the specified encoding.

StringView.addStringCodec(encoding, readerFunc, writerFunc)

Adds support for encoding and decoding the specified encoding to all DataView instances. The readerFunc and writerFunc arguments should be functions adhering to the following specification.

readerFunc

The reader function should accept the following arguments:

  • buf - the DataView object to operate on
  • byteOffset - the offset in the data buffer to begin reading
  • bytesToRead - the number of bytes to read, or undefined for terminated strings.
  • terminator - the char code for the terminator character (if bytesToRead is undefined)

The reader function should return an object with two properties - str being the decoded string, and byteLength representing the number of bytes consumed in reading the string. The string should not include the terminator character, but the character should be included in the byteLength value.

writerFunc

The writer function should accept a single argument - the string to encode, and must return a JavaScript array of unsigned byte values representing that string in the specified encoding.

Supported Encodings

The following encodings are supported by default:

  • UTF-8
  • ASCII

Support for additional encodings can be added using the StringView.addStringCodec method.

Invalid UTF-8 Characters

Any invalid UTF-8 characters encountered are replaced with one or more U+FFFD characters (the 'replacement character').

Examples

Please see test.mjs for usage examples.