Skip to content

Instantly share code, notes, and snippets.

@jcready
jcready / timestamp.log.js
Created April 24, 2014 17:34
Timestamps for console.log()
(function(){
var log = console.log;
console.log = function(){
var timestamp = '['+new Date().toJSON().slice(14,-1)+']';
if (arguments.length) {
if (typeof arguments[0] === "string") {
var args = Array.prototype.slice.call(arguments, 0);
args[0] = "%s " + arguments[0];
args.splice(1, 0, timestamp);
return log.apply(console, args);
@jcready
jcready / MediaElementSource.js
Last active December 27, 2015 02:39
Processing Web Audio ChannelData from MediaElementSource.
var audioContext, audioProcess, audioSource,
result = document.createElement('h3'),
output = document.createElement('span'),
mp3 = 'http://songs.jonathancoulton.com/mp3/First%20of%20May.mp3',
ogg = 'http://upload.wikimedia.org/wikipedia/en/4/45/ACDC_-_Back_In_Black-sample.ogg',
gotData = false, data, audio = new Audio();
function connect() {
audioContext = window.AudioContext ? new AudioContext() : new webkitAudioContext(),
audioSource = audioContext.createMediaElementSource( audio ),
@jcready
jcready / mediaMatch.js
Last active December 20, 2015 09:09
Winamp Toolbar's "Media Match" functionality duplicated in plain JavaScript.
(function(){
var links = document.getElementsByTagName('a'),
audio = document.getElementsByTagName('audio'),
media = [],
match = /\.(m3u|pls|smil|aac|aif|alac|flac|ogg|mpa|mp3|mp4|m4a|wav|wma)$/i;
for (var i = 0; i < links.length; i++)
if (match.test(links[i].pathname))
media.push(links[i].href)
for (var i = 0; i < audio.length; i++)
if (match.test(audio[i].src))
navigator.geolocation.watchPosition(function(g){
if (g.coords.latitude === null && g.coords.longitude === null) return;
var c = g.coords, l = { acc: c.accuracy };
if (c.latitude !== null) l.lat = ~~(c.latitude*10000)/10000;
if (c.longitude !== null)l.long = ~~(c.longitude*10000)/10000;
if (c.speed !== null) l.speed = c.speed;
if (c.heading !== null && !isNaN(c.heading)) l.dir = c.heading;
if (c.altitude) { l.alt = c.altitude; l.altacc = c.altitudeAccuracy }
console.log(l);
});
@jcready
jcready / Log-.md
Created May 30, 2013 01:09 — forked from bgrins/Log-.md

Javascript log Function

Every time I start a new project, I want to pull in a log function that allows the same functionality as the console.log, including the full functionality of the Console API.

There are a lot of ways to do this, but many are lacking. A common problem with wrapper functions is that the line number that shows up next to the log is the line number of the log function itself, not where log was invoked. There are also times where the arguments get logged in a way that isn't quite the same as the native function.

This is an attempt to once and for all document the function that I pull in to new projects. There are two different options:

  • The full version: Inspired by the plugin in HTML5 Boilerplate. Use this if you are writing an application and want to create a window.log function. Additionally,
function fnFormatNumber( number, decimals, dec_point, thousands_sep ) {
var n = number, c = isNaN(decimals = Math.abs(decimals)) ? 2 : decimals;
var d = dec_point == undefined ? "," : dec_point;
var t = thousands_sep == undefined ? "." : thousands_sep, s = n < 0 ? "-" : "";
var i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "", j = (j = i.length) > 3 ? j % 3 : 0;
var p = c ? Math.abs(n - i).toFixed(c).slice(2) : 0;
return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + ((Math.floor(p) > 0) ? d + p : "");
}
@jcready
jcready / typeof.js
Last active April 12, 2016 16:09
Better typeof() Function
var typeOf = (function(_, g, u) {
return function(o) {
return o === g
? 'global'
: o == u
? o === u
? 'undefined'
: 'null'
: o !== o
? 'NaN'
@jcready
jcready / globals.js
Last active December 13, 2015 20:39
Detect Global Variables
(function(){
var differences = {},
ignoreList = (prompt('Ignore filter (comma sep)?', 'jQuery, Backbone, _, $').split(/,\s?/) || []),
iframe = document.createElement('iframe'),
count = 0; ignoreList.push('prop');
for (prop in window) {
differences[prop] = {
type: typeof window[prop],
val: window[prop]
function parsePls(pls) {
//get number of entries
var matched = pls.match(/numberofentries=\d+/i);
matched = matched[0].split("=");
var numberOfEntries = matched[1];
var i = 1;
var files = [], file, title, length;
while (i <= numberOfEntries) {
var fileRegex = new RegExp('File'+i+'=\\S+');
@jcready
jcready / _.map.js
Last active December 10, 2015 08:38
function map(collection, callback, thisArg) {
var index = -1,
length = collection ? collection.length : 0,
result = isArray(collection) ? Array(length) : {};
callback = createCallback(callback, thisArg);
if (isArray(result)) {
while (++index < length) {
result[index] = callback(collection[index], index, collection);
}