Skip to content

Instantly share code, notes, and snippets.

View EmilienD's full-sized avatar

Emilien Durieu EmilienD

View GitHub Profile
@EmilienD
EmilienD / event-loop-accumulator.js
Created May 6, 2021 13:38
"Accumulate" all the calls to a function in a each iteration of the event loop so that they can be treated together ("insert all" instead of "insert one" for every call, or actually applying only the last "update" of a model for example)
function accumulator(callback){
let acc = []
let timeout = null
return (val) =>{
acc.push(val)
clearTimeout(timeout)
timeout = setTimeout(() => {
callback(acc)
acc = []
@EmilienD
EmilienD / promise-queue.js
Created May 4, 2021 10:24
simple promise queue to execute promises consecutively
function createQueue() {
const queue = [];
let pendingPromise = false;
function enqueue(callback) {
queue.push(callback);
dequeue();
}
function dequeue() {
@EmilienD
EmilienD / immutable-fifo.js
Created April 29, 2021 09:15
One liner to use arrays as fifo cache without mutating the array
const fifo = limit => array => item => array.length === limit ? array.slice(1).concat(item) : array.concat(item)
@EmilienD
EmilienD / designModeToggler.user.js
Last active July 25, 2019 13:47
User script to easily toggle document.designMode on a webpage
// ==UserScript==
// @name DesignMode toggler
// @version 1
// @grant none
// @include *
// ==/UserScript==
const shortcut = {
key: '1',
ctrlKey: true,
@EmilienD
EmilienD / BRANCH_NAME_MAKER.js
Last active July 25, 2019 13:10
Unsophisticated user script to create branch name out of Jira ticket page (triggered by clicking on the tomato dot in the top right corner, or ctrl+shift+B)
// ==UserScript==
// @name BRANCH_NAME_MAKER
// @include https://qover001.atlassian.net/browse/*
// @grant GM.setClipboard
// ==/UserScript==
const clickZone = document.createElement('div')
clickZone.style = `
position: absolute;
top: 15px;
@EmilienD
EmilienD / google-analytics-dataLayer-displayer.js
Created November 15, 2018 10:56
Showing the current state of the dataLayer in a few lines of javascript
var dataLayerDisplayer = document.createElement('div')
dataLayerDisplayer.style= "font-size: 8; background-color: rgba(255,255,255,0.5); position: fixed; top:0; left: 0; z-index: 9001; max-height: 50vh; overflow: scroll;"
dataLayerDisplayer.id = "dataLayerDisplayer"
document.querySelector('body').append(dataLayerDisplayer)
setInterval(() => document.querySelector('#dataLayerDisplayer').innerHTML = JSON.stringify(dataLayer.concat().reverse(), null, 2).split('\n').join('<br>').split(' ').join('&nbsp;'), 500)
@EmilienD
EmilienD / index-dot-js-maker.js
Last active March 16, 2018 09:48
generate an index.js file as a commonjs style module
// usage: `node index-dot-js-maker.js <target directory>`
const Promise = require('bluebird');
const fs = Promise.promisifyAll(require('fs'));
const path = require('path');
const _ = require('lodash');
const cwd = process.cwd();
const maindir = path.resolve(cwd, process.argv[process.argv.length - 1]);
@EmilienD
EmilienD / .vimrc
Created February 21, 2017 21:25
simple vimrc
set smartindent
set number
set shiftwidth=2
set softtabstop=2
set tabstop=2
set expandtab