Skip to content

Instantly share code, notes, and snippets.

@codestaintin
Forked from mikowl/oneliners.js
Created April 8, 2019 12:20
Show Gist options
  • Save codestaintin/930074c1d70efe7d08dd1aa9c7645149 to your computer and use it in GitHub Desktop.
Save codestaintin/930074c1d70efe7d08dd1aa9c7645149 to your computer and use it in GitHub Desktop.

Revisions

  1. @mikowl 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
  2. @mikowl 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
  3. @mikowl 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)]
  4. @mikowl 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