Skip to content

Instantly share code, notes, and snippets.

View jmakeig's full-sized avatar

Justin Makeig jmakeig

View GitHub Profile
@jmakeig
jmakeig / pr.md
Last active August 29, 2015 14:09 — forked from piscisaureus/pr.md

Locate the section for your github remote in the .git/config file. It looks like this:

[remote "origin"]
	fetch = +refs/heads/*:refs/remotes/origin/*
	url = git@github.com:joyent/node.git

Now add the line fetch = +refs/pull/*/head:refs/remotes/origin/pr/* to this section. Obviously, change the github url to match your project's URL. It ends up looking like this:

@jmakeig
jmakeig / args-types.js
Created November 18, 2014 21:33
Type assertions on arguments
// WORK IN PROGRESS
function assertArgsTypes(args , typeAssertions) {
var oc = Object.prototype.toString.call;
var op = Object.getPrototypeOf;
if(args.length !== typeAssertions.length) return false;
for(var i = 0; i < args.length; i++) {
if(typeof typeAssertions[i] == "string") {
if(typeof args[i] != typeAssertions[i]) {
//throw (typeof args[i]) + " " + typeAssertions[i];
@jmakeig
jmakeig / delegates.js
Last active August 29, 2015 14:11
Function and generator delegates for ES6
function Builder() { this.initialState = 'initialState'; };
Builder.prototype.setState = chain(function(k, v) { this[k] = v; }, Builder);
Builder.prototype.generate = delegate(generate, Builder);
// (this instanceof C) ? this : new C();
function selfOrNew(C, obj) {
if('undefined' === typeof obj) return new C();
if(obj instanceof C) return obj;
return new C();
}
@jmakeig
jmakeig / app.sjs
Last active August 29, 2015 14:11
Proof of concept of Node.js-style global console object for MarkLogic
var util = require('./util.sjs');
var console = util.console;
console.error('%d is a number', 55);
console.info('%s is a %s', 'asdf', typeof 'asdf');
console.log(util.inspect(util, { showHidden: true, depth: null }));
@jmakeig
jmakeig / iterator-prototype.js
Last active August 29, 2015 14:11
Extending the built-in ES6 iterator prototype with Array/Observable-like conveniences. (This is probably a bad idea.)
// I'm not sure why this works.
var proto = Object.getPrototypeOf(
Object.getPrototypeOf(
[][Symbol.iterator]() // Creates an Array and gets its iterator property
)
);
proto.toArray = function() { // Add a new function to the iterator prototype
var out = [];
for(var v of this) { // iterate on the iterator instance and accumlate the results in an array
out.push(v);
@jmakeig
jmakeig / gist:9094074ab62436fe44c0
Created January 16, 2015 00:44
Get the script directory without having to change $PWD.
# $0 is the path of the script. ${0%/* } takes $0, deletes the smallest substring matching /* from the right (the filename) and returns the rest, which would be the directory of the script. So this changes the directory to the directory the script is located in.
# "the ${0%/*} pulls the path out of $0, cd's into the specified directory, then uses $PWD to figure out where that directory lives - and all this in a subshell, so we don't affect $PWD"
$SCRIPT_DIR = $(cd "${0%/*}")
@jmakeig
jmakeig / proxy-array-test.js
Last active August 29, 2015 14:13
Proxy array to wrap Array.prototype functions that can alter data, similar to AOP-style advice. This is much cleaner with ES6 proxies. DOES NOT support proxying updates to individual items, just the actual function properties, such as slice or push.
var me = new MyType(consoleHandler);
console.log(Array.isArray(me.messages)); // log => true
me.messages.push(1); // => 'push'
me.messages.push(2); // => 'push'
me.messages.slice(1); // => 'slice'
me.messages = ['a', 'b', 'c']; // => 'set'
me.messages.slice(0); // => 'slice'
Array.prototype.splice.call(me.messages, 0, 2); // ['a, b']
// Back-door (or feature?):
// Does NOT log.
@jmakeig
jmakeig / shadow-facets.js
Last active August 29, 2015 14:15
"Shadow facets" implementation with the MarkLogic Node.js Client API that runs at a consistent timestamp using a multi-statement transaction.
var Promise = require('bluebird');
var marklogic = require('marklogic');
var conn = require('../marklogic-config.js').connection;
var db = marklogic.createDatabaseClient(conn);
var qb = marklogic.queryBuilder;
var queryString = '';
var constraints = {
sender: ['taskgated'],
@jmakeig
jmakeig / Document.prototype.js
Last active August 29, 2015 14:22
Monkey patched update method on MarkLogic Document.prototype
Document.prototype.update = function(objectNodeOrMutatorFunction) {
if('undefined' === typeof objectNodeOrMutatorFunction) {
throw new ReferenceError('Can\'t update to an undefined');
}
if('function' === typeof objectNodeOrMutatorFunction) {
var node = this;
// If it's JSON (or a Document containing JSON) convert it to an object.
// Otherwise, send along the raw Node.
if('json' === documentKind(this)) {
node = this.toObject();
@jmakeig
jmakeig / iterable.js
Last active August 29, 2015 14:23
Pattern for making a custom prototype iterable with ES6.
function MyCollection() {}
MyCollection.prototype[Symbol.iterator] = function *() {
yield 1;
yield 2;
yield 3;
};