Skip to content

Instantly share code, notes, and snippets.

@jpiccari
jpiccari / RequirejsInfo.js
Last active April 26, 2024 23:11
Chrome Snippets
(function() {
var moduleList = []
module,
key;
if (typeof requirejs !== 'undefined') {
console.groupCollapsed('%cRequire.js v%s module list', 'color: #0080ff; font: 1.4em Menlo, monospace', requirejs.version);
for (key in requirejs.s.contexts._.defined) {
module = requirejs.s.contexts._.defined[key];
moduleList.push({
@jpiccari
jpiccari / observer.v2.js
Last active August 29, 2015 14:01
An improved observer that tracks changes to existing object properties while still allowing native object access. Should be supported in IE8+, Firefox, Chrome, Safari, etc. (235 bytes minified, gzipped)
define(
'observer',
[
'EventEmitter'
],
function(EventEmitter) {
'use strict';
var OBSERVER_KEY = '_isObserver',
isStandardDefineProperties = true;
@jpiccari
jpiccari / Observer.js
Last active August 29, 2015 14:01
AMD Observer module, depends on EventEmitter (see my other gists). (235 bytes minified, gzipped)
define(
'Observer',
[
'EventEmitter'
],
function(EventEmitter) {
'use strict';
function copyValue(value) {
if (typeof value === 'object') {
@jpiccari
jpiccari / EventEmitter.js
Last active August 29, 2015 14:01
AMD EventEmitter module that supports event filters (a.k.a namespaced events). Extends object passed to constructor. There is also a global emitter accessible via EventEmitter.global. (565 bytes minified, gzipped)
define(
'EventEmitter',
function() {
'use strict';
/**
* Parse event types into topic and filters.
*/
function parseEvent(event) {
event = event.split('.');
@jpiccari
jpiccari / gist:8622410
Last active January 4, 2016 12:39
Simple function to dynamically create an array of a given length where the values are equal to the indices. (407 bytes; 87 bytes minified)
/**
* Creates an array in which each value stored in the array represets the value's index (zero-based).
* @param {number} len - Length of array to produce.
* @returns {array} The resulting array.
*/
function arrayOfNumbers(len) {
/*
* You could replace Number with eval and save 2 additional bytes,
* however that is less secure and probalby not worth the 2 bytes.
*
@jpiccari
jpiccari / bwrite.sh
Last active January 3, 2016 02:29
I found myself updating a few bytes within files often. This script quickly allows the update of 1 or more bytes so I don't have to open a hex editor for such a simple task.
#!/bin/sh
if [ "$#" -ne 3 ] || ! [ -f "$1" ]; then
# usage
echo "$(basename $0) [file] [offset] [bytes]"
exit 1
fi
bytes=$(printf "$3" | xxd -p)
printf $bytes | xxd -r -p | dd of="$1" seek=$(( $2 )) count=$(( ${#bytes} / 2 )) bs=1 conv=notrunc &> /dev/null
@jpiccari
jpiccari / metadata.js
Last active December 20, 2015 22:58
Weekend puzzle. Recursively generates all possible vowel combinations in the word metadata. The main function, dfs(), is basically a DFS but instead of exploring unexplored adjacent nodes we explore all nodes in 'set' while watching our current tree depth to figure out when we have enough vowels to generate a new result. (310 bytes; 190 bytes mi…
var result = [];
(function dfs(set, len, stack) {
if(len === 0)
return result.push(Array.prototype.map.call('metadata', function(x, i) {
return i%2 ? stack[i>>1] : x;
}).join(''));
for(var i = 0; i < set.length; dfs(set, len-1, (stack || []).concat(set[i++])));
}(['a', 'e', 'i', 'o', 'u', 'y'], 4))