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

any-data

v1.0.3

Published

Convert data of any type to text, json, formData, TypedArray, Blob, or ArrayBuffer

Downloads

18

Readme

any-data

NPM Link Language Build Status Code Coverage Gzipped Size Dependency details Tree shakeable ISC License

Convert data of any type to text, json, formData, TypedArray, Blob, or ArrayBuffer

npm install any-data

Motivation

I needed a tool for dealing with the wide variety of data types that can be passed to new Response(). So AnyData allows lazy data conversion for response bodies to simplify http server middleware such as calculating etags and compression with Brotli.

Note that AnyData does not support ReadableStream even though Response does.

Table of contents

  1. Example usage
  2. API
  3. Supported data types
  4. Changes
    1. Changelog
    2. Roadmap
  5. Community
    1. Contributing
    2. ISC license

Example usage

import { AnyData } from 'any-data';

await new AnyData('hello world').bytes(); // UTF-8 encoded in Uint8Array
await new AnyData(myArrayBuffer).bytes(); // Array buffer as Uint8Array
await new AnyData(myBlob).arrayBuffer(); // Blob as ArrayBuffer

API

import { AnyData } from 'any-data';

const hello = new AnyData('hello world');

// getting data
await hello.text(); // get data as text
await hello.json(); // get data as an object; will throw if data isn't valid JSON
await hello.arrayBuffer(); // get data as ArrayBuffer
await hello.blob(); // get data as Blob
await hello.bytes(); // get data as Uint8Array
await hello.formData(); // get data as FormData object

// setting data to something new
hello.set('foo bar');

// utility methods
hello.clone(); // returns a new AnyData object with a DEEP clone of the underlying data
hello.isEmpty(); // true if data is empty
hello.isSupported(); // true if data type is supported
hello.getDataCategory(); // Either bytes, text, or unknown

Supported data types

| data type / method | text() | json() | formData() | bytes() | blob() | arrayBuffer() | clone() | | ------------------- | ------ | ------------- | ------------- | ------- | ------- | ------------- | --------- | | null | "" | undefined | (empty) | (empty) | (empty) | (empty) | null | | string | itself | If valid JSON | If valid | Yes | Yes | Yes | itself | | Record<string, any> | JSON | itself | Yes | JSON | JSON | JSON | deep copy | | Array | JSON | itself | as entries | JSON | JSON | JSON | deep copy | | Response | text() | json() | formData() | bytes() | blob() | arrayBuffer() | clone() | | Blob | Yes | If valid JSON | If valid JSON | Yes | itself | Yes | deep copy | | ArrayBuffer | Yes | If valid JSON | If valid JSON | Yes | Yes | Yes | deep copy | | TypedArray | Yes | If valid JSON | If valid JSON | Yes | Yes | Yes | deep copy | | DataView | Yes | If valid JSON | If valid JSON | Yes | Yes | Yes | deep copy | | FormData | Yes | Error | itself | Yes | Yes | Yes | deep copy | | ReadableStream | Yes | If valid JSON | If valid JSON | Yes | Yes | Yes | deep copy | | URLSearchParams | URL | entries | Yes | URL | URL | URL | deep copy |

Note: all FormData is serialized to text with multi-part form boundary markers

Community

Contributing

Please open a ticket or PR on GitHub. All contributions are subject to the Code of Conduct.

ISC License

ISC License