Skip to content

Instantly share code, notes, and snippets.

View KylePDavis's full-sized avatar

Kyle P Davis KylePDavis

View GitHub Profile
@KylePDavis
KylePDavis / mocha_test.js
Last active October 12, 2015 19:18
Self-hosted Mocha Test Case Template
"use strict";
// Mocha one-liner to make these tests self-hosted
if (!module.parent) return require.cache[__filename] = 0, (new(require("mocha"))()).addFile(__filename).ui("exports").run(process.exit);
// requires
var assert = require("assert"),
MyClass = require("../../lib/MyClass");
@KylePDavis
KylePDavis / js_object_copy_slice.js
Created November 12, 2012 15:20
Experimenting with the idea of copying Object slices and creating composite Objects via slices
var util = require("util");
var o = {
p1: {
c1:[{l1:"a"}, {l2:"b"}],
c2:{l3:3, l4:4}
},
p2: {foo:"bar"}
};
process.stdout.write("// Original object:\n");
@KylePDavis
KylePDavis / js_class_template.js
Created October 16, 2012 19:42
JavaScript Classes Template (with proper inheritance)
//NOTE: if not in Node.js then you'll need to remove references to module.exports and require()
var MyClassName = (function(){
// CONSTRUCTOR
var klass = module.exports = MyClassName = function MyClassName(){
//[CONSTRUCTOR]; e.g., if(arguments.length > 0) throw new Error("Unexpected args!");
//[INSTANCE MEMBERS]; e.g., this.value = 123;
base.call(this);
}, base = require("./BaseClass"), proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
// PRIVATE STATIC MEMBERS
@KylePDavis
KylePDavis / sh_env_var_opts.sh
Last active September 14, 2023 01:26
Simple bash shell script templates. There are two versions: 1) simple env var based options, and 2) with added command line argument parsing and error handling.
#!/bin/bash -e
# A SHORT DESCRIPTION OF YOUR SCRIPT GOES HERE
# USAGE:
# DESCRIPTION OF ENV VARS HERE
###############################################################################
set -e # exit on command errors (so you MUST handle exit codes properly!)
set -o pipefail # capture fail exit codes in piped commands
#set -x # execution tracing debug messages
# Get command info
@KylePDavis
KylePDavis / FormatStringRegExpBuilderExample.js
Last active October 10, 2015 21:38
XRegExp tokenizer and parser that builds a new RegExp to validate strings against a given format string
// Get XRegExp in all it's goodness and install native stuff
var XRegExp = require("xregexp");
XRegExp.install("natives");
///////////////////////////////////////////////////////////////////////////////
/// LEXER
///////////////////////////////////////////////////////////////////////////////
// Create a formatToken XRegExp to match against the formatStr