Skip to content

Instantly share code, notes, and snippets.

create table foo (
id bigserial primary key,
prev bigint
);
insert into foo(id, prev) values
(1, NULL),
(2, 1),
(4, 2),
(12, 4),
@caub
caub / .light
Last active March 15, 2018 09:50
#!/bin/bash
regexp='^[-+]?[0-9]*(\.[0-9]*)?$'
validateNumber() {
if [[ $1 == *[0-9]* && $1 =~ $regexp ]]; then
echo "$1"
else
printf >&2 '%s is not a valid number\n' "$1"
fi
}
/**
This middleware enhances redux-thunk, to deal with asynchronous actions
conventions and usage:
- an action is a plain object `{type: TYPE_CONSTANT, value: ANY_VALUE}` or an array of those actions (handled by reducer)
- you can shape action creators as `queryArgs => promise` (where the promise returns an action)
- or `queryArgs => action` (where the action value is a promise).
examples:
const getProfile = id => ({ type: GET_PROFILE, value: fetchApi(`/profile/${id}`) });
import { graphql } from 'react-apollo';
import gql from 'graphql-tag';
const TodoApp = ({ loading, error, data: { todos, refetch }, ...props }) => {
if (loading) return <p>Loading...</p>;
if (error) return <p>Error :(</p>;
return (
<div {...props}>
<button onClick={() => refetch()}>Refresh</button>
<ul>{todos && todos.map(todo => <li key={todo.id}>{todo.text}</li>)}</ul>
import React from 'react';
/**
* Simple HOC, taking a fetcher function (returning a promise) then a Component
* @returns Component accepting a ({ data, error, loading }) => {..} function as children
*/
export default fetcher => Component =>
class extends React.PureComponent {
state = { loading: true, data: [], error: undefined };
const DIR = os.homedir() + '/.foo';
// adapted from https://stackoverflow.com/a/24977085/3183756
const downloadFromFs = (req, res) => {
const { name } = req.params;
const filePath = path.join(DIR, name);
fs.stat(filePath, (err, stats) => {
if (err) {
if (err.code === 'ENOENT') {
// 404 Error if file not found
// ## like \1 but named:
/(?<fruit>apple|fig)==\k<fruit>/u.test('fig==fig')
// true
// ## named group captures:
'we can'.replace(/(?<subject>\w+) (?<verb>\w+)/u, '$<subject>...$<verb>')
//"we...can"
'yes, we can'.replace(/(?<w>\w+)[,\s]+/gu, '$<w>...')
export default class BetterMap {
constructor(data) {
this.m = new Map(Array.isArray(data) || data instanceof Map ? data : Object.entries(data));
}
set(k, v) { // return BetterMap
this.m.set(k, v);
return new SortableMap(this.m);
}
delete(k) { // return BetterMap
this.m.delete(k, v);
@caub
caub / _output
Last active May 16, 2018 18:11
Dataloader knex benchmark
mapTo: 428.186ms
mapTo2: 319.058ms
ord: 348.390ms
mapToMany: 786.141ms
mapToMany2: 780.661ms
ord agg: 31856.442ms