Skip to content

Instantly share code, notes, and snippets.

View spiralx's full-sized avatar

James Skinner spiralx

View GitHub Profile
@spiralx
spiralx / data-cache.user.js
Created June 15, 2015 03:30
Library to wrap storing JSON data using GM_getValue/GM_setValue
function DataCache(key) {
this._key = key;
this._data = {};
this.load();
}
DataCache.prototype = {
getValue: function(k) {
return this._data[k];
},
@spiralx
spiralx / jquery-sort-es6.js
Last active August 29, 2015 14:24
jQuery plugin to sort items by a property or function
($ => {
$.fn.sort = function(key) {
'use strict'
key = typeof key === 'function' ? key : el => el[key]
let $elem = $(this),
items = $elem.children().get()
@spiralx
spiralx / README.md
Created October 12, 2015 11:31 — forked from johan/README.md
JS debug breakpoint / log snippets

stopBefore.js

2min screencast

These tools inject a breakpoint, console.log or console.count in any function you want to spy on via stopBefore('Element.prototype.removeChild') or ditto stopAfter, logBefore / logAfter / logAround / logCount.

Works in Chrome DevTools and Safari Inspector; Firefox dev tools reportedly less so.

@spiralx
spiralx / examples.py
Last active October 14, 2015 00:48
Simple example of using doctests to test small functions within a module
#! /usr/bin/python3
def tupleise(*values):
"""
Return a tuple based on the arguments - if there are none then return an
empty tuple or if there are more than one create and return a new tuple from
the arguments. If there is one arg, if it is iterable but not a string then
create a tuple from its items, otherwise return tuple with a single-element
equal to that argument.
/*
* Easing Functions - inspired from http://gizma.com/easing/
* only considering the t value for the range [0, 1] => [0, 1]
*/
EasingFunctions = {
// no easing, no acceleration
linear: function (t) { return t },
// accelerating from zero velocity
easeInQuad: function (t) { return t*t },
// decelerating to zero velocity
@spiralx
spiralx / prism-custom.js
Created November 13, 2015 06:34
Highlight current page using the Prism highlighter
/*
http://prismjs.com/download.html?themes=prism-okaidia&languages=markup+css+clike+javascript+aspnet+bash+batch+c+bison+csharp+cpp+coffeescript+ruby+css-extras+diff+gherkin+git+go+groovy+handlebars+http+ini+jade+java+less+lua+markdown+perl+php+php-extras+powershell+python+jsx+rest+rust+sass+scss+scala+sql+stylus+typescript+wiki+yaml&plugins=line-highlight+line-numbers+show-invisibles+autolinker+wpd+highlight-keywords+remove-initial-line-feed+previewer-base+previewer-color+previewer-gradient
*/
var _self = (typeof window !== 'undefined')
? window // if in browser
: (
(typeof WorkerGlobalScope !== 'undefined' && self instanceof WorkerGlobalScope)
? self // if in worker
: {} // if in node js
@spiralx
spiralx / parse-uri-es6.js
Last active December 22, 2015 09:38
ES5 and 6 functions to parse URIs mostly following the RFC 1738 spec
/*jshint asi:true */
;(function(global) {
'use strict'
const parser = /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,
queryKeyParser = /(?:^|&)([^&=]*)=?([^&]*)/g,
keys = "href protocol authority userInfo user password host port relative path directory file query anchor".split(" ");
/**
@spiralx
spiralx / jQuery.batch.js
Created September 10, 2013 16:18
Creates jQuery instance methods for returning data from each item selected e.g. $el.attrs(), $.texts()
(function($) {
$.fn.batch = function(method, args) {
var func = $.fn[method], results = [];
this.each(function() {
results.push(func.apply(this, args));
});
return results;
};
var funcs = "attr css=styles prop html text val offset width height".split(" ");
@spiralx
spiralx / promise-example.js
Created November 28, 2013 16:37
Quick promise example
var Q = require("q");
var FS = require("fs");
function process_file(fn) {
var deferred = Q.defer();
FS.readFile(fn, "utf-8", function(error, content) {
if (error) {
deferred.reject(new Error(error));
}
@spiralx
spiralx / json-format.js
Created January 19, 2016 19:19
Bookmarklet for formatting a JSON page
javascript:(function(b) {
b.innerHTML = (b.childElementCount === 1 && b.children[0].tagName === 'PRE' ? b.children[0] : b).textContent.trim()
.replace(/^(?:([^(]+)\()?(.*?)(?:\);?)?$/, (a, f, j) => {
j = JSON.stringify(JSON.parse(j), null, 2);
return '<pre>' + (f ? `<b>${f}(</b>${j}<b>);</b>` : j) + '</pre>'
})
})(document.body)