Skip to content

Instantly share code, notes, and snippets.

View julien-f's full-sized avatar

Julien Fontanet julien-f

View GitHub Profile
@julien-f
julien-f / index.js
Created October 10, 2019 18:23
Dependencies usage
const { readFileSync } = require("fs");
const depsCount = {};
function processDeps(deps) {
deps !== undefined &&
Object.keys(deps).forEach(dep => {
depsCount[dep] = 1 + (depsCount[dep] || 0);
});
}
function dispatch() {
if (this._dispatched) {
return
}
this._dispatched = true
const resolve = this._resolve
if (resolve !== undefined) {
this._resolve = undefined
@julien-f
julien-f / combine-predicates.js
Created January 29, 2019 16:46
combine predicates
import { isFunction } from 'lodash'
export default function combinePredicates(...predicates) {
predicates = predicates.filter(isFunction)
const n = predicates.length
return n === 0
? undefined
: n === 1
? predicates[0]
: function() {
@julien-f
julien-f / definitely.js
Last active February 22, 2019 10:12
Error when accessing missing properties
class MissingPropertyAccess extends TypeError {
get name() {
return this.constructor.name;
}
constructor(object, property) {
super(`access to missing property ${String(property)}`);
this.object = object;
this.property = property;
}
@julien-f
julien-f / index.js
Last active December 7, 2018 12:07
fs.open flags
export const O_APPEND = 1024
export const O_CREAT = 64
export const O_EXCL = 128
export const O_RDONLY = 0
export const O_RDWR = 2
export const O_SYNC = 1052672
export const O_TRUNC = 512
export const O_WRONLY = 1
// https://docs.microsoft.com/en-us/windows/desktop/api/fileapi/nf-fileapi-createfilea
// create a bound function which receive itself injected as first arg
function rbind(fn, thisArg, ...args) {
function bound() {
return fn.apply(
thisArg === undefined ? this : thisArg,
args.concat(arguments)
);
}
args.unshift(bound);
return bound;
@julien-f
julien-f / test-rrds
Created June 18, 2018 16:17
Test RRDs
#!/usr/bin/env node
const hrp = require('http-request-plus').default
const JSON5 = require('json5')
const required = param => {
throw new Error(`missing required param ${param}`)
}
async function main (args) {
export default (stream, n) => new Promise((resolve, reject) => {
const chunks = []
let i = 0
function clean () {
stream.removeListener('readable', onReadable)
stream.removeListener('end', onEnd)
stream.removeListener('error', onError)
}
function resolve2 (done) {
clean()
@julien-f
julien-f / README.md
Created May 5, 2018 12:14
Programing principles
@julien-f
julien-f / example.js
Last active April 12, 2018 17:41
Async generator from generator
const makeAsyncGenerator = require('./make-async-generator')
const f = makeAsyncGenerator(function * () {
yield 'foo'
const value = yield Promise.resolve('bar') // yield promises to await them
yield value
try {
yield Promise.reject('baz')
} catch (value) {
yield value