Skip to content

Instantly share code, notes, and snippets.

@awerlang
awerlang / angular-directives
Last active March 6, 2017 14:27
Angular.js directives sorted by priority
/*
I've compiled a list of angular directives according to their priorities (from most priority to lesser priority).
Also, terminal property is included for each directive that asserts it
*/
ng-switch 1200
ng-repeat 1000 terminal
ng-if 600 terminal
ng-controller 500
ng-init 450
@awerlang
awerlang / gist:007e11fd415a95c6bb44
Last active August 29, 2015 14:11
Apply some delay on Http Requests
// enabled when $rootScope.configHttpDelay == true
(function () {
"use strict";
angular.module("app")
.config(function ($provide) {
function decorator($delegate) {
var proxy = function (method, url, data, callback, headers) {
var interceptor = function () {
@awerlang
awerlang / lexiSort.js
Created January 5, 2015 16:42
natural compare function
// benchmark: http://jsperf.com/natural-compare
"use strict";
function lexiSort(config) {
return function (a, b) {
if (typeof a === "number" && typeof b === "number")
return a - b;
if (a === null || b === null)
return NaN;
if (a === undefined || b === undefined)
return NaN;
@awerlang
awerlang / gist:ef16cb32ff9605ab8c0d
Created February 1, 2015 21:06
99 bottles of beer, ES6 version
function* bottlesOfBeer(howMany=99){
let pluralizeBottles = count => count !== 1 ? "bottles" : "bottle";
let takeNext = next => next >= 1 ? next.toString() : "No more";
for (let i = howMany; i >= 0; i--) {
let bottle = pluralizeBottles(i);
let bottleNext = pluralizeBottles(i-1);
let left = takeNext(i);
let toTake = i > 1 ? "one" : (i == 1 ? "it" : "no more");
let toTakeNext = takeNext(i-1);
let line1 = `${left} ${bottle} of beer on the wall, ${left.toLowerCase()} ${bottle} of beer.`;
@awerlang
awerlang / README.md
Last active August 29, 2015 14:27 — forked from hsablonniere/README.md
scrollIntoViewIfNeeded 4 everyone!!!

scrollIntoViewIfNeeded 4 everyone!!!

This gist provides a simple JavaScript implementation of the non-standard WebKit method scrollIntoViewIfNeeded that can be called on DOM elements.

Usage

Just use the code in index.js in your app or website. You can see usage in the test page test.html.

The parent element will only scroll if the element being called is out of the view. The boolean can force the element to be centered in the scrolling area.

@awerlang
awerlang / ngrxintro.md
Created April 19, 2016 17:50 — forked from btroncone/ngrxintro.md
A Comprehensive Introduction to @ngrx/store - Companion to Egghead.io Series
@awerlang
awerlang / webpack.config.js
Created September 5, 2016 14:20
webpack dev config for Angular 2 / Typescript
var webpack = require('webpack');
var path = require('path');
// Webpack Config
var webpackConfig = {
entry: {
'polyfills': './src/polyfills.browser.ts',
'vendor': './src/vendor.browser.ts',
'main': './src/main.browser.ts',
@awerlang
awerlang / toJSON.js
Created November 16, 2016 15:21
Convert to JSON, replacing cyclic dependencies
function toJSON(object, fieldsToOmit) {
const map = new Map();
let index = 0;
return JSON.stringify(object, (key, value) => {
if (~fieldsToOmit.indexOf(key)) return undefined;
return value != null && typeof value === 'object'
? map.get(value) || (map.set(value, '$' + ++index), value)
: value;
}, 2);
}
@awerlang
awerlang / CustomError.ts
Created November 16, 2016 15:25
Sample implementation of a custom error
class CustomError extends Error {
get name(): string { return 'CustomError'; };
public stack: any;
constructor(public code: number, public message: string) {
super(message);
if (typeof (<any>Error).captureStackTrace === 'function') {
(<any>Error).captureStackTrace(this, this.constructor);
} else {
/**
Mixes in objects in `properties` into a class.
To be used as a decorator.
@mixin({...})
class MyClass {...}
*/
function mixin(properties: {[key: string]: any}): Function {
if (properties == null) {
throw new Error(`Argument for 'properties' is required.`);