mirror of
https://github.com/pezkuwichain/pezkuwi-common.git
synced 2026-07-16 14:55:44 +00:00
chore: update to version 14.0.11 and align website URLs
This commit is contained in:
Vendored
+90
@@ -0,0 +1,90 @@
|
||||
/**
|
||||
* @name _0n
|
||||
* @summary BigInt constant for 0.
|
||||
*/
|
||||
export declare const _0n: bigint;
|
||||
/**
|
||||
* @name _1n
|
||||
* @summary BigInt constant for 1.
|
||||
*/
|
||||
export declare const _1n: bigint;
|
||||
/**
|
||||
* @name _2n
|
||||
* @summary BigInt constant for 2.
|
||||
*/
|
||||
export declare const _2n: bigint;
|
||||
/**
|
||||
* @name _3n
|
||||
* @summary BigInt constant for 3.
|
||||
*/
|
||||
export declare const _3n: bigint;
|
||||
/**
|
||||
* @name _4n
|
||||
* @summary BigInt constant for 4.
|
||||
*/
|
||||
export declare const _4n: bigint;
|
||||
/**
|
||||
* @name _5n
|
||||
* @summary BigInt constant for 5.
|
||||
*/
|
||||
export declare const _5n: bigint;
|
||||
/**
|
||||
* @name _6n
|
||||
* @summary BigInt constant for 6.
|
||||
*/
|
||||
export declare const _6n: bigint;
|
||||
/**
|
||||
* @name _7n
|
||||
* @summary BigInt constant for 7.
|
||||
*/
|
||||
export declare const _7n: bigint;
|
||||
/**
|
||||
* @name _8n
|
||||
* @summary BigInt constant for 8.
|
||||
*/
|
||||
export declare const _8n: bigint;
|
||||
/**
|
||||
* @name _9n
|
||||
* @summary BigInt constant for 9.
|
||||
*/
|
||||
export declare const _9n: bigint;
|
||||
/**
|
||||
* @name _10n
|
||||
* @summary BigInt constant for 10.
|
||||
*/
|
||||
export declare const _10n: bigint;
|
||||
/**
|
||||
* @name _100n
|
||||
* @summary BigInt constant for 100.
|
||||
*/
|
||||
export declare const _100n: bigint;
|
||||
/**
|
||||
* @name _1000n
|
||||
* @summary BigInt constant for 1000.
|
||||
*/
|
||||
export declare const _1000n: bigint;
|
||||
/**
|
||||
* @name _1Mn
|
||||
* @summary BigInt constant for 1,000,000 (million).
|
||||
*/
|
||||
export declare const _1Mn: bigint;
|
||||
/**
|
||||
* @name _1Bn
|
||||
* @summary BigInt constant for 1,000,000,000 (billion).
|
||||
*/
|
||||
export declare const _1Bn: bigint;
|
||||
/**
|
||||
* @name _1Qn
|
||||
* @summary BigInt constant for 1,000,000,000,000,000,000 (quitillion).
|
||||
*/
|
||||
export declare const _1Qn: bigint;
|
||||
/**
|
||||
* @name _2pow53n
|
||||
* @summary BigInt constant for MAX_SAFE_INTEGER
|
||||
*/
|
||||
export declare const _2pow53n: bigint;
|
||||
/**
|
||||
* @name _sqrt2pow53n
|
||||
* @summary BigInt constant for Math.sqrt(MAX_SAFE_INTEGER)
|
||||
*/
|
||||
export declare const _sqrt2pow53n: bigint;
|
||||
@@ -0,0 +1,91 @@
|
||||
import { BigInt } from '@pezkuwi/x-bigint';
|
||||
/**
|
||||
* @name _0n
|
||||
* @summary BigInt constant for 0.
|
||||
*/
|
||||
export const _0n = /*#__PURE__*/ BigInt(0);
|
||||
/**
|
||||
* @name _1n
|
||||
* @summary BigInt constant for 1.
|
||||
*/
|
||||
export const _1n = /*#__PURE__*/ BigInt(1);
|
||||
/**
|
||||
* @name _2n
|
||||
* @summary BigInt constant for 2.
|
||||
*/
|
||||
export const _2n = /*#__PURE__*/ BigInt(2);
|
||||
/**
|
||||
* @name _3n
|
||||
* @summary BigInt constant for 3.
|
||||
*/
|
||||
export const _3n = /*#__PURE__*/ BigInt(3);
|
||||
/**
|
||||
* @name _4n
|
||||
* @summary BigInt constant for 4.
|
||||
*/
|
||||
export const _4n = /*#__PURE__*/ BigInt(4);
|
||||
/**
|
||||
* @name _5n
|
||||
* @summary BigInt constant for 5.
|
||||
*/
|
||||
export const _5n = /*#__PURE__*/ BigInt(5);
|
||||
/**
|
||||
* @name _6n
|
||||
* @summary BigInt constant for 6.
|
||||
*/
|
||||
export const _6n = /*#__PURE__*/ BigInt(6);
|
||||
/**
|
||||
* @name _7n
|
||||
* @summary BigInt constant for 7.
|
||||
*/
|
||||
export const _7n = /*#__PURE__*/ BigInt(7);
|
||||
/**
|
||||
* @name _8n
|
||||
* @summary BigInt constant for 8.
|
||||
*/
|
||||
export const _8n = /*#__PURE__*/ BigInt(8);
|
||||
/**
|
||||
* @name _9n
|
||||
* @summary BigInt constant for 9.
|
||||
*/
|
||||
export const _9n = /*#__PURE__*/ BigInt(9);
|
||||
/**
|
||||
* @name _10n
|
||||
* @summary BigInt constant for 10.
|
||||
*/
|
||||
export const _10n = /*#__PURE__*/ BigInt(10);
|
||||
/**
|
||||
* @name _100n
|
||||
* @summary BigInt constant for 100.
|
||||
*/
|
||||
export const _100n = /*#__PURE__*/ BigInt(100);
|
||||
/**
|
||||
* @name _1000n
|
||||
* @summary BigInt constant for 1000.
|
||||
*/
|
||||
export const _1000n = /*#__PURE__*/ BigInt(1_000);
|
||||
/**
|
||||
* @name _1Mn
|
||||
* @summary BigInt constant for 1,000,000 (million).
|
||||
*/
|
||||
export const _1Mn = /*#__PURE__*/ BigInt(1_000_000);
|
||||
/**
|
||||
* @name _1Bn
|
||||
* @summary BigInt constant for 1,000,000,000 (billion).
|
||||
*/
|
||||
export const _1Bn = /*#__PURE__*/ BigInt(1_000_000_000);
|
||||
/**
|
||||
* @name _1Qn
|
||||
* @summary BigInt constant for 1,000,000,000,000,000,000 (quitillion).
|
||||
*/
|
||||
export const _1Qn = _1Bn * _1Bn;
|
||||
/**
|
||||
* @name _2pow53n
|
||||
* @summary BigInt constant for MAX_SAFE_INTEGER
|
||||
*/
|
||||
export const _2pow53n = /*#__PURE__*/ BigInt(Number.MAX_SAFE_INTEGER);
|
||||
/**
|
||||
* @name _sqrt2pow53n
|
||||
* @summary BigInt constant for Math.sqrt(MAX_SAFE_INTEGER)
|
||||
*/
|
||||
export const _sqrt2pow53n = /*#__PURE__*/ BigInt(94906265);
|
||||
Vendored
+2
@@ -0,0 +1,2 @@
|
||||
/** @internal */
|
||||
export declare function createCmp<T>(cmp: (a: T, b: T) => boolean): (...items: T[]) => T;
|
||||
@@ -0,0 +1,16 @@
|
||||
/** @internal */
|
||||
export function createCmp(cmp) {
|
||||
return (...items) => {
|
||||
const count = items.length;
|
||||
if (count === 0) {
|
||||
throw new Error('Must provide one or more arguments');
|
||||
}
|
||||
let result = items[0];
|
||||
for (let i = 1; i < count; i++) {
|
||||
if (cmp(items[i], result)) {
|
||||
result = items[i];
|
||||
}
|
||||
}
|
||||
return result;
|
||||
};
|
||||
}
|
||||
Vendored
+9
@@ -0,0 +1,9 @@
|
||||
/**
|
||||
* @summary Utility methods to convert to and from `bigint` objects
|
||||
*/
|
||||
export { nMax, nMin } from './min.js';
|
||||
export { nSqrt } from './sqrt.js';
|
||||
export { nToBigInt } from './toBigInt.js';
|
||||
export { nToHex } from './toHex.js';
|
||||
export { nToU8a } from './toU8a.js';
|
||||
export * from './consts.js';
|
||||
@@ -0,0 +1,9 @@
|
||||
/**
|
||||
* @summary Utility methods to convert to and from `bigint` objects
|
||||
*/
|
||||
export { nMax, nMin } from './min.js';
|
||||
export { nSqrt } from './sqrt.js';
|
||||
export { nToBigInt } from './toBigInt.js';
|
||||
export { nToHex } from './toHex.js';
|
||||
export { nToU8a } from './toU8a.js';
|
||||
export * from './consts.js';
|
||||
Vendored
+10
@@ -0,0 +1,10 @@
|
||||
/**
|
||||
* @name nMax
|
||||
* @summary Finds and returns the highest value in an array of bigint.
|
||||
*/
|
||||
export declare const nMax: (...items: bigint[]) => bigint;
|
||||
/**
|
||||
* @name nMin
|
||||
* @summary Finds and returns the lowest value in an array of bigint.
|
||||
*/
|
||||
export declare const nMin: (...items: bigint[]) => bigint;
|
||||
@@ -0,0 +1,11 @@
|
||||
import { createCmp } from './helpers.js';
|
||||
/**
|
||||
* @name nMax
|
||||
* @summary Finds and returns the highest value in an array of bigint.
|
||||
*/
|
||||
export const nMax = /*#__PURE__*/ createCmp((a, b) => a > b);
|
||||
/**
|
||||
* @name nMin
|
||||
* @summary Finds and returns the lowest value in an array of bigint.
|
||||
*/
|
||||
export const nMin = /*#__PURE__*/ createCmp((a, b) => a < b);
|
||||
Vendored
+7
@@ -0,0 +1,7 @@
|
||||
import type { BN } from '../bn/index.js';
|
||||
import type { ToBigInt, ToBn } from '../types.js';
|
||||
/**
|
||||
* @name nSqrt
|
||||
* @summary Calculates the integer square root of a bigint
|
||||
*/
|
||||
export declare function nSqrt<ExtToBn extends ToBn | ToBigInt>(value: ExtToBn | BN | bigint | string | number | null): bigint;
|
||||
@@ -0,0 +1,29 @@
|
||||
import { BigInt } from '@pezkuwi/x-bigint';
|
||||
import { _0n, _1n, _2pow53n, _sqrt2pow53n } from './consts.js';
|
||||
import { nToBigInt } from './toBigInt.js';
|
||||
/**
|
||||
* @name nSqrt
|
||||
* @summary Calculates the integer square root of a bigint
|
||||
*/
|
||||
export function nSqrt(value) {
|
||||
const n = nToBigInt(value);
|
||||
if (n < _0n) {
|
||||
throw new Error('square root of negative numbers is not supported');
|
||||
}
|
||||
// https://stackoverflow.com/questions/53683995/javascript-big-integer-square-root/
|
||||
// shortcut <= 2^53 - 1 to use the JS utils
|
||||
if (n <= _2pow53n) {
|
||||
// ~~ is more performant that Math.floor
|
||||
return BigInt(~~Math.sqrt(Number(n)));
|
||||
}
|
||||
// Use sqrt(MAX_SAFE_INTEGER) as starting point. since we already know the
|
||||
// output will be larger than this, we expect this to be a safe start
|
||||
let x0 = _sqrt2pow53n;
|
||||
while (true) {
|
||||
const x1 = ((n / x0) + x0) >> _1n;
|
||||
if (x0 === x1 || (x0 === (x1 - _1n))) {
|
||||
return x0;
|
||||
}
|
||||
x0 = x1;
|
||||
}
|
||||
}
|
||||
Vendored
+7
@@ -0,0 +1,7 @@
|
||||
import type { BN } from '../bn/bn.js';
|
||||
import type { ToBigInt, ToBn } from '../types.js';
|
||||
/**
|
||||
* @name nToBigInt
|
||||
* @summary Creates a bigInt value from a BN, bigint, string (base 10 or hex) or number input.
|
||||
*/
|
||||
export declare function nToBigInt<ExtToBn extends ToBigInt | ToBn>(value?: ExtToBn | BN | bigint | string | number | null): bigint;
|
||||
@@ -0,0 +1,25 @@
|
||||
import { BigInt } from '@pezkuwi/x-bigint';
|
||||
import { hexToBigInt } from '../hex/toBigInt.js';
|
||||
import { isBn } from '../is/bn.js';
|
||||
import { isHex } from '../is/hex.js';
|
||||
import { isToBigInt } from '../is/toBigInt.js';
|
||||
import { isToBn } from '../is/toBn.js';
|
||||
/**
|
||||
* @name nToBigInt
|
||||
* @summary Creates a bigInt value from a BN, bigint, string (base 10 or hex) or number input.
|
||||
*/
|
||||
export function nToBigInt(value) {
|
||||
return typeof value === 'bigint'
|
||||
? value
|
||||
: !value
|
||||
? BigInt(0)
|
||||
: isHex(value)
|
||||
? hexToBigInt(value.toString())
|
||||
: isBn(value)
|
||||
? BigInt(value.toString())
|
||||
: isToBigInt(value)
|
||||
? value.toBigInt()
|
||||
: isToBn(value)
|
||||
? BigInt(value.toBn().toString())
|
||||
: BigInt(value);
|
||||
}
|
||||
Vendored
+7
@@ -0,0 +1,7 @@
|
||||
import type { BN } from '../bn/bn.js';
|
||||
import type { HexString, NumberOptions, ToBigInt, ToBn } from '../types.js';
|
||||
/**
|
||||
* @name nToHex
|
||||
* @summary Creates a hex value from a bigint object.
|
||||
*/
|
||||
export declare function nToHex<ExtToBn extends ToBn | ToBigInt>(value?: ExtToBn | BN | bigint | number | null, { bitLength, isLe, isNegative }?: NumberOptions): HexString;
|
||||
@@ -0,0 +1,9 @@
|
||||
import { u8aToHex } from '../u8a/index.js';
|
||||
import { nToU8a } from './toU8a.js';
|
||||
/**
|
||||
* @name nToHex
|
||||
* @summary Creates a hex value from a bigint object.
|
||||
*/
|
||||
export function nToHex(value, { bitLength = -1, isLe = false, isNegative = false } = {}) {
|
||||
return u8aToHex(nToU8a(value || 0, { bitLength, isLe, isNegative }));
|
||||
}
|
||||
Vendored
+7
@@ -0,0 +1,7 @@
|
||||
import type { BN } from '../bn/bn.js';
|
||||
import type { NumberOptions, ToBigInt, ToBn } from '../types.js';
|
||||
/**
|
||||
* @name nToU8a
|
||||
* @summary Creates a Uint8Array object from a bigint.
|
||||
*/
|
||||
export declare function nToU8a<ExtToBn extends ToBn | ToBigInt>(value?: ExtToBn | BN | bigint | number | null, { bitLength, isLe, isNegative }?: NumberOptions): Uint8Array;
|
||||
@@ -0,0 +1,49 @@
|
||||
import { BigInt } from '@pezkuwi/x-bigint';
|
||||
import { _0n, _1n } from './consts.js';
|
||||
import { nToBigInt } from './toBigInt.js';
|
||||
const DIV = BigInt(256);
|
||||
const NEG_MASK = BigInt(0xff);
|
||||
function toU8a(value, isLe, isNegative) {
|
||||
const arr = [];
|
||||
const withSigned = isNegative && (value < _0n);
|
||||
if (withSigned) {
|
||||
value = (value + _1n) * -_1n;
|
||||
}
|
||||
while (value !== _0n) {
|
||||
const mod = value % DIV;
|
||||
const val = Number(withSigned
|
||||
? mod ^ NEG_MASK
|
||||
: mod);
|
||||
if (isLe) {
|
||||
arr.push(val);
|
||||
}
|
||||
else {
|
||||
arr.unshift(val);
|
||||
}
|
||||
value = (value - mod) / DIV;
|
||||
}
|
||||
return Uint8Array.from(arr);
|
||||
}
|
||||
/**
|
||||
* @name nToU8a
|
||||
* @summary Creates a Uint8Array object from a bigint.
|
||||
*/
|
||||
export function nToU8a(value, { bitLength = -1, isLe = true, isNegative = false } = {}) {
|
||||
const valueBi = nToBigInt(value);
|
||||
if (valueBi === _0n) {
|
||||
return bitLength === -1
|
||||
? new Uint8Array(1)
|
||||
: new Uint8Array(Math.ceil((bitLength || 0) / 8));
|
||||
}
|
||||
const u8a = toU8a(valueBi, isLe, isNegative);
|
||||
if (bitLength === -1) {
|
||||
return u8a;
|
||||
}
|
||||
const byteLength = Math.ceil((bitLength || 0) / 8);
|
||||
const output = new Uint8Array(byteLength);
|
||||
if (isNegative) {
|
||||
output.fill(0xff);
|
||||
}
|
||||
output.set(u8a, isLe ? 0 : byteLength - u8a.length);
|
||||
return output;
|
||||
}
|
||||
Reference in New Issue
Block a user