add util.ts comments

This commit is contained in:
LouisLam 2021-09-21 21:28:45 +08:00
parent 17073fd786
commit a71814c80b
3 changed files with 109 additions and 102 deletions

View file

@ -19,6 +19,7 @@
"start": "npm run start-server", "start": "npm run start-server",
"start-server": "node server/server.js", "start-server": "node server/server.js",
"build": "vite build", "build": "vite build",
"tsc": "tsc",
"vite-preview-dist": "vite preview --host", "vite-preview-dist": "vite preview --host",
"build-docker": "npm run build-docker-debian && npm run build-docker-alpine", "build-docker": "npm run build-docker-debian && npm run build-docker-alpine",
"build-docker-alpine": "docker buildx build -f dockerfile-alpine --platform linux/amd64,linux/arm64,linux/arm/v7 -t louislam/uptime-kuma:alpine -t louislam/uptime-kuma:1-alpine -t louislam/uptime-kuma:1.6.0-alpine --target release . --push", "build-docker-alpine": "docker buildx build -f dockerfile-alpine --platform linux/amd64,linux/arm64,linux/arm/v7 -t louislam/uptime-kuma:alpine -t louislam/uptime-kuma:1-alpine -t louislam/uptime-kuma:1.6.0-alpine --target release . --push",

View file

@ -1,101 +1,104 @@
"use strict"; "use strict";
// Common Util for frontend and backend // Common Util for frontend and backend
// Backend uses the compiled file util.js //
// Frontend uses util.ts // DOT NOT MODIFY util.js!
// Need to run "tsc" to compile if there are any changes. // Need to run "tsc" to compile if there are any changes.
Object.defineProperty(exports, "__esModule", { value: true }); //
exports.getRandomInt = exports.getRandomArbitrary = exports.TimeLogger = exports.polyfill = exports.debug = exports.ucfirst = exports.sleep = exports.flipStatus = exports.STATUS_PAGE_PARTIAL_DOWN = exports.STATUS_PAGE_ALL_UP = exports.STATUS_PAGE_ALL_DOWN = exports.PENDING = exports.UP = exports.DOWN = exports.appName = exports.isDev = void 0; // Backend uses the compiled file util.js
const _dayjs = require("dayjs"); // Frontend uses util.ts
const dayjs = _dayjs; Object.defineProperty(exports, "__esModule", { value: true });
exports.isDev = process.env.NODE_ENV === "development"; exports.getRandomInt = exports.getRandomArbitrary = exports.TimeLogger = exports.polyfill = exports.debug = exports.ucfirst = exports.sleep = exports.flipStatus = exports.STATUS_PAGE_PARTIAL_DOWN = exports.STATUS_PAGE_ALL_UP = exports.STATUS_PAGE_ALL_DOWN = exports.PENDING = exports.UP = exports.DOWN = exports.appName = exports.isDev = void 0;
exports.appName = "Uptime Kuma"; const _dayjs = require("dayjs");
exports.DOWN = 0; const dayjs = _dayjs;
exports.UP = 1; exports.isDev = process.env.NODE_ENV === "development";
exports.PENDING = 2; exports.appName = "Uptime Kuma";
exports.STATUS_PAGE_ALL_DOWN = 0; exports.DOWN = 0;
exports.STATUS_PAGE_ALL_UP = 1; exports.UP = 1;
exports.STATUS_PAGE_PARTIAL_DOWN = 2; exports.PENDING = 2;
function flipStatus(s) { exports.STATUS_PAGE_ALL_DOWN = 0;
if (s === exports.UP) { exports.STATUS_PAGE_ALL_UP = 1;
return exports.DOWN; exports.STATUS_PAGE_PARTIAL_DOWN = 2;
} function flipStatus(s) {
if (s === exports.DOWN) { if (s === exports.UP) {
return exports.UP; return exports.DOWN;
} }
return s; if (s === exports.DOWN) {
} return exports.UP;
exports.flipStatus = flipStatus; }
function sleep(ms) { return s;
return new Promise(resolve => setTimeout(resolve, ms)); }
} exports.flipStatus = flipStatus;
exports.sleep = sleep; function sleep(ms) {
/** return new Promise(resolve => setTimeout(resolve, ms));
* PHP's ucfirst }
* @param str exports.sleep = sleep;
*/ /**
function ucfirst(str) { * PHP's ucfirst
if (!str) { * @param str
return str; */
} function ucfirst(str) {
const firstLetter = str.substr(0, 1); if (!str) {
return firstLetter.toUpperCase() + str.substr(1); return str;
} }
exports.ucfirst = ucfirst; const firstLetter = str.substr(0, 1);
function debug(msg) { return firstLetter.toUpperCase() + str.substr(1);
if (exports.isDev) { }
console.log(msg); exports.ucfirst = ucfirst;
} function debug(msg) {
} if (exports.isDev) {
exports.debug = debug; console.log(msg);
function polyfill() { }
/** }
* String.prototype.replaceAll() polyfill exports.debug = debug;
* https://gomakethings.com/how-to-replace-a-section-of-a-string-with-another-one-with-vanilla-js/ function polyfill() {
* @author Chris Ferdinandi /**
* @license MIT * String.prototype.replaceAll() polyfill
*/ * https://gomakethings.com/how-to-replace-a-section-of-a-string-with-another-one-with-vanilla-js/
if (!String.prototype.replaceAll) { * @author Chris Ferdinandi
String.prototype.replaceAll = function (str, newStr) { * @license MIT
// If a regex pattern */
if (Object.prototype.toString.call(str).toLowerCase() === "[object regexp]") { if (!String.prototype.replaceAll) {
return this.replace(str, newStr); String.prototype.replaceAll = function (str, newStr) {
} // If a regex pattern
// If a string if (Object.prototype.toString.call(str).toLowerCase() === "[object regexp]") {
return this.replace(new RegExp(str, "g"), newStr); return this.replace(str, newStr);
}; }
} // If a string
} return this.replace(new RegExp(str, "g"), newStr);
exports.polyfill = polyfill; };
class TimeLogger { }
constructor() { }
this.startTime = dayjs().valueOf(); exports.polyfill = polyfill;
} class TimeLogger {
print(name) { constructor() {
if (exports.isDev) { this.startTime = dayjs().valueOf();
console.log(name + ": " + (dayjs().valueOf() - this.startTime) + "ms"); }
} print(name) {
} if (exports.isDev) {
} console.log(name + ": " + (dayjs().valueOf() - this.startTime) + "ms");
exports.TimeLogger = TimeLogger; }
/** }
* Returns a random number between min (inclusive) and max (exclusive) }
*/ exports.TimeLogger = TimeLogger;
function getRandomArbitrary(min, max) { /**
return Math.random() * (max - min) + min; * Returns a random number between min (inclusive) and max (exclusive)
} */
exports.getRandomArbitrary = getRandomArbitrary; function getRandomArbitrary(min, max) {
/** return Math.random() * (max - min) + min;
* From: https://stackoverflow.com/questions/1527803/generating-random-whole-numbers-in-javascript-in-a-specific-range }
* exports.getRandomArbitrary = getRandomArbitrary;
* Returns a random integer between min (inclusive) and max (inclusive). /**
* The value is no lower than min (or the next integer greater than min * From: https://stackoverflow.com/questions/1527803/generating-random-whole-numbers-in-javascript-in-a-specific-range
* if min isn't an integer) and no greater than max (or the next integer *
* lower than max if max isn't an integer). * Returns a random integer between min (inclusive) and max (inclusive).
* Using Math.round() will give you a non-uniform distribution! * The value is no lower than min (or the next integer greater than min
*/ * if min isn't an integer) and no greater than max (or the next integer
function getRandomInt(min, max) { * lower than max if max isn't an integer).
min = Math.ceil(min); * Using Math.round() will give you a non-uniform distribution!
max = Math.floor(max); */
return Math.floor(Math.random() * (max - min + 1)) + min; function getRandomInt(min, max) {
} min = Math.ceil(min);
exports.getRandomInt = getRandomInt; max = Math.floor(max);
return Math.floor(Math.random() * (max - min + 1)) + min;
}
exports.getRandomInt = getRandomInt;

View file

@ -1,7 +1,10 @@
// Common Util for frontend and backend // Common Util for frontend and backend
//
// DOT NOT MODIFY util.js!
// Need to run "tsc" to compile if there are any changes.
//
// Backend uses the compiled file util.js // Backend uses the compiled file util.js
// Frontend uses util.ts // Frontend uses util.ts
// Need to run "tsc" to compile if there are any changes.
import * as _dayjs from "dayjs"; import * as _dayjs from "dayjs";
const dayjs = _dayjs; const dayjs = _dayjs;