tiinvo
Version:
A library of types and utilities for your TypeScript and JavaScript projects
213 lines (212 loc) • 4.46 kB
JavaScript
//#region guards
/**
* Checks if a parameter `x` is a `BigInt`
*
* @example
*
* ```ts
* import { BigInt } from 'tiinvo'
*
* BigInt.guard(10) // false
* BigInt.guard(10n) // true
* ```
*
* @param x the parameter to check
* @returns returns true if is a bigint, false otherwise
* @group Guards
* @since 4.0.0
*/
export const guard = (x) => typeof x === 'bigint';
export function cmp(a, b) {
if (guard(a) && guard(b)) {
return a < b ? -1 : a > b ? 1 : 0;
}
return (b) => a < b ? -1 : a > b ? 1 : 0;
}
export function eq(a, b) {
if (arguments.length === 2) {
return guard(a) && guard(b) && a === b;
}
return (x) => guard(a) && guard(x) && x === a;
}
export function map(a, m) {
if (guard(a) && typeof m === 'function') {
return m(a);
}
else if (typeof a === 'function') {
return (b) => guard(b) ? a(b) : new TypeError("a is not a bigint");
}
return new TypeError("a is not a bigint and m is not a Mappable functor");
}
;
export function mapOr(a, m, b) {
if (typeof m === 'function') {
return guard(a) ? m(a) : b;
}
else if (typeof a === 'function') {
return (c) => guard(c) ? a(c) : m;
}
return b;
}
export function add(a, b) {
if (guard(b)) {
return a + b;
}
return (c) => c + a;
}
export function div(a, b) {
if (guard(b)) {
return a / b;
}
return (c) => c / a;
}
export function mod(a, b) {
if (guard(a) && guard(b)) {
return a % b;
}
return (c) => c % a;
}
export function mul(a, b) {
if (guard(b)) {
return a * b;
}
return (c) => c * a;
}
export function pow(a, b) {
if (guard(a) && guard(b)) {
return a ** b;
}
return (c) => c ** a;
}
export function root(a, b) {
const _r = (base, root) => {
let s = base + 1n;
let k1 = root - 1n;
let u = base;
while (u < s) {
s = u;
u = ((u * k1) + base / (u ** k1)) / root;
}
return s;
};
if (guard(b) && guard(a)) {
return _r(a, b);
}
return (c) => _r(c, a);
}
export function sub(a, b) {
if (guard(b)) {
return a - b;
}
return (c) => c - a;
}
export function asc(a, b) {
if (guard(b)) {
return cmp(a, b);
}
return (c) => cmp(c, a);
}
export function desc(a, b) {
if (guard(b)) {
return cmp(b, a);
}
return (c) => cmp(a, c);
}
//#endregion
//#region serializables
/**
* Returns a bigint in binary notation.
*
* If the passed argument at runtime is not a bigint, an Error will be returned.
*
* @example
*
* ```ts
* import { BigInt } from 'tiinvo';
*
* BigInt.toBin(10n) // "0b1010"
* ```
*
* @param a the bigint
* @returns the binary bigint value
* @group Serializables
* @since 4.0.0
*/
export const toBin = map(x => '0b' + x.toString(2));
/**
* Returns a bigint in hexadecimal notation
*
* If the passed argument at runtime is not a bigint, an Error will be returned.
*
* @example
*
* ```ts
* import { BigInt } from 'tiinvo';
*
* BigInt.toHex(10n) // "0xa"
* ```
*
* @param a the bigint
* @returns the hexadecimal bigint value
* @group Serializables
* @since 4.0.0
*/
export const toHex = map(x => '0x' + x.toString(16));
/**
* Returns a bigint in octal notation
*
* If the passed argument at runtime is not a bigint, an Error will be returned.
*
* @example
*
* ```ts
* import { BigInt } from 'tiinvo';
*
* BigInt.toOct(10n) // "0o12"
* ```
*
* @param a the bigint
* @returns the octal bigint value
* @group Serializables
* @since 4.0.0
*/
export const toOct = map(x => '0o' + x.toString(8));
/**
* Returns a bigint in json notation.
*
* If the passed argument at runtime is not a bigint, an Error will be returned.
*
* @example
*
* ```ts
* import { BigInt } from 'tiinvo';
*
* BigInt.toJSON(10n) // "10"
* ```
*
* @param a the bigint
* @returns the json bigint value
* @group Serializables
* @since 4.0.0
*/
export const toJSON = map(x => String(x));
/**
* Returns a stringified number.
*
* If the passed argument at runtime is not a bigint, an Error will be returned.
*
* @example
*
* ```ts
* import { BigInt } from 'tiinvo';
*
* BigInt.toString(10n) // "10"
* ```
*
* @param a the bigint
* @returns the string
* @group Serializables
* @since 4.0.0
*/
export const toString = map(String);
//#endregion