@0xsequence/connect
Version:
Connect package for Sequence Web SDK
127 lines • 3.76 kB
JavaScript
export const isEmailValid = (email) => {
return /^\S+@\S+\.\S{2,}$/.test(email);
};
export const compareAddress = (a, b) => {
return a.toLowerCase() === b.toLowerCase();
};
var ValueType;
(function (ValueType) {
ValueType[ValueType["VERY_LARGE"] = 0] = "VERY_LARGE";
ValueType[ValueType["FRACTION"] = 1] = "FRACTION";
ValueType[ValueType["VERY_TINY"] = 2] = "VERY_TINY";
ValueType[ValueType["MIXED"] = 3] = "MIXED";
})(ValueType || (ValueType = {}));
export const formatDisplay = (_val, options) => {
if (isNaN(Number(_val))) {
console.error(`display format error ${_val} is not a number`);
return 'NaN';
}
const val = Number(_val);
if (val === 0) {
return '0';
}
let valMode;
if (val > 100000000) {
if (options?.disableCompactNotation) {
valMode = ValueType.MIXED;
}
else {
valMode = ValueType.VERY_LARGE;
}
}
else if (val < 0.0000000001) {
if (options?.disableScientificNotation) {
valMode = ValueType.FRACTION;
}
else {
valMode = ValueType.VERY_TINY;
}
}
else if (val < 1) {
valMode = ValueType.FRACTION;
}
else {
valMode = ValueType.MIXED;
}
let notation = undefined;
let config;
const maximumSignificantDigits = options?.significantDigits ?? 4;
switch (valMode) {
case ValueType.VERY_LARGE:
notation = 'compact';
config = {
maximumSignificantDigits,
maximumFractionDigits: options?.maximumFractionDigits
};
break;
case ValueType.VERY_TINY:
notation = 'scientific';
config = {
maximumSignificantDigits,
maximumFractionDigits: options?.maximumFractionDigits
};
break;
case ValueType.FRACTION:
notation = 'standard';
config = {
maximumSignificantDigits,
maximumFractionDigits: options?.maximumFractionDigits
};
break;
default:
notation = 'standard';
config = {
maximumSignificantDigits,
maximumFractionDigits: options?.maximumFractionDigits
};
}
return Intl.NumberFormat('en-US', {
notation,
...config
}).format(val);
};
export const capitalize = (word) => {
return word.charAt(0).toUpperCase() + word.slice(1);
};
export const truncateAtMiddle = (text, truncateAt) => {
let finalText = text;
if (text.length >= truncateAt) {
finalText = text.slice(0, truncateAt / 2) + '...' + text.slice(text.length - truncateAt / 2, text.length);
}
return finalText;
};
export const truncateAtIndex = (text, truncateIndex) => {
let finalText = text;
if (text.length >= truncateIndex) {
finalText = text.slice(0, truncateIndex) + '...' + text.slice(text.length - 4, text.length);
}
return finalText;
};
export const formatAddress = (text) => {
return `0x${truncateAtMiddle(text?.substring(2) || '', 8)}`;
};
export const isJSON = (str) => {
if (!str) {
return false;
}
try {
JSON.parse(str);
}
catch (e) {
return false;
}
return true;
};
export const normalizeChainId = (chainId) => {
if (typeof chainId === 'object') {
return normalizeChainId(chainId.chainId);
}
if (typeof chainId === 'string') {
return Number.parseInt(chainId, chainId.trim().substring(0, 2) === '0x' ? 16 : 10);
}
if (typeof chainId === 'bigint') {
return Number(chainId);
}
return chainId;
};
//# sourceMappingURL=helpers.js.map