0.2.40 • Published 10 months ago

@orochi-network/utilities v0.2.40

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
10 months ago

@orochi-network/utilities

An one for all utilities library, this contain all utilities that need for Smart Contract test, Oracle, Orand.... This package is working for both cjs and esm and It's supposed to work on node and browser. It it isn't working please let us know.

Installation:

yarn add @orochi-network/utilities

FixedFloat: A JavaScript Fixed-Point Number Library

This library provides a FixedFloat class for representing and manipulating fixed-point decimal numbers in JavaScript. Fixed-point numbers offer an alternative to floating-point numbers, ensuring consistent decimal precision for financial calculations and other scenarios where exact decimal representation is crucial.

Features:

  • Represents numbers with a fixed number of decimal places.
  • Supports basic arithmetic operations (addition, subtraction, multiplication, division).
  • Handles decimal point scaling for consistent calculations.
  • Provides methods for formatting numbers in decimal and pretty-printed formats.

Usage:

  1. Import the FixedFloat class:
import { FixedFloat } from '@orochi-network/utilities';
  1. Create FixedFloat instances:
  • From a number or string, .from(): This method is simply tell the FixedFloat to take the given value and convert it for a given decimals. For example FixedFloat.from(12.3456, 2) will be convert to { basedValue: 1234n, decimals:2 }. It's represent for 12.34. Apparently, basedValue = givenValue * 10^decimals.
const fixed1: FixedFloat = FixedFloat.from(12.3456); // Represents 12.3456 in 4 decimal places
const fixed2: FixedFloat = FixedFloat.from('3.14159'); // Represents 3.1415 in 5 decimal places
  • From a IFixedFloat, .fromFixedFloat(): This method create a new instance of FixedFloat from its basedValue and decimals. If decimals < 0, basedValue = basedValue * 10^(|decimals|), otherwise it will take basedValue and decimals as input and created new instance of FixedFloat
const fixed3: FixedFloat = FixedFloat.fromFixedFloat({ basedValue: 123456n, decimals: 4 }); // Represents 12.3456
  1. Perform calculations:
const value: FixedFloat = fixed1.add(fixed2).sub(fixed1);
  1. Format the output:
const decimalString: string = FixedFloat.fromFixedFloat({ basedValue: 1234n, decimals: 2 }).toFixed(); // Output: "12.34"
const prettyString: string = FixedFloat.from(1234.5672).pretty(); // Output: "1,234.5672" (locale-specific formatting)

Example:

import FixedFloat from '@orochi-network/utilities';

const price: FixedFloat = FixedFloat.from(12.99, 2);
const quantity: FixedFloat = FixedFloat.fromFixedFloat({ basedValue: 2n, decimals: 0 });

const total: FixedFloat = price.mul(quantity);

console.log(`Total price: ${total.toFixed(2)}`); // Output: Total price: 25.98

Additional Notes:

  • The decimals parameter during creation defines the number of decimal places for the FixedFloat instance.
  • The toFixed and pretty methods allow for customized decimal formatting.
  • The library performs validation on input values and throws errors for invalid operations like division by zero.

BigInt Scalar Type: A custom scalar type for GraphQL to handle BigInt in data responses.

Usage:

  1. Import the ScalarType class:
import ScalarType from '@orochi-network/utilities';
  1. Add the BigInt scalar in the type definitions of your GraphQL schema:
scalar BigInt
  1. Add the BigInt scalar in the resolvers of your GraphQL schema:
const resolvers = {
  BigInt: ScalarType.BigInt
  // other resolvers
};

Random

Usage: You must init it first since it gonna check the environment and load compatible version

await Random.init();
console.log(Random.bytes(32));
console.log(Random.string(32));
console.log(Random.integer(2, 8));
console.log(Random.bigInteger());
0.2.40

10 months ago

0.2.39

10 months ago

0.2.38

10 months ago

0.2.37

10 months ago

0.2.36

11 months ago

0.2.27

12 months ago

0.2.26

12 months ago

0.2.24

1 year ago

0.2.23

1 year ago

0.2.22

1 year ago

0.2.21

1 year ago

0.2.20

1 year ago

0.2.30

11 months ago

0.2.35

11 months ago

0.2.34

11 months ago

0.2.33

11 months ago

0.2.32

11 months ago

0.2.31

11 months ago

0.2.29

12 months ago

0.2.28

12 months ago

0.2.19

1 year ago

0.2.18

1 year ago

0.2.17

1 year ago

0.2.16

1 year ago

0.2.15

1 year ago

0.2.13

1 year ago

0.2.12

1 year ago

0.2.11

1 year ago

0.2.10

1 year ago

0.2.7

1 year ago

0.1.8

1 year ago

0.2.6

1 year ago

0.2.9

1 year ago

0.2.8

1 year ago

0.1.9

1 year ago

0.2.3

1 year ago

0.2.2

1 year ago

0.2.5

1 year ago

0.2.4

1 year ago

0.1.7

1 year ago

0.1.6

1 year ago

0.1.5

1 year ago

0.1.4

1 year ago

0.1.3

1 year ago