Skip to content

Instantly share code, notes, and snippets.

@naholyr
naholyr / promise-all.js
Last active September 8, 2016 11:04
Run promise-based async API in series or concurrency
// ( T1 => Promise<T2> ) => Array<T1> => Promise<Array<T2>>
export const concurrent = foo => vals =>
Promise.all(vals.map(foo))
export const series = foo => vals =>
vals.reduce((p, val) => p.then(rs => foo(val).then(r => rs.concat([val]))), Promise.resolve([]))
// Usage: promiseOfValues.then(promiseAll.concurrent(functionReturningAPromiseFromAValue))
@naholyr
naholyr / index.js
Created May 4, 2016 21:28
Node config module
"use strict";
const { readFileSync } = require("fs")
const { parse } = require("json5")
const { merge } = require("lodash")
module.exports = merge({},
// Global defaults
requireConfig("defaults"),
// Environment defaults
@naholyr
naholyr / npm-check-used.sh
Created April 12, 2016 08:40
Check if your npm dependencies are actually used
#!/bin/bash
# Run from your project's root
# Run "npm install" or "npm update" before
# Requirement: jq (https://stedolan.github.io/jq/)
required () {
grep -R "from ['\"]${1}[\\/'\"]\|require(['\"]${1}[\\/'\"]" --exclude-dir=node_modules --include='*.js' > /dev/null
}
@naholyr
naholyr / .zshrc
Created March 3, 2016 14:41
Notify when a zsh command ends in a shell not currently focused
# Requires zsh
# Requires xdotool
the_command="?????"
preexec()
{
if [ "x$TTY" != "x" ]; then
the_command="$2"
fi
@naholyr
naholyr / cache.js
Created January 22, 2016 20:51
Redis cache client
import config from '…'
import Redis from 'ioredis'
const client = new Redis(config.redis)
const cachePrefix = config.cache.prefix
const cacheDefaultTTL = config.cache.defaultTTL
// (string) => Promise<any>
export function get (key /* string */) {
return client.get(cachePrefix + key)
@naholyr
naholyr / app1.js
Last active January 21, 2016 13:54
Time a middleware
// generic reusable
app.use(timeMiddleware('session', session));
// …
function timeMiddleware (description, middleware) {
return function (req, res, next) {
var start = process.hrtime();
middleware(req, res, function (err) {
@naholyr
naholyr / client.js
Created December 21, 2015 08:46
Express + socket.io CLI client
var Promise = require('bluebird');
var request = require('request-promise');
var io = require('socket.io-client');
var jar = request.jar();
/**
* Connect to web interface
*/
request({
uri: loginActionUrl,
// foo() fait une opération compliquée
// mais c'est codé un peu avec les pieds et un des EventEmitter déclaré (et non exposé)
// dans la fonction émet parfois un "error" non catché qui pète mon application :(
var foo = require("./module-pourri");
// Ça fait parfois péter mon app :(
//foo();

Keybase proof

I hereby claim:

  • I am naholyr on github.
  • I am naholyr (https://keybase.io/naholyr) on keybase.
  • I have a public key whose fingerprint is EC19 8266 EF73 F79E 84AA E931 B111 3B99 36F2 E232

To claim this, I am signing this object:

function dirTree (filename, cb) {
fs.lstat(filename, function (err, stat) {
if (err) {
return cb(err);
}
var info = {
"path": filename,
"name": path.basename(filename)