@orochi-network/utilities
v0.2.40
Published
One for all utilities library
Downloads
541
Readme
@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:
- Import the
FixedFloat
class:
import { FixedFloat } from '@orochi-network/utilities';
- Create
FixedFloat
instances:
- From a number or string,
.from()
: This method is simply tell theFixedFloat
to take the givenvalue
and convert it for a givendecimals
. For exampleFixedFloat.from(12.3456, 2)
will be convert to{ basedValue: 1234n, decimals:2 }
. It's represent for12.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 ofFixedFloat
from itsbasedValue
anddecimals
. Ifdecimals < 0
,basedValue = basedValue * 10^(|decimals|)
, otherwise it will takebasedValue
anddecimals
as input and created new instance ofFixedFloat
const fixed3: FixedFloat = FixedFloat.fromFixedFloat({ basedValue: 123456n, decimals: 4 }); // Represents 12.3456
- Perform calculations:
const value: FixedFloat = fixed1.add(fixed2).sub(fixed1);
- 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 theFixedFloat
instance. - The
toFixed
andpretty
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:
- Import the
ScalarType
class:
import ScalarType from '@orochi-network/utilities';
- Add the
BigInt
scalar in the type definitions of your GraphQL schema:
scalar BigInt
- 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());