Skip to content

Instantly share code, notes, and snippets.

function sleep(ms = 0) {
return new Promise(r => setTimeout(r, ms))
}
async function run() {
await sleep(2000)
console.log('2s later')
}
run()
@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
}
@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
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 / 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)
async function sleep(ms) {
return new Promise(r => setTimeout(r, ms))
}
async function foo() { console.log('s foo'); await sleep(2000); console.log('d foo'); return 'foo'; }
async function bar() { console.log('s bar'); await sleep(1000); console.log('d bar'); return 'bar'; }
async function baz() { console.log('s baz'); await sleep(2000); console.log('d baz'); return 'baz'; }
async function run() {
console.log('start')
@danharper
danharper / decorators.js
Created April 9, 2015 22:15
ES7 Decorators
@ngInject('bus', '$ionicModal')
class Adder {
constructor(num) {
this.num = num
}
// order matters
// if you @swallow first, then @log won't log caught exceptions
@log
@swallow
@danharper
danharper / query-params.php
Last active August 29, 2015 14:20
query params. experiment in something a little different
<?php
namespace param {
class QueryParam {
const SINGLE = 'single';
const MANY = 'many';
const MANY_CSV = 'csv';
public $queryField;
private $nullable = false;
jQuery("ul.popover.level0").menuAim({
activate: function(row) { jQuery(row).addClass('active') },
deactivate: function(row) { jQuery(row).removeClass('active') },
rowSelector: 'li.level1.parent',
exitMenu: function() { return true; }
});
@danharper
danharper / validators.md
Last active August 29, 2015 14:26
Validation Library

To define your validation rules, provide a Map. Keys are field names, values are either a validation rule, or an array of validation rules.

// one rule
const rules = new Map([
  ['name', required]
])

// multiple rules
const rules = new Map([