Skip to content

Instantly share code, notes, and snippets.

@tjconcept
tjconcept / keybase.md
Created December 5, 2014 09:23
Keybase proof

Keybase proof

I hereby claim:

  • I am tjconcept on github.
  • I am thomasjensen (https://keybase.io/thomasjensen) on keybase.
  • I have a public key whose fingerprint is CC4C DE2E 97F2 EEA0 5C3A C2AE 60BE 7EA0 1EB0 4B01

To claim this, I am signing this object:

@tjconcept
tjconcept / index.md
Last active January 11, 2016 20:06
Surcharge

If you wish to put the entire fee on your customer, remember that the customer must also pay the fee of collecting the fee itself.

r = 0.025 // (rate fee)
f = 0.25 // (flat fee)

O = 100 // original amount (the cost of some product)

Naive (wrong) approach:

Array.from({ length: 1000 }, (v, k) => k)
.filter(x => x%3 === 0 || x%5 === 0)
.reduce((t, x) => t + x, 0)
@tjconcept
tjconcept / index.js
Created December 5, 2016 10:34
Minimal HTTP server
'use strict';
const http = require('http');
const Promise = require('bluebird');
const tell = require('http-tell');
const infer = require('http-infer');
const dispatch = require('http-dispatch');
const a404 = { code: 404 };
@tjconcept
tjconcept / index.js
Last active November 7, 2022 23:08
"join" polyfill
export default function using(...args) {
const fn = args.at(-1)
if (typeof fn !== 'function') {
throw new Error('Missing expected function argument')
}
if (args.length < 2) {
throw new Error('At least two arguments must be passed')
}
return Promise.all(args.slice(0, -1)).then((v) => fn(...v))
}
@tjconcept
tjconcept / index.js
Last active September 13, 2022 22:16
"join" with deterministic behavior for rejections
class Rejection {
constructor(reason) {
this.reason = reason
}
}
export default function using(...args) {
const lastIdx = args.length - 1
const fn = args[lastIdx]
if (typeof fn !== 'function') {
@tjconcept
tjconcept / index.js
Created September 13, 2022 22:15
"join" for objects (Promise.props)
export default function using(hash, fn) {
if (typeof fn !== 'function') {
throw new Error('Missing expected function argument')
}
return Promise.all(Object.values(hash)).then((values) =>
fn(
Object.fromEntries(
Object.keys(hash).map((key, idx) => [key, values[idx]])
)
)