Skip to content

Instantly share code, notes, and snippets.

View jamesseanwright's full-sized avatar
🤔

James Wright jamesseanwright

🤔
View GitHub Profile
@jamesseanwright
jamesseanwright / node-args-parser.js
Created March 23, 2020 13:05
A simple Node.js command line args parser
// Invoked with node script.js --db-url=postgres://0.0.0.0:5432/db --commit --item-count=5
const parseArgs = (args) =>
new Map(args.map((arg) => arg.match(/^--([a-z0-9-]+)=?(.*)$/i).slice(1)));
const args = parseArgs(process.argv.slice(2));
const dbUrl = args.get('db-url'); // 'postgres://0.0.0.0:5432/db'
const shouldCommit = args.has('commit'); // true
const itemCount = Number.parseInt(args.get('item-count'), 10);
@jamesseanwright
jamesseanwright / manc-web-postponement.md
Created March 12, 2020 20:19
March Manc Web Postponement

Hey there, Manc Webbers,

In the wake of the ongoing COVID-19 outbreak, we've made the decision to postpone March's Manc Web, due to take place on Wednesday 18th; this event will now take place on Wednesday 17th June 2020. While we're still in the early stages of this pandemic, we wouldn't want our group to serve as a conduit for the spread of the virus, and like many other communities in the city have opted to err on the side of caution. We would like to thank our hosts, sponsors, videographer, and speakers for their flexibility.

Note that we also have a meetup planned for Wednesday 20th May; this has been fully planned and will be announced next month, in the hopes of the situation being more manageable by then.

Take care, and stay safe!

James

@jamesseanwright
jamesseanwright / rot13.py
Created November 15, 2019 11:18
132-byte ROT13 implementation in Python 3
def rot13(s):return"".join(map(lambda c:chr(c+(0,13*(1,-1)[c>(77,109)[c>96]])[(c>96and c<123)or(c>64and c<91)]),[ord(c)for c in s]))
@jamesseanwright
jamesseanwright / deep-iterable-equality-chai.js
Last active November 3, 2019 21:30
Deep equality of iterables with Chai
import { expect as chaiExpect } from 'chai';
describe('deep iterable equality', () => {
it('should deeply compare iterables based upon their type', () => {
chaiExpect([1, 2, 3]).to.eql([1, 2, 3]);
chaiExpect([1, [2, 3]]).to.eql([1, [2, 3]]);
chaiExpect([1, 2, 3]).not.to.eql([3, 2, 1]);
chaiExpect(new Set()).to.eql(new Set());
chaiExpect(new Set([1, 2, 3])).to.eql(new Set([1, 2, 3]));
@jamesseanwright
jamesseanwright / deep-iterable-equality-jest.js
Last active November 3, 2019 18:01
Deep equality of iterables in Jest
describe('deep iterable equality', () => {
it('should deeply compare iterables based upon their type', () => {
expect([1, 2, 3]).toEqual([1, 2, 3]);
expect([1, [2, 3]]).toEqual([1, [2, 3]]);
expect([1, 2, 3]).not.toEqual([3, 2, 1]);
expect(new Set()).toEqual(new Set());
expect(new Set([1, 2, 3])).toEqual(new Set([1, 2, 3]));
expect(new Set([1, 2, 3])).toEqual(new Set([3, 2, 1]));
expect(new Set([1, [2, 3]])).toEqual(new Set([[2, 3], 1]));
@jamesseanwright
jamesseanwright / stackCreateElementRenderer.jsx
Last active October 11, 2019 08:56
Toy, stack-based HTML renderer with React.createElement-compliant element creator
'use strict';
const indentWidth = 2;
const selfClosingElements = [
'meta',
'link',
];
const indent = (string, depth) =>
@jamesseanwright
jamesseanwright / lazy-routes.tsx
Created July 17, 2019 16:40
Lazy route loading with React.lazy and Suspense
import * as React from 'react';
import Nav from './Nav.jsx';
import { Router } from './routing';
const routes = new Map<string, React.ComponentType>([
['/', () => <p>Pick an Ipsum!</p>],
['/lorem', React.lazy(() => import('./pages/Lorem'))],
['/bacon', React.lazy(() => import('./pages/Bacon'))],
['/hipster', React.lazy(() => import('./pages/Hipster'))],
['/office', React.lazy(() => import('./pages/Office'))],
import * as React from 'react';
const Lorem = React.lazy(() => import('./pages/Lorem'));
const App = () => (
<React.Suspense fallback={<div className="loading-spinner" />}>
<Lorem />
</React.Suspense>
);
@jamesseanwright
jamesseanwright / react-lazy-res-impl.js
Created July 17, 2019 16:21
React lazy resolution state tracking
switch (status) {
case Resolved: {
const Component: T = result;
return Component;
}
case Rejected: {
const error: mixed = result;
throw error;
}
case Pending: {
import * as React from 'react';
const App = () => (
<main>
<h1>My App</h1>
<React.Suspense fallback={<LoadingSpinner />}>
<Page />
<React.Suspense fallback={<MiniSpinner />}>
<PageMetadata />
</React.Suspense>