Skip to content

Instantly share code, notes, and snippets.

View karlpokus's full-sized avatar
💭
wohoo!

carl-fredrik grimberg karlpokus

💭
wohoo!
View GitHub Profile
@karlpokus
karlpokus / arrayFunctions.js
Last active December 13, 2016 14:14
JS filter, map and reduce - homemade
// MAPPY
Array.prototype.mappy = function(fn, that) {
var out = [],
x,
that = that || {};
for (var i = 0; i < this.length; i++) {
x = fn.call(that, this[i], i, this);
out.push(x);
}
@karlpokus
karlpokus / middlewareTest.js
Last active July 15, 2016 18:04
How to test a single middeware with tape without a server running - in node or in browser
// This only works if you call - return next - in your middleware. Otherwise the cb (i.e next) won't return anything and the var will be undefined
// And you don't even need node - You can run it in a browser - Totally runtime agnostic
// Reference -> https://github.com/karlpokus/konstapel/blob/master/test/tests.js
var test = require('tape'),
m = require('module');
test('.usernameIsValid', function(t){
// mock req, res and next
var pass = m.usernameIsValid({user: {}}, null, function(){ return true }),
fail = m.usernameIsValid({}, null, function(err){ return err });
@karlpokus
karlpokus / timingExecution.js
Last active July 6, 2016 08:52
Timing execution time in server or browser
// start
console.time('foo');
// do stuff
// end
console.timeEnd('foo');
// log output will be in ms -> foo: 268ms
@karlpokus
karlpokus / mute.js
Last active July 6, 2016 08:40
Mute and capture console.log in the browser
// completely mute
console.log = function() {};
// captured data in log
var log = [];
console.log = function() {
log.push([].slice.call(arguments));
};
// fancy namespaced version. Demo -> http://codepen.io/KarlPokus/pen/rLwXQX/
@karlpokus
karlpokus / iife.js
Created July 7, 2016 06:09
IIFE - modular pattern in JS
// from http://appendto.com/2010/10/how-good-c-habits-can-encourage-bad-javascript-habits-part-1/
(function(foo, $, undefined){ // parameters
// private var in closure
var data = [1, 2, 3];
// public fn
foo.moo = function() {
console.log(data);
}
return foo;
@karlpokus
karlpokus / fongo.js
Created July 7, 2016 08:44
fongo in js - stub mongo for testing [WIP]
// Like this -> https://github.com/fakemongo/fongo
// Based on this wrapper API from the shell
db[collectionName].query({selectors}, {projections}, cb(err, data));
// Pass dummy data to constructor
var db = new Fongo({
collections: [],
users: [ // should match to array above
{} // simple objects
@karlpokus
karlpokus / filterByRegex.js
Created August 14, 2016 08:56
Array filter by regex and some weird arguments applied
var matches = ["sun", "clouds", "rain", "hail", "snow"].filter(/./.test, /i/);
@karlpokus
karlpokus / curl.sh
Last active December 14, 2016 13:43
bash curl API
#!/bin/bash
# simple GET
curl <url>
# POST w json
curl -X POST -H "Content-Type: application/json" -d '{"data":"POST with json"}' <url>
# POST w urlencoded
curl -X POST -H "Content-Type: application/x-www-form-urlencoded" -d 'foo=1&boo=2' <url>
@karlpokus
karlpokus / bindAll.js
Created August 29, 2016 11:54
Pass 1+ objects to bind all functions to it - creating a useful <this>
// Problem: Callbacks are called with the window object as <this>
// Solution: Bind all functions to their object
// An implementation of http://underscorejs.org/#bindAll
// Demo -> http://codepen.io/KarlPokus/pen/Lkwqyj
function bindAll(x) {
var wat = Object.prototype.toString;
if (wat.call(x) === '[object Object]') {
x = [x];
}
if (wat.call(x) === '[object Array]') {
@karlpokus
karlpokus / npm_link.md
Last active July 13, 2022 15:32
npm link for dummies