Skip to content

Instantly share code, notes, and snippets.

View aurbano's full-sized avatar
🎯
ML

Alejandro U. Alvarez aurbano

🎯
ML
View GitHub Profile
@aurbano
aurbano / ageFromTimestamp.php
Created September 15, 2014 15:29
Calculate age in PHP from timestamp
<?php
function getAge($birth){
$t = time();
$age = ($birth < 0) ? ( $t ($birth * -1) ) : $t - $birth;
return floor($age/31536000);
}
@aurbano
aurbano / expressProxy.js
Last active September 21, 2021 17:00
Nodejs + Express : Simple proxy
/**
* Simple image proxy for Nodejs
* @param {Express} app Express app
* @return {function}
*/
var http = require('http'),
url = require('url');
module.exports = function (app) {
app.get('/proxy/:image', function (request_from_client, response_to_client) {
@aurbano
aurbano / timeBetween.php
Last active May 13, 2024 01:48
Display the time between two dates in a human readable format (i.e. 3 seconds, 4 days, 1 week... )
<?php
/**
* Calculate the elapsed time between two timestamps
* and display in a pretty human way.
* @param integer $start Initial timestamp
* @param integer $end Final timestamp, set to -1 for current time.
* @return string Formatted elapsed time.
*/
function timeBetween($start, $end=-1){
if($end < 0) $end = time();
@aurbano
aurbano / .zshrc
Created November 8, 2014 21:16
My .zshrc file
# start_time="$(date +%s)"
# Antigen — A zsh plugin manager
export ANTIGEN_DEFAULT_REPO_URL=https://github.com/sharat87/oh-my-zsh.git
source ~/antigen.zsh
# Load the oh-my-zsh's library.
antigen use oh-my-zsh
# Bundles from the default repo declared above.
@aurbano
aurbano / addHash.js
Created April 18, 2015 11:05
Add the hash to the url
// Check if url includes #/
if(window.location.hash.length < 1 || window.location.hash == ''){
window.location = window.location.origin + window.location.pathname + '#/' + window.location.search;
}
@aurbano
aurbano / httpResource.js
Created April 18, 2015 11:07
AngularJS returning data from an endpoint as a resource
angular.module('serviceName', [])
.factory('resourceName', ['$resource',
function($resource) {
var status = $resource('/endpoint');
return status.get().$promise;
}
]);
@aurbano
aurbano / angularDraggable.js
Created April 20, 2015 12:23
Angular Draggable directive, with support for dragging another element, and container
angular.module('aurbano.draggable', [])
.directive('draggable', ['$document', function($document) {
return {
scope: {
config: '=draggable'
},
link: function(scope, element, attr) {
var dragElement = element;
if(scope.config.target){
if(scope.config.target === 'parent'){
@aurbano
aurbano / htmlEncode.js
Created May 1, 2015 11:49
HTML Encode content easily, requires jQuery
function htmlEncode(value){
// Create a in-memory div, set it's inner text(which jQuery automatically encodes)
// then grab the encoded contents back out. The div never exists on the page.
return $('<div/>').text(value).html();
}
@aurbano
aurbano / removeKeys.js
Last active November 29, 2022 21:57
Remove a property from a nested object, recursively
/**
* Remove all specified keys from an object, no matter how deep they are.
* The removal is done in place, so run it on a copy if you don't want to modify the original object.
* This function has no limit so circular objects will probably crash the browser
*
* @param obj The object from where you want to remove the keys
* @param keys An array of property names (strings) to remove
*/
function removeKeys(obj, keys){
var index;
@aurbano
aurbano / normalizeArrayId.js
Created July 2, 2015 14:53
Given an array of objects that have an Id, return an array where the Id is the key
/**
* Given an array of objects that have an Id, return an array where the Id is the key
* @param arr Array to normalize
* @param key [Optional] Object key to use, defaults to Id
* @returns {Array} Normalized array
*/
function normalizeArrayId(arr, key){
var total = arr.length,
index = key || 'Id',
ret = [];