lotus-sdk
Version:
Central repository for several classes of tools for integrating with, and building for, the Lotusia ecosystem
46 lines (45 loc) • 1.28 kB
JavaScript
export class JSUtil {
static isHexa(value) {
if (typeof value !== 'string') {
return false;
}
return /^[0-9a-fA-F]+$/.test(value);
}
static isHexaString(str) {
return typeof str === 'string' && /^[0-9a-fA-F]+$/.test(str);
}
static isValidJSON(arg) {
if (typeof arg !== 'string') {
return false;
}
try {
const parsed = JSON.parse(arg);
return typeof parsed === 'object';
}
catch (e) {
return false;
}
}
static cloneArray(arr) {
return [...arr];
}
static isNaturalNumber(value) {
return ((typeof value === 'number' &&
isFinite(value) &&
Math.floor(value) === value &&
value >= 0) ||
(typeof value === 'string' && /^[0-9]+$/.test(value)) ||
(typeof value === 'bigint' && value >= 0n));
}
static defineImmutable(obj, properties) {
Object.keys(properties).forEach(key => {
Object.defineProperty(obj, key, {
value: properties[key],
writable: false,
enumerable: true,
configurable: false,
});
});
return obj;
}
}