Skip to content

Instantly share code, notes, and snippets.

View andresmatasuarez's full-sized avatar

Andrés Mata Suárez andresmatasuarez

  • Buenos Aires, Argentina
View GitHub Profile
@andresmatasuarez
andresmatasuarez / default_value.coffee
Last active August 29, 2015 14:15
AngularJS | Directive | Makes an input fall back to a default value in case of being empty.
'use strict'
app = angular.module 'app'
app.directive 'defaultValue', ->
require : 'ngModel'
restrict : 'A'
link : ($scope, element, attrs, ngModel) ->
ngModel.$parsers.push (value) ->
if _.isEmpty value
@andresmatasuarez
andresmatasuarez / limit_length.coffee
Last active August 29, 2015 14:15
AngularJS | Directive | Limits an input length by trimming exceeding characters
'use strict'
app = angular.module 'app'
app.directive 'limitLength', ->
require : 'ngModel'
restrict : 'A'
link : ($scope, element, attrs, ngModel) ->
ngModel.$parsers.push (value) ->
if !_.isEmpty(value) and value.length > attrs.limitLength
@andresmatasuarez
andresmatasuarez / auth_utils.js
Last active August 29, 2015 14:11
Passport Express utils. ensureAuthenticated and redirectToRememberedURL
/** @module AuthUtils */
'use strict';
var _ = require('lodash');
var response = require('./response');
module.exports = {
ensureAuthenticated: function(loginUrl){
return function(req, res, next){
@andresmatasuarez
andresmatasuarez / prevent_default.coffee
Last active August 29, 2015 14:11
AngularJS | Directive | Binds event.stopPropagation and/or event.preventDefault to click. All browsers supported.
'use strict'
app = angular.module 'app'
app.directive 'preventDefault', ($state) ->
restrict : 'A'
link : ($scope, element, attrs) ->
element.bind 'click', (event) ->
if event.preventDefault
event.preventDefault()
@andresmatasuarez
andresmatasuarez / three_dots.coffee
Created December 3, 2014 21:06
AngularJS | Filter | Replace exceeding characters in a string with '...'
'use strict'
app = angular.module 'app'
app.filter 'threeDots', ($filter) ->
(value, maxLength) ->
if value.length > maxLength
$filter('limitTo')(value, maxLength).trim() + '...'
else
value
@andresmatasuarez
andresmatasuarez / file_reader.coffee
Created December 2, 2014 20:39
AngularJS | Service | FileReader and ImageReader services (HTML5 File API)
'use strict'
app = angular.module 'app'
app.factory 'FileReader', ($q) ->
readDataAsUrl: (file, scope) ->
deferred = $q.defer()
reader = new FileReader()
@andresmatasuarez
andresmatasuarez / route_utils.js
Last active August 29, 2015 14:10
Middleware Utils: Mongo ObjectID validation, Mongo fetch and populate request by ID, Mongo ValidationError cleaner, CastError mapper, SSL enforcer
/** @module RouteUtils */
'use strict';
var _ = require('lodash');
var mongoose = require('mongoose');
var url = require('url');
var lodashDeep = require('lodash-deep');
var Response = require('./response');
@andresmatasuarez
andresmatasuarez / example.js
Last active February 29, 2016 02:08
Node Bluebird Exponential Backoff
// Example usage
// The example will fetch current weather data from Buenos Aires,
// failing on purpose the first 2 times to demonstrate the Backoff strategy.
// Ensure you have Bluebird and superagent installed.
//
// Save the following code in example.js and run it with 'node example.js'
const Bluebird = require('bluebird');
const superagent = require('superagent');
const promiseUtils = require('./promise_utils');