Skip to content

Instantly share code, notes, and snippets.

View m3g4p0p's full-sized avatar
💭
waits until idle

m3g4p0p

💭
waits until idle
View GitHub Profile
@m3g4p0p
m3g4p0p / comments.md
Created October 8, 2017 13:15
Comments

Comments

Please leave your comments here.

@m3g4p0p
m3g4p0p / infinite-slider.js
Created September 5, 2017 05:34
An infinite swipe slider
const KEY = {
LEFT: 37,
RIGHT: 39
}
const cycle = (value, max) => (value + max) % max
const toTouchClientX = func => (event) => {
const { changedTouches: [first] } = event
@m3g4p0p
m3g4p0p / mockAnimationFrame.js
Last active August 3, 2017 12:33
Replace requestAnimationFrame related API methods with mock object for testing. Extended fork from https://gist.github.com/ischenkodv/43934774f4509fcb5791
/**
* Replace requestAnimationFrame related API methods with mock object for testing.
* Extended fork from https://gist.github.com/ischenkodv/43934774f4509fcb5791
*
* @type {Object}
*/
const mockAnimationFrame = {
original: {
requestAnimationFrame: window.requestAnimationFrame,
cancelAnimationFrame: window.cancelAnimationFrame,
@m3g4p0p
m3g4p0p / usenext.js
Last active May 5, 2017 07:04
Piping functions with multiple arguments
/**
* In JavaScript, we can only return one value from a function.
*
* This restricts piping to unary functions -- we couldn't pipe, say,
* both the request and the response object, or the error and the data
* argument from one function to the next without wrapping them in
* another object or an array. Yuck.
*
* So we can't return them both, but we could call another function
* with both! Ok, now we just wrapped them in another function instead
@m3g4p0p
m3g4p0p / private.js
Last active January 7, 2017 12:30
A simple publish/subscribe store using a WeakMap to keep the subscribers private
const createStore = (function () {
const scope = new WeakMap
const storeProto = {
subscribe (prop, fn) {
const ownScope = scope.get(this)
ownScope.subscribers[prop] = ownScope.subscribers[prop] || []
ownScope.subscribers[prop].push(fn)
@m3g4p0p
m3g4p0p / dom-helper.js
Last active June 23, 2020 05:05
Mini jQuery, sort of.
/**
* A collection of helper prototype for everyday DOM traversal, manipulation,
* and event binding. Sort of a minimalist jQuery, mainly for demonstration
* purposes. MIT @ m3g4p0p
*/
window.$ = (function (undefined) {
/**
* Duration constants
* @type {Object}
@m3g4p0p
m3g4p0p / make.js
Created December 24, 2016 10:12
A universal factory function
const make = function make ({
proto = {},
self = {},
init = function init (obj) {
Object.assign(this, obj)
return this
},
mixins = []
}) {
const mixProto = {}
@m3g4p0p
m3g4p0p / curry.js
Last active December 17, 2016 18:50
JS currying
const curry = (fn, thisArg) => function accumulate (...args) {
return args.length < fn.length
? accumulate.bind(null, ...args)
: fn.apply(thisArg, args)
}
const fn = (a, b, c) => a + b + c
curry(fn)(1)(2)(3) // 6
curry(fn)(1)(2, 3) // 6 etc...
@m3g4p0p
m3g4p0p / curry.js
Last active November 11, 2016 20:34
Simple curry function
// Placeholder
const _ = function _ () {}
// Curry a given function, optionally specifying
// a context on which it should finally get called
const curry = function curry (fn, thisArg) {
// Accumulate the call arguments for the curried function
return function accumulate () {
@m3g4p0p
m3g4p0p / runner.js
Created October 13, 2016 21:30
A most basic generator runner
function run (iterator) {
// Kick off the promise chain
Promise.resolve().then(
function resolved (value) {
// Get the next yielded iterator step,
// passing in the value from the last
// fulfillment
const next = iterator.next(value)