MCPcopy Create free account
hub / github.com/PageLeay/celestial-runtime / getFormat

Function getFormat

lib.esm/utils/fixednumber.js:56–97  ·  view source on GitHub ↗
(value)

Source from the content-addressed store, hash-verified

54 return val;
55}
56function getFormat(value) {
57 if (typeof (value) === "number") {
58 value = `fixed128x${value}`;
59 }
60 let signed = true;
61 let width = 128;
62 let decimals = 18;
63 if (typeof (value) === "string") {
64 // Parse the format string
65 if (value === "fixed") {
66 // defaults...
67 }
68 else if (value === "ufixed") {
69 signed = false;
70 }
71 else {
72 const match = value.match(/^(u?)fixed([0-9]+)x([0-9]+)$/);
73 assertArgument(match, "invalid fixed format", "format", value);
74 signed = (match[1] !== "u");
75 width = parseInt(match[2]);
76 decimals = parseInt(match[3]);
77 }
78 }
79 else if (value) {
80 // Extract the values from the object
81 const v = value;
82 const check = (key, type, defaultValue) => {
83 if (v[key] == null) {
84 return defaultValue;
85 }
86 assertArgument(typeof (v[key]) === type, "invalid fixed format (" + key + " not " + type + ")", "format." + key, v[key]);
87 return v[key];
88 };
89 signed = check("signed", "boolean", signed);
90 width = check("width", "number", width);
91 decimals = check("decimals", "number", decimals);
92 }
93 assertArgument((width % 8) === 0, "invalid FixedNumber width (not byte aligned)", "format.width", width);
94 assertArgument(decimals <= 80, "invalid FixedNumber decimals (too large)", "format.decimals", decimals);
95 const name = (signed ? "" : "u") + "fixed" + String(width) + "x" + String(decimals);
96 return { signed, width, decimals, name };
97}
98function toString(val, decimals) {
99 let negative = "";
100 if (val < BN_0) {

Callers 3

fromValueMethod · 0.70
fromStringMethod · 0.70
fromBytesMethod · 0.70

Calls 2

assertArgumentFunction · 0.90
checkFunction · 0.70

Tested by

no test coverage detected