Skip to content

Instantly share code, notes, and snippets.

View moleike's full-sized avatar
🌟
dare to think for yourself

Alexandre Moreno moleike

🌟
dare to think for yourself
  • Barcelona
  • 07:35 (UTC +02:00)
  • X @moleike
View GitHub Profile
const parseAttributes = payload => payload
.split(',')
.map(pair => pair.split('='))
.filter(pair => pair.length === 2)
.map(([key,val]) => ({ [key]: val }))
// > parseAttributes('pmin=5,pmax=40')
// [ { pmin: '5' }, { pmax: '40' } ]
@moleike
moleike / reverse-proxy.js
Last active August 26, 2016 02:56
an http api for mqtt-shepherd
// reverse proxy (HTTP(s) to LWM2M over MQTT)
// this should be mounted on an Express app
const router = require('express').Router()
const shepherd = require('lib/shepherd');
const ch = require('lib/status'); // coap to http status
const herrero = require('herrero');
const bodyParser = require('body-parser')
const { NotFoundError } = herrero;
@moleike
moleike / gist:5bc65c431d65e1cd06954f62677bf8ce
Created May 3, 2016 15:55 — forked from pguillory/gist:729616
Hooking into Node.js stdout
var util = require('util')
function hook_stdout(callback) {
var old_write = process.stdout.write
process.stdout.write = (function(write) {
return function(string, encoding, fd) {
write.apply(process.stdout, arguments)
callback(string, encoding, fd)
}
@moleike
moleike / defaults-overrides.md
Created April 21, 2016 02:44 — forked from ericelliott/defaults-overrides.md
ES6 defaults / overrides pattern

ES6 Defaults / Overrides Pattern

Combine default parameters and destructuring for a compact version of the defaults / overrides pattern.

function foo ({
    bar = 'no',
    baz = 'works!'
  } = {}) {
@moleike
moleike / lazy.js
Created April 8, 2016 09:21 — forked from kana/lazy.js
Lazy evaluation in JavaScript
function delay(expressionAsFunction) {
var result;
var isEvaluated = false;
return function () {
if (!isEvaluated)
result = expressionAsFunction();
return result;
};
}
@moleike
moleike / http-errors.js
Last active December 22, 2019 21:13 — forked from justmoon/custom-error.js
HTTP Error classes in Node.js
'use strict';
const statusCodes = require('http').STATUS_CODES;
function createError(code, name) {
return function(message) {
Error.captureStackTrace(this, this.constructor);
this.name = name;
this.message = message;
this.statusCode = code;
}
@moleike
moleike / restful.js
Created March 29, 2016 01:46 — forked from BinaryMuse/restful.js
Express API with Async/Await
import express from "express";
/**
* Takes a route handling function and returns a function
* that wraps it after first checking that the strings in
* `reserved` are not part of `req.body`. Used for ensuring
* create and update requests do not overwrite server-generated
* values.
*/
function checkReservedParams(routeHandler, ...reserved) {
@moleike
moleike / tags.cc
Created November 19, 2015 17:32
tag dispatching based on type traits
#include <iostream>
struct fast_speed_tag {};
struct slow_speed_tag {};
template <typename T>
struct traits { // default
typedef slow_speed_tag speed;
};
@moleike
moleike / server.js
Last active September 12, 2015 05:18 — forked from mixonic/server.js
Node.js + Socket.io + Bash. A collaborative terminal for your browser.
//
// This server will start a bash shell and expose it
// over socket.io to a browser. See ./term.html for the
// client side.
//
// You should probably:
//
// npm install socket.io
// curl -O https://github.com/LearnBoost/Socket.IO/raw/master/socket.io.min.js
//
@moleike
moleike / list.cc
Last active September 15, 2015 07:13
Linked list using polymorphic lambdas
// http://lists.boost.org/Archives/boost/2014/06/214213.php
// http://stackoverflow.com/questions/25338795/is-there-a-name-for-this-tuple-creation-idiom
#include <iostream>
#include <string>
#include <functional>
auto list = [](auto ...xs) {
return [=](auto access) { return access(xs...); };
};