Source: user-data-access-docs.js

/**
 * Class to handle user data access with localStorage.
 * Provides methods for retrieving, adding, updating, and deleting user data.
 */
class UserDataAccess {

    ///////////////////////////////////////////////
    // PRIVATE INSTANCE VARIABLES (start with #)
    ///////////////////////////////////////////////

    /**
     * Dummy data to populate the localStorage database.
     * @type {Array<{id: number, firstName: string, lastName: string, email: string}>}
     */
    #dummyData = [
        {id: 1, firstName: "Jane", lastName: "Doe", email: "jdoe@acme.com"},
        {id: 2, firstName: "Tony", lastName: "Thompsom", email: "tony@acme.com"},
        {id: 3, firstName: "Jesse", lastName: "Jones", email: "jesse@acme.com"},
        {id: 4, firstName: "Bob", lastName: "Doe", email: "jdoe@acme.com"},
        {id: 5, firstName: "ToM", lastName: "Thompsom", email: "tony@acme.com"},
        {id: 6, firstName: "Susie", lastName: "Jones", email: "jesse@acme.com"}
    ];

    ////////////////////////////////////
    // CONSTRUCTOR
    ////////////////////////////////////

    /**
     * Initializes the UserDataAccess instance. Checks if the "userData" key exists in localStorage,
     * if not, initializes it with the dummy data.
     */
    constructor() {
        if (!localStorage.getItem("userData")) {
            localStorage.setItem("userData", JSON.stringify(this.#dummyData));
        }
    }

    ////////////////////////////////////
    // PUBLIC METHODS
    ////////////////////////////////////

    /**
     * Retrieves all users from localStorage.
     * @returns {Array<{id: number, firstName: string, lastName: string, email: string}>} List of users.
     */
    getAllUsers() {
        const str = localStorage.getItem("userData");
        const users = JSON.parse(str);
        return users;
    }

    /**
     * Retrieves a user by their ID from localStorage.
     * @param {number} id - The ID of the user to retrieve.
     * @returns {Object|null} The user object if found, else null.
     */
    getUserById(id) {
        const str = localStorage.getItem("userData");
        const users = JSON.parse(str);
        const user = users.find((u) => u.id == id);
        return user || null;
    }

    /**
     * Inserts a new user into the localStorage database.
     * @param {Object} newUser - The user object to insert.
     * @param {number} newUser.id - The user's ID (automatically assigned).
     * @param {string} newUser.firstName - The user's first name.
     * @param {string} newUser.lastName - The user's last name.
     * @param {string} newUser.email - The user's email address.
     */
    insertUser(newUser) {
        newUser.id = this.#getMaxId() + 1;
        const str = localStorage.getItem("userData");
        const users = JSON.parse(str);
        users.push(newUser);
        localStorage.setItem("userData", JSON.stringify(users));
    }

    /**
     * Updates an existing user in the localStorage database.
     * @param {Object} updatedUser - The user object to update.
     * @param {number} updatedUser.id - The ID of the user to update.
     * @param {string} updatedUser.firstName - The updated first name.
     * @param {string} updatedUser.lastName - The updated last name.
     * @param {string} updatedUser.email - The updated email.
     */
    updateUser(updatedUser) {
        const str = localStorage.getItem("userData");
        const users = JSON.parse(str);
        const indexOfUserToUpdate = users.findIndex(user => updatedUser.id == user.id);
        users[indexOfUserToUpdate] = updatedUser;
        localStorage.setItem("userData", JSON.stringify(users));
    }

    /**
     * Deletes a user from the localStorage database by their ID.
     * @param {number} id - The ID of the user to delete.
     */
    deleteUser(id) {
        const str = localStorage.getItem("userData");
        const users = JSON.parse(str);
        const indexOfUserToDelete = users.findIndex(u => u.id == id);
        users.splice(indexOfUserToDelete, 1);
        localStorage.setItem("userData", JSON.stringify(users));
    }

    ////////////////////////////////////
    // PRIVATE METHODS (start with #)
    ////////////////////////////////////

    /**
     * Private method to find the maximum user ID in the localStorage database.
     * @returns {number} The highest user ID currently stored.
     */
    #getMaxId() {
        const str = localStorage.getItem("userData");
        const users = JSON.parse(str);
        let maxId = 0;

        for (let i = 0; i < users.length; i++) {
            if (users[i].id > maxId) {
                maxId = users[i].id;
            }
        }
        return maxId;
    }
}