Skip to content

Instantly share code, notes, and snippets.

View DannyMoerkerke's full-sized avatar

Danny Moerkerke DannyMoerkerke

View GitHub Profile
@DannyMoerkerke
DannyMoerkerke / test.js
Created January 11, 2024 10:47
test.js
console.log('foo')
@DannyMoerkerke
DannyMoerkerke / sw.js
Last active December 1, 2020 08:54
Full-fledged service worker which caches API requests and responses using IndexedDB
const version = 1;
const cacheName = `our_awesome_cache`
// the static files we want to cache
const staticFiles = [
'index.html',
'src/css/styles.css',
'src/img/logo.png',
'src/js/app.js'
];
@DannyMoerkerke
DannyMoerkerke / service-worker.js
Last active April 27, 2020 22:58
Bare bones service worker
// give your cache a name
const cacheName = 'my-cache';
// put the static assets and routes you want to cache here
const filesToCache = [
'/',
'/about',
'/index.html',
'/about.html',
'/css/styles.css',
@DannyMoerkerke
DannyMoerkerke / haskey.ts
Created October 16, 2019 10:51
A typesafe curried getter
// HasKey checks whether the given object has the specified key 'K'
type HasKey<K extends string, V = unknown> = {[_ in K]: V};
// curried getter
const get = <K extends string, O extends HasKey<K>>(key: K) => (obj: O): O[K] => obj[key];
// the object to check
const Person = {name: 'Danny', age: '47'};
const getAge = get('age');
@DannyMoerkerke
DannyMoerkerke / style.js
Created October 3, 2019 07:03
Set multiple styles on an HTML element in one go
// select an element to style
const container = document.querySelector('#container');
// this is not ideal...
container.style.width = '400px;';
container.style.height = '300px';
container.style.top = '0';
container.style.left = '50%';
// let's assign all styles in one go!
@DannyMoerkerke
DannyMoerkerke / curry.js
Last active August 29, 2015 14:11
Currying in just 6 lines of Javascript code
// this helper function takes as argument the function that will be curried
function curry(func) {
return function accumulator() {
var args = [].slice.call(arguments);
return args.length < func.length ? accumulator.bind.apply(accumulator, [null].concat(args)) : func.apply(null, args);
};
}
// Use it: first create two function that will be curried
function add(a,b,c) {