dop-stick
Version:
Source control tooling for versionable-upgradeable smart contracts
100 lines • 3.06 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.utils = exports.truncateMiddle = exports.formatNumber = exports.ensureSuffix = exports.safeStringify = exports.isDefined = exports.formatDuration = exports.sleep = void 0;
/**
* Utility function to pause execution for a specified duration
* @param ms Duration to sleep in milliseconds
* @returns Promise that resolves after the specified duration
*/
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
exports.sleep = sleep;
/**
* Formats a duration in milliseconds to a human-readable string
* @param ms Duration in milliseconds
* @returns Formatted duration string
*/
function formatDuration(ms) {
if (ms < 1000)
return `${ms}ms`;
const seconds = Math.floor(ms / 1000);
const minutes = Math.floor(seconds / 60);
const hours = Math.floor(minutes / 60);
const parts = [];
if (hours > 0) {
parts.push(`${hours}h`);
}
if (minutes % 60 > 0) {
parts.push(`${minutes % 60}m`);
}
if (seconds % 60 > 0) {
parts.push(`${seconds % 60}s`);
}
return parts.join(' ');
}
exports.formatDuration = formatDuration;
/**
* Checks if a value is defined (not null or undefined)
* @param value Value to check
* @returns Boolean indicating if the value is defined
*/
function isDefined(value) {
return value !== null && value !== undefined;
}
exports.isDefined = isDefined;
/**
* Safely stringifies a value, handling circular references
* @param value Value to stringify
* @returns String representation of the value
*/
function safeStringify(value) {
try {
return JSON.stringify(value, null, 2);
}
catch (error) {
return String(value);
}
}
exports.safeStringify = safeStringify;
/**
* Ensures a string ends with a specific suffix
* @param str String to check
* @param suffix Suffix to ensure
* @returns String with the suffix
*/
function ensureSuffix(str, suffix) {
return str.endsWith(suffix) ? str : `${str}${suffix}`;
}
exports.ensureSuffix = ensureSuffix;
/**
* Formats a number with commas for better readability
* @param num Number to format
* @returns Formatted number string
*/
function formatNumber(value) {
return value.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',');
}
exports.formatNumber = formatNumber;
/**
* Truncates a string with ellipsis in the middle
* @param str String to truncate
* @param maxLength Maximum length of the output string
* @returns Truncated string
*/
function truncateMiddle(str, maxLength) {
if (str.length <= maxLength)
return str;
const ellipsis = '...';
const charsToShow = maxLength - ellipsis.length;
const frontChars = Math.ceil(charsToShow / 2);
const backChars = Math.floor(charsToShow / 2);
return str.substr(0, frontChars) + ellipsis + str.substr(str.length - backChars);
}
exports.truncateMiddle = truncateMiddle;
exports.utils = {
formatDuration,
formatNumber,
truncateMiddle
};
//# sourceMappingURL=common.js.map