Skip to content

Instantly share code, notes, and snippets.

View flipjs's full-sized avatar
:octocat:
hjkl

Felipe Apostol flipjs

:octocat:
hjkl
View GitHub Profile
@flipjs
flipjs / iife.js
Last active August 29, 2015 14:04
IIFE + Augmentation
/*
Sample IIFE + Augmentation
Here I created a module counter,
and augmented it (added reset function).
*/
var counter = (function() {
var count = 0
return {
@flipjs
flipjs / metalsmith.js
Created July 28, 2014 22:58
Metalsmith config sample
var Metalsmith = require('metalsmith')
, markdown = require('metalsmith-markdown')
, templates = require('metalsmith-templates')
, Handlebars = require('handlebars')
, fs = require('fs')
, collections = require('metalsmith-collections')
, permalinks = require('metalsmith-permalinks')
Handlebars.registerPartial('header', fs.readFileSync(__dirname + '/templates/partials/header.hbt').toString())
Handlebars.registerPartial('footer', fs.readFileSync(__dirname + '/templates/partials/footer.hbt').toString())
@flipjs
flipjs / switch-object.js
Last active August 29, 2015 14:04
Switch statement alternative using object
var classifyShape = function(shape) {
var isTriangle = function() {
console.log('A triangle is a polygon with 3 sides.')
}
var isQuadrangle = function() {
console.log('A quadrangle is a polygon with 4 sides.')
}
@flipjs
flipjs / pojo.js
Created July 28, 2014 23:20
POJO
var pojo = function () {
var members = arguments
, ctr = 0
return function () {
var obj = {}
, i = 0
, len = members.length
ctr += 1
@flipjs
flipjs / observe.js
Created August 3, 2014 00:40
Object.observe()
var myObject = { name : 'flipjs' }
console.log('Start observing...')
Object.observe(myObject, function(changes) {
changes.forEach(function(change) {
console.log(change.name
+ " was "
+ change.type
+ " and is now "
@flipjs
flipjs / truthey-tester.js
Created August 3, 2014 20:53
Truthey Tester by Angus Croll
var trutheyTester = function(expr) {
return expr ? "truthey" : "falsey";
}
trutheyTester({}); //truthey (an object is always true)
trutheyTester(false); //falsey
trutheyTester(new Boolean(false)); //truthey (an object!)
trutheyTester(""); //falsey
@flipjs
flipjs / to-type.js
Last active August 29, 2015 14:04
toType() by Angus Croll
var toType = function(obj) {
return ({}).toString.call(obj).match(/\s([a-zA-Z]+)/)[1].toLowerCase()
}
toType({a: 4}); //"object"
toType([1, 2, 3]); //"array"
(function() {console.log(toType(arguments))})(); //arguments
toType(new ReferenceError); //"error"
toType(new Date); //"date"
toType(/a-z/); //"regexp"
@flipjs
flipjs / waldo.js
Created August 4, 2014 08:28
Waldo by Angus Croll
(function(){
var traverse = function(util, searchTerm, options) {
var options = options || {};
var obj = options.obj || window;
var path = options.path || ((obj==window) ? "window" : "");
var props = Object.keys(obj);
props.forEach(function(prop) {
if ((tests[util] || util)(searchTerm, obj, prop)){
console.log([path, ".", prop].join(""), "->",["(", typeof obj[prop], ")"].join(""), obj[prop]);
}
@flipjs
flipjs / node_mgd_conn.js
Created August 6, 2014 09:37
MongoDB and NodeJS
var mongo = require('mongodb')
var Server = mongo.Server
, Db = mongo.Db
, BSON = mongo.BSONPure
var server = new Server('localhost', 27017, {auto_reconnect: true})
, db = new Db('dbName', server)
db.open(function(err, db) {
@flipjs
flipjs / iterator_async.js
Created August 6, 2014 22:36
Async Iterator Snippet
var arr = [1, 2, 3, 4, 5, 6, 7]
, results = []
, len = arr.length
;(function iterator(i) {
if (i >= len) {
callback(null, results)
} else {
async_work(function(err, res) {
if (err) {