Skip to content

Instantly share code, notes, and snippets.

@miguelmota
miguelmota / app.js
Last active August 29, 2015 14:02
CORS in express.js
// @credit http://stackoverflow.com/questions/7067966/how-to-allow-cors-in-express-nodejs
function allowCrossDomain(req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization');
if ('OPTIONS' == req.method) {
res.send(200);
}
@miguelmota
miguelmota / inheritance.js
Last active August 29, 2015 14:02
Class inheritance in JavaScript
function F() {
}
F.prototype.beep = function() {
console.log('beep');
};
function G() {
@miguelmota
miguelmota / wtf.js
Created June 24, 2014 19:09
The force is strong with this one.
(function f(Infinity, length, __proto__) {
return [,,~0.[0|0]][f.__proto__.length && Infinity, -~String(this).length >> __proto__] << (0. === .0) + Infinity;
}).apply(typeof f, [,,2]);
@miguelmota
miguelmota / directive.js
Last active August 29, 2015 14:03
Angular.js directive template, snippet taken from the AngularJS book by Brad Green.
var myModule = angular.module(...);
myModule.directive('namespaceDirectiveName', function factory(injectables) {
var directiveDefinitionObject = {
// Declare how directive can be used in a template as an element, attribute, class, comment, or any combination.
restrict: string,
// Set the order of execution in the template relative to other directives on the element.
priority: number,
@miguelmota
miguelmota / .editorconfig
Last active August 29, 2015 14:03
EditorConfig sample file http://editorconfig.org/
# EditorConfig is awesome: http://EditorConfig.org
# top-most EditorConfig file
root = true
[*]
# use line feed
end_of_line = lf
# ensure file ends with a newline when saving
@miguelmota
miguelmota / async-reader.js
Created July 6, 2014 04:09
Async parallel JSON file reader using Promises.
// @credit: http://2013.jsconf.eu/speakers/forbes-lindesay-promises-and-generators-control-flow-utopia.html
var fs = require('fs');
var path = require('path');
var Promise = require('es6-promise').Promise;
function async(makeGenerator) {
return function() {
var generator = makeGenerator.apply(this, arguments);
@miguelmota
miguelmota / controller.js
Last active August 29, 2015 14:03
Google Analytics in Angular.js
angular.module('App')
.controller('MyController', function ($scope, $location, $window, $rootScope) {
$rootScope.$on('$routeChangeSuccess', function(e) {
$window.ga('send', 'pageview', { page: $location.path() });
});
$scope.gaEvent = function() {
$window.ga('send', {
'hitType': 'event',
@miguelmota
miguelmota / Brocfile.js
Last active August 29, 2015 14:04
Broccoli compass on Ember CLI app
var pickFiles = require('broccoli-static-compiler');
var mergeTrees = require('broccoli-merge-trees');
var compileCompass = require('broccoli-compass');
var EmberApp = require('ember-cli/lib/broccoli/ember-app');
var app = new EmberApp();
app.styles = function() {
var vendorTree = this._processedVendorTree();
@miguelmota
miguelmota / .bashrc
Created July 23, 2014 22:51 — forked from henrik/.bashrc
# http://henrik.nyh.se/2008/12/git-dirty-prompt
# http://www.simplisticcomplexity.com/2008/03/13/show-your-git-branch-name-in-your-prompt/
# username@Machine ~/dev/dir[master]$ # clean working directory
# username@Machine ~/dev/dir[master*]$ # dirty working directory
function parse_git_dirty {
[[ $(git status 2> /dev/null | tail -n1) != "nothing to commit (working directory clean)" ]] && echo "*"
}
function parse_git_branch {
git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e "s/* \(.*\)/[\1$(parse_git_dirty)]/"
@miguelmota
miguelmota / .bashrc
Created July 23, 2014 22:52
Bash prompt with git branch status
function parse_git_dirty {
[[ $(git status 2> /dev/null | tail -n1) != "nothing to commit (working directory clean)" ]] && echo "*"
}
function parse_git_branch {
git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e "s/* \(.*\)/[\1$(parse_git_dirty)]/"
}
export PS1='\n\n\[\033[0;36m\]\u@$(hostname)\[\033[00m\]\[\033[0;32m\] : \w\[\033[00m\] \[\033[00;36m\]$(parse_git_branch)\n\[\033[00;32m\]\$\[\033[00m\] '