Skip to content

Instantly share code, notes, and snippets.

@caub
caub / .gitconfig
Last active August 24, 2020 20:50
[user]
email = cyril.auburtin@gmail.com
name = caub
[alias]
a = add --all
au = add -u
b = branch
br = branch -r
amend = commit --amend -C HEAD

Mongo script gotchas

  • no console.log, use print instead
  • no Object.entries, Object.values use Object.keys
  • Map, Set are not spec-compliant, Set values are stringified, Map are not iterable, don't use them
  • for (const foo of [1]) {} is also broken, it needs a let/var instead of const (warning: let might not be block-scoped as per spec, they probably just replace it by var)
  • Use doc._id + '' to get the objectId as hex string, don't rely on template strings also, they seem to incorrectly invoke doc._id.toString() which returns 'ObjectId(hexstring)'
  • Outdated RegExp, not supporting look-behinds
@caub
caub / Sux.js
Last active June 28, 2019 20:33
simple react state management
import React from 'react';
const SuxContext = React.createContext();
export class SuxProvider extends React.Component {
state = {};
render() {
return (
<SuxContext.Provider value={{...this.state, dispatch: data => this.setState(data)}}>
const fetch = require('node-fetch');
const http = require('http');
class Express {
constructor(
notFoundHandler = (req, res) => {
res.statusCode = 404;
res.write('not found');
res.end()
},
var formatDate = d=>`${d.getFullYear()}-${`${d.getMonth()+1}`.padStart(2,0)}-${`${d.getDate()}`.padStart(2,0)}`
var es=document.body.textContent.trim().split('\n').map(l => {let [,d,s]=l.split(/\[|\] /g); return {d: new Date(d), s}}).sort((a,b)=>a.d-b.d)
var xs=es.map(({d,s}) => { let m=d.getMinutes(); if (d.getHours()===23){d.setDate(d.getDate()+1);m=0;} return {date: formatDate(d), m,d,s}} )
var days=xs.reduce((o,{date,m,s})=>{const slots=o[date]&&o[date].slots||Array.from({length:60},()=>0); let g=s.match(/#\d+/); if (s.startsWith('falls')){for(let i=m;i<slots.length;i++)slots[i]=1} if (s.startsWith('wakes')){for(let i=m;i<slots.length;i++)slots[i]=0} return {...o,[date]:{...o[date],slots,...g&&{guard:g[0]}}} }, {})
var guards=Object.values(days).reduce((o, {slots,guard}) => {let name=guard.slice(1); const sleeptime=o[name]||0; return {...o,[name]:sleeptime+slots.reduce((a,b)=>a+b)};}, {})
var ds = Object.entries(days).filter(([date, o]) => o.guard==='#2593').map(([date, {slots}]) => slots)
Math.max(...ds.
create table foo (
id bigserial primary key,
name text,
created date
);
insert into foo(name,created) values
('lol', '2018-01-09'),
('lol', '2018-01-08'),
('lol', '2018-01-08'),
@caub
caub / server.js
Last active October 15, 2018 06:41
https with letsencrypt
// d run -d --name=ok -p 443:6100 -p 80:6000 --workdir=/home/node -v /etc/letsencrypt/:/etc/letsencrypt/:ro -v $PWD/server.js:/home/node/server.js node node server
var https = require("https");
var http = require("http");
var fs = require("fs");
var p="/etc/letsencrypt/live/ulti.pw/";
var hs=https.createServer({
key: fs.readFileSync(p+"privkey.pem")+"",
@caub
caub / jQuery4.js
Last active September 30, 2018 21:40
v4.1
function $(container, selector) {
const els =
typeof selector === 'string'
? container.querySelectorAll(selector)
: typeof container === 'string' ? document.querySelectorAll(container) : [container];
const fns = {
removeClass(...cls) {
els.forEach(el => {
el.classList.remove(...cls);
// render .js files as templates (they must be function taking options as arguments and outputting a valid HTML string)
app.engine('js', function (filePath, options, callback) {
if (process.env.NODE_ENV !== 'production') {
require.cache[filePath] = undefined; // invalidate require cache in dev
}
callback(null, require(filePath)(options)); // note: should wrap in a safeHtml in prod
});
app.set('views', __dirname + '/views');
app.set('view engine', 'js');
@caub
caub / promise-middleware.js
Created May 22, 2018 17:58
Don't use redux-saga
const promiseMiddleware = store => next => action => {
if (typeof action.then === 'function') {
return Promise.resolve(action).then(next);
} else if (action.value && typeof action.value.then === 'function') {
return Promise.resolve(action.value).then(value => next({ type: action.type, value }));
}
// else, not a promise
try {
return next(action);
} catch (e) {