Skip to content

Instantly share code, notes, and snippets.

@mLuby
mLuby / promise.js
Last active June 3, 2017 00:40
Implementation of Promises
"use strict"
// promise constructor and methods
function P (callback) {
let status
let value
const thens = []
const catches = []
setTimeout(() => callback(
data => (status = "resolved", value = processData(data, thens)),
error => (status = "rejected", value = processData(error, catches))
@mLuby
mLuby / raceWithTimeout.js
Last active January 19, 2017 20:39
Race all promises once and resolve with the first resolved promise or reject with the last error. Useful for connecting.
"use strict"
// race all promises once and resolve with the first resolved promise or reject with the last error.
function raceWithTimeout (promiseList) {
const TIMEOUT = 50
return Promise.race([
...promiseList.map(suppressReject), // if any resolve, resolve with that result.
lastRejectionWithTimeout(promiseList, TIMEOUT) // if all reject, reject with last error; if timeout, reject with latest error.
])
}
@mLuby
mLuby / dedup-unique.js
Created February 9, 2017 00:34
one-liner to remove duplicate values from a list in JS. (immutable)
const dedup = list => list.reduce((uniques, item) => uniques.concat(uniques.includes(item) ? [] : [item]), [])
dedup(["a",2,"a",3,"b",2]) // ["a", 2, 3, "b"]
@mLuby
mLuby / combos.js
Created February 21, 2017 10:20
create all variants of an object given an object whose keys have arrays of values
// point-free version
function combos (obj) {
return Object.keys(obj).reduce((results, key) => (Array.isArray(obj[key]) ? obj[key] : [obj[key]]).map(value => results.map(result => Object.assign({}, result, {[key]: value}))).reduce((flattened, list) => [...flattened, ...list]), [{}])
}
test(combos({x: 1, y: [2, 3]}), [{x: 1, y: 2}, {x: 1, y: 3}])
test(combos({x: [1, 2], y: 3}), [{x: 1, y: 3}, {x: 2, y: 3}])
test(combos({x: [1, 2], y: [4, 5], z: 7}), [{x: 1, y: 4, z: 7}, {x: 2, y: 4, z: 7}, {x: 1, y: 5, z: 7}, {x: 2, y: 5, z: 7}])
test(combos({x: [1, 2], y: [4, 5]}), [{x: 1, y: 4}, {x: 2, y: 4}, {x: 1, y: 5}, {x: 2, y: 5}])
test(combos({x: [1, 2, 3], y: [4, 5]}), [
@mLuby
mLuby / SimulateDragDrop.js
Created April 13, 2017 01:07
Simulates drag and drop functionality
// // Try it here: https://www.w3schools.com/HTML/html5_draganddrop.asp
// var dragElem = document.querySelector("#drag1")
// var dropElem = document.querySelector("#div2")
// simulateDragDrop(dragElem, dropElem)
function simulateDragDrop (dragElem, dropElem) {
var event = createEvent("dragstart")
dispatchEvent(dragElem, "dragstart", event)
var dropEvent = createEvent("drop")
dropEvent.dataTransfer = event.dataTransfer
@mLuby
mLuby / ZenhubPrioritzationStats.js
Created June 15, 2017 03:03
Determines average time for zenhub issue to be prioritized.
"use strict"
// CONFIG
const ZENHUB_TOKEN = "GET IT FROM ZENHUB"
const GITHUB_TOKEN= "GET IT FROM GITHUB"
const ORG_NAME = "GITHUB ORG NAME"
const REPO_NAME = "REPO NAME"
// IMPORTS
const req = require("request-promise-native")
const moment = require("moment")
@mLuby
mLuby / privateMethodsAndData.js
Created June 20, 2017 00:49
Two ways to get private methods and data in JS.
"use strict"
console.log("––– Con –––")
function Con () {
let privateData = 123
private_makeMethodsPublic(this, [public_connect])
function public_connect (username, password) {
privateData = username + password
}
git hist master..
# last commit is abc1234
# -Xtheirs resolves merge conflicts in favor of them (current branch)
git rebase -i abc1234^ -Xtheirs
# in vim:
# cwr[escape]
# :%s/^pick /squash /gc[enter]
# :wq[enter]
# Also worth noting to get a single commit with diff between
@mLuby
mLuby / queryParser.js
Created July 20, 2018 19:17
one-liner to parse query string into key-values.
window.location.search.slice(1).match(/([^=&;]+)[=;]([^?=&;]*)/g).reduce((obj,kv)=>(obj[kv.split("=")[0].slice(1)]=kv.split("=")[1],obj),{})
@mLuby
mLuby / replace_word_in_git_history.sh
Created August 7, 2018 06:21
replace word in git history
git co master && git branch -D master-backup ; git co -b master-backup && git co master
ls -ra db/ | grep -E '\.![0-9]+\!' # should return no results
cat known/location/of/secret | grep secret # should return results
LC_CTYPE=C && LANG=C && git filter-branch --tree-filter "find . -type f -exec sed -i '' -e 's/secret/replacement/g' {} \;" -f
ls -ra db/ | grep -E '\.![0-9]+\!' # should return no results
cat known/location/of/secret | grep secret # should return no results
git filter-branch --tree-filter "grep -r 'secret' * || true"
git push -f origin master