markugen
Version:
Markdown to HTML/PDF static site generation tool
100 lines (99 loc) • 3.2 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.keysToCamelCase = keysToCamelCase;
exports.timeParts = timeParts;
exports.timeFormat = timeFormat;
exports.formatNumber = formatNumber;
exports.replaceLast = replaceLast;
/**
* Replaces any hyphens in the keys with the camel case version
* @param obj the object to convert
* @returns a new object with the keys converted to camel case
*/
function keysToCamelCase(obj) {
if (!obj)
return {};
let result = {};
for (const [key, value] of Object.entries(obj)) {
const camel = key.replace(/-([a-z])/ig, (match, p1) => {
return p1.toUpperCase();
});
result[camel] = value;
}
return result;
}
/**
* Computes the parts of time from the given milliseconds
* @param ms the milliseconds to compute
* @returns the time parts
*/
function timeParts(ms) {
return {
days: Math.floor(ms / 86400000),
hours: Math.floor(ms / 3600000) % 24,
minutes: Math.floor(ms / 60000) % 60,
seconds: Math.floor(ms / 1000) % 60,
milliseconds: ms % 1000,
};
}
/**
* Formats the given milliseconds into human readable format
*/
function timeFormat(ms, format) {
const parts = timeParts(ms);
let text = '';
if (parts.days)
text += `${parts.days} days`;
if (parts.hours)
text += (text !== '' ? ' ' : '') + `${formatNumber(parts.hours, format)} hours`;
if (parts.minutes)
text += (text !== '' ? ' ' : '') + `${formatNumber(parts.minutes, format)} mins`;
if (parts.seconds)
text += (text !== '' ? ' ' : '') + `${formatNumber(parts.seconds, format)} secs`;
if (parts.milliseconds)
text += (text !== '' ? ' ' : '') + `${formatNumber(parts.milliseconds, format)} ms`;
return text;
}
/**
* Formats the given number with precision or fixed decimals
* @param num the number to format
* @param format the formatting to use
* @returns the formatted number as a string
*/
function formatNumber(num, format) {
let out = '';
if (!Number.isInteger(num) && format) {
if (format.precision)
out = num.toPrecision(format.precision);
else if (format.fixed)
out = num.toFixed(format.fixed);
}
return out === '' ? num.toString() : out;
}
/**
* Replaces the last occurrence of {@link search} in the {@link text}
* @param text the text to search
* @param search the text or regex to search for
* @param replace the text to replace the match with
* @returns the string with the last occurrence replaced
*/
function replaceLast(text, search, replace) {
let lastIndex = -1;
let length = 0;
if (typeof search === 'string') {
lastIndex = text.lastIndexOf(search);
length = search.length;
}
else {
const matches = text.matchAll(search);
// get the last match
let match = undefined;
for (const m of matches)
match = m;
if (match) {
lastIndex = match.index;
length = match[0].length;
}
}
return lastIndex < 0 ? text : `${text.slice(0, lastIndex)}${replace}${text.slice(lastIndex + length)}`;
}