Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@danharper
danharper / ngBridge.js
Last active August 29, 2015 14:16
ngBridge - wrap your Angular modules to support ES6 classes, and stricter assurances of DI
export function ngBridged(name, deps) {
return ngBridge(angular.module(name, deps))
}
export function ngBridge(ngModule) {
const register = (type, ...args) => ngBridge(ngModule[type](...args))
const injectable = type => (name, injectableFunc) => register(type, name, ngInject(injectableFunc))
const pass = type => (...args) => register(type, ...args)
export default class SayingHelloComponent {
static ngInject() {
return [] // register your dependencies here (Angular 1 style, with string names)
}
static ddo() {
return {
bind: {
person: '@'
},
name: 'sayingHello', // component's name
@danharper
danharper / ngDirective.js
Created March 6, 2015 11:44
ngDirective - note I haven't exactly dove deep into directives, so I'm sure there are other forms this doesn't account for (e.g. only having the link method?)
import ngInject from './ngInject'
export default function ngDirective(directive) {
let func = function(...injectedArgs) {
let link = (...directiveArgs) => new directive(...injectedArgs, ...directiveArgs)
return {...directive.ddo(), link}
}
func.$inject = ngInject(directive).$inject
@danharper
danharper / ngInject.js
Created March 5, 2015 21:58
ngInject - define injections as a static method in your class
// ngInject.js
function ngInject(func) {
if (func.ngInject)) {
func.$inject = func.ngInject()
}
return func
}
function sleep(ms = 0) {
return new Promise(r => setTimeout(r, ms))
}
async function run() {
await sleep(2000)
console.log('2s later')
}
run()
@danharper
danharper / gulpfile.js
Last active April 11, 2024 08:31
New ES6 project with Babel, Browserify & Gulp
var gulp = require('gulp');
var sourcemaps = require('gulp-sourcemaps');
var source = require('vinyl-source-stream');
var buffer = require('vinyl-buffer');
var browserify = require('browserify');
var watchify = require('watchify');
var babel = require('babelify');
function compile(watch) {
var bundler = watchify(browserify('./src/index.js', { debug: true }).transform(babel));
@danharper
danharper / CancellationTokenSource.js
Last active January 7, 2024 17:58
JavaScript "CancellationToken" for cancelling Async/Promise functions
const CANCEL = Symbol();
class CancellationToken {
constructor() {
this.cancelled = false;
}
throwIfCancelled() {
if (this.isCancelled()) {
@danharper
danharper / TimeZones.php
Last active July 5, 2016 22:10
Generates a list of all timezone identifiers, with their offset & made (slightly) more human-readable
<?php
class TimeZones {
/**
* @return array
*/
public function generate()
{
$identifiers = DateTimeZone::listIdentifiers();
@danharper
danharper / circle.yml
Created February 11, 2015 12:46
How we deploy InventoryBase from CircleCI
machine:
php:
version: 5.6.5
dependencies:
cache_directories:
- vendor
- node_modules
pre:
- sudo pip install awscli
@danharper
danharper / keyboard.coffee
Last active August 29, 2015 14:15
Small wrapper around Ionic's Keyboard plugin for Cordova; works in Browser (https://github.com/driftyco/ionic-plugins-keyboard)
define [], () ->
hasCordova = window.cordova?
class Keyboard
open: ->
if hasCordova
cordova.plugins.Keyboard.show()
else
console.info 'would open Keyboard'