prs-utils
Version:
Prs Utilities
140 lines (121 loc) • 4.09 kB
JavaScript
export function getCurrentTimeInfo() {
const now = new Date()
const year = now.getFullYear() // Current year
const month = now.getMonth() + 1 // Current month (0-based, so add 1)
const day = now.getDate() // Current day of the month
const hour = now.getHours() // Current hour
const minutes = now.getMinutes() // Current minutes
const seconds = now.getSeconds() // Current seconds
const milliseconds = now.getMilliseconds() // Current milliseconds
return { year, month, day, hour, minutes, seconds, milliseconds }
}
export function timestamp() {
function pd(x, n = 2) {
return (x + "").padStart(n, "0")
}
let d = getCurrentTimeInfo()
let r = `${pd(d.year, 4)}-${pd(d.month)}-${pd(d.day)}--${pd(d.hour)}-${pd(d.minutes)}-${pd(d.seconds)}-${pd(
d.milliseconds,
3
)}`
return r
}
export function timestampIso() {
return new Date().toISOString()
}
export function guid() {
return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function (c) {
var r = (Math.random() * 16) | 0,
v = c == "x" ? r : (r & 0x3) | 0x8
return v.toString(16)
})
}
export function base64UrlEncode(input, valOnErr = "") {
let base64
if (typeof window === "undefined") {
// Node.js environment
try {
base64 = Buffer.from(input).toString("base64")
} catch (err) {
console.log(err)
return valOnErr
}
} else {
// Browser environment
try {
base64 = btoa(input)
} catch (err) {
console.log(err)
console.log("method input argument", input)
return valOnErr
}
}
// Convert Base64 to Base64URL
const base64Url = base64
.replace(/\+/g, "-") // Replace + with -
.replace(/\//g, "_") // Replace / with _
.replace(/=+$/, "") // Remove padding
return base64Url
}
export function base64UrlDecode(input) {
// Convert Base64URL to Base64
const base64 = input
.replace(/-/g, "+") // Replace - with +
.replace(/_/g, "/") // Replace _ with /
.padEnd(input.length + ((4 - (input.length % 4)) % 4), "=") // Add padding
let decodedString
if (typeof window === "undefined") {
// Node.js environment
decodedString = Buffer.from(base64, "base64").toString("utf-8")
} else {
// Browser environment
decodedString = atob(base64)
}
return decodedString
}
export function uni(arr) {
return Array.from(new Set(arr || []))
}
export function gei(id) {
return document.getElementById(id)
}
export function log(msgText, config = {}) {
let txt = msgText
if (typeof window !== "undefined") {
// Set a default color for Browser
if (config.clr == null) config.clr = "black"
if (config.bg == null) config.bg = "white"
if (config.ts == null) config.ts = 0
// Browser console (using CSS)
if (config.ts === 1) {
const timestamp = new Date().toISOString()
txt = timestamp + " -> " + msgText
}
const style = `background-color: ${config.bg}; color: ${config.clr}; padding: 2px 4px; border-radius: 4px;`
console.log(`%c${txt}`, style)
} else {
// Set a default color for Browser
if (config.clr == null) config.clr = "white"
if (config.bg == null) config.bg = "black"
if (config.ts == null) config.ts = 0
if (config.ts === 1) {
const timestamp = new Date().toISOString()
txt = timestamp + " -> " + msgText
}
// Node.js console (using ANSI escape sequences)
const colorCodes = {
black: { fg: 30, bg: 40 },
red: { fg: 31, bg: 41 },
green: { fg: 32, bg: 42 },
yellow: { fg: 33, bg: 43 },
blue: { fg: 34, bg: 44 },
magenta: { fg: 35, bg: 45 },
cyan: { fg: 36, bg: 46 },
white: { fg: 37, bg: 47 },
}
const fgCode = colorCodes[config.clr] ? colorCodes[config.clr].fg : 37
const bgCode = colorCodes[config.bg] ? colorCodes[config.bg].bg : 40
const coloredMessage = `\x1b[${fgCode}m\x1b[${bgCode}m${txt}\x1b[0m`
console.log(coloredMessage)
}
}