Skip to content

Instantly share code, notes, and snippets.

@jpiccari
jpiccari / successiveFilter.js
Created March 12, 2015 06:40
Find the smallest, non-empty, array given a set of successively applied filters.
/**
* Succesively filter an array until the minimum result set is found
* @param {array} array - Array to be filtered
* @param {function...} fn1..fnN - Any number of filter functions
* @returns {array} The smallest non-empty array given the set of filters
*/
function successiveFilter(array /* ... */) {
var prevArray,
fnArray = Array.prototype.slice.call(arguments, 1);
@jpiccari
jpiccari / EntityToUnicode.js
Last active August 29, 2015 14:07
RequireJS module to convert strings containing HTML entities to unicode text.
define(
'EntityToUnicode',
function() {
var el = document.createElement('p');
/**
* HTML Entities to unicode text
* @param {string} str - String which contains HTML entities to decode
* @returns {string} A string of the equivalent unicode text
*/
@jpiccari
jpiccari / dateFormat.js
Last active August 29, 2015 14:06
RequireJS module that provides the basic date formatting functionality that is shockingly missing from the JavaScript spec. (~1k minified, ~500 bytes gzipped)
define(
'dateFormat',
function() {
var DAYS = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Firday', 'Saturday'],
MONTHS = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
MONTHS_ABBREV = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'June', 'July', 'Aug', 'Sept', 'Oct', 'Nov', 'Dec'],
datePrototype = Date.prototype,
formatCharacters = {
// DAY
// Numeric representation (with leading zero)
@jpiccari
jpiccari / Eventable.js
Last active August 29, 2015 14:05
Reincarnation of EventEmitter, stripped down for speed and size. Still supports filters. <1k minified
define(
'Eventable',
function() {
function extend(dest, src) {
for (var key in src) {
if (src.hasOwnProperty(key)) {
dest[key] = src[key];
}
}
@jpiccari
jpiccari / Heap.js
Created August 18, 2014 04:48
AMD module for binary heaps. Supports heapify, merge, insert, extract, remove (given an offset), decrease/increase key, and custom sorting functions.
define(
'Heap',
function() {
function Heap(array, fn) {
var instance = Object.create(Heap.prototype);
instance._sortFn = typeof fn === 'function' ? fn : Heap.defaultSort;
instance._store = Array.isArray(array) ? array : [];
Heap.heapify(instance._store, instance._sortFn);
@jpiccari
jpiccari / console.js
Created July 29, 2014 04:53
Simple console module that allows mimics the Console API but which can be toggled to help keep your console clean when not debugging.
define(
'console',
function() {
var QUEUED_API = [
'count',
'dir',
'error',
'group',
'groupCollapsed',
'groupEnd',
@jpiccari
jpiccari / select.js
Last active August 29, 2015 14:03
Quick demo of how a JavaScript based selector engine might work.
function select(selector) {
var elements = [document],
queryStart = 0,
callQueue = [],
temp,
i;
for (i = 0; i < selector.length; i++) {
switch (selector[i]) {
case '#':
/**
* Creates a string using indexed format string
* @param {string} Format of string
* @param {...string} One or more strings to be using in the format
*/
function formatString(/* ... */) {
var args = Array.apply(0, arguments);
return args.shift().replace(/\{(\d+)\}/g, function(match, index) {
return index < args.length
? args[index]
@jpiccari
jpiccari / gist:027e83ca507a06515e41
Last active August 29, 2015 14:02
Created this function to help determine how much to contribute (annually) to my 401(k) to retire a decamillionaire.
/**
* Calculate the annual contributions required to reach a desired future value.
* @param {number} P - Current principle value.
* @param {number} FV - Desired future value.
* @param {number} r - Rate of return.
* @param {number} Y - Years of growth.
* @returns {number} Annual contribution.
*/
function annualContributions(P, FV, r, Y) {
var z = 1 + r;
@jpiccari
jpiccari / constructorFactory.js
Last active August 29, 2015 14:02
Factory for creating constructors that don't require new keywords with minimal boilerplate.
/**
* Factory to take some of the boilerplate out of creating constructors
* which instantiate objects even without the new keyword.
* @param {function} fn - Input function to create constructor from.
* @returns The updated constructor.
*/
function constructorFactory(fn) {
if (typeof fn !== 'function') {
throw new TypeError('Expected function!');
}