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

utf8-bytes-swap

v1.0.0

Published

This module converts UTF8 <-> Bytes Array

Downloads

3

Readme

utf8-bytes-swap

return an utf8 string or bytes array

This module converts UTF8 <-> Bytes Array

Installation

Use the package manager npm to install utf8-bytes-swap.

npm install utf8-bytes-swap

example

Storing strings on the blockchain is expensive. Using string to bytes array significantly reduces transaction cost for storing IPFS CID hashes.

A IPFS cidV1 hash is 59 byte and fit inside a bytes32[2]. Solidity does not accept incomplete byteArrays: fill=true is required for the 5 unused bytes.

const { stringToBytesArray, bytesArrayToString } = require('utf8-bytes-swap');

let cidV1 = 'bafkreicizr7rctuvyki43dtpp7n23go56r6ouw3qxc3iq3r4dtkf2izv6i';
let bytesArray = stringToBytesArray(cidV1, 32, true);
/* 
[
  Uint32Array(32) [
     98,  97, 102, 107, 114, 101, 105,
     99, 105, 122, 114,  55, 114,  99,
    116, 117, 118, 121, 107, 105,  52,
     51, 100, 116, 112, 112,  55, 110,
     50,  51, 103, 111
  ],
  Uint32Array(32) [
     53,  54, 114,  54, 111, 117, 119,  51,
    113, 120,  99,  51, 105, 113,  51, 114,
     52, 100, 116, 107, 102,  50, 105, 122,
    118,  54, 105,   0,   0,   0,   0,   0
  ]
]
*/

let retrievedCidV1 = bytesArrayToString(bytesArray);
// bafkreicizr7rctuvyki43dtpp7n23go56r6ouw3qxc3iq3r4dtkf2izv6i

methods

// default: size to 32, fill partial bytesArray
function stringToBytesArray(str, size=32, fill=true){...};

// returns bytesArray to utf8 string
function bytesArrayToString(bytesArray){...};

stringToBytesArray(< string >)

Return an array of integers from 0 through 255, inclusive, representing the bytes in the unicode string str.

bytesArrayToString(<uint array | hex array>)

Return an unicode str from a hex or integer array of 0 through 255.

license

MIT,