mirror of
https://github.com/louislam/uptime-kuma.git
synced 2024-11-23 14:54:05 +00:00
Chore: Fix console colors & add JSDoc (#4170)
This commit is contained in:
parent
ad4629cb03
commit
46b300808d
2 changed files with 154 additions and 67 deletions
91
src/util.js
91
src/util.js
|
@ -8,11 +8,13 @@
|
||||||
// Backend uses the compiled file util.js
|
// Backend uses the compiled file util.js
|
||||||
// Frontend uses util.ts
|
// Frontend uses util.ts
|
||||||
*/
|
*/
|
||||||
|
var _a;
|
||||||
Object.defineProperty(exports, "__esModule", { value: true });
|
Object.defineProperty(exports, "__esModule", { value: true });
|
||||||
exports.debug = exports.ucfirst = exports.sleep = exports.flipStatus = exports.CONSOLE_STYLE_BgGray = exports.CONSOLE_STYLE_BgWhite = exports.CONSOLE_STYLE_BgCyan = exports.CONSOLE_STYLE_BgMagenta = exports.CONSOLE_STYLE_BgBlue = exports.CONSOLE_STYLE_BgYellow = exports.CONSOLE_STYLE_BgGreen = exports.CONSOLE_STYLE_BgRed = exports.CONSOLE_STYLE_BgBlack = exports.CONSOLE_STYLE_FgPink = exports.CONSOLE_STYLE_FgBrown = exports.CONSOLE_STYLE_FgViolet = exports.CONSOLE_STYLE_FgLightBlue = exports.CONSOLE_STYLE_FgLightGreen = exports.CONSOLE_STYLE_FgOrange = exports.CONSOLE_STYLE_FgGray = exports.CONSOLE_STYLE_FgWhite = exports.CONSOLE_STYLE_FgCyan = exports.CONSOLE_STYLE_FgMagenta = exports.CONSOLE_STYLE_FgBlue = exports.CONSOLE_STYLE_FgYellow = exports.CONSOLE_STYLE_FgGreen = exports.CONSOLE_STYLE_FgRed = exports.CONSOLE_STYLE_FgBlack = exports.CONSOLE_STYLE_Hidden = exports.CONSOLE_STYLE_Reverse = exports.CONSOLE_STYLE_Blink = exports.CONSOLE_STYLE_Underscore = exports.CONSOLE_STYLE_Dim = exports.CONSOLE_STYLE_Bright = exports.CONSOLE_STYLE_Reset = exports.MIN_INTERVAL_SECOND = exports.MAX_INTERVAL_SECOND = exports.SQL_DATETIME_FORMAT_WITHOUT_SECOND = exports.SQL_DATETIME_FORMAT = exports.SQL_DATE_FORMAT = exports.STATUS_PAGE_MAINTENANCE = exports.STATUS_PAGE_PARTIAL_DOWN = exports.STATUS_PAGE_ALL_UP = exports.STATUS_PAGE_ALL_DOWN = exports.MAINTENANCE = exports.PENDING = exports.UP = exports.DOWN = exports.appName = exports.isDev = void 0;
|
exports.ucfirst = exports.sleep = exports.flipStatus = exports.CONSOLE_STYLE_BgGray = exports.CONSOLE_STYLE_BgWhite = exports.CONSOLE_STYLE_BgCyan = exports.CONSOLE_STYLE_BgMagenta = exports.CONSOLE_STYLE_BgBlue = exports.CONSOLE_STYLE_BgYellow = exports.CONSOLE_STYLE_BgGreen = exports.CONSOLE_STYLE_BgRed = exports.CONSOLE_STYLE_BgBlack = exports.CONSOLE_STYLE_FgPink = exports.CONSOLE_STYLE_FgBrown = exports.CONSOLE_STYLE_FgViolet = exports.CONSOLE_STYLE_FgLightBlue = exports.CONSOLE_STYLE_FgLightGreen = exports.CONSOLE_STYLE_FgOrange = exports.CONSOLE_STYLE_FgGray = exports.CONSOLE_STYLE_FgWhite = exports.CONSOLE_STYLE_FgCyan = exports.CONSOLE_STYLE_FgMagenta = exports.CONSOLE_STYLE_FgBlue = exports.CONSOLE_STYLE_FgYellow = exports.CONSOLE_STYLE_FgGreen = exports.CONSOLE_STYLE_FgRed = exports.CONSOLE_STYLE_FgBlack = exports.CONSOLE_STYLE_Hidden = exports.CONSOLE_STYLE_Reverse = exports.CONSOLE_STYLE_Blink = exports.CONSOLE_STYLE_Underscore = exports.CONSOLE_STYLE_Dim = exports.CONSOLE_STYLE_Bright = exports.CONSOLE_STYLE_Reset = exports.MIN_INTERVAL_SECOND = exports.MAX_INTERVAL_SECOND = exports.SQL_DATETIME_FORMAT_WITHOUT_SECOND = exports.SQL_DATETIME_FORMAT = exports.SQL_DATE_FORMAT = exports.STATUS_PAGE_MAINTENANCE = exports.STATUS_PAGE_PARTIAL_DOWN = exports.STATUS_PAGE_ALL_UP = exports.STATUS_PAGE_ALL_DOWN = exports.MAINTENANCE = exports.PENDING = exports.UP = exports.DOWN = exports.appName = exports.isNode = exports.isDev = void 0;
|
||||||
exports.intHash = exports.localToUTC = exports.utcToLocal = exports.utcToISODateTime = exports.isoToUTCDateTime = exports.parseTimeFromTimeObject = exports.parseTimeObject = exports.getMaintenanceRelativeURL = exports.getMonitorRelativeURL = exports.genSecret = exports.getCryptoRandomInt = exports.getRandomInt = exports.getRandomArbitrary = exports.TimeLogger = exports.polyfill = exports.log = void 0;
|
exports.intHash = exports.localToUTC = exports.utcToLocal = exports.utcToISODateTime = exports.isoToUTCDateTime = exports.parseTimeFromTimeObject = exports.parseTimeObject = exports.getMaintenanceRelativeURL = exports.getMonitorRelativeURL = exports.genSecret = exports.getCryptoRandomInt = exports.getRandomInt = exports.getRandomArbitrary = exports.TimeLogger = exports.polyfill = exports.log = exports.debug = void 0;
|
||||||
const dayjs = require("dayjs");
|
const dayjs = require("dayjs");
|
||||||
exports.isDev = process.env.NODE_ENV === "development";
|
exports.isDev = process.env.NODE_ENV === "development";
|
||||||
|
exports.isNode = typeof process !== "undefined" && ((_a = process === null || process === void 0 ? void 0 : process.versions) === null || _a === void 0 ? void 0 : _a.node);
|
||||||
exports.appName = "Uptime Kuma";
|
exports.appName = "Uptime Kuma";
|
||||||
exports.DOWN = 0;
|
exports.DOWN = 0;
|
||||||
exports.UP = 1;
|
exports.UP = 1;
|
||||||
|
@ -140,40 +142,67 @@ class Logger {
|
||||||
}
|
}
|
||||||
const levelColor = consoleLevelColors[level];
|
const levelColor = consoleLevelColors[level];
|
||||||
const moduleColor = consoleModuleColors[intHash(module, consoleModuleColors.length)];
|
const moduleColor = consoleModuleColors[intHash(module, consoleModuleColors.length)];
|
||||||
let timePart = exports.CONSOLE_STYLE_FgCyan + now + exports.CONSOLE_STYLE_Reset;
|
let timePart;
|
||||||
let modulePart = "[" + moduleColor + module + exports.CONSOLE_STYLE_Reset + "]";
|
let modulePart;
|
||||||
let levelPart = levelColor + `${level}:` + exports.CONSOLE_STYLE_Reset;
|
let levelPart;
|
||||||
if (level === "INFO") {
|
let msgPart;
|
||||||
console.info(timePart, modulePart, levelPart, msg);
|
if (exports.isNode) {
|
||||||
}
|
switch (level) {
|
||||||
else if (level === "WARN") {
|
case "DEBUG":
|
||||||
console.warn(timePart, modulePart, levelPart, msg);
|
timePart = exports.CONSOLE_STYLE_FgGray + now + exports.CONSOLE_STYLE_Reset;
|
||||||
}
|
break;
|
||||||
else if (level === "ERROR") {
|
default:
|
||||||
let msgPart;
|
timePart = exports.CONSOLE_STYLE_FgCyan + now + exports.CONSOLE_STYLE_Reset;
|
||||||
if (typeof msg === "string") {
|
break;
|
||||||
msgPart = exports.CONSOLE_STYLE_FgRed + msg + exports.CONSOLE_STYLE_Reset;
|
|
||||||
}
|
}
|
||||||
else {
|
modulePart = "[" + moduleColor + module + exports.CONSOLE_STYLE_Reset + "]";
|
||||||
msgPart = msg;
|
levelPart = levelColor + `${level}:` + exports.CONSOLE_STYLE_Reset;
|
||||||
}
|
switch (level) {
|
||||||
console.error(timePart, modulePart, levelPart, msgPart);
|
case "ERROR":
|
||||||
}
|
if (typeof msg === "string") {
|
||||||
else if (level === "DEBUG") {
|
msgPart = exports.CONSOLE_STYLE_FgRed + msg + exports.CONSOLE_STYLE_Reset;
|
||||||
if (exports.isDev) {
|
}
|
||||||
timePart = exports.CONSOLE_STYLE_FgGray + now + exports.CONSOLE_STYLE_Reset;
|
else {
|
||||||
let msgPart;
|
msgPart = msg;
|
||||||
if (typeof msg === "string") {
|
}
|
||||||
msgPart = exports.CONSOLE_STYLE_FgGray + msg + exports.CONSOLE_STYLE_Reset;
|
break;
|
||||||
}
|
case "DEBUG":
|
||||||
else {
|
if (typeof msg === "string") {
|
||||||
|
msgPart = exports.CONSOLE_STYLE_FgGray + msg + exports.CONSOLE_STYLE_Reset;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
msgPart = msg;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
default:
|
||||||
msgPart = msg;
|
msgPart = msg;
|
||||||
}
|
break;
|
||||||
console.debug(timePart, modulePart, levelPart, msgPart);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
console.log(timePart, modulePart, msg);
|
timePart = now;
|
||||||
|
modulePart = `[${module}]`;
|
||||||
|
levelPart = `${level}:`;
|
||||||
|
msgPart = msg;
|
||||||
|
}
|
||||||
|
switch (level) {
|
||||||
|
case "ERROR":
|
||||||
|
console.error(timePart, modulePart, levelPart, msgPart);
|
||||||
|
break;
|
||||||
|
case "WARN":
|
||||||
|
console.warn(timePart, modulePart, levelPart, msgPart);
|
||||||
|
break;
|
||||||
|
case "INFO":
|
||||||
|
console.info(timePart, modulePart, levelPart, msgPart);
|
||||||
|
break;
|
||||||
|
case "DEBUG":
|
||||||
|
if (exports.isDev) {
|
||||||
|
console.debug(timePart, modulePart, levelPart, msgPart);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
console.log(timePart, modulePart, levelPart, msgPart);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
info(module, msg) {
|
info(module, msg) {
|
||||||
|
|
130
src/util.ts
130
src/util.ts
|
@ -1,3 +1,4 @@
|
||||||
|
/* eslint-disable camelcase */
|
||||||
/*!
|
/*!
|
||||||
// Common Util for frontend and backend
|
// Common Util for frontend and backend
|
||||||
//
|
//
|
||||||
|
@ -17,6 +18,7 @@ import * as timezone from "dayjs/plugin/timezone";
|
||||||
import * as utc from "dayjs/plugin/utc";
|
import * as utc from "dayjs/plugin/utc";
|
||||||
|
|
||||||
export const isDev = process.env.NODE_ENV === "development";
|
export const isDev = process.env.NODE_ENV === "development";
|
||||||
|
export const isNode = typeof process !== "undefined" && process?.versions?.node;
|
||||||
export const appName = "Uptime Kuma";
|
export const appName = "Uptime Kuma";
|
||||||
export const DOWN = 0;
|
export const DOWN = 0;
|
||||||
export const UP = 1;
|
export const UP = 1;
|
||||||
|
@ -93,7 +95,8 @@ const consoleLevelColors : Record<string, string> = {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Flip the status of s
|
* Flip the status of s
|
||||||
* @param s
|
* @param s input status: UP or DOWN
|
||||||
|
* @returns {number} UP or DOWN
|
||||||
*/
|
*/
|
||||||
export function flipStatus(s: number) {
|
export function flipStatus(s: number) {
|
||||||
if (s === UP) {
|
if (s === UP) {
|
||||||
|
@ -110,6 +113,7 @@ export function flipStatus(s: number) {
|
||||||
/**
|
/**
|
||||||
* Delays for specified number of seconds
|
* Delays for specified number of seconds
|
||||||
* @param ms Number of milliseconds to sleep for
|
* @param ms Number of milliseconds to sleep for
|
||||||
|
* @returns {Promise<void>} Promise that resolves after ms
|
||||||
*/
|
*/
|
||||||
export function sleep(ms: number) {
|
export function sleep(ms: number) {
|
||||||
return new Promise(resolve => setTimeout(resolve, ms));
|
return new Promise(resolve => setTimeout(resolve, ms));
|
||||||
|
@ -117,7 +121,8 @@ export function sleep(ms: number) {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* PHP's ucfirst
|
* PHP's ucfirst
|
||||||
* @param str
|
* @param str string input
|
||||||
|
* @returns {string} string with first letter capitalized
|
||||||
*/
|
*/
|
||||||
export function ucfirst(str: string) {
|
export function ucfirst(str: string) {
|
||||||
if (!str) {
|
if (!str) {
|
||||||
|
@ -130,7 +135,8 @@ export function ucfirst(str: string) {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @deprecated Use log.debug (https://github.com/louislam/uptime-kuma/pull/910)
|
* @deprecated Use log.debug (https://github.com/louislam/uptime-kuma/pull/910)
|
||||||
* @param msg
|
* @param msg Message to write
|
||||||
|
* @returns {void}
|
||||||
*/
|
*/
|
||||||
export function debug(msg: unknown) {
|
export function debug(msg: unknown) {
|
||||||
log.log("", msg, "debug");
|
log.log("", msg, "debug");
|
||||||
|
@ -180,6 +186,7 @@ class Logger {
|
||||||
* @param module The module the log comes from
|
* @param module The module the log comes from
|
||||||
* @param msg Message to write
|
* @param msg Message to write
|
||||||
* @param level Log level. One of INFO, WARN, ERROR, DEBUG or can be customized.
|
* @param level Log level. One of INFO, WARN, ERROR, DEBUG or can be customized.
|
||||||
|
* @returns {void}
|
||||||
*/
|
*/
|
||||||
log(module: string, msg: any, level: string) {
|
log(module: string, msg: any, level: string) {
|
||||||
if (level === "DEBUG" && !isDev) {
|
if (level === "DEBUG" && !isDev) {
|
||||||
|
@ -203,35 +210,72 @@ class Logger {
|
||||||
const levelColor = consoleLevelColors[level];
|
const levelColor = consoleLevelColors[level];
|
||||||
const moduleColor = consoleModuleColors[intHash(module, consoleModuleColors.length)];
|
const moduleColor = consoleModuleColors[intHash(module, consoleModuleColors.length)];
|
||||||
|
|
||||||
let timePart = CONSOLE_STYLE_FgCyan + now + CONSOLE_STYLE_Reset;
|
let timePart: string;
|
||||||
let modulePart = "[" + moduleColor + module + CONSOLE_STYLE_Reset + "]";
|
let modulePart: string;
|
||||||
let levelPart = levelColor + `${level}:` + CONSOLE_STYLE_Reset;
|
let levelPart: string;
|
||||||
|
let msgPart: string;
|
||||||
|
|
||||||
if (level === "INFO") {
|
if (isNode) {
|
||||||
console.info(timePart, modulePart, levelPart, msg);
|
// Add console colors
|
||||||
} else if (level === "WARN") {
|
switch (level) {
|
||||||
console.warn(timePart, modulePart, levelPart, msg);
|
case "DEBUG":
|
||||||
} else if (level === "ERROR") {
|
timePart = CONSOLE_STYLE_FgGray + now + CONSOLE_STYLE_Reset;
|
||||||
let msgPart :string;
|
break;
|
||||||
if (typeof msg === "string") {
|
default:
|
||||||
msgPart = CONSOLE_STYLE_FgRed + msg + CONSOLE_STYLE_Reset;
|
timePart = CONSOLE_STYLE_FgCyan + now + CONSOLE_STYLE_Reset;
|
||||||
} else {
|
break;
|
||||||
msgPart = msg;
|
|
||||||
}
|
}
|
||||||
console.error(timePart, modulePart, levelPart, msgPart);
|
|
||||||
} else if (level === "DEBUG") {
|
modulePart = "[" + moduleColor + module + CONSOLE_STYLE_Reset + "]";
|
||||||
if (isDev) {
|
|
||||||
timePart = CONSOLE_STYLE_FgGray + now + CONSOLE_STYLE_Reset;
|
levelPart = levelColor + `${level}:` + CONSOLE_STYLE_Reset;
|
||||||
let msgPart :string;
|
|
||||||
if (typeof msg === "string") {
|
switch (level) {
|
||||||
msgPart = CONSOLE_STYLE_FgGray + msg + CONSOLE_STYLE_Reset;
|
case "ERROR":
|
||||||
} else {
|
if (typeof msg === "string") {
|
||||||
|
msgPart = CONSOLE_STYLE_FgRed + msg + CONSOLE_STYLE_Reset;
|
||||||
|
} else {
|
||||||
|
msgPart = msg;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case "DEBUG":
|
||||||
|
if (typeof msg === "string") {
|
||||||
|
msgPart = CONSOLE_STYLE_FgGray + msg + CONSOLE_STYLE_Reset;
|
||||||
|
} else {
|
||||||
|
msgPart = msg;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
default:
|
||||||
msgPart = msg;
|
msgPart = msg;
|
||||||
}
|
break;
|
||||||
console.debug(timePart, modulePart, levelPart, msgPart);
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
console.log(timePart, modulePart, msg);
|
// No console colors
|
||||||
|
timePart = now;
|
||||||
|
modulePart = `[${module}]`;
|
||||||
|
levelPart = `${level}:`;
|
||||||
|
msgPart = msg;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Write to console
|
||||||
|
switch (level) {
|
||||||
|
case "ERROR":
|
||||||
|
console.error(timePart, modulePart, levelPart, msgPart);
|
||||||
|
break;
|
||||||
|
case "WARN":
|
||||||
|
console.warn(timePart, modulePart, levelPart, msgPart);
|
||||||
|
break;
|
||||||
|
case "INFO":
|
||||||
|
console.info(timePart, modulePart, levelPart, msgPart);
|
||||||
|
break;
|
||||||
|
case "DEBUG":
|
||||||
|
if (isDev) {
|
||||||
|
console.debug(timePart, modulePart, levelPart, msgPart);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
console.log(timePart, modulePart, levelPart, msgPart);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -239,6 +283,7 @@ class Logger {
|
||||||
* Log an INFO message
|
* Log an INFO message
|
||||||
* @param module Module log comes from
|
* @param module Module log comes from
|
||||||
* @param msg Message to write
|
* @param msg Message to write
|
||||||
|
* @returns {void}
|
||||||
*/
|
*/
|
||||||
info(module: string, msg: unknown) {
|
info(module: string, msg: unknown) {
|
||||||
this.log(module, msg, "info");
|
this.log(module, msg, "info");
|
||||||
|
@ -248,6 +293,7 @@ class Logger {
|
||||||
* Log a WARN message
|
* Log a WARN message
|
||||||
* @param module Module log comes from
|
* @param module Module log comes from
|
||||||
* @param msg Message to write
|
* @param msg Message to write
|
||||||
|
* @returns {void}
|
||||||
*/
|
*/
|
||||||
warn(module: string, msg: unknown) {
|
warn(module: string, msg: unknown) {
|
||||||
this.log(module, msg, "warn");
|
this.log(module, msg, "warn");
|
||||||
|
@ -257,6 +303,7 @@ class Logger {
|
||||||
* Log an ERROR message
|
* Log an ERROR message
|
||||||
* @param module Module log comes from
|
* @param module Module log comes from
|
||||||
* @param msg Message to write
|
* @param msg Message to write
|
||||||
|
* @returns {void}
|
||||||
*/
|
*/
|
||||||
error(module: string, msg: unknown) {
|
error(module: string, msg: unknown) {
|
||||||
this.log(module, msg, "error");
|
this.log(module, msg, "error");
|
||||||
|
@ -266,6 +313,7 @@ class Logger {
|
||||||
* Log a DEBUG message
|
* Log a DEBUG message
|
||||||
* @param module Module log comes from
|
* @param module Module log comes from
|
||||||
* @param msg Message to write
|
* @param msg Message to write
|
||||||
|
* @returns {void}
|
||||||
*/
|
*/
|
||||||
debug(module: string, msg: unknown) {
|
debug(module: string, msg: unknown) {
|
||||||
this.log(module, msg, "debug");
|
this.log(module, msg, "debug");
|
||||||
|
@ -276,6 +324,7 @@ class Logger {
|
||||||
* @param module Module log comes from
|
* @param module Module log comes from
|
||||||
* @param exception The exception to include
|
* @param exception The exception to include
|
||||||
* @param msg The message to write
|
* @param msg The message to write
|
||||||
|
* @returns {void}
|
||||||
*/
|
*/
|
||||||
exception(module: string, exception: unknown, msg: unknown) {
|
exception(module: string, exception: unknown, msg: unknown) {
|
||||||
let finalMessage = exception;
|
let finalMessage = exception;
|
||||||
|
@ -297,6 +346,7 @@ declare global { interface String { replaceAll(str: string, newStr: string): str
|
||||||
* https://gomakethings.com/how-to-replace-a-section-of-a-string-with-another-one-with-vanilla-js/
|
* https://gomakethings.com/how-to-replace-a-section-of-a-string-with-another-one-with-vanilla-js/
|
||||||
* @author Chris Ferdinandi
|
* @author Chris Ferdinandi
|
||||||
* @license MIT
|
* @license MIT
|
||||||
|
* @returns {void}
|
||||||
*/
|
*/
|
||||||
export function polyfill() {
|
export function polyfill() {
|
||||||
if (!String.prototype.replaceAll) {
|
if (!String.prototype.replaceAll) {
|
||||||
|
@ -325,6 +375,7 @@ export class TimeLogger {
|
||||||
/**
|
/**
|
||||||
* Output time since start of monitor
|
* Output time since start of monitor
|
||||||
* @param name Name of monitor
|
* @param name Name of monitor
|
||||||
|
* @returns {void}
|
||||||
*/
|
*/
|
||||||
print(name: string) {
|
print(name: string) {
|
||||||
if (isDev && process.env.TIMELOGGER === "1") {
|
if (isDev && process.env.TIMELOGGER === "1") {
|
||||||
|
@ -335,8 +386,9 @@ export class TimeLogger {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a random number between min (inclusive) and max (exclusive)
|
* Returns a random number between min (inclusive) and max (exclusive)
|
||||||
* @param min
|
* @param min minumim value, inclusive
|
||||||
* @param max
|
* @param max maximum value, exclusive
|
||||||
|
* @returns {number} Random number
|
||||||
*/
|
*/
|
||||||
export function getRandomArbitrary(min: number, max: number) {
|
export function getRandomArbitrary(min: number, max: number) {
|
||||||
return Math.random() * (max - min) + min;
|
return Math.random() * (max - min) + min;
|
||||||
|
@ -350,8 +402,9 @@ export function getRandomArbitrary(min: number, max: number) {
|
||||||
* if min isn't an integer) and no greater than max (or the next integer
|
* if min isn't an integer) and no greater than max (or the next integer
|
||||||
* lower than max if max isn't an integer).
|
* lower than max if max isn't an integer).
|
||||||
* Using Math.round() will give you a non-uniform distribution!
|
* Using Math.round() will give you a non-uniform distribution!
|
||||||
* @param min
|
* @param min minumim value, inclusive
|
||||||
* @param max
|
* @param max maximum value, exclusive
|
||||||
|
* @returns {number} Random number
|
||||||
*/
|
*/
|
||||||
export function getRandomInt(min: number, max: number) {
|
export function getRandomInt(min: number, max: number) {
|
||||||
min = Math.ceil(min);
|
min = Math.ceil(min);
|
||||||
|
@ -362,6 +415,7 @@ export function getRandomInt(min: number, max: number) {
|
||||||
/**
|
/**
|
||||||
* Returns either the NodeJS crypto.randomBytes() function or its
|
* Returns either the NodeJS crypto.randomBytes() function or its
|
||||||
* browser equivalent implemented via window.crypto.getRandomValues()
|
* browser equivalent implemented via window.crypto.getRandomValues()
|
||||||
|
* @returns {Uint8Array} Random bytes
|
||||||
*/
|
*/
|
||||||
const getRandomBytes = (
|
const getRandomBytes = (
|
||||||
(typeof window !== "undefined" && window.crypto)
|
(typeof window !== "undefined" && window.crypto)
|
||||||
|
@ -467,6 +521,7 @@ export function getMaintenanceRelativeURL(id: string) {
|
||||||
* Parse to Time Object that used in VueDatePicker
|
* Parse to Time Object that used in VueDatePicker
|
||||||
* @param {string} time E.g. 12:00
|
* @param {string} time E.g. 12:00
|
||||||
* @returns object
|
* @returns object
|
||||||
|
* @throws {Error} if time string is invalid
|
||||||
*/
|
*/
|
||||||
export function parseTimeObject(time: string) {
|
export function parseTimeObject(time: string) {
|
||||||
if (!time) {
|
if (!time) {
|
||||||
|
@ -494,8 +549,9 @@ export function parseTimeObject(time: string) {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param obj
|
* Parse time to string from object {hours: number, minutes: number, seconds?: number}
|
||||||
* @returns string e.g. 12:00
|
* @param obj object to parse
|
||||||
|
* @returns {string} e.g. 12:00
|
||||||
*/
|
*/
|
||||||
export function parseTimeFromTimeObject(obj : any) {
|
export function parseTimeFromTimeObject(obj : any) {
|
||||||
if (!obj) {
|
if (!obj) {
|
||||||
|
@ -523,7 +579,8 @@ export function isoToUTCDateTime(input : string) {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param input
|
* @param input valid datetime string
|
||||||
|
* @returns {string} ISO DateTime string
|
||||||
*/
|
*/
|
||||||
export function utcToISODateTime(input : string) {
|
export function utcToISODateTime(input : string) {
|
||||||
return dayjs.utc(input).toISOString();
|
return dayjs.utc(input).toISOString();
|
||||||
|
@ -531,8 +588,8 @@ export function utcToISODateTime(input : string) {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* For SQL_DATETIME_FORMAT
|
* For SQL_DATETIME_FORMAT
|
||||||
* @param input
|
* @param input valid datetime string
|
||||||
* @param format
|
* @param format Format to return
|
||||||
* @returns A string date of SQL_DATETIME_FORMAT
|
* @returns A string date of SQL_DATETIME_FORMAT
|
||||||
*/
|
*/
|
||||||
export function utcToLocal(input : string, format = SQL_DATETIME_FORMAT) : string {
|
export function utcToLocal(input : string, format = SQL_DATETIME_FORMAT) : string {
|
||||||
|
@ -553,6 +610,7 @@ export function localToUTC(input : string, format = SQL_DATETIME_FORMAT) {
|
||||||
* Generate a decimal integer number from a string
|
* Generate a decimal integer number from a string
|
||||||
* @param str Input
|
* @param str Input
|
||||||
* @param length Default is 10 which means 0 - 9
|
* @param length Default is 10 which means 0 - 9
|
||||||
|
* @returns {number} output number
|
||||||
*/
|
*/
|
||||||
export function intHash(str : string, length = 10) : number {
|
export function intHash(str : string, length = 10) : number {
|
||||||
// A simple hashing function (you can use more complex hash functions if needed)
|
// A simple hashing function (you can use more complex hash functions if needed)
|
||||||
|
|
Loading…
Reference in a new issue