Skip to content

Instantly share code, notes, and snippets.

View gliese1337's full-sized avatar

Logan Kearsley gliese1337

View GitHub Profile
@gliese1337
gliese1337 / mozilla-readasarraybuffer.js
Created June 21, 2011 04:07 — forked from also/mozilla-readasarraybuffer.js
readAsArrayBuffer polyfill for Firefox 4 with caching
if (!FileReader.prototype.readAsArrayBuffer) {
FileReader.prototype.readAsArrayBuffer = function readAsArrayBuffer () {
this.readAsBinaryString.apply(this, arguments);
this.__defineGetter__('resultString', this.__lookupGetter__('result'));
Object.defineProperty(this, 'result', {
get : function () {
var string = this.resultString,
result = new Uint8Array(string.length);
for (var i = 0; i < string.length; i++) {
result[i] = string.charCodeAt(i);
@gliese1337
gliese1337 / DataView-polyfill.js
Created June 21, 2011 05:06
A simple implementation of the DataView ArrayBuffer wrapper.
var DataView = (function(){
var littleEndian = (
(new Uint16Array((new Uint8Array([0x12, 0x34])).buffer)[0] === 0x3412)
?true:false); //Determine native endianness
function dv(buffer){
this.buffer = buffer;
this.bytes = new Uint8Array(buffer);
}
@gliese1337
gliese1337 / Matrix.js
Created June 22, 2011 21:16
Map a 2D array onto an underlying contiguous 1D array.
Matrix = (function(){
function id(){return this;}
function _set(idx,src){this[idx]=src;}
function _get(idx){return this[idx];}
Array.prototype.subarray = function(start,end){
var j,row = {};
for(j=0;start<end;j++,start++){
Object.defineProperty(row,j,{
set:_set.bind(this,start),
@gliese1337
gliese1337 / MemoryStream.js
Created June 23, 2011 23:54
Wrapper to make a string or Buffer look like a Stream object.
function emit_string(obj,data,encoding){
obj.emit('data',(data instanceof Buffer
?data.toString(encoding)
:data[0])
);
}
function emit_buffer(obj,data){
obj.emit('data',(data instanceof Buffer
?data:new Buffer(data[0],data[1]))
@gliese1337
gliese1337 / StreamAccumulator.js
Created June 24, 2011 00:01
Accumulate the output of a stream into a string.
/*
* No sentinel results in reading the entire stream until it ends, a
* numeric sentinel reads fixed length chunks, and a string sentinel
* reads chunks bounded by the sentinel string
*/
exports.accumulate = function(stream,opts,callback){
stream.pause();
if(!callback){
callback = opts;
opts = {encoding:'utf8'};
@gliese1337
gliese1337 / WebVTTParser.js
Created August 16, 2011 20:16
Non-incremental parser for WebVTT files.
/*
http://www.whatwg.org/specs/web-apps/current-work/webvtt.html
*/
function parseWebVTT(input){
"use strict";
var line,l,p,cue_list=[],
cue,cue_text,id,fields,
time_pat = /\s*(\d*:?[0-5]\d:[0-5]\d\.\d\d\d)\s*-->\s*(\d*:?[0-5]\d:[0-5]\d\.\d\d\d)\s*(.*)/;
//If the first character is a BYTE ORDER MARK (BOM) character, advance position to the next character in input.
@gliese1337
gliese1337 / wavaudio.js
Created October 19, 2011 19:15
AudioData polyfill based on AudioLib
Audio = (function(){
return {
sampleRate:44100,
channelCount:1,
WriteAudio:function(soundData){
new Audio( 'data:audio/wav;base64,'+btoa(
audioLib.PCMData.encode({
data: soundData,
sampleRate: this.sampleRate,
channelCount: this.channelCount,
@gliese1337
gliese1337 / submsNow.js
Created August 26, 2012 16:07 — forked from jeromeetienne/submsNow.js
a submillisecond version of Date.now() based on based on window.performance.now()
/**
* precise version of Date.now() -
* It provide submillisecond precision based on window.performance.now() when
* available, fall back on Date.now()
* see http://updates.html5rocks.com/2012/05/requestAnimationFrame-API-now-with-sub-millisecond-precision
*/
var nowSubms = (function(){
var p = window.performance || {};
if( p.now ) return function(){ return p.timing.navigationStart + p.now(); };
else if( p.mozNow ) return function(){ return p.timing.navigationStart + p.mozNow(); };
@gliese1337
gliese1337 / FastFib.js
Created January 1, 2014 21:49
Fast Fibonacci Implementations
/* Looping version */
function fib(n){
var fk = 0, fkp = 1,
f2k, f2kp, b;
for(b = Math.floor(Math.log(n)/Math.LN2); b > 0; b--){
f2k = fk*(2*fkp-fk);
f2kp = fkp*fkp+fk*fk;
if(n&(1<<b)){
fk = f2kp;
@gliese1337
gliese1337 / earley.js
Last active November 20, 2015 00:55
Incremental, Back-Trackable Earley Recognizer for CFGs
var Earley = (function(){
function Grammar(rules) {
this.symbols = {};
rules.forEach(function(rule) {
// "A -> B C | D" -> ["A ", " B C | D"]
var parts = rule.split('->');
var lhs = parts[0].trim();
var rhss = parts[1].trim();
// "B C | D" -> ["B C", "D"]