Skip to content

Instantly share code, notes, and snippets.

@caub
caub / co.js
Last active November 2, 2016 15:33
coroutine
module.exports = function(gen) { // coroutine, equivalent to https://github.com/tj/co
const it = gen();
return Promise.resolve().then(function pump(v) {
const next = it.next(v);
if(next.done) return next.value;
return Promise.resolve(next.value).then(pump, it.throw.bind(it));
});
};
@caub
caub / my.sql
Last active August 29, 2017 21:01
versioning
create table foo_version (
id VARCHAR(32),
v BIGINT,
data TEXT,
PRIMARY KEY (id, v)
);
create table foo (
id VARCHAR(32),
// 2 globals defined at boot of nodejs engine
rl = cb => require('readline').createInterface({input:process.stdin}).on('line',cb);
print = console.log;
/*
Let's call an integer "repetitive" if it contains two of the same digit in a row. For example, 12232 is repetitive but 1232 is not.
Given a positive integer n < 10^15, return the number of nonrepetitive integers in [1..n].
*/
// prog
rl(s=>{r=0;d=s.length-1;for(i=0;i<=d;i++){r+=(i>0&&s[i-1]<s[i]?s[i]-1:s[i])*9**(d-i);if(s[i-1]==s[i]){r--;break}}for(i=0;i<d;i++)r+=9**i;print(r)})
@caub
caub / yes.js
Last active October 12, 2017 19:43
a fast yes implementation
const { Readable } = require('stream');
const ys = Buffer.from('y\n'.repeat(2**16));
class Y extends Readable {
_read(size) {
this.push(ys.slice(0, size));
}
}
new Y().pipe(process.stdout); // test with node yes | pv -r > /dev/null
@caub
caub / session-store.js
Created November 23, 2017 16:12
Custom session store
const { Store } = require('express-session');
const { knex } = require('.');
class PGStore extends Store {
destroy(sid, cb) {
return knex('sessions').where('id', sid).delete().then(() => cb());
}
get(sid, cb) {
// console.log('get', sid);
@caub
caub / JS wishlist.md
Last active November 28, 2017 23:45
Things I'd love to see in ES....
  • Shortening const to something like ref, val, cst because

    • it's the most frequently used keyword
    • conflicts with console.log auto-completion
  • null should be Object.freeze(Object.create(null)) or a special Symbol.nullable

  • ([] =~= []) // true shallow compare

  • ({ a: {} } =*= { a: {} }) // true deep compare

  • Making function arguments flexible and coherent with arrays

@caub
caub / knex-bin.diff
Last active November 30, 2017 09:44
knex CLI patch, for allowing --config=connectionString. add a npm script: "cd node_modules/knex && curl https://cdn.rawgit.com/caub/a8c23f97aa1942f2012ad8639769168c/raw/b3e96e3f09b2895329315b3aab13ce79a50049bc/knex-bin.diff | patch -p1 -t"
diff --git a/bin/cli.js b/bin/cli.js
index 9dc79f9..cf68699 100755
--- a/bin/cli.js
+++ b/bin/cli.js
@@ -33,11 +33,23 @@ function checkLocalModule(env) {
}
function initKnex(env) {
-
checkLocalModule(env);

Keybase proof

I hereby claim:

  • I am caub on github.
  • I am caub (https://keybase.io/caub) on keybase.
  • I have a public key whose fingerprint is 252A 77AD 73A4 A8BC FF76 D655 303C 5F10 6F57 4D9B

To claim this, I am signing this object:

@caub
caub / import.js
Last active January 1, 2018 22:12
browser shim for window.import()
window.import = function (transformer) {
const ROOT = location.origin;
const REQUIRE_CACHE = new Map();
const RE_EXT = /\.js\w*$/;
function fqn(url, dirname) {
const {href} = new URL(url.startsWith('http') ? url : (url.startsWith('/') ? ROOT : dirname) + url),
last = href.lastIndexOf('/'), dir = href.slice(0, last+1), name = href.slice(last+1),
filename = name ? (RE_EXT.test(name) ? name: name+'.js') : 'index.js';
return {dir, abs: dir+filename};
@caub
caub / context-demo#.js
Created January 13, 2018 15:21
React context demo
// https://codesandbox.io/s/jznpj65nm5
// ------- index.js
import React from 'react';
import { render } from 'react-dom';
import Hello from './Hello';
import DataProvider from './DataProvider';
import * as data from './dataStore';
const styles = {