Skip to content

Instantly share code, notes, and snippets.

@mikowl
Last active September 24, 2025 19:23
Show Gist options
  • Select an option

  • Save mikowl/5fa1a06c06264afaa544bb6b8af641b1 to your computer and use it in GitHub Desktop.

Select an option

Save mikowl/5fa1a06c06264afaa544bb6b8af641b1 to your computer and use it in GitHub Desktop.

Revisions

  1. mikowl revised this gist Apr 29, 2023. 1 changed file with 1 addition and 2 deletions.
    3 changes: 1 addition & 2 deletions oneliners.js
    Original file line number Diff line number Diff line change
    @@ -90,14 +90,13 @@ const getSelectedText = () => window.getSelection().toString();
    // Simple star rating ex: rating(5) will print ★★★★★
    const rating = star => "★".repeat(Math.max(0, Math.min(star, 5))).padEnd(5,"☆" );


    // because this primeagen tweet:
    // https://twitter.com/ThePrimeagen/status/1646204396655591439
    // Check if is an array
    const isArr = (arr) => Array.isArray(arr);

    // Left pad function
    const lefPad = (s, l, c = ' ') => c.repeat(l - s.length) + s;
    const leftPad = (s, l, c = ' ') => c.repeat(l - s.length) + s;

    // Check if num is positive
    const isPosi = (num) => num > 0;
  2. mikowl revised this gist Apr 13, 2023. 1 changed file with 11 additions and 0 deletions.
    11 changes: 11 additions & 0 deletions oneliners.js
    Original file line number Diff line number Diff line change
    @@ -91,3 +91,14 @@ const getSelectedText = () => window.getSelection().toString();
    const rating = star => "★".repeat(Math.max(0, Math.min(star, 5))).padEnd(5,"☆" );


    // because this primeagen tweet:
    // https://twitter.com/ThePrimeagen/status/1646204396655591439
    // Check if is an array
    const isArr = (arr) => Array.isArray(arr);

    // Left pad function
    const lefPad = (s, l, c = ' ') => c.repeat(l - s.length) + s;

    // Check if num is positive
    const isPosi = (num) => num > 0;

  3. mikowl revised this gist Apr 4, 2023. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion oneliners.js
    Original file line number Diff line number Diff line change
    @@ -36,7 +36,7 @@ const add42 = n => (console.log(n), n + 42);
    console.log({ a, b, c, d, e});

    // Random hex color
    const generateRandomHexColor = () => `#${Math.floor(Math.random() * 0xffffff).toString(16).padEnd(6, "0"}`;
    const generateRandomHexColor = () => `#${Math.floor(Math.random() * 0xffffff).toString(16).padEnd(6, "0")}`;

    // We love Javascript that's why instead of Math.floor we use
    // Note: Use with caution, it won't work for big (>32bit) or negative numbers
  4. mikowl revised this gist Jan 18, 2023. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions oneliners.js
    Original file line number Diff line number Diff line change
    @@ -87,3 +87,7 @@ const navigateBack = () => history.back();
    // Get selected text
    const getSelectedText = () => window.getSelection().toString();

    // Simple star rating ex: rating(5) will print ★★★★★
    const rating = star => "★".repeat(Math.max(0, Math.min(star, 5))).padEnd(5,"☆" );


  5. mikowl revised this gist Oct 2, 2022. 1 changed file with 1 addition and 3 deletions.
    4 changes: 1 addition & 3 deletions oneliners.js
    Original file line number Diff line number Diff line change
    @@ -36,9 +36,7 @@ const add42 = n => (console.log(n), n + 42);
    console.log({ a, b, c, d, e});

    // Random hex color
    '#'+(~~(Math.random()*0xffffff)).toString(16).padEnd(6,0)
    // or
    const generateRandomHexColor = () => `#${Math.floor(Math.random() * 0xffffff).toString(16)}`;
    const generateRandomHexColor = () => `#${Math.floor(Math.random() * 0xffffff).toString(16).padEnd(6, "0"}`;

    // We love Javascript that's why instead of Math.floor we use
    // Note: Use with caution, it won't work for big (>32bit) or negative numbers
  6. mikowl revised this gist Aug 19, 2022. 1 changed file with 9 additions and 5 deletions.
    14 changes: 9 additions & 5 deletions oneliners.js
    Original file line number Diff line number Diff line change
    @@ -62,11 +62,15 @@ console.log(getUnique(arr));
    const isDarkMode = () => window.matchMedia && window.matchMedia("(prefers-color-scheme: dark)").matches;
    console.log(isDarkMode());

    // Scroll to Top
    const scrollToTop = (element) => element.scrollIntoView({ behavior: "smooth", block: "start" });

    // Scroll To Bottom
    const scrollToBottom = (element) => element.scrollIntoView({ behavior: "smooth", block: "end" });
    // Scroll to top of page
    const scrollToTop = () => window.scrollTo({ top: 0, behavior: 'smooth' });
    // Scroll to bottom of page
    const scrollToBottom = () => window.scrollTo({ top: document.body.scrollHeight, behavior: 'smooth' });

    // Scroll to top of an element
    const scrollToEl = (element) => element.scrollIntoView({ behavior: "smooth", block: "start" });
    // Scroll to elements bottom
    const scrollToElb = (element) => element.scrollIntoView({ behavior: "smooth", block: "end" });

    // Check if an element is focused
    const elem = document.querySelector(' .text-input');
  7. mikowl revised this gist Jun 17, 2022. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions oneliners.js
    Original file line number Diff line number Diff line change
    @@ -79,5 +79,9 @@ const arrIsEmpty = !(Array.isArray(arr) && arr.length > 0);
    // Redirect user
    const redirect = url => location.href = url // ex: recdirect("https://google.com")

    // Go back to previous page
    const navigateBack = () => history.back();

    // Get selected text
    const getSelectedText = () => window.getSelection().toString();

  8. mikowl revised this gist May 11, 2022. 1 changed file with 15 additions and 1 deletion.
    16 changes: 15 additions & 1 deletion oneliners.js
    Original file line number Diff line number Diff line change
    @@ -66,4 +66,18 @@ console.log(isDarkMode());
    const scrollToTop = (element) => element.scrollIntoView({ behavior: "smooth", block: "start" });

    // Scroll To Bottom
    const scrollToBottom = (element) => element.scrollIntoView({ behavior: "smooth", block: "end" });
    const scrollToBottom = (element) => element.scrollIntoView({ behavior: "smooth", block: "end" });

    // Check if an element is focused
    const elem = document.querySelector(' .text-input');
    const isFocus = elem == document.activeElemnt;

    // Check if an array is empty
    let arr = [1,2,3,4];
    const arrIsEmpty = !(Array.isArray(arr) && arr.length > 0);

    // Redirect user
    const redirect = url => location.href = url // ex: recdirect("https://google.com")



  9. mikowl revised this gist Mar 11, 2022. 1 changed file with 27 additions and 2 deletions.
    29 changes: 27 additions & 2 deletions oneliners.js
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,4 @@
    // By @coderitual
    // https://twitter.com/coderitual/status/1112297299307384833
    // Inspired by https://twitter.com/coderitual/status/1112297299307384833 and https://tapajyoti-bose.medium.com/7-killer-one-liners-in-javascript-33db6798f5bf

    // Remove any duplicates from an array of primitives.
    const unique = [...new Set(arr)]
    @@ -38,7 +37,33 @@ console.log({ a, b, c, d, e});

    // Random hex color
    '#'+(~~(Math.random()*0xffffff)).toString(16).padEnd(6,0)
    // or
    const generateRandomHexColor = () => `#${Math.floor(Math.random() * 0xffffff).toString(16)}`;

    // We love Javascript that's why instead of Math.floor we use
    // Note: Use with caution, it won't work for big (>32bit) or negative numbers
    ~~anyNumber

    // Shuffle Array
    const shuffleArray = (arr) => arr.sort(() => Math.random() - 0.5);
    const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
    console.log(shuffleArray(arr));

    // Copy to Clipboard
    const copyToClipboard = (text) => navigator.clipboard?.writeText && navigator.clipboard.writeText(text);
    copyToClipboard("Hello World!");

    // Unique Elements
    const getUnique = (arr) => [...new Set(arr)];
    const arr = [1, 1, 2, 3, 3, 4, 4, 4, 5, 5];
    console.log(getUnique(arr));

    // Detect Dark Mode
    const isDarkMode = () => window.matchMedia && window.matchMedia("(prefers-color-scheme: dark)").matches;
    console.log(isDarkMode());

    // Scroll to Top
    const scrollToTop = (element) => element.scrollIntoView({ behavior: "smooth", block: "start" });

    // Scroll To Bottom
    const scrollToBottom = (element) => element.scrollIntoView({ behavior: "smooth", block: "end" });
  10. mikowl revised this gist Mar 17, 2020. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions oneliners.js
    Original file line number Diff line number Diff line change
    @@ -6,6 +6,8 @@ const unique = [...new Set(arr)]

    // Sleep in async functions. Use: await sleep(2000).
    const sleep = (ms) => (new Promise(resolve => setTimeout(resolve, ms)));
    // or
    const sleep = util.promisify(setTimeout);

    // Type this in your code to break chrome debugger in that line.
    debugger;
  11. mikowl revised this gist Apr 2, 2019. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions oneliners.js
    Original file line number Diff line number Diff line change
    @@ -38,4 +38,5 @@ console.log({ a, b, c, d, e});
    '#'+(~~(Math.random()*0xffffff)).toString(16).padEnd(6,0)

    // We love Javascript that's why instead of Math.floor we use
    // Note: Use with caution, it won't work for big (>32bit) or negative numbers
    ~~anyNumber
  12. mikowl revised this gist Apr 2, 2019. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions oneliners.js
    Original file line number Diff line number Diff line change
    @@ -29,13 +29,13 @@ items[n % items.length]
    const addFortyTwo = number => console.log(number) || number + 42

    // Same as above
    const add42 = n => (console.log(n), number + 42);
    const add42 = n => (console.log(n), n + 42);

    // Log variables with names. I love this trick with object ❤️
    console.log({ a, b, c, d, e});

    // Random hex color
    '#'+Math.floor(Math.random()*16777215).toString(16)
    '#'+(~~(Math.random()*0xffffff)).toString(16).padEnd(6,0)

    // We love Javascript that's why instead of Math.floor we use
    ~~anyNumber
  13. mikowl renamed this gist Apr 1, 2019. 1 changed file with 2 additions and 3 deletions.
    5 changes: 2 additions & 3 deletions gistfile1.txt → oneliners.js
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,5 @@
    By @coderitual
    https://twitter.com/coderitual/status/1112297299307384833
    --
    // By @coderitual
    // https://twitter.com/coderitual/status/1112297299307384833

    // Remove any duplicates from an array of primitives.
    const unique = [...new Set(arr)]
  14. mikowl created this gist Apr 1, 2019.
    42 changes: 42 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,42 @@
    By @coderitual
    https://twitter.com/coderitual/status/1112297299307384833
    --

    // Remove any duplicates from an array of primitives.
    const unique = [...new Set(arr)]

    // Sleep in async functions. Use: await sleep(2000).
    const sleep = (ms) => (new Promise(resolve => setTimeout(resolve, ms)));

    // Type this in your code to break chrome debugger in that line.
    debugger;

    // Just plain english.
    [...].every(Number.isFinite);

    // Returns all non-falsy values from an array.
    [...].filter(Boolean)

    // Array destructuring to see matching elements.
    let [r, g, b, a] = [255, 0, 0, 255];

    // Object destructuring to reduce multiple lines of code to a single line.
    let {width, height} = resolution;

    // Gets an item from the list and wraps around to the start if n is larger than the list.
    items[n % items.length]

    // Console.log in array function without adding curly braces.
    const addFortyTwo = number => console.log(number) || number + 42

    // Same as above
    const add42 = n => (console.log(n), number + 42);

    // Log variables with names. I love this trick with object ❤️
    console.log({ a, b, c, d, e});

    // Random hex color
    '#'+Math.floor(Math.random()*16777215).toString(16)

    // We love Javascript that's why instead of Math.floor we use
    ~~anyNumber