@swapkit/helpers
Advanced tools
Comparing version 1.0.0-rc.11 to 1.0.0-rc.12
@@ -105,2 +105,3 @@ import { Chain } from '@swapkit/types'; | ||
toFixed(fixedDigits?: number): string; | ||
toAbbreviation(digits?: number): string | number; | ||
} | ||
@@ -107,0 +108,0 @@ |
@@ -52,3 +52,3 @@ var bt = Object.defineProperty; | ||
[D.Fastest]: 2 | ||
}, Ct = ({ chain: s, symbol: t }) => { | ||
}, $t = ({ chain: s, symbol: t }) => { | ||
switch (s) { | ||
@@ -79,3 +79,3 @@ case o.Bitcoin: | ||
} | ||
}, $t = (s) => { | ||
}, Ct = (s) => { | ||
switch (s) { | ||
@@ -211,3 +211,3 @@ case "ETH.THOR": | ||
return Math.abs(u.div(l).baseValueNumber); | ||
}, kt = ({ | ||
}, Mt = ({ | ||
symbol: s, | ||
@@ -236,3 +236,3 @@ ticker: t, | ||
case g.WITHDRAW: { | ||
const { chain: e, ticker: r, symbol: n, basisPoints: i, targetAssetString: a, singleSide: c } = t, u = !c && a ? `:${a}` : "", l = kt({ chain: e, symbol: n, ticker: r }); | ||
const { chain: e, ticker: r, symbol: n, basisPoints: i, targetAssetString: a, singleSide: c } = t, u = !c && a ? `:${a}` : "", l = Mt({ chain: e, symbol: n, ticker: r }); | ||
return `${s}:${e}${c ? "/" : "."}${l}:${i}${u}`; | ||
@@ -310,3 +310,3 @@ } | ||
return t; | ||
}, qt = (() => { | ||
}, kt = (() => { | ||
let s = !1, t = !1; | ||
@@ -322,3 +322,3 @@ const e = typeof globalThis.ReadableStream == "function", r = typeof globalThis.Request == "function"; | ||
}).headers.has("Content-Type")), s && !t; | ||
})(), Mt = typeof globalThis.AbortController == "function", xt = typeof globalThis.ReadableStream == "function", Pt = typeof globalThis.FormData == "function", ct = ["get", "post", "put", "patch", "head", "delete"], Nt = { | ||
})(), qt = typeof globalThis.AbortController == "function", xt = typeof globalThis.ReadableStream == "function", Pt = typeof globalThis.FormData == "function", ct = ["get", "post", "put", "patch", "head", "delete"], Nt = { | ||
json: "application/json", | ||
@@ -407,5 +407,5 @@ text: "text/*", | ||
}; | ||
class q { | ||
class k { | ||
static create(t, e) { | ||
const r = new q(t, e), n = async () => { | ||
const r = new k(t, e), n = async () => { | ||
if (typeof r._options.timeout == "number" && r._options.timeout > L) | ||
@@ -500,3 +500,3 @@ throw new RangeError(`The \`timeout\` option cannot be greater than ${L}`); | ||
} | ||
if (Mt) { | ||
if (qt) { | ||
if (this.abortController = new globalThis.AbortController(), this._options.signal) { | ||
@@ -510,3 +510,3 @@ const r = this._options.signal; | ||
} | ||
if (qt && (this._options.duplex = "half"), this.request = new globalThis.Request(this._input, this._options), this._options.searchParams) { | ||
if (kt && (this._options.duplex = "half"), this.request = new globalThis.Request(this._input, this._options), this._options.searchParams) { | ||
const n = "?" + (typeof this._options.searchParams == "string" ? this._options.searchParams.replace(/^\?/, "") : new URLSearchParams(this._options.searchParams).toString()), i = this.request.url.replace(/(?:\?.*?)?(?=#|$)/, n); | ||
@@ -607,5 +607,5 @@ (Pt && this._options.body instanceof globalThis.FormData || this._options.body instanceof URLSearchParams) && !(this._options.headers && this._options.headers["content-type"]) && this.request.headers.delete("content-type"), this.request = new globalThis.Request(new globalThis.Request(i, { ...this.request }), this._options); | ||
const W = (s) => { | ||
const t = (e, r) => q.create(e, V(s, r)); | ||
const t = (e, r) => k.create(e, V(s, r)); | ||
for (const e of ct) | ||
t[e] = (r, n) => q.create(r, V(s, n, { method: e })); | ||
t[e] = (r, n) => k.create(r, V(s, n, { method: e })); | ||
return t.create = (e) => W(V(e)), t.extend = (e) => W(V(s, e)), t.stop = ut, t; | ||
@@ -631,7 +631,7 @@ }, zt = W(), Jt = zt, et = Jt.create({ | ||
); | ||
}, $ = 8, v = (s) => 10n ** BigInt(s), R = (s) => Math.log10(parseFloat(s.toString())); | ||
function M({ | ||
}, C = 8, v = (s) => 10n ** BigInt(s), R = (s) => Math.log10(parseFloat(s.toString())); | ||
function q({ | ||
value: s, | ||
bigIntDecimal: t = $, | ||
decimal: e = $ | ||
bigIntDecimal: t = C, | ||
decimal: e = C | ||
}) { | ||
@@ -649,3 +649,3 @@ const r = s < 0n; | ||
} | ||
var w, S, x, ht, P, dt, E, G, A, C, O, Y; | ||
var w, S, x, ht, P, dt, E, G, A, $, O, Y; | ||
const N = class N { | ||
@@ -664,3 +664,3 @@ constructor(t) { | ||
this.decimal = typeof t == "object" ? t.decimal : void 0, this.decimalMultiplier = v( | ||
Math.max(f(this, O, Y).call(this, f(this, A, C).call(this, e)), this.decimal || 0) | ||
Math.max(f(this, O, Y).call(this, f(this, A, $).call(this, e)), this.decimal || 0) | ||
), f(this, x, ht).call(this, e); | ||
@@ -671,3 +671,3 @@ } | ||
decimal: e, | ||
value: M({ value: t, bigIntDecimal: e, decimal: e }) | ||
value: q({ value: t, bigIntDecimal: e, decimal: e }) | ||
}); | ||
@@ -761,7 +761,7 @@ } | ||
return t.bigIntValue; | ||
const r = U(t), n = f(this, A, C).call(this, r); | ||
const r = U(t), n = f(this, A, $).call(this, r); | ||
return n === "0" || n === "undefined" ? 0n : f(this, E, G).call(this, n, e); | ||
} | ||
formatBigIntToSafeValue(t, e) { | ||
const r = e || this.decimal || $, n = Math.max( | ||
const r = e || this.decimal || C, n = Math.max( | ||
r, | ||
@@ -803,2 +803,9 @@ R(this.decimalMultiplier) | ||
} | ||
toAbbreviation(t = 2) { | ||
const e = this.getValue("number"), r = ["", "K", "M", "B", "T", "Q", "Qi", "S"], n = Math.floor(Math.log10(Math.abs(e)) / 3), i = r[n]; | ||
if (!i) | ||
return e; | ||
const a = 10 ** (n * 3); | ||
return `${(e / a).toFixed(t)}${i}`; | ||
} | ||
}; | ||
@@ -827,3 +834,3 @@ w = new WeakSet(), S = function(t, ...e) { | ||
this.bigIntValue * n / this.decimalMultiplier | ||
), a = M({ | ||
), a = q({ | ||
bigIntDecimal: r, | ||
@@ -835,13 +842,13 @@ decimal: Math.max(r, R(this.decimalMultiplier)), | ||
}, x = new WeakSet(), ht = function(t) { | ||
const e = f(this, A, C).call(this, t) || "0"; | ||
const e = f(this, A, $).call(this, t) || "0"; | ||
this.bigIntValue = f(this, E, G).call(this, e); | ||
}, P = new WeakSet(), dt = function(...t) { | ||
const e = t.map( | ||
(r) => typeof r == "object" ? r.decimal || R(r.decimalMultiplier) : f(this, O, Y).call(this, f(this, A, C).call(this, r)) | ||
(r) => typeof r == "object" ? r.decimal || R(r.decimalMultiplier) : f(this, O, Y).call(this, f(this, A, $).call(this, r)) | ||
).filter(Boolean); | ||
return Math.max(...e, $); | ||
return Math.max(...e, C); | ||
}, E = new WeakSet(), G = function(t, e) { | ||
const r = e ? v(e) : this.decimalMultiplier, n = R(r), [i = "", a = ""] = t.split("."); | ||
return BigInt(`${i}${a.padEnd(n, "0")}`); | ||
}, A = new WeakSet(), C = function(t) { | ||
}, A = new WeakSet(), $ = function(t) { | ||
const r = `${typeof t == "number" ? Number(t).toLocaleString("fullwide", { | ||
@@ -855,3 +862,3 @@ useGrouping: !1, | ||
const e = ((r = t.split(".")[1]) == null ? void 0 : r.length) || 0; | ||
return Math.max(e, $); | ||
return Math.max(e, C); | ||
}; | ||
@@ -862,3 +869,3 @@ let I = N; | ||
} | ||
const k = (s, t) => typeof s == "bigint" ? M({ value: s, bigIntDecimal: t, decimal: t }) : s; | ||
const M = (s, t) => typeof s == "bigint" ? q({ value: s, bigIntDecimal: t, decimal: t }) : s; | ||
let z; | ||
@@ -871,3 +878,3 @@ const F = (s) => { | ||
Qt(s); | ||
const e = await it(J(s)), r = k(t, e); | ||
const e = await it(J(s)), r = M(t, e); | ||
return new b({ decimal: e, value: r, identifier: s }); | ||
@@ -909,3 +916,3 @@ }; | ||
e | ||
), a = k(r, n); | ||
), a = M(r, n); | ||
return i ? new b({ decimal: n, identifier: i, value: a }) : void 0; | ||
@@ -917,7 +924,7 @@ } | ||
static fromIdentifierSync(e, r = 0) { | ||
const { decimal: n, identifier: i } = F(e), a = k(r, n); | ||
const { decimal: n, identifier: i } = F(e), a = M(r, n); | ||
return new b({ decimal: n, identifier: i, value: a }); | ||
} | ||
static fromChainOrSignature(e, r = 0) { | ||
const { decimal: n, identifier: i } = $t(e), a = k(r, n); | ||
const { decimal: n, identifier: i } = Ct(e), a = M(r, n); | ||
return new b({ value: a, decimal: n, identifier: i }); | ||
@@ -989,3 +996,3 @@ } | ||
chain: r, | ||
isGasAsset: Ct({ chain: r, symbol: n }), | ||
isGasAsset: $t({ chain: r, symbol: n }), | ||
isSynthetic: t, | ||
@@ -1064,3 +1071,3 @@ symbol: a ? `${i}-${(a == null ? void 0 : a.toLowerCase()) ?? ""}` : n, | ||
decimal: e, | ||
value: M({ value: t, bigIntDecimal: e, decimal: e }) | ||
value: q({ value: t, bigIntDecimal: e, decimal: e }) | ||
}); | ||
@@ -1078,3 +1085,3 @@ } | ||
se as filterAssets, | ||
M as formatBigIntToSafeValue, | ||
q as formatBigIntToSafeValue, | ||
ee as gasFeeMultiplier, | ||
@@ -1086,3 +1093,3 @@ It as getAssetType, | ||
ne as getAsymmetricRuneWithdrawAmount, | ||
$t as getCommonAssetInfo, | ||
Ct as getCommonAssetInfo, | ||
it as getDecimal, | ||
@@ -1096,4 +1103,4 @@ ae as getEstimatedPoolShare, | ||
le as getTHORNameCost, | ||
Ct as isGasAsset, | ||
$t as isGasAsset, | ||
he as validateTHORName | ||
}; |
@@ -45,3 +45,3 @@ { | ||
"types": "./dist/index.d.ts", | ||
"version": "1.0.0-rc.11", | ||
"version": "1.0.0-rc.12", | ||
"scripts": { | ||
@@ -48,0 +48,0 @@ "build": "vite build", |
@@ -156,2 +156,33 @@ import { describe, expect, test } from 'vitest'; | ||
describe('toAbbreviation', () => { | ||
test('returns abbreviation with up to 3 integer digits', () => { | ||
const skNumber = new SwapKitNumber(1234.5678); | ||
expect(skNumber.toAbbreviation()).toBe('1.23K'); | ||
const skNumber2 = new SwapKitNumber(1234567.5678); | ||
expect(skNumber2.toAbbreviation()).toBe('1.23M'); | ||
const skNumber3 = new SwapKitNumber(1234567890.5678); | ||
expect(skNumber3.toAbbreviation()).toBe('1.23B'); | ||
const skNumber4 = new SwapKitNumber(1234567890123.5678); | ||
expect(skNumber4.toAbbreviation()).toBe('1.23T'); | ||
const skNumber5 = new SwapKitNumber(1234567890123456.5678); | ||
expect(skNumber5.toAbbreviation()).toBe('1.23Q'); | ||
const skNumber6 = new SwapKitNumber(1234567890123456789.5678); | ||
expect(skNumber6.toAbbreviation()).toBe('1.23Qi'); | ||
const skNumber7 = new SwapKitNumber(1234567890123456789012.5678); | ||
expect(skNumber7.toAbbreviation()).toBe('1.23S'); | ||
const skNumber8 = new SwapKitNumber(1234.5678); | ||
expect(skNumber8.toAbbreviation(0)).toBe('1K'); | ||
const skNumber9 = new SwapKitNumber(1234.5678); | ||
expect(skNumber9.toAbbreviation(1)).toBe('1.2K'); | ||
}); | ||
}); | ||
describe('add', () => { | ||
@@ -158,0 +189,0 @@ test('adds same type numbers correctly', () => { |
@@ -273,2 +273,16 @@ import { BaseDecimal } from '@swapkit/types'; | ||
toAbbreviation(digits = 2) { | ||
const value = this.getValue('number'); | ||
const abbreviations = ['', 'K', 'M', 'B', 'T', 'Q', 'Qi', 'S']; | ||
const tier = Math.floor(Math.log10(Math.abs(value)) / 3); | ||
const suffix = abbreviations[tier]; | ||
if (!suffix) return value; | ||
const scale = 10 ** (tier * 3); | ||
const scaled = value / scale; | ||
return `${scaled.toFixed(digits)}${suffix}`; | ||
} | ||
#arithmetics(method: 'add' | 'sub' | 'mul' | 'div', ...args: InitialisationValueType[]): this { | ||
@@ -275,0 +289,0 @@ const precisionDecimal = this.#retrievePrecisionDecimal(this, ...args); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
192299
3549