Skip to content

Instantly share code, notes, and snippets.

@miguelmota
miguelmota / complete.js
Last active August 29, 2015 13:56
Reusable module for Node.js and browser
View complete.js
;(function(root) {
var fglobal = typeof global === 'object' && global;
if (fglobal.global === fglobal || fglobal.window === fglobal) {
root = fglobal;
}
if (typeof define === 'function' && typeof define.amd === 'object') {
define([], function() {
return mymodule;
});
@miguelmota
miguelmota / .jshintrc
Last active August 29, 2015 13:56
.jshintrc boilerplate I use
View .jshintrc
{
"node": true,
"browser": true,
"esnext": true,
"bitwise": true,
"camelcase": true,
"curly": true,
"eqeqeq": true,
"immed": true,
"indent": 4,
View redis.js
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()
View asyncSpec.js
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.
View dir.sh
#!/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
View within-circle.js
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
View distance.js
// @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
View loadavg.sh
# 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
View error.js
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
View readfiles.js
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);