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 / 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 / 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 / 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 / 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 / phishing_test.md
Created September 9, 2016 07:20
phishing test @Company - implementation details

Inspired by this -> https://insight.duo.com/

Simple usage

  1. get a list of e-mail addresses
  2. send e-mails with personal URL from hashed e-mail address
  3. log each requests (ts, url, user) on server

result

  • e-mail addresses of personel who clicked the link
  • % personel who clicked
@karlpokus
karlpokus / encoder.md
Created September 13, 2016 14:22
encode urls in js. Safe characters and what-not.

encode URLs

src

TL;DR

  • encodeURI() will not encode ~!@#$&*()=:/,;?+' Use on entire string
  • encodeURIComponent() will not encode: ~!*()' Use on tricky values. Will ruin = and &amp;
@karlpokus
karlpokus / piping.md
Last active September 27, 2016 08:37
Piping is fun! Maybe control flow is a more suitable title but who cares?

Piping is fun!

unix

$ ls | grep test

ExpressJS

app.get('/', fn1, fn2, fn3); // or pass an array
@karlpokus
karlpokus / mute.md
Created September 27, 2016 16:05
console.mute @uppsalaJS 2016-09-27

console.mute

usage

  • readme

how does it work?

  • node api - console
  • console.js

some tests

@karlpokus
karlpokus / url.md
Last active October 13, 2016 12:59
URL parts for dummies