Code Monkey home page Code Monkey logo

bigint-conversion's Introduction

License: MIT JavaScript Style Guide Node CI Coverage Status

bigint-conversion

Convert to/from ES-2020 native JS implementation of BigInt from/to:

  • Buffer (node.js) or ArrayBuffer|TypedArray (native js),
  • hex string,
  • utf8-encoded text string.

It provides a common interface for the conversions that works for both node.js and native javascript.

Note that there is not a directly visible TypedArray() constructor, but a set of typed array ones: Int8Array(), Uint8Array(), Uint8ClampedArray(), Int16Array(), Uint16Array(), Int32Array(), Uint32Array(), Float32Array(), Float64Array(), BigInt64Array(), BigUint64Array().

Installation

bigint-conversion is distributed for web browsers and/or webviews supporting BigInt as an ES6 module or an IIFE file; and for Node.js as a CJS module.

bigint-conversion can be imported to your project with npm:

npm install bigint-conversion

NPM installation defaults to the ES6 module for browsers and the CJS one for Node.js. For web browsers, you can also directly download the IIFE bundle or the ESM bundle from the repository.

Import your module as :

  • Node.js
    const bigintConversion = require('bigint-conversion')
    ... // your code here
  • JavaScript native or TypeScript project
    import * as bigintConversion from 'bigint-conversion'
    ... // your code here

    BigInt is ES-2020. In order to use it with TypeScript you should set lib (and probably also target and module) to esnext in tsconfig.json.

  • JavaScript native browser ES6 mod
    <script type="module">
       import * as bigintConversion from 'lib/index.browser.bundle.mod.js'  // Use you actual path to the broser mod bundle
       ... // your code here
     </script>
  • JavaScript native browser IIFE
    <script src="../../lib/index.browser.bundle.iife.js"></script> <!-- Use you actual path to the browser bundle -->
    <script>
      ... // your code here
    </script>

API reference documentation

bigintToBuf(a, [returnArrayBuffer]) ⇒ ArrayBuffer | Buffer

Converts a bigint to an ArrayBuffer or a Buffer (default for Node.js)

Kind: global function
Returns: ArrayBuffer | Buffer - An ArrayBuffer or a Buffer with a binary representation of the input bigint

Param Type Default Description
a bigint
[returnArrayBuffer] boolean false In Node JS forces the output to be an ArrayBuffer instead of a Buffer (default).

bufToBigint(buf) ⇒ bigint

Converts an ArrayBuffer, TypedArray or Buffer (node.js) to a bigint

Kind: global function
Returns: bigint - A BigInt

Param Type
buf ArrayBuffer | TypedArray | Buffer

bigintToHex(a) ⇒ str

Converts a bigint to a hexadecimal string

Kind: global function
Returns: str - A hexadecimal representation of the input bigint

Param Type
a bigint

hexToBigint(hexStr) ⇒ bigint

Converts a hexadecimal string to a bigint

Kind: global function
Returns: bigint - A BigInt

Param Type
hexStr string

bigintToText(a) ⇒ string

Converts a bigint representing a binary array of utf-8 encoded text to a string of utf-8 text

Kind: global function
Returns: string - A string text with utf-8 encoding

Param Type Description
a bigint A bigint representing a binary array of utf-8 encoded text.

textToBigint(text) ⇒ bigint

Converts a utf-8 string to a bigint (from its binary representaion)

Kind: global function
Returns: bigint - A bigint representing a binary array of the input utf-8 encoded text

Param Type Description
text string A string text with utf-8 encoding

bufToText(buf) ⇒ string

Converts an ArrayBuffer, TypedArray or Buffer (in Node.js) containing utf-8 encoded text to a string of utf-8 text

Kind: global function
Returns: string - A string text with utf-8 encoding

Param Type Description
buf ArrayBuffer | TypedArray | Buffer A buffer containing utf-8 encoded text

textToBuf(str, [returnArrayBuffer]) ⇒ ArrayBuffer | Buffer

Converts a string of utf-8 encoded text to an ArrayBuffer or a Buffer (default in Node.js)

Kind: global function
Returns: ArrayBuffer | Buffer - An ArrayBuffer or a Buffer containing the utf-8 encoded text

Param Type Default Description
str string A string of text (with utf-8 encoding)
[returnArrayBuffer] boolean false In Node JS forces the output to be an ArrayBuffer instead of a Buffer (default).

bufToHex(buf) ⇒ string

Returns the hexadecimal representation of a buffer.

Kind: global function
Returns: string - A string with a hexadecimal representation of the input buffer

Param Type
buf ArrayBuffer | TypedArray | Buffer

hexToBuf(hexStr, [returnArrayBuffer]) ⇒ ArrayBuffer | Buffer

Converts a hexadecimal string to a buffer

Kind: global function
Returns: ArrayBuffer | Buffer - An ArrayBuffer or a Buffer

Param Type Default Description
hexStr string A string representing a number with hexadecimal notation
[returnArrayBuffer] boolean false In Node JS forces the output to be an ArrayBuffer instead of a Buffer (default).

TypedArray : Int8Array | Uint8Array | Uint8ClampedArray | Int16Array | Uint16Array | Int32Array | Uint32Array | Float32Array | Float64Array | BigInt64Array | BigUint64Array

A TypedArray object describes an array-like view of an underlying binary data buffer.

Kind: global typedef


bigint-conversion's People

Contributors

juanelas avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.