Skip to content

Instantly share code, notes, and snippets.

Cyril Auburtin caub

Block or report user

Report or block caub

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View new unicode regex flags.js
// ## 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>...')
View downloadFromFs.js
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
View apollo.js
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>
@caub
caub / _output
Last active May 16, 2018
Dataloader knex benchmark
View _output
mapTo: 428.186ms
mapTo2: 319.058ms
ord: 348.390ms
mapToMany: 786.141ms
mapToMany2: 780.661ms
ord agg: 31856.442ms
View withData.js
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 };
View .light
#!/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
}
View linked-list.sql
create table foo (
id bigserial primary key,
prev bigint
);
insert into foo(id, prev) values
(1, NULL),
(2, 1),
(4, 2),
(12, 4),
View promiseMiddleware.js
/**
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}`) });
View pagination-done-right.sql
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 / context-demo#.js
Created Jan 13, 2018
React context demo
View context-demo#.js
// 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 = {
You can’t perform that action at this time.