Skip to content

Instantly share code, notes, and snippets.

Nicolas Chambrier naholyr

Block or report user

Report or block naholyr

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View 00-context.jsx
import * as React from 'react';
// TODO inject counters
const Counter = () => {
const [value, setValue] = React.useState(1);
const incr = React.useCallback(() => setValue(value + 1), [value]);
return (
<button type="button" onClick={incr}>
Click to incr. ({value})
View run.js
const { promisify } = require('util')
const delay = promisify(setTimeout)
const pad = (s, len = 4) => s.length >= len ? s : pad(`0${s}`, len)
// 300 tasks (id for log, run for action)
const tasks = []
for (let i = 0; i < 300; i++) {
const id = String(i)
const run = () => delay(2000 + Math.random() * 8000)
tasks.push({ id, run })
View test.jsx
import React, { Fragment, Component } from 'react';
import ReactDOM from 'react-dom';
class ContextData {
watchers = []
constructor(initialState = {}) {
this.state = initialState
View README.md

scan-dependencies

Usage : node scan-dependencies.js <path>

  • will find every root project inside <path> (a project is a folder with package.json file, himself not inside a node_modules directory)
  • will npm install then npm outdated from project
  • will list all *.js files and check for missing require(…) for every dependency
View README.md

Shitty code, version 1:

const concat = s => s.reduce((r,s) => r + s)
const mult = (a, b) => a * b
const foo = a => b => c => concat([c, ' = ', mult(a, b)])
const bar = a => Number(a) + 1
const baz = b => b ? 42 : 33
const res = foo(1)('bar')('baz')
console.log(res)
@naholyr
naholyr / promise-all.js
Last active Sep 8, 2016
Run promise-based async API in series or concurrency
View promise-all.js
// ( 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
Node config module
View index.js
"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 Apr 12, 2016
Check if your npm dependencies are actually used
View npm-check-used.sh
#!/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 Mar 3, 2016
Notify when a zsh command ends in a shell not currently focused
View .zshrc
# Requires zsh
# Requires xdotool
the_command="?????"
preexec()
{
if [ "x$TTY" != "x" ]; then
the_command="$2"
fi
@naholyr
naholyr / cache.js
Created Jan 22, 2016
Redis cache client
View cache.js
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)
You can’t perform that action at this time.