Skip to content

Instantly share code, notes, and snippets.

View rosschapman's full-sized avatar

Ross Chapman rosschapman

View GitHub Profile
@rosschapman
rosschapman / validate-org.js
Created July 15, 2020 19:37
Validate Org Chart
// This is an example of one of our inputs.
// This data sructure represents the org structure of a company.
// At the top level here we have an _array_ of employees with various properties.
// 1. Required keys need to exist
// 2. Values need to match the specified type
// 3. Extraneous keys are invalid and should error
// 4. Return when you encounter the very first error
export function validate(data, schema) {
try {
@rosschapman
rosschapman / extract-host-tld.js
Last active July 15, 2020 19:38
Extract Host and TLD from valid url
// Assumes a valid url
export function extractHostAndTLD(url) {
const reDot = /\./g;
const reAfterDot = /\.(.*)$/;
const boxedUrl = new URL(url);
const host = boxedUrl.host;
if (host.match(reDot).length > 1) {
// Ignore the inclusive match and only extract the captured group
const [_, afterDot] = host.match(reAfterDot);
@rosschapman
rosschapman / SketchSystems.spec
Last active May 15, 2020 17:36
Retool Challenge
Retool Challenge
IDLE
try -> EDITING
EDITING
untry -> IDLE
execute -> WAITING
WAITING
data -> HAS_DATA_EDITING
HAS_DATA_EDITING
untry -> HAS_DATA
@rosschapman
rosschapman / maybe-fetch-display.ts
Created April 27, 2020 00:01
Maybe fetch and display
if (books.ids.length > 0) {
this.stateMachine(this.statuses.waiting as Status);
let suggestedBooks = await this.apiAdapater.books.suggest();
this.stateMachine(this.statuses.success as Status);
this.model.updateAll("suggestedBooks", suggestedBooks);
}
@rosschapman
rosschapman / app-conductor-interface.jsx
Last active April 26, 2020 23:27
App conductor interface
<AppConductor>
{({ submitForm, getModel }) => (...)}
</AppConductor>
@rosschapman
rosschapman / action-dispatch-process.ts
Last active April 24, 2020 20:12
Separate action dispatch and process
private processEntityCreate = async (payload: any) => {
// Update component status (sync)
this.statusMachine(this.statuses.waiting);
// Post request (async)
await this.apiAdapater.MY_ENTITY.post(action.payload);
// Update component status (sync)
this.statusMachine(this.statuses.success);
};
private actionDispatch = async (action: Action) => {
@rosschapman
rosschapman / typed-user-actions.tsx
Last active April 24, 2020 19:19
Typed user actions object
type UserAction = "SUBMIT_FORM";
type UserActions = {
[key: string]: UserAction;
};
class AppConductor extends React.Component<Props, State> {
readonly userActions: UserActions = {
submitForm: "SUBMIT_FORM"
};
//...
@rosschapman
rosschapman / app-conductor-manager-and-status.js
Last active April 25, 2020 02:44
AppConductor with action manager and state machine
processBookCreate = async (payload) => {
// Update component status (sync)
this.statusMachine(this.statuses.waiting);
// Post request (async)
await this.apiAdapater.books.post(action.payload);
// Update component status (sync)
this.statusMachine(this.statuses.success);
};
statusMachine = (nextStatus: Status) => {
@rosschapman
rosschapman / app-conductor-basic.tsx
Last active April 26, 2020 23:27
AppConductor with basic action manager
class AppConductor extends React.Component {
userActions = {
submitForm: "SUBMIT_FORM"
};
actionRouter = async (action) => {
switch (action.type) {
case "SUBMIT_FORM":
// wondering where all those calls are gonna go?? 😎
default:
@rosschapman
rosschapman / app-conductor-basic.jsx
Last active April 23, 2020 03:52
AppConductor basic skeleton
<AppConductor>
{({ submitForm }) => {
return (
<>
<Form handleOnSubmit={submitForm} />
<>
);
}}
</AppConductor>