Skip to content

Instantly share code, notes, and snippets.

View mlms13's full-sized avatar
⚗️

Michael Martin mlms13

⚗️
View GitHub Profile
@mlms13
mlms13 / node-ip.js
Created June 3, 2014 15:16
Find IP address in node
var getIpAddress = function (interfaces) {
'use strict';
var ip = [],
iface = null;
// loop through each interface in the interfaces object
for (iface in interfaces) {
// loop through each of the objects in the iface array
@mlms13
mlms13 / map-reduce-grouping.js
Created September 8, 2014 19:35
Map/Reduce to group blog posts by year
// Assume you retrieve an array of blog posts, with each item in the array looking like:
// {
// title: "",
// date: new Date(),
// ... other properties that are less interesting
// }
// And we want to sort this array by date, print a pretty date,
// and group posts from the same year under the same heading.
@mlms13
mlms13 / review-reduce.js
Last active August 29, 2015 14:06
Reduce an array of reviews so that they are grouped by publication
db.reviews.find({}, function (err, result) {
if (err) next(err);
// each item in the result array looks like this
// {
// source: 'The Washington Post',
// location: 'City, State',
// review: 'Several paragraphs of review text'
// }
@mlms13
mlms13 / module.js
Last active August 29, 2015 14:06
Unit tests for a function that calculates items that fit in a row, given row width, item width, and gutter between items.
module.exports = {
/**
* Given the width of a container, the width of an item, and the gutter between inner items,
* calculate the number of items that can fit on a single row inside the container.
* @param {number} rowWidth the width, in pixels, of the container
* @param {number} itemWidth the width of each individual item in the container
* @param {number} [gutter] the space, in pixels, between each item (optional)
* @return {number} the number of items that fit in a row without overflow, never less than 1
*/
calculateItemsPerRow: function (rowWidth, itemWidth, gutter) {
@mlms13
mlms13 / typecheck.js
Created September 26, 2014 19:23
Why type-checking (even in an environment like Phantom) is virtually impossible in Javascript
var foo = {
filter: function () {}
};
var bar = [];
// return filtered array or object
function filterer(baz) {
return baz.filter();
}
@mlms13
mlms13 / audit.js
Created February 2, 2015 18:22
Audit Browserify Dependencies
var mdeps = require('module-deps'),
_ = require('lodash'),
deps = [];
var md = mdeps({
transform: 'hbsfy',
global: true
});
md.on('end', function () {
@mlms13
mlms13 / anagram.js
Last active August 29, 2015 14:16
Solve Anagrams
// beautiful
function findAnagram(parent, child) {
// split the parent string into chars and iterate
return parent.split('').map(function (char, index, arr) {
// map each char in parent to an array of substrings with the same length as child.length
// and alphabetize the substrings for easy comparison later
return arr.slice(index, index + child.length).sort().join('');
}).filter(function (subset) {
// compare the alphabetized substring to the alphabetized child
return subset === child.split('').sort().join('');
@mlms13
mlms13 / main.js
Last active August 29, 2015 14:23
Promises and Events! and Pseudo-Code!
function tellMeWhenThatThingHappens() {
var promise = new Promise(function (resolve, reject) {
window.addEventListener('thatAwesomeThingHappens', function () {
resolve(/* maybe with some data here */);
});
// if it's possible for that awesome thing to fail...
window.addEventListener('refusingToBeAwesome', function () {
reject(/* maybe with an error */);
});
@mlms13
mlms13 / pro.js
Last active August 29, 2015 14:23
A promise library for professionals.
function Pro(callback) {
this.isResolved;
this.isRejected;
this.thens = [];
this.catches = [];
var resolve = function (data) {
if (this.isResolved || this.isRejected) return;
this.thens.forEach(function (fn) {
fn(data);
@mlms13
mlms13 / cycle.css
Created July 10, 2015 19:03
Jeet cycling
li {
background: #ddd;
height: 40px;
margin-top: 5px;
margin-bottom: 5px;
}
@media screen and (min-width: 640px) {
li {
*zoom: 1;
float: left;