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

token-unit

v0.2.1

Published

Convert between token units and ether units

Downloads

10

Readme

TokenUnit Javascript Library

TokenUnit is a conversion library for Ethereum-based tokens to easily scale tokens and convert between tokens and ether. Future releases will include the ability to convert between tokens at real-time prices based on passing in an Oracle class.

Installation

$ npm install --save token-unit

If you want the ES5 version, use:
$ npm install --save token-unit-es5

Usage

const TokenUnit = require('token-unit');
// Or for ES5 compatible, use:
// var TokenUnit = require('token-unit-es5');

let purchaseValue = new TokenUnit(10, 'ether');
let myTokenAmount = purchaseValue.to('myToken');
let newAmount = myTokenAmount.add(new TokenUnit(200, 'kwei'));
let executeOrderAmount = newAmount.get('anchorAmount');

Public Object

{
  scaledUnit (string),
  scaledAmount (string representing a number),
  anchorUnit (string),
  anchorAmount (string representing a number),
  etherEquivalent (string representing a number)
}

Public Methods Available

We treat the calling object as immutable, hence all public methods return new instances of a TokenUnit with the new values from the operation.

  • new TokenUnit(amount, unit, options = {}): Create a new TokenUnit of the specified amount and unit combination. Optional options object (unsupported as of yet). Returns a new TokenUnit.
  • to(unit): Convert the TokenUnit to the new specified unit. This function either does either a scaling or currency conversion. A scaling conversion, which is re-classifying the same unit into a new scale value (i.e. changing cents to dollars), is done when the two units share a common nominal base unit. A currency conversion occurs when the two units do not share a common nominal base unit (i.e. changing cents to yuan). This method returns a new TokenUnit.
  • add(tokenUnit): Add a TokenUnit to another TokenUnit. If the two do not share a common nominal base unit, then we do a currency conversion using the to() method. Returns a new TokenUnit of the same scale as the calling TokenUnit.
  • subtract(tokenUnit): Subtract a TokenUnit to another TokenUnit. If the two do not share a common nominal base unit, then we do a currency conversion using the to() method. Returns a new TokenUnit of the same scale as the calling TokenUnit.
  • sub(tokenUnit): A facade method for subtract() because how many times do developers get confused if it's sub() or subtract() in libraries?!
  • toString(): Output the minimal information needed for console logging. It actually returns an object.
  • get(key): Get a specific key from the toString() object. Useful if you want to display the data.

ES5 or ES6?

Since we made this library compatible with both NodeJS and React, we realized that there is an issue with Webpack and ES6 files. To solve this, we released an ES5 compatible version of the library as v0.2.1. Moving forward, we will transpile our ES6 code to ES5 standards for publishing on NPM.

Questions or comments?

Reach out to the Airswap team at [email protected] with any questions or comments. We welcome well-constructed PRs into this library.