Skip to content

Instantly share code, notes, and snippets.

@nogorilla
Created May 1, 2017 18:36
Show Gist options
  • Select an option

  • Save nogorilla/15b277ce489ddc81b9e3842142ae3127 to your computer and use it in GitHub Desktop.

Select an option

Save nogorilla/15b277ce489ddc81b9e3842142ae3127 to your computer and use it in GitHub Desktop.
ES6 one-liners
// NodeList to Array:
var headings = [ ... document.querySelectorAll('h1') ];
// Unique Arrays:
[ ...new Set(array) ]
// Destructuring:
var {foo, bar} = {foo: "lorem", bar: "ipsum"};
// foo => lorem and bar => ipsum
// Swap like a snake:
[a,b] = [b,a]
// Max in array?:
Math.max(...array);
// List comprehension:
[ for (value of ["Harriet", "178"]) value ].join(" was ");
// Would give us "Harriet was 178"
// BTW it was Charles Darwin's tortoise.
// _map?
[{id: 1}, {id: 2}].map(x => x.id)
// [1,2]
// Object.isz!
let Object.isz = (x, y) => x === y || Object.is(x, y);
// Is it a hidden file?:
isHidden = (file) => /^\.|~$/.test(file);
isHidden(".DS_STORE") // true
// Repeat with me:
(0/0+"").repeat("7")+ " BatMan!"
// "NaNNaNNaNNaNNaNNaNNaN BatMan!"
// remove falsey values from arrays, like null and undefined
array.filter(Boolean);
// sum of array of numbers
array.reduce((sum, num) => sum + num, 0);
// Object.values polyfill
Object.values = (obj) => Object.keys(obj).map(key => obj[key]);
// Sleep an async function for a second
await new Promise(r => setTimeout(r, 1000));
// Wait for up to a second for work to be done
await Promise.race([doWork(), new Promise(r => setTimeout(r, 1000))]);
// Bonus: (not a one liner, but great for testing async in the console)
(async () => { await doWork(); await moreWork(); })().then(console.log, console.error);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment