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 / AOP.js
Created September 19, 2016 08:47
(function(global) {
global.AOP = function(subject) {
return {
after(method, advice) {
const original = subject[method];
subject[method] = function() {
const result = original.apply(this, arguments);
@m3g4p0p
m3g4p0p / sortable.js
Last active September 20, 2016 20:24
Generate a sortable table from a data array
const sortable = function(data) {
const table = document.createElement('table');
const thead = document.createElement('thead');
const tbody = document.createElement('tbody');
const currentOrder = {};
// Function to populate the tbody with the data
const _populate = current => {
const row = tbody.insertRow();
@m3g4p0p
m3g4p0p / ez-state.js
Last active September 29, 2016 22:22
A state container that allows calling actions directly on the container instance
;(function init (global) {
'use strict'
// Constructor function, initialise states and actions
global.Container = function constructor (initialState) {
this._states = initialState === undefined ? [] : [initialState]
this._actions = {}
}
// Get the current state
@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)
@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 / 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 / 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 / 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 / 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 / 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