Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

any-data

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

any-data

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

  • 1.0.3
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
3
decreased by-25%
Maintainers
1
Weekly downloads
 
Created
Source

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 / methodtext()json()formData()bytes()blob()arrayBuffer()clone()
null""undefined(empty)(empty)(empty)(empty)null
stringitselfIf valid JSONIf validYesYesYesitself
Record<string, any>JSONitselfYesJSONJSONJSONdeep copy
ArrayJSONitselfas entriesJSONJSONJSONdeep copy
Responsetext()json()formData()bytes()blob()arrayBuffer()clone()
BlobYesIf valid JSONIf valid JSONYesitselfYesdeep copy
ArrayBufferYesIf valid JSONIf valid JSONYesYesYesdeep copy
TypedArrayYesIf valid JSONIf valid JSONYesYesYesdeep copy
DataViewYesIf valid JSONIf valid JSONYesYesYesdeep copy
FormData YesErroritselfYesYesYesdeep copy
ReadableStreamYesIf valid JSONIf valid JSONYesYesYesdeep copy
URLSearchParamsURLentriesYesURLURLURLdeep 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

FAQs

Package last updated on 26 Sep 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc