Skip to content

Instantly share code, notes, and snippets.

View spiralx's full-sized avatar

James Skinner spiralx

View GitHub Profile
@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.
@spiralx
spiralx / fmt-browser-demo.html
Last active August 11, 2016 12:04
A small-ish module for doing string interpolation, variable substitution and simple transforms for when you don't want a full-blown templating system
<!DOCTYPE html>
<html>
<head>
<title>String format() function demo</title>
</head>
<body>
<h1>String format() function demo</h1>
<div>
<p>Module format not loaded</p>
@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 / bookmarklet-generator.js
Created March 11, 2014 16:10
Bookmarklets I've written or found useful
/*
* Prompts for the URL of a script, then creates a bookmarklet to load and run the script.
*
* javascript:(function(u) {u=prompt('Script URL');u&&prompt('Bookmarklet', 'javascript:(function(d,s){s=d.createElement("script");s.src="'+u.replace(/^https?:/,'')+'?cb="+Math.random();d.body.appendChild(s)})(document)') })()
*/
(function(u) {
u = prompt('Script URL');
u && prompt('Bookmarklet',
'javascript:(function(d,s){s=d.createElement("script");s.src="' +
u.replace(/^https?:/,'') +
@spiralx
spiralx / classof.js
Last active February 17, 2017 12:00
An accurate function for getting the type of a variable in JavaScript, using the fact that Object.prototype.toString() is guaranteed to return an object's internal [[Class]] property when called on it. There's also the isclass() helper function that takes an object and one or more class name strings, and returns True if the class of obj matches …
/**
* classof(obj)
*
* Returns the object's internal [[Class]] property, see
* the other file for example output.
*
* @param {Object?} object
* @return {String}
*/
const classof = v => Object.prototype.toString.call(v).replace(/^\[object\s(.*)\]$/, '$1').toLowerCase();
@spiralx
spiralx / regex-named-groups.js
Created April 1, 2014 01:16
Regular Expressions with Named Groups
/**
* Allows you to specify named groups in regular expressions like in Perl/Python/.NET/etc. You have to
* use a string to specify the regex, and the function returns an object that has `exec` and `replace`
* functions like a normal RegExp object, but matches they generate have keys for the values of the named
* groups.
*
* n = NamedRegExp("(?<protocol>http|ftp)://(?<host>[\\w-]+\\.com)(?<path>/.+\\.html)?$");
* res = n.exec("http://foobar.com"); -> res.protocol = http, res.host = foobar.com res.path = undefined
* res = n.exec("http://foobar.com/"); -> res.protocol = http, res.host = foobar.com res.path = /
@spiralx
spiralx / buttons.css
Last active August 29, 2015 13:58
A set of icons in data URI format and other utility classes
.buttonRow {
background-color: white;
padding: 0.8em;
border: solid 2px black;
z-index: 999999999;
position: fixed;
top: 2em;
left: 2em;
}
@spiralx
spiralx / dopus-utils.jscript
Created May 19, 2014 16:16
Collection and debug functions for Directory Opus 11 scripts
@script:jscript
/**
* Return an array of the results of running func on each item in coll e.g.
* map(DOpus.vars, function(v, i) {
* return v + '=' + v.value;
* }
*/
function map(coll, func) {
var res = [];