Skip to content

Instantly share code, notes, and snippets.

@cferdinandi
Created April 8, 2025 16:48
Show Gist options
  • Save cferdinandi/30d4bdad7eb4f76047b7c0cc72be684c to your computer and use it in GitHub Desktop.
Save cferdinandi/30d4bdad7eb4f76047b7c0cc72be684c to your computer and use it in GitHub Desktop.

Revisions

  1. cferdinandi created this gist Apr 8, 2025.
    27 changes: 27 additions & 0 deletions 1-nested.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,27 @@
    function handleClick (event) {

    // Make sure clicked element has the .save-data class
    if (event.target.matches('.save-data')) {

    // Get the value of the [data-id] attribute
    let id = event.target.getAttribute('data-id');

    // Make sure there's an ID
    if (id) {

    // Get the user token from localStorage
    let token = localStorage.getItem('token');

    // Make sure there's a token
    if (token) {

    // Save the ID to localStorage
    localStorage.setItem(`${token}_${id}`, true);

    }

    }

    }

    }
    21 changes: 21 additions & 0 deletions 2-early-return.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,21 @@
    function handleClick (event) {

    // Make sure clicked element has the .save-data class
    if (!event.target.matches('.save-data')) return;

    // Get the value of the [data-id] attribute
    let id = event.target.getAttribute('data-id');

    // Make sure there's an ID
    if (!id) return;

    // Get the user token from localStorage
    let token = localStorage.getItem('token');

    // Make sure there's a token
    if (!token) return;

    // Save the ID to localStorage
    localStorage.setItem(`${token}_${id}`, true);

    }
    17 changes: 17 additions & 0 deletions 3-early-return-cleanup.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,17 @@
    function handleClick (event) {

    // Make sure clicked element has the .save-data class
    if (!event.target.matches('.save-data')) return;

    // Get the value of the [data-id] attribute
    let id = event.target.getAttribute('data-id');
    if (!id) return;

    // Get the user token from localStorage
    let token = localStorage.getItem('token');
    if (!token) return;

    // Save the ID to localStorage
    localStorage.setItem(`${token}_${id}`, true);

    }