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 / 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 / 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 / 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 / array_access.php
Created September 5, 2016 20:50
Access a specific element of a nested array
<?php
/**
* Return the reference to an element/subarray
* of an array, specified by a key array
*
* @param array &$array Array to access by reference
* @param mixed $keys Array of the keys
* @return mixed Reference to the match
*/
function &array_access(&$array, $keys) {
@m3g4p0p
m3g4p0p / dom-helpers.js
Last active August 26, 2016 15:51
Some ideas for simply DOM helper functions
/**
* Get an array (!) of elements in a given context
*/
const find = (selector, context) =>
Array.from((context || document).querySelectorAll(selector));
/**
* Create a new element and optionally initialize it
* with some content and attributes
*/
@m3g4p0p
m3g4p0p / state_archivist.php
Last active August 19, 2016 07:37
Store and restore the states of objects
<?php
/**
* Optional convenience class. Objects extending the
* StateHandler class can register with a StateArchivist
* instance; then calling remember() or restore() on the
* archivist will be propagated to all registered objects.
*/
class StateArchivist {
/**
@m3g4p0p
m3g4p0p / logdash.js
Last active July 22, 2016 17:23
Allows logging messages to the console.
window._ = function() {console.log(...arguments)};
(function($) {
$.fn.watch = function(callback, once) {
var options = {
childList: true,
subtree: true
};
var _callOnNode = function() {
$.fn.observe = function(callback, options) {
options = options || {
attributes: true,
childList: true,
characterData: true
};
$(this).each(function() {
var observer = new MutationObserver(callback.bind(this));
observer.observe(this, options);
@m3g4p0p
m3g4p0p / jquery-viewport.js
Last active July 21, 2016 22:59
jQuery plugin to reduce the set of elements to those being within the viewport
/**
* Plugin to reduce a set of elements to those
* visible in the viewport
*
* @param {Object}
* @param {Function}
* @param {Function}
* @return {jQuery}
*/
(function($) {