Skip to content

Instantly share code, notes, and snippets.

@kelvin8773
Forked from mikowl/oneliners.js
Created April 3, 2019 00:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kelvin8773/028406ce27f14e4f2e03c1b785616f33 to your computer and use it in GitHub Desktop.
Save kelvin8773/028406ce27f14e4f2e03c1b785616f33 to your computer and use it in GitHub Desktop.
πŸ‘‘ Awesome one-liners you might find useful while coding.
// 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), n + 42);
// Log variables with names. I love this trick with object ❀️
console.log({ a, b, c, d, e});
// Random hex color
'#'+(~~(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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment