Skip to content

Instantly share code, notes, and snippets.

@arturparkhisenko
Last active January 13, 2019 04:58
Show Gist options
  • Save arturparkhisenko/51a7e5b596dd2488718ec73d64bfa126 to your computer and use it in GitHub Desktop.
Save arturparkhisenko/51a7e5b596dd2488718ec73d64bfa126 to your computer and use it in GitHub Desktop.
js-utils.js
// https://twitter.com/0x00A/status/904419238110261250
// Unique array (of primitives)
const haystack = ['a', 'b', 'a']
const unique = [...new Set(haystack)]
// --------------------------------------------------
// https://twitter.com/rauschma/status/961020269446991872
// Removing duplicate characters from a string:
const filtered = [...new Set('cabbcc')].join(''); // 'cab'
// --------------------------------------------------
// https://twitter.com/MylesBorins/status/930882459427835904
(async () => { }()); // old (function () {}())
// --------------------------------------------------
// https://twitter.com/WebReflection/status/933759941818626054?s=09
// splice: a single method to rule them all
const a = [5];
a.splice(0, 0, 1, 2); // unshift(1, 2)
a.splice(2, 1, 3, 4); // swap(5).with(3, 4) => [5]
a.splice(a.length, 0, 9, 8);// push(9, 8)
a.splice(4, 1); // remove 9 => [9]
a.splice(4, 0, 6, 7); // insert(6, 7).after(4)
// --------------------------------------------------
// https://twitter.com/lukejacksonn/status/928244319760220160/photo/1
// 8 immutable arrays operations
clone = x => [...x];
push = y => x => [...x, y];
pop = x => x.slice(0, -1);
unshift = y => x => [y, ...x];
shift = x => x.slice(1);
sort = f => x => [...x].sort(f);
del = i => x => [...x.slice(0,i), ...x.slice(i+1)];
splice = (s, c, ...y) => x => [...x.slice(0, s), ...y, ...x.slice(s+c)];
// --------------------------------------------------
// Overflow type value protection:
const u = (a, b) => (a + b) / 2; // usual
const f = (a, b) => a + ((b - a) / 2); // fixed
// --------------------------------------------------
// get param from url query (2d option for hash based routing)
const searchParams = new URLSearchParams(window.location.search || window.location.hash.substr(2));
const code = searchParams.get('code');
// --------------------------------------------------
// https://30secondsofcode.org/
const pipeAsyncFunctions = (...fns) => arg => fns.reduce((p, f) => p.then(f), Promise.resolve(arg));
// --------------------------------------------------
// Add Error if parameter is missing
const required = () => {throw new Error('Missing parameter')};
const add = (a = required(), b = required()) => a + b;
add(1, 2) //3
add(1) // Error: Missing parameter.
// --------------------------------------------------
// https://dev.to/vaheqelyan/http-request-with-es6-tagged-templates-2d72
// https://github.com/kay-is/awesome-tagged-templates
const { data } = await get`https://api.github.com/repos/zeit/next.js`;
// --------------------------------------------------
// https://twitter.com/kyleshevlin/status/1080906004030644224?s=21
let prop;
let result = {...(prop && {prop})};
// prop === {}
prop = 'value';
result = {...(prop && {prop})};
// result === {prop: 'value'}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment