Skip to content

Instantly share code, notes, and snippets.

View davideast's full-sized avatar
💜
Working on Project IDX (and Firebase too)

David East davideast

💜
Working on Project IDX (and Firebase too)
View GitHub Profile
@davideast
davideast / MyViewController.swift
Last active December 27, 2017 05:19
Using Firebase references in a UIViewController
import UIKit
class MyViewController: UIViewController {
// Store ref and handle as implicitly unwrapped optionals
var ref: Firebase!
var handle: UInt!
override func viewDidLoad() {
super.viewDidLoad()
@davideast
davideast / selectText.js
Created October 19, 2017 17:55
Select Text
function selectText(selection) {
var range = selection.getRangeAt(0);
function replaceRange(s, start, end, substitute) {
return s.substring(0, start) + substitute + s.substring(end);
}
var text = selection.baseNode.textContent.substring(range.startOffset, range.endOffset);
var mark = `<mark>${text}</mark>`;
range.startContainer.parentElement.innerHTML = replaceRange(range.startContainer.textContent, range.startOffset, range.endOffset, mark);
}
@davideast
davideast / index.ts
Last active August 10, 2017 17:18
Angular Universal Server
import { angularUniversal } from 'angular-universal-express';
import * as express from 'express';
const app = express();
// serving static requests
app.use(express.static(__dirname + '/static'));
// serving dynamic requests
app.get('/*', angularUniversal({
index: __dirname + '/index.html',
main: __dirname + '/main.<your-hash>.bundle',
@davideast
davideast / build.sh
Last active August 10, 2017 17:18
Angular Universal Build
# Build the browser bundle
ng build --prod
# Build the universal bundle
ng build --prod --app 1
# Move the browser index.html to generate the SSR version
mv dist/index.html dist-server/
# Treat dist as static within dist-server
mv dist/ dist-server/static
@davideast
davideast / build.sh
Last active August 10, 2017 17:17
Angular Universal + TS server build
# Build the browser bundle
ng build --prod
# Build the universal bundle
ng build --prod --app 1
# Move the browser index.html to generate the SSR version
mv dist/index.html dist-server/
# Treat dist as static within dist-server
mv dist/ dist-server/static
# Transpile TS server code
node_modules/.bin/tsc -p server/tsconfig.json
@davideast
davideast / tsconfig.json
Created August 10, 2017 16:34
Angular Universal Server Build
{
"compilerOptions": {
"outDir": "../dist-server",
"sourceMap": true,
"moduleResolution": "node",
"target": "es5",
"lib": [ "es2017" ]
}
}
@davideast
davideast / server.ts
Last active August 10, 2017 16:25
Angular Universal Express Sample
import { angularUniversal } from 'angular-universal-express';
import * as express from 'express';
const app = express();
/*
I usually copy my Angular CLI "dist" build into my "dist-server" build
and serve them as static files so they aren't treated as dynamic routes.
*/
app.use(express.static(__dirname + '/dist'));
app.get('/*', angularUniversal({
@davideast
davideast / naked-import.js
Created March 21, 2017 17:49
Firebase ES2015 imports
import * as firebase from 'firebase/app'; // Provides firebase-app.js only
import 'firebase/auth'; // Not named, just naked
const app = firebase.initializeApp({ });
console.log(app.auth()); // auth object
console.log(app.database()); // undefined
@davideast
davideast / incrementViews.js
Last active January 7, 2017 22:03
Guillermo Views Transaction
/**
* Increases view count by one.
* @param: db {FirebaseDatabase} - Firebase Database instance
* @param: id {String} - id of post
* @return: Promise<{committed: boolean, snapshot: nullable firebase.database.DataSnapshot}>
* @docs: https://firebase.google.com/docs/reference/js/firebase.database.Reference#transaction
*/
const incrementViews = (db, id) => {
const ref = db.ref('views').child(id)
return ref.transaction(currentViews => {
@davideast
davideast / rules.bolt
Last active December 15, 2015 18:33
Securing user data - Bolt rules
isUser(uid) = auth != null && auth.uid == uid;
type Post {
uid: String;
title: String;
description: String;
timestamp: Number;
}
type User {