add publish code

This commit is contained in:
CounterFire2023 2024-01-17 13:08:07 +08:00
parent b7bcbcfb56
commit 7af683d82c
12 changed files with 1547 additions and 2 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
node_modules

0
README.md Normal file
View File

163
dist/index.cjs vendored Normal file
View File

@ -0,0 +1,163 @@
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/index.ts
var src_exports = {};
__export(src_exports, {
ethUnitMap: () => ethUnitMap,
fromWei: () => fromWei,
hexToNumber: () => hexToNumber,
isHex: () => isHex,
toBigInt: () => toBigInt,
toBigWei: () => toBigWei,
toNumber: () => toNumber,
toWei: () => toWei
});
module.exports = __toCommonJS(src_exports);
// src/utils/bn.util.ts
var isHexStrict = (hex) => typeof hex === "string" && /^((-)?0x[0-9a-f]+|(0x))$/i.test(hex);
var isHex = (hex) => typeof hex === "number" || typeof hex === "bigint" || typeof hex === "string" && /^((-0x|0x|-)?[0-9a-f]+|(0x))$/i.test(hex);
var base = BigInt(10);
var expo10 = (expo) => base ** BigInt(expo);
var ethUnitMap = {
noether: BigInt("0"),
wei: BigInt(1),
kwei: expo10(3),
Kwei: expo10(3),
babbage: expo10(3),
femtoether: expo10(3),
mwei: expo10(6),
Mwei: expo10(6),
lovelace: expo10(6),
picoether: expo10(6),
gwei: expo10(9),
Gwei: expo10(9),
shannon: expo10(9),
nanoether: expo10(9),
nano: expo10(9),
szabo: expo10(12),
microether: expo10(12),
micro: expo10(12),
finney: expo10(15),
milliether: expo10(15),
milli: expo10(15),
ether: expo10(18),
kether: expo10(21),
grand: expo10(21),
mether: expo10(24),
gether: expo10(27),
tether: expo10(30)
};
var hexToNumber = (value) => {
if (!isHexStrict(value)) {
throw new Error("Invalid hex string");
}
const [negative, hexValue] = value.startsWith("-") ? [true, value.slice(1)] : [false, value];
const num = BigInt(hexValue);
if (num > Number.MAX_SAFE_INTEGER) {
return negative ? -num : num;
}
if (num < Number.MIN_SAFE_INTEGER) {
return num;
}
return negative ? -1 * Number(num) : Number(num);
};
var toNumber = (value) => {
if (typeof value === "number") {
return value;
}
if (typeof value === "bigint") {
return value >= Number.MIN_SAFE_INTEGER && value <= Number.MAX_SAFE_INTEGER ? Number(value) : value;
}
if (typeof value === "string" && isHexStrict(value)) {
return hexToNumber(value);
}
try {
return toNumber(BigInt(value));
} catch {
throw new Error("ivalid number: " + value);
}
};
var toBigInt = (value) => {
if (typeof value === "number") {
return BigInt(value);
}
if (typeof value === "bigint") {
return value;
}
if (typeof value === "string" && isHex(value)) {
return BigInt(value);
}
if (typeof value === "string" && value.indexOf(",") >= 0) {
return BigInt(value.replace(/,/g, ""));
}
throw new Error("invalid number" + value);
};
var toBigWei = (number, unit = "ether") => {
return toBigInt(toWei(number, unit));
};
var toWei = (number, unit = "ether") => {
const denomination = ethUnitMap[unit];
if (!denomination) {
throw new Error("error unit: " + unit);
}
typeof number === "string" && number.indexOf(",") >= 0 && (number = number.replace(/,/g, ""));
const [integer, fraction] = String(typeof number === "string" && !isHexStrict(number) ? number : toNumber(number)).split(".").concat("");
const value = BigInt(`${integer}${fraction}`);
const updatedValue = value * denomination;
const numberOfZerosInDenomination = denomination.toString().length - 1;
const decimals = Math.min(fraction.length, numberOfZerosInDenomination);
if (decimals === 0) {
return updatedValue.toString();
}
return updatedValue.toString().padStart(decimals, "0").slice(0, -decimals);
};
var fromWei = (number, unit = "ether") => {
const denomination = ethUnitMap[unit];
if (!denomination) {
throw new Error("invalid unit: " + unit);
}
const value = String(toNumber(number));
const numberOfZerosInDenomination = denomination.toString().length - 1;
if (numberOfZerosInDenomination <= 0) {
return value.toString();
}
const zeroPaddedValue = value.padStart(numberOfZerosInDenomination, "0");
const integer = zeroPaddedValue.slice(0, -numberOfZerosInDenomination);
const fraction = zeroPaddedValue.slice(-numberOfZerosInDenomination).replace(/\.?0+$/, "");
if (integer === "") {
return `0.${fraction}`;
}
if (fraction === "") {
return integer;
}
return `${integer}.${fraction}`;
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
ethUnitMap,
fromWei,
hexToNumber,
isHex,
toBigInt,
toBigWei,
toNumber,
toWei
});
//# sourceMappingURL=index.cjs.map

1
dist/index.cjs.map vendored Normal file

File diff suppressed because one or more lines are too long

72
dist/index.d.cts vendored Normal file
View File

@ -0,0 +1,72 @@
declare type HexString = string;
declare type Numbers = number | bigint | string | HexString;
declare type ValidInputTypes = Uint8Array | bigint | string | number | boolean;
declare const isHex: (hex: ValidInputTypes) => boolean;
declare const ethUnitMap: {
noether: bigint;
wei: bigint;
kwei: bigint;
Kwei: bigint;
babbage: bigint;
femtoether: bigint;
mwei: bigint;
Mwei: bigint;
lovelace: bigint;
picoether: bigint;
gwei: bigint;
Gwei: bigint;
shannon: bigint;
nanoether: bigint;
nano: bigint;
szabo: bigint;
microether: bigint;
micro: bigint;
finney: bigint;
milliether: bigint;
milli: bigint;
ether: bigint;
kether: bigint;
grand: bigint;
mether: bigint;
gether: bigint;
tether: bigint;
};
type EtherUnits = keyof typeof ethUnitMap;
/**
* Converts value to it's number representation
*/
declare const hexToNumber: (value: string) => bigint | number;
declare const toNumber: (value: Numbers) => number | bigint;
/**
* Auto converts any given value into it's bigint representation
*
* @param value - The value to convert
* @returns - Returns the value in bigint representation
* @example
* ```ts
* console.log(web3.utils.toBigInt(1));
* > 1n
* ```
*/
declare const toBigInt: (value: unknown) => bigint;
declare const toBigWei: (number: Numbers, unit?: EtherUnits) => bigint;
declare const toWei: (number: Numbers, unit?: EtherUnits) => string;
/**
* Takes a number of wei and converts it to any other ether unit.
* @param number - The value in wei
* @param unit - The unit to convert to
* @returns - Returns the converted value in the given unit
*
* @example
* ```ts
* console.log(web3.utils.fromWei("1", "ether"));
* > 0.000000000000000001
*
* console.log(web3.utils.fromWei("1", "shannon"));
* > 0.000000001
* ```
*/
declare const fromWei: (number: Numbers, unit?: EtherUnits) => string;
export { type EtherUnits, type HexString, type Numbers, type ValidInputTypes, ethUnitMap, fromWei, hexToNumber, isHex, toBigInt, toBigWei, toNumber, toWei };

72
dist/index.d.ts vendored Normal file
View File

@ -0,0 +1,72 @@
declare type HexString = string;
declare type Numbers = number | bigint | string | HexString;
declare type ValidInputTypes = Uint8Array | bigint | string | number | boolean;
declare const isHex: (hex: ValidInputTypes) => boolean;
declare const ethUnitMap: {
noether: bigint;
wei: bigint;
kwei: bigint;
Kwei: bigint;
babbage: bigint;
femtoether: bigint;
mwei: bigint;
Mwei: bigint;
lovelace: bigint;
picoether: bigint;
gwei: bigint;
Gwei: bigint;
shannon: bigint;
nanoether: bigint;
nano: bigint;
szabo: bigint;
microether: bigint;
micro: bigint;
finney: bigint;
milliether: bigint;
milli: bigint;
ether: bigint;
kether: bigint;
grand: bigint;
mether: bigint;
gether: bigint;
tether: bigint;
};
type EtherUnits = keyof typeof ethUnitMap;
/**
* Converts value to it's number representation
*/
declare const hexToNumber: (value: string) => bigint | number;
declare const toNumber: (value: Numbers) => number | bigint;
/**
* Auto converts any given value into it's bigint representation
*
* @param value - The value to convert
* @returns - Returns the value in bigint representation
* @example
* ```ts
* console.log(web3.utils.toBigInt(1));
* > 1n
* ```
*/
declare const toBigInt: (value: unknown) => bigint;
declare const toBigWei: (number: Numbers, unit?: EtherUnits) => bigint;
declare const toWei: (number: Numbers, unit?: EtherUnits) => string;
/**
* Takes a number of wei and converts it to any other ether unit.
* @param number - The value in wei
* @param unit - The unit to convert to
* @returns - Returns the converted value in the given unit
*
* @example
* ```ts
* console.log(web3.utils.fromWei("1", "ether"));
* > 0.000000000000000001
*
* console.log(web3.utils.fromWei("1", "shannon"));
* > 0.000000001
* ```
*/
declare const fromWei: (number: Numbers, unit?: EtherUnits) => string;
export { type EtherUnits, type HexString, type Numbers, type ValidInputTypes, ethUnitMap, fromWei, hexToNumber, isHex, toBigInt, toBigWei, toNumber, toWei };

130
dist/index.js vendored Normal file
View File

@ -0,0 +1,130 @@
// src/utils/bn.util.ts
var isHexStrict = (hex) => typeof hex === "string" && /^((-)?0x[0-9a-f]+|(0x))$/i.test(hex);
var isHex = (hex) => typeof hex === "number" || typeof hex === "bigint" || typeof hex === "string" && /^((-0x|0x|-)?[0-9a-f]+|(0x))$/i.test(hex);
var base = BigInt(10);
var expo10 = (expo) => base ** BigInt(expo);
var ethUnitMap = {
noether: BigInt("0"),
wei: BigInt(1),
kwei: expo10(3),
Kwei: expo10(3),
babbage: expo10(3),
femtoether: expo10(3),
mwei: expo10(6),
Mwei: expo10(6),
lovelace: expo10(6),
picoether: expo10(6),
gwei: expo10(9),
Gwei: expo10(9),
shannon: expo10(9),
nanoether: expo10(9),
nano: expo10(9),
szabo: expo10(12),
microether: expo10(12),
micro: expo10(12),
finney: expo10(15),
milliether: expo10(15),
milli: expo10(15),
ether: expo10(18),
kether: expo10(21),
grand: expo10(21),
mether: expo10(24),
gether: expo10(27),
tether: expo10(30)
};
var hexToNumber = (value) => {
if (!isHexStrict(value)) {
throw new Error("Invalid hex string");
}
const [negative, hexValue] = value.startsWith("-") ? [true, value.slice(1)] : [false, value];
const num = BigInt(hexValue);
if (num > Number.MAX_SAFE_INTEGER) {
return negative ? -num : num;
}
if (num < Number.MIN_SAFE_INTEGER) {
return num;
}
return negative ? -1 * Number(num) : Number(num);
};
var toNumber = (value) => {
if (typeof value === "number") {
return value;
}
if (typeof value === "bigint") {
return value >= Number.MIN_SAFE_INTEGER && value <= Number.MAX_SAFE_INTEGER ? Number(value) : value;
}
if (typeof value === "string" && isHexStrict(value)) {
return hexToNumber(value);
}
try {
return toNumber(BigInt(value));
} catch {
throw new Error("ivalid number: " + value);
}
};
var toBigInt = (value) => {
if (typeof value === "number") {
return BigInt(value);
}
if (typeof value === "bigint") {
return value;
}
if (typeof value === "string" && isHex(value)) {
return BigInt(value);
}
if (typeof value === "string" && value.indexOf(",") >= 0) {
return BigInt(value.replace(/,/g, ""));
}
throw new Error("invalid number" + value);
};
var toBigWei = (number, unit = "ether") => {
return toBigInt(toWei(number, unit));
};
var toWei = (number, unit = "ether") => {
const denomination = ethUnitMap[unit];
if (!denomination) {
throw new Error("error unit: " + unit);
}
typeof number === "string" && number.indexOf(",") >= 0 && (number = number.replace(/,/g, ""));
const [integer, fraction] = String(typeof number === "string" && !isHexStrict(number) ? number : toNumber(number)).split(".").concat("");
const value = BigInt(`${integer}${fraction}`);
const updatedValue = value * denomination;
const numberOfZerosInDenomination = denomination.toString().length - 1;
const decimals = Math.min(fraction.length, numberOfZerosInDenomination);
if (decimals === 0) {
return updatedValue.toString();
}
return updatedValue.toString().padStart(decimals, "0").slice(0, -decimals);
};
var fromWei = (number, unit = "ether") => {
const denomination = ethUnitMap[unit];
if (!denomination) {
throw new Error("invalid unit: " + unit);
}
const value = String(toNumber(number));
const numberOfZerosInDenomination = denomination.toString().length - 1;
if (numberOfZerosInDenomination <= 0) {
return value.toString();
}
const zeroPaddedValue = value.padStart(numberOfZerosInDenomination, "0");
const integer = zeroPaddedValue.slice(0, -numberOfZerosInDenomination);
const fraction = zeroPaddedValue.slice(-numberOfZerosInDenomination).replace(/\.?0+$/, "");
if (integer === "") {
return `0.${fraction}`;
}
if (fraction === "") {
return integer;
}
return `${integer}.${fraction}`;
};
export {
ethUnitMap,
fromWei,
hexToNumber,
isHex,
toBigInt,
toBigWei,
toNumber,
toWei
};
//# sourceMappingURL=index.js.map

1
dist/index.js.map vendored Normal file

File diff suppressed because one or more lines are too long

View File

@ -2,11 +2,24 @@
"name": "zutils",
"version": "1.0.0",
"description": "",
"main": "index.js",
"main": "./dist/index.js",
"module": "./dist/index.mjs",
"types": "./dist/index.d.ts",
"files": [
"dist"
],
"type": "module",
"scripts": {
"build": "tsup",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
"license": "ISC",
"devDependencies": {
"ts-node": "^10.9.2",
"typescript": "^5.3.3"
},
"dependencies": {
"tsup": "^8.0.1"
}
}

23
tsconfig.json Normal file
View File

@ -0,0 +1,23 @@
{
"ts-node": {
"files": true
},
"compilerOptions": {
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"module": "commonjs",
"esModuleInterop": true,
"resolveJsonModule": true,
"target": "es2020",
"moduleResolution": "node",
"sourceMap": true,
"outDir": "./dist",
"baseUrl": "./src",
"rootDir": "./src"
},
"lib": ["es2020"],
"include": [
"src/**/*.ts",
"typings/extend.d.ts"
]
}

10
tsup.config.ts Normal file
View File

@ -0,0 +1,10 @@
import { defineConfig } from "tsup";
export default defineConfig({
entry: ["src/index.ts"],
format: ["cjs", "esm"], // Build for commonJS and ESmodules
dts: true, // Generate declaration file (.d.ts)
splitting: false,
sourcemap: true,
clean: true,
});

1059
yarn.lock Normal file

File diff suppressed because it is too large Load Diff