docs: add JSDoc comments to SharedService

This commit is contained in:
Chneemann 2024-11-14 04:41:53 +01:00
parent 8557a2f7f0
commit 11e9161135

View file

@ -22,26 +22,65 @@ export class SharedService {
// CHECK VIEW WIDTH
/**
* Initializes the window resize listener.
*
* @remarks
* This function will set up the window resize listener and bind the
* onResize() method to it.
*/
private initResizeListener() {
this.resizeListener = this.onResize.bind(this);
window.addEventListener('resize', this.resizeListener);
}
/**
* Removes the window resize listener.
*
* @remarks
* This function will remove the window resize listener that was set up in
* the constructor. This is useful when the service is being destroyed.
*/
private removeResizeListener() {
window.removeEventListener('resize', this.resizeListener);
}
/**
* Window resize event listener method.
*
* @remarks
* This method will update the isPageViewMedia and isContactViewMedia
* properties based on the current window width whenever the window is
* resized.
*/
private onResize() {
this.isPageViewMedia = window.innerWidth <= 650;
this.isContactViewMedia = window.innerWidth <= 800;
}
/**
* Cleans up the service by removing the window resize listener.
*
* @remarks
* This function is useful when the service is being destroyed.
*/
cleanup() {
this.removeResizeListener();
}
// RANDOM COLOR
/**
* Generates a random color string.
*
* @remarks
* The generated color is a hex string that is not a shade of gray.
* The color is generated by randomly selecting a hex digit from the
* string '0123456789ABCDEF' and appending it to the color string. The
* loop continues until a non-gray color is generated.
*
* @returns A random color string.
*/
generateRandomColor(): string {
const letters = '0123456789ABCDEF';
let color = '#';
@ -62,6 +101,13 @@ export class SharedService {
return color;
}
/**
* Converts a hex color string to an RGB object.
*
* @param hex A color string in hex format, e.g. '#FF0000'.
* @returns An object with r, g, and b properties, each an integer between
* 0 and 255, representing the red, green, and blue components of the color.
*/
hexToRgb(hex: string): { r: number; g: number; b: number } {
const bigint = parseInt(hex.substring(1), 16);
const r = (bigint >> 16) & 255;
@ -72,6 +118,12 @@ export class SharedService {
// SECURITY
/**
* Sanitizes the input string by replacing any potential cross-site scripting characters.
* This ensures that the input string is safe to be stored in the Firebase Realtime Database.
* @param input The input string to be sanitized.
* @returns The sanitized string.
*/
replaceXSSChars(input: string) {
return input.replace(/</g, '&lt;').replace(/>/g, '&gt;');
}