Skip to content

Instantly share code, notes, and snippets.

@miguelmota
miguelmota / .jshintrc
Last active August 29, 2015 13:56
.jshintrc boilerplate I use
{
"node": true,
"browser": true,
"esnext": true,
"bitwise": true,
"camelcase": true,
"curly": true,
"eqeqeq": true,
"immed": true,
"indent": 4,
module.exports = {
DB1: redis.createClient(),
DB2: redis.createClient(),
DB3: redis.createClient(),
init: function(next) {
var select = redis.RedisClient.prototype.select;
require('async').parallel([
select.bind(this.DB1, 1),
select.bind(this.DB2, 2),
select.bind(this.DB3, 3)
@miguelmota
miguelmota / asyncSpec.js
Last active August 29, 2015 13:57
Jasmine async beforeEach()
define('Jasmine' , function () {
beforeEach(function(){
runs(function () {
getRemoteData(function(res){
this.data = res;
}.bind(this));
});
waitsFor(function() {
@miguelmota
miguelmota / dir.sh
Last active August 29, 2015 13:58
Current directory of script in bash.
#!/bin/bash
DIR="$( cd "$( dirname "$0" )" && pwd )"
echo $DIR
@miguelmota
miguelmota / within-circle.js
Last active August 29, 2015 13:58
Check if coordinate is within circle
function withinCircle(x, y, centerX, centerY, radius) {
return Math.pow((x - centerX), 2) + Math.pow((y - centerY), 2) < Math.pow(radius, 2);
}
@miguelmota
miguelmota / distance.js
Last active August 29, 2015 13:58
Get distance from latitude and longitude in kilometers
// @credit http://stackoverflow.com/questions/27928/how-do-i-calculate-distance-between-two-latitude-longitude-points
function getDistanceFromLatLonInKm(lat1,lon1,lat2,lon2) {
var R = 6371; // Radius of the earth in km
var dLat = deg2rad(lat2-lat1);
var dLon = deg2rad(lon2-lon1);
var a =
Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) *
Math.sin(dLon/2) * Math.sin(dLon/2)
@miguelmota
miguelmota / loadavg.sh
Last active August 29, 2015 13:58
Get load average on Mac OSX
# Method one - w
w | head -n1 | cut -d":" -f4
# Method two - uptime
uptime | cut -d":" -f4- | sed s/,//g
# Method three - loads.d
sudo loads.d | awk '/./ { printf "%.2f %.2f %.2f\n", $7, $8, $9 }'
@miguelmota
miguelmota / error.js
Created May 11, 2014 17:31
Custom error type in JavaScript
function MyError(message) {
this.name = 'MyError';
this.message = message;
this.stack = (new Error()).stack;
}
MyError.prototype = new Error();
MyError.prototype.constructor = MyError;
try {
@miguelmota
miguelmota / readfiles.js
Last active August 29, 2015 14:01
Node.js read directory files and write to JSON file
var fs = require('fs');
fs.readdir([__dirname,'/app/images'].join(''), function(err, files) {
function filter(file) {
return /(\.png|\.jpg|\.jpeg)/.test(file);
}
var filtered = JSON.stringify(files.filter(filter));
fs.writeFile([__dirname,'/app/data/images.json'].join(''), filtered, function(err) {
if (err) throw new Error(err);
console.log(filtered);
@miguelmota
miguelmota / test_controller.js
Created June 18, 2014 03:38
Angular.js $httpBackend example
(function(module) {
module.controller('TestController', ['$scope', '$http',
function($scope, $http) {
$http.get('/api/stuffs')
.then(function(data) {
$scope.stuffs = data;
});
}]);