Skip to content

Instantly share code, notes, and snippets.

// usage:
// var diff = new customdiff();
// data1 = data1.split('\n');
// data2 = data2.split('\n');
// var diffs = diff._diff(data1, data2);
var customdiff = function() {
var format = function(str) {
var args = Array.prototype.slice.call(arguments, 1);
var replaceFunc = function(matchedStr) {
var index = parseInt(matchedStr.substring(1, matchedStr.length-1), 10);
return args[index] != null ? args[index] : '';
};
return str.replace(/\{[0-9]+\}/g, replaceFunc);
};
format("Hello {0}", "world");
var namespace = function(str) {
var parts = str.split('.');
var nsLevel = window;
for (var i = 0, n = parts.length; i < n; i++) {
nsLevel = nsLevel[parts[i]] = nsLevel[parts[i]] || {};
}
};
namespace('myNamespace.subNamespace');
var hasOwnProperty = Object.prototype.hasOwnProperty;
var equivalent = function(actual, expected) {
if (actual === expected) {
return true;
}
else if (expected instanceof Date) {
if (actual instanceof Date && actual.getTime() == expected.getTime()) {
return true;
}
var i = 0;
var func = function() { i++; };
func();
print(i); // 0 or 1? (answer: 1)
// This file is SUPER RAW. Debugging and test code abound!
// This is a pure JS PNG encoder which skips zlib compression. Good enough for small dynamic data URIs though
var testData = [[25, 0, 151, 255, 25, 0, 151, 255, 25, 0, 151, 255], [25, 0, 151, 255, 25, 0, 151, 255, 25, 0, 151, 255], [25, 0, 151, 255, 25, 0, 151, 255, 25, 0, 151, 128]];
var preFilteredPngImage = [[0x19, 0, 0x97, 0xff, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x81]];
var preFilteredPngData = [1, 0x19, 0, 0x97, 0xff, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x81];
var encodePng = function(data) {
@jbrantly
jbrantly / gist:7a374d27f329a651fbec
Created January 19, 2015 20:58
Prototype of typescript webpack loader
var typescript = require('typescript')
var path = require('path')
var compilerOptions = {
target: typescript.ScriptTarget.ES5
}
var files = {};
var servicesHost = {
getScriptFileNames: function () { return Object.keys(files) },
@jbrantly
jbrantly / react-0.13.0.d.ts
Last active August 29, 2015 14:14
Idea for separating react and react/addons while sharing interfaces
// Type definitions for React 0.13.0
// Project: http://facebook.github.io/react/
// Definitions by: Asana <https://asana.com>, AssureSign <http://www.assuresign.com>
// Definitions: https://github.com/borisyankov/DefinitelyTyped
declare module React {
//
// React Elements
// ----------------------------------------------------------------------
@jbrantly
jbrantly / cjs-es6-modules.js
Created March 22, 2016 15:34
The problem with the CJS-ES6 gap
// ES6 exports
export var foo = 1
export var bar = true
// CJS analog
module.exports = {
foo: 1,
bar: true
(function() {
var pre = document.createElement('pre');
document.body.appendChild(pre);
console = {
log: function() {
pre.innerHTML += Array.prototype.slice.call(arguments).map(function(s) { if (s === undefined) return 'undefined'; return s }).join(" ") + "\n";
}
}
})()