Skip to content

Instantly share code, notes, and snippets.

View zeusdeux's full-sized avatar
👾
i wear my sunglasses at night so i can — so i can justify the money spent on 'em

Mudit zeusdeux

👾
i wear my sunglasses at night so i can — so i can justify the money spent on 'em
View GitHub Profile
@zeusdeux
zeusdeux / intersectionObserver.js
Created May 13, 2018 19:52
IntersectionObserver weirdness
// navigate to https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API#Intersection_observer_concepts_and_usage
// open console and run the code below
const target = document.getElementById('How_intersection_is_calculated')
const observer = new IntersectionObserver(entries => console.log(entries[0], entries[0].isIntersecting), {root: null, threshold: 0.9})
observer.observe(target)
@zeusdeux
zeusdeux / play-in-dev-console.js
Last active March 7, 2018 03:39
Async generators as wrappers around event emitters
let socket = new WebSocket('wss://echo.websocket.org')
async function main(socket) {
for await (let {value, done} of getDataFromWebSocket(socket)) {
if (!done) console.log('Data:', value)
else console.log('WebSocket is closed.')
}
return 'Done running the main fn!'
} // hit return/enter
@zeusdeux
zeusdeux / negativeIndexArray.js
Last active January 13, 2018 23:45
Make arrays negative indexable using a Proxy
function makeArrayNegativeIndexable(arr) {
if (!Array.isArray(arr)) return arr
return new Proxy(arr, {
get(target, prop) {
return target[getIndex(target.length, prop)]
},
set(target, prop, val) {
let index = getIndex(target.length, prop)
@zeusdeux
zeusdeux / destructuring.js
Last active June 16, 2021 09:44
Ways I use destructuring in JS
// basic destructuring
const { key1 } = { key1: 10 } // key1 = 10
const [x, y, z] = [1, 2, 3] // x = 1, y = 2, z = 3
const [head, ...tail] = [1, 2, 3] // head = 1, tail = [2, 3]
const { a: { b } } = { a: { b: 20 } } // b = 20
// storing value in a variable with a different name
const { key1: varForKey1 } = { key1: 20 } // varForKey1 = 20
const { 'quoted-key': x } = { 'quoted-key': 10 } // x = 10
@zeusdeux
zeusdeux / co-simple.js
Last active March 2, 2017 15:35
Simple co implementation that works with only promises. It Let's you write sync code using generators and promises.
function co(genInstance) {
const success = v => step(genInstance, {value: v, failed: false})
const failure = err => step(genInstance, {error: err, failed: true})
if ('function' === typeof genInstance) genInstance = genInstance()
// kick start generator
const retVal = genInstance.next()
if (retVal.done) return retVal.value
else return retVal.value.then(success, failure)
@zeusdeux
zeusdeux / .ctags
Created January 31, 2017 14:03 — forked from redguardtoo/.ctags.sample
my ~/.ctags (ctags setup), optimized for angular
--exclude=*.hg*
--exclude=*.cvs*
--exclude=*.svn*
--exclude=*.git*
--exclude=*compiled*
--exclude=*public_html*
--exclude=*.idea*
--exclude=*bower_components*
--exclude=*images*
--exclude=*.DS_Store*
@zeusdeux
zeusdeux / events.hs
Created October 2, 2016 23:30 — forked from aaronlevin/events.hs
LambdaWorld 2016: Type-Level DSLs
-- Our goal is to create a type describing a list of events. This is our
-- type-level DSL.
-- We will then use typeclass resolution to "interpret" this type-level DSL
-- into two things:
-- 1. A comma-separated list of events
-- 2. A method that, when given an event name and a payload, will try to parse
-- that event type with the payload. A form of dynamic dispatching
--
-- To model a list of types we will use tuples. You can imagine the list of
-- types "Int, String, Char" to look like:
@zeusdeux
zeusdeux / debounceVariations.js
Last active August 17, 2016 15:04
Debounce and throttle?
// If multiple calls to fn are made within time t
// this will call fn only once after a delay of
// t milliseconds after the most recent call
function debounce1(fn, t) {
let timeout
return function (...args) {
let self = this
clearTimeout(timeout)
@zeusdeux
zeusdeux / tmux-cheatsheet.markdown
Created March 14, 2016 15:14 — forked from MohamedAlaa/tmux-cheatsheet.markdown
tmux shortcuts & cheatsheet

tmux shortcuts & cheatsheet

start new:

tmux

start new with session name:

tmux new -s myname
@zeusdeux
zeusdeux / simple-vm.go
Created November 21, 2015 19:04 — forked from tomnomnom/simple-vm.go
Simple VM in Go
package main
import (
"fmt"
"log"
"strings"
)
// Ops
const (