Skip to content

Instantly share code, notes, and snippets.

@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 '#':
@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 / 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 / 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 / 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 / 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 / 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);