Skip to content

Instantly share code, notes, and snippets.

@mikelehen
mikelehen / generate-pushid.js
Created February 11, 2015 17:34
JavaScript code for generating Firebase Push IDs
/**
* Fancy ID generator that creates 20-character string identifiers with the following properties:
*
* 1. They're based on timestamp so that they sort *after* any existing ids.
* 2. They contain 72-bits of random data after the timestamp so that IDs won't collide with other clients' IDs.
* 3. They sort *lexicographically* (so the timestamp is converted to characters that will sort properly).
* 4. They're monotonically increasing. Even if you generate more than one in the same timestamp, the
* latter ones will sort after the former ones. We do this by using the previous random bits
* but "incrementing" them by 1 (only in the case of a timestamp collision).
*/
@mikelehen
mikelehen / timestamped-console-log.js
Created April 16, 2013 19:06
Wraps console.log to include a timestamp (in seconds since the wrapper was initialized) with every log message.
console.log = (function() {
var console_log = console.log;
var timeStart = new Date().getTime();
return function() {
var delta = new Date().getTime() - timeStart;
var args = [];
args.push((delta / 1000).toFixed(2) + ':');
for(var i = 0; i < arguments.length; i++) {
args.push(arguments[i]);
POST https://firestore.googleapis.com/v1beta1/projects/firestore-fun/databases/(default)/documents:commit?key={YOUR_API_KEY}
{
"writes": [
{
"update": {
"name": "projects/firestore-fun/databases/(default)/documents/foo/bar",
"fields": {
"some-field": {
"stringValue": "some-value"
@mikelehen
mikelehen / presence-example.js
Created January 10, 2014 20:51
Per-session presence bits.
var presenceRef = new Firebase('https://mike.firebaseio.com/users/michael/presence');
presenceRef.root().child('.info/connected').on('value', function(s) {
if (s.val() === true) {
// we're connected. Set up presence with a per-session presence bit.
var ref = presenceRef.push();
ref.onDisconnect().remove();
ref.set(true);
}
});
@mikelehen
mikelehen / .vimrc
Created July 29, 2013 00:04
My vimrc
set autoindent ignorecase noerrorbells ruler hlsearch sw=4 sts=4 expandtab
set textwidth=79 nowrapscan formatoptions-=t incsearch tabstop=4
set scrolloff=2 norestorescreen history=100 shortmess=ao nobackup
set matchtime=1 showmatch smartcase wildmenu wildmode=longest:full
syntax on
map Q gq
map Y y$
behave xterm
@mikelehen
mikelehen / prune-firepad-history.js
Created April 10, 2013 19:37
Prune history of a Firepad.
// This should be easier, but due to an oversight in the checkpoint id's, it's a tad tricky (you need this revision/id stuff).
// I'll rework things to be cleaner in the near future.
function pruneHistory(firepadRef) {
var characters = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
function revisionToId(revision) {
if (revision === 0) {
return 'A0';
}
codeMirror.setOption('onDragEvent', function(cm, e) {
// Move the cursor as they drag.
var pos = codeMirror.coordsChar({left: e.x, top: e.y });
codeMirror.setCursor(pos);
codeMirror.focus();
var isImageDrop = e.type == 'drop' && e.dataTransfer.files && e.dataTransfer.files.length > 0 && e.dataTransfer.files[0].type && e.dataTransfer.files[0].type.indexOf('image/') > -1;
if (!isImageDrop) return;
event.preventDefault();
@mikelehen
mikelehen / firepad-node.js
Created December 16, 2013 22:43
Curtesy of Clément (https://github.com/iclems), a node script that instantiates firepad in order to extract the text contents.
var jsdom = require('jsdom');
var fs = require('fs');
var Firepad = {};
Firepad.load = function(ref, callback) {
jsdom.env('<head></head><body><div id="firepad"></div></body>', function (errors, window) {
var document = document || window.document;
@mikelehen
mikelehen / firepad-insert-image.js
Created October 23, 2013 19:05
Inserting an image into firepad.
firepad.insertEntity('img', {
'src' : 'http://cdn.dashburst.com/wp-content/uploads/2013/01/Grumpy-Cat.jpg',
});
@mikelehen
mikelehen / auto-expand-firepad.css
Created September 28, 2013 23:15
Styles to make firepad auto-expand to fit its content.
.firepad {
height: auto;
}
.firepad-toolbar {
position: relative;
margin-top: 20px;
margin-left: 10px;
top: auto;
left: auto;