@davigomesdev/format 0.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,11 @@
1
+ import type { BigNumberish } from 'ethers';
2
+ import { Decimal } from 'decimal.js';
3
+ export declare class Format {
4
+ static formatToWei(value: Decimal.Value, decimals?: number): bigint;
5
+ static formatFromWei(value: BigNumberish, decimals?: number): string;
6
+ static formatTrimDecimals(value: Decimal.Value, maxDecimals?: number): string;
7
+ static formatAddress(address: string, startChars?: number, endChars?: number): string;
8
+ static formatSanitizeInput(value: string, maxDecimals?: number): string;
9
+ static formatCountDecimals(value: string): number;
10
+ }
11
+ //# sourceMappingURL=format.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"format.d.ts","sourceRoot":"","sources":["../src/format.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,QAAQ,CAAC;AAE3C,OAAO,EAAE,OAAO,EAAE,MAAM,YAAY,CAAC;AAGrC,qBAAa,MAAM;WACH,WAAW,CAAC,KAAK,EAAE,OAAO,CAAC,KAAK,EAAE,QAAQ,GAAE,MAAW,GAAG,MAAM;WAKhE,aAAa,CAAC,KAAK,EAAE,YAAY,EAAE,QAAQ,GAAE,MAAW,GAAG,MAAM;WAKjE,kBAAkB,CAAC,KAAK,EAAE,OAAO,CAAC,KAAK,EAAE,WAAW,GAAE,MAAW,GAAG,MAAM;WAoB1E,aAAa,CAAC,OAAO,EAAE,MAAM,EAAE,UAAU,GAAE,MAAU,EAAE,QAAQ,GAAE,MAAU,GAAG,MAAM;WAWpF,mBAAmB,CAAC,KAAK,EAAE,MAAM,EAAE,WAAW,GAAE,MAAW,GAAG,MAAM;WAgBpE,mBAAmB,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM;CAOzD"}
package/dist/format.js ADDED
@@ -0,0 +1,53 @@
1
+ import { Decimal } from 'decimal.js';
2
+ import { formatUnits, parseUnits } from 'ethers';
3
+ export class Format {
4
+ static formatToWei(value, decimals = 18) {
5
+ const num = Format.formatTrimDecimals(value, decimals);
6
+ return parseUnits(num, decimals);
7
+ }
8
+ static formatFromWei(value, decimals = 18) {
9
+ const num = Format.formatTrimDecimals(value.toString(), decimals);
10
+ return formatUnits(num, decimals);
11
+ }
12
+ static formatTrimDecimals(value, maxDecimals = 18) {
13
+ if (value.toString().trim() === '')
14
+ return '0';
15
+ const num = new Decimal(value);
16
+ if (!num.isFinite())
17
+ return '0';
18
+ const fixedValue = num.toFixed(maxDecimals);
19
+ const [integerPart, decimalPart] = fixedValue.split('.');
20
+ if (!decimalPart)
21
+ return integerPart;
22
+ let truncatedDecimal = decimalPart.slice(0, maxDecimals);
23
+ truncatedDecimal = truncatedDecimal.replace(/0+$/, '');
24
+ if (truncatedDecimal === '')
25
+ return integerPart;
26
+ return `${integerPart}.${truncatedDecimal}`;
27
+ }
28
+ static formatAddress(address, startChars = 6, endChars = 4) {
29
+ if (!address || typeof address !== 'string' || address.length < startChars + endChars) {
30
+ return address;
31
+ }
32
+ const start = address.slice(0, startChars);
33
+ const end = address.slice(-endChars);
34
+ return `${start}...${end}`;
35
+ }
36
+ static formatSanitizeInput(value, maxDecimals = 18) {
37
+ let sanitized = value.replace(/[^0-9.]/g, '');
38
+ const parts = sanitized.split('.');
39
+ if (parts.length > 1) {
40
+ sanitized = parts[0] + '.' + parts.slice(1).join('');
41
+ }
42
+ if (Format.formatCountDecimals(sanitized) >= maxDecimals) {
43
+ sanitized = Format.formatTrimDecimals(sanitized, maxDecimals);
44
+ }
45
+ return sanitized;
46
+ }
47
+ static formatCountDecimals(value) {
48
+ if (value.includes('.')) {
49
+ return value.split('.')[1].length;
50
+ }
51
+ return 0;
52
+ }
53
+ }
@@ -0,0 +1,2 @@
1
+ export * from './format';
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,UAAU,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1 @@
1
+ export * from './format';
package/package.json ADDED
@@ -0,0 +1,29 @@
1
+ {
2
+ "name": "@davigomesdev/format",
3
+ "version": "0.0.0",
4
+ "main": "dist/index.js",
5
+ "types": "dist/index.d.ts",
6
+ "files": [
7
+ "dist"
8
+ ],
9
+ "scripts": {
10
+ "build": "tsc",
11
+ "prepublishOnly": "npm run build"
12
+ },
13
+ "devDependencies": {
14
+ "@eslint/js": "^9.39.2",
15
+ "@types/node": "^25.0.2",
16
+ "@typescript-eslint/eslint-plugin": "^8.50.0",
17
+ "@typescript-eslint/parser": "^8.50.0",
18
+ "eslint": "^9.39.2",
19
+ "eslint-plugin-prettier": "^5.5.4",
20
+ "globals": "^16.5.0",
21
+ "prettier": "^3.7.4",
22
+ "typescript": "~5.9.3",
23
+ "typescript-eslint": "^8.50.0"
24
+ },
25
+ "dependencies": {
26
+ "decimal.js": "^10.6.0",
27
+ "ethers": "^6.16.0"
28
+ }
29
+ }