Skip to content

Instantly share code, notes, and snippets.

@brigand
brigand / lazy-example.js
Last active March 3, 2018 11:43
ECMAScript Idea: `lazy` operator?
/*
Just an idea, not a formal proposal.
Promises are likely the most common use case for this.
*/
const str = await Promise.fromLazy(
// Pretend the names lazy/yield are something else. Naming is hard.
lazy of `Hello, ${(yield getUser()).name}.
You have ${(yield getMessages()).length} messages.`
);
@brigand
brigand / xtPosts.hr.js
Created January 1, 2018 23:22
Concept for better reducers, with transparent meta properties for lists and id mappings.
const xtPosts = makeState({
// Give it a name for logs
name: 'xtPosts',
// Specify that we're using lists and byId mappings
// maybe these aren't needed.
list: true,
byId: true,
// Handle some redux actions
# parse json into global variables
# e.g. s_parse_json prefix <<<'{"x": 1, "y": {"z": 2}}'
# creates prefix_x and prefix_y_z
# doesn't create prefix_y
#
# also arrays:
# e.g. s_parse_json prefix <<<'{"x": ["y", "z"]}'
# creates prefix_x_length prefix_x_0 and prefix_x_1
function s_parse_json () {
@brigand
brigand / express-saga.md
Last active January 14, 2022 14:23
express-saga is a web framework based on express and the saga pattern (redux-saga's interpretation) – just an idea, let's see if it turns out decent :-)

This is an idea, the code doesn't exist

express-saga is a web framework based on express and the saga pattern (redux-saga's interpretation).

You can use all existing express middleware without issue, but often there are better alternatives in express-saga.

Most effects from redux-saga are available, along with new effects for web server development.

This library is designed with a heavy focus on unit-testability. You don't need module level mocks due to dependency injection. Your routers are pure functions, so you don't need to run a http server in tests.

@brigand
brigand / Tabs.js
Created July 3, 2017 11:54
An animated tabs component
import React from 'react';
// import {findDOMNode} from 'react-dom';
import './Tabs.css';
const transitionTime = 200;
const transitionStyle = `left ${transitionTime}ms, right ${transitionTime}ms`;
class Tabs extends React.Component {
constructor(props) {
@brigand
brigand / di.jsx
Last active April 18, 2017 00:03
React DI provider, proof of concept
export class DIProvider extends React.Component {
static childContextTypes = {
injector: PropTypes.func,
};
getChildContext() {
return {injector: this.props.injector};
}
render() {
@brigand
brigand / genserve.md
Last active January 13, 2017 15:00
genserve - a concept for a generator-based server with pure controllers and many features built in

Note: this is just a concept, it'll probably never exist or be significantly different.

To start, we'll create a basic server with two routes. Note that none of these generator functions have side effects. They yield effect descriptors. This allows easy unit testing without mocking modules.

const gs = require('genserve');
const config = {
  port: 3000,
@brigand
brigand / secretary.md
Last active January 5, 2017 13:01
react-secretary: an idea for a react helper library

You set up the secretary like this:

import {hire} from 'react-secretary';
import secConfig from '../utils/secConfig';
class Foo extends React.Compoennt {
  constructor(props) {
    super(props);
    this.sec = hire(this, secConfig);
  }
@brigand
brigand / high-impact-redux-tests.md
Last active April 12, 2017 21:01
A guide to writing high impact redux tests.

Some people will tell you to test everything in your app. If you have time to kill, go for it. For most of us, we want to focus on tests that help us sleep at night. We'll be using jest in this guide, mostly because it provides snapshot testing.

mapDispatchToProps

This isn't something people normally test, but it saves us from typos that only show up in response to user interaction. Often we fail to dispatch all of the actions in our manual testing of UIs.

The premise here is simple, we want to know that all of our keys are actual functions. We'll be

@brigand
brigand / writing-open-source-component.md
Last active October 21, 2017 23:00
React.js: Writing open source components

Open source is an important part of the react ecosystem. Most of the time you'll use components written by others, but you may wish to create components other people can use. For this guide, you'll need to have node.js and git installed.

Setup

First you'll need to create a project directory, and open a terminal in that directory. React components are published as npm packages, so we need a package.json file. To create one, run npm init and follow the prompts.