Files
lucia-frontend/src/utils/jsUtils.js
2026-03-06 18:57:58 +08:00

40 lines
1.3 KiB
JavaScript

// The Lucia project.
// Copyright 2023-2026 DSP, inc. All rights reserved.
// Authors:
// chiayin.kuo@dsp.im (chiayin), 2023/1/31
// imacat.yang@dsp.im (imacat), 2023/9/23
// cindy.chang@dsp.im (Cindy Chang), 2024/5/30
/** @module jsUtils General JavaScript utility functions. */
/**
* Recursively prints an object's properties to the console.
* @param {Object} obj - The object to print.
* @param {number} [indent=0] - The current indentation level.
*/
export const printObject = (obj, indent = 0) => {
const padding = ' '.repeat(indent);
for (const key in obj) {
if (obj.hasOwnProperty(key)) {
if (typeof obj[key] === 'object' && obj[key] !== null) {
console.log(`${padding}${key}: {`);
printObject(obj[key], indent + 2);
console.log(`${padding}}`);
} else {
console.log(`${padding}${key}: ${obj[key]}`);
}
}
}
};
/**
* Returns a cryptographically random integer between 0 and max (inclusive).
* @param {number} max - The upper bound (inclusive).
* @returns {number} A random integer in [0, max].
*/
export const getRandomInt = (max) => {
const array = new Uint32Array(1);
window.crypto.getRandomValues(array);
return Math.floor(array[0] / (0xFFFFFFFF + 1) * (max + 1));
};