Skip to content

Instantly share code, notes, and snippets.

View fabienjuif's full-sized avatar
🦝
meow!

Fabien JUIF fabienjuif

🦝
meow!
  • Cantina
  • Saumur, France
  • 11:17 (UTC +02:00)
View GitHub Profile
@fabienjuif
fabienjuif / batch_promises.md
Last active October 5, 2025 08:03
Javascript / Batch / Promises

Snippet

const Promise = require('bluebird');

const batch = async (args, callback, options = {}) => {
  const { max = 2 } = options;
  const results = [];

  let running = args.slice(0, max).map(a => callback(...a));
 let picked = running.length;
import { useState } from 'react'
const useRandom = (min = 0, max = Number.MAX_SAFE_INTEGER) => {
const [value, setValue] = useState(undefined)
const rand = () => {
setValue(Math.floor(Math.random() * max) + min)
}
if (value === undefined) rand()
@fabienjuif
fabienjuif / usage.jsx
Created January 15, 2019 13:58
react / hoc / mouse tracker
import React, { Component } from 'react';
import withMouseTracker from './withMouseTracker'
class Timeline extends Component {
render() {
console.log(this.props.mouseTracker)
return (
<div
className="timeline"
ref={this.props.trackerRef}
@fabienjuif
fabienjuif / README.md
Last active October 9, 2018 13:36
yarn upgrade all
@fabienjuif
fabienjuif / Entity.js
Created August 28, 2018 08:30
matter-js / dirty fix for CCD and velocity that doesn't scale with delta ticks
const update = (entity, delta) => {
const { body } = entity
// move using `setPosition` and scale it with time since `setVelocity` doesn't scale with time (yet ?)
Body.setPosition(
body,
Vector.add(
body.position,
Vector.mult(
{ x: 1, y: 0 },
/* eslint-env jest */
// init avec default data
// 100 SET d'un tableau de 1000
// 100 ADD
// 100 UPDATE
// 100 ADD_OR_UPDATE
// 100 REMOVE
// <time>
// 1000 de chaque selector
const { keyValue } = require('.')
@fabienjuif
fabienjuif / clean-mac.sh
Last active May 30, 2018 08:40
clean local branches
git branch -vv | awk '{print $1}' | grep -v HEAD | grep -v master | grep -v '*' | xargs git branch -d --force
@fabienjuif
fabienjuif / cache.js
Last active June 30, 2019 10:04
Node.js / utils / cache
const crypto = require('crypto')
const toMD5 = object => crypto.createHash('md5').update(JSON.stringify(object)).digest('hex')
module.exports = ({
timeout = (/* 1 h */ 1 * 60 * 60 * 1000),
maxEntries = 100,
name = 'cache',
log = console.log,
} = {}) => {
git branch -vv | awk '{print $1}' | grep -Ev 'HEAD|master|*' | xargs git branch -d --force
@fabienjuif
fabienjuif / simple-cache-callback.js
Created December 20, 2017 09:15
Simple cache (async/await) with timeout support
const getKey = args => JSON.stringify(args)
const callAndSave = cache => callback => async (...args) => {
const value = await callback(...args)
cache[getKey(args)] = { // eslint-disable-line no-param-reassign
timestamp: Date.now(),
value,
}