Skip to content

Instantly share code, notes, and snippets.

View djfm's full-sized avatar
💭
Freelance. Won't set foot in office again.

François-Marie de Jouvencel djfm

💭
Freelance. Won't set foot in office again.
View GitHub Profile
@djfm
djfm / humanDuration.js
Last active April 22, 2021 20:14
Translate a number of seconds to a human-readable duration, e.g. 700000 is converted to "1 week, 1 day, 2 hours, 26 minutes and 40 seconds"
const pluralize = (n, [singular, plural]) => (n === 1 ? `${n} ${singular}` : `${n} ${plural}`);
const translateBiggestUnit = (
n,
[divisor, ...divisors],
[unit, ...units],
candidateDivisor,
divisorsUsed,
unitsUsed,
) => {
const scale = {
c: 0,
'c#': 1,
d: 2,
'd#': 3,
e: 4,
f: 5,
'f#': 6,
g: 7,
'g#': 8,
const scale = {
c: 0,
'c#': 1,
d: 2,
'd#': 3,
e: 4,
f: 5,
'f#': 6,
g: 7,
'g#': 8,
const {
Cluster,
N1qlQuery,
MutationState,
} = require('couchbase');
const URL = require('url');
const addQueryParams = params => url => {
const parsed = URL.parse(url);
const just = x => Promise.resolve(x);
const nothing = () => Promise.reject();
chain(
() => just(1),
() => nothing(),
(_, x, y) => just(x / y)
)()
.should.be.rejected;
@djfm
djfm / chain-do.js
Last active September 23, 2016 13:24
const chainStep = fn => (argSource, history) => {
if (argSource && (typeof argSource === 'object')) {
const then = argSource.then;
if (typeof then === 'function') {
return Promise.all(history).then(
settledHistory =>
then.call(argSource, arg => fn(arg, ...settledHistory))
);
}
}
do
a <- makeId "dress"
b <- makeId "dress"
c <- makeId "dress"
return checkOrdered a b c
storage.makeId('dress')
.then(
a =>
storage.makeId('dress')
.then(
b =>
storage.makeId('dress')
.then(
c => {
a.should.be.lessThan(b);
chain(
() => storage.makeId('dress'),
() => storage.makeId('dress'),
() => storage.makeId('dress'),
(_, a, b, c) => {
a.should.be.lessThan(b);
a.should.be.lessThan(c);
b.should.be.lessThan(c);
}
)()
const chain = (...fns) =>
initialValue => fns.reduce(
(result, fn) => {
if (result && (typeof result === 'object')) {
const then = result.then;
if (typeof then === 'function') {
return then.call(result, fn);
}
}
return fn(result);