Skip to content

Instantly share code, notes, and snippets.

@katowulf
katowulf / diffList.js
Last active July 28, 2016 18:42
Diff services for monitoring objects and arrays, and creating changelists in Angular.js
/**
* A diff utility that compares arrays and returns a list of added, removed, and updated items
*
* Returns an object with two methods:
* diff: do a one-time diff of two arrays
* watch: observe a variable on scope and report any changes to a callback
*
* Invoking the factory is done like so:
* <code>
* function(listDiff) {
@katowulf
katowulf / jwtparser.js
Last active September 19, 2017 13:11
Deconstruct a JWT token for Firebase
// Helper function to extract claims from a JWT. Does *not* verify the
// validity of the token.
// credits: https://github.com/firebase/angularFire/blob/master/angularFire.js#L370
// polyfill window.atob() for IE8: https://github.com/davidchambers/Base64.js
// or really fast Base64 by Fred Palmer: https://code.google.com/p/javascriptbase64/
function deconstructJWT(token) {
var segments = token.split(".");
if (!segments instanceof Array || segments.length !== 3) {
throw new Error("Invalid JWT");
}
@katowulf
katowulf / 1_query_timestamp.js
Last active September 21, 2023 20:28
Get only new items from Firebase
// assumes you add a timestamp field to each record (see Firebase.ServerValue.TIMESTAMP)
// pros: fast and done server-side (less bandwidth, faster response), simple
// cons: a few bytes on each record for the timestamp
var ref = new Firebase(...);
ref.orderByChild('timestamp').startAt(Date.now()).on('child_added', function(snapshot) {
console.log('new record', snap.key());
});
@katowulf
katowulf / 1_using_queries.js
Last active April 24, 2023 07:14
Methods to search for user accounts by email address in Firebase
/***************************************************
* Simple and elegant, no code complexity
* Disadvantages: Requires warming all data into server memory (could take a long time for MBs of data or millions of records)
* (This disadvantage should go away as we add optimizations to the core product)
***************************************************/
var fb = firebase.database.ref();
/**
* @param {string} emailAddress
@katowulf
katowulf / extend.js
Created September 17, 2013 18:06
A simple extend function for JavaScript
function extend(base) {
var parts = Array.prototype.slice.call(arguments, 1);
parts.forEach(function (p) {
if (p && typeof (p) === 'object') {
for (var k in p) {
if (p.hasOwnProperty(k)) {
base[k] = p[k];
}
}
}
@katowulf
katowulf / no_dup_emails.js
Last active May 17, 2020 10:43
Enforce no duplicate emails in Firebase
/**
* My Firebase data structure:
*
* /email_index/$email/user_id
* /user/$user_id/email
*/
var fb = new Firebase(URL);
function isDuplicateEmail(email, callback) {
@katowulf
katowulf / push_to_urbarairship.js
Last active February 16, 2016 13:58
Node.js process to queue from Firebase to Urban Airship.
// UNTESTED; THEORETICAL, based on http://docs.urbanairship.com/connect/ios_push.html
https://github.com/mikeal/request
var request = require('request');
/** Presumably the snapshot will contain something like this:
*
{
"audience": {"device_token": "FE66489F304DC75B8D6E8200DFF8A456E8DAEACEC428B427E9518741C92C6660"},
"notification": {
@katowulf
katowulf / module.simpleLoginTools.js
Last active March 3, 2016 04:26
A service and several directives based on ng-cloak, which wait for angularFire to finish authenticating--rather than just until Angular bootstraps--before displaying it's content. Handy to get rid of those blips that display when user isn't logged in--oh, wait, now they are.
'use strict';
/**
* This module monitors angularFire's authentication and performs actions based on authentication state.
* directives/directive.ngcloakauth.js depends on this file
*
* Modify ng-cloak to hide content until FirebaseSimpleLogin resolves. Also
* provides ng-show-auth methods for displaying content only when certain login
* states are active.
*
@katowulf
katowulf / I Dream in Code.js
Last active January 26, 2023 17:01
I Dream in Code...
/*********************
** I Dream in Code **
*********************/
while( I .sleep() ) {
I.dream() in code;
I.dream() in algorithms;
I.dream() in subroutines;
Rewind.age(5) && I.dream.of(["missing pants", "driving hotwheels", "peeing in fountains"]) ); //hint: it’s my bed
@katowulf
katowulf / FirebaseMonitor.js
Last active January 6, 2024 04:19
Listen for Firebase events and survive authentication logouts and restarts.
/**
* This little helper is called instead of on() to monitor data and handle auth expiration
* It assumes that you monitor authentication state and re-auth if
* your auth token expires. It also doesn't help with any transactions, set ops, or
* other writes which could be in progress when authentication is lost--those will
* explode in glorious flashes of failure
*
* To support IE8, you will want these polyfills:
* forEach: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach#Polyfill
* indexOf: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf#Polyfill