Skip to content

Instantly share code, notes, and snippets.

View jamesseanwright's full-sized avatar
🤔

James Wright jamesseanwright

🤔
View GitHub Profile
@jamesseanwright
jamesseanwright / rock-paper-scissors.js
Last active June 27, 2017 15:46
A n00b's attempt at JavaScript code golf...
/* uncompressed - 430 bytes
* minified (UglifyJS) - 273 bytes
* FYI - indented using tabs because one character vs 4, yo.
* Append ?ts = 4 to URL to format this correctly.
*/
function p() {
var a, b,
m = Math,
r = m.random,
@jamesseanwright
jamesseanwright / nodejs-cpu-capture.js
Last active January 7, 2016 12:24
Rudimentary capturing of CPU busy time in Node.js
'use strict';
var os = require('os');
var hasWrittenHeader = false;
var fileStream = require('fs').createWriteStream('cpuProfile.out');
function buildResults() {
var output = '';
var cpus = os.cpus();
var cpu;
@jamesseanwright
jamesseanwright / closure-compiler
Created August 14, 2016 17:44
Shell wrapper for Closure Compiler
#!/usr/bin/env sh
java -jar /usr/local/bin/closure-compiler.jar $@
@jamesseanwright
jamesseanwright / express-promise-rejection.js
Created September 16, 2017 09:02
Handling Promise rejection within Express routes
'use strict';
const fetch = require('node-fetch');
const express = require('express');
const router = express.Router();
/* This function returns an inner function(i.e. a higher-order
* function) that will be invoked by Express when a particular
* path is request. This inner function will invoke the wrapped
* handler, and invoke Express' next function if the returned
@jamesseanwright
jamesseanwright / ecs-functional-system.js
Last active May 24, 2018 11:16
Sect - functional System API
'use strict';
class System {
constructor(name, next) {
this.name = name;
this.components = [];
this.next = next;
}
getOtherComponents(component) {
@jamesseanwright
jamesseanwright / vectors.js
Last active July 5, 2018 07:58
Manipulating Vectors of Numbers in JavaScript
'use strict';
const getVectorSize = vector => Math.sqrt(
vector.map(n => Math.pow(n, 2))
.reduce((total, n) => total + n, 0)
);
const addVectors = (a, b) => a.map((number, i) => number + b[i]);
const multiplyVectors = (a, b) => a.map((number, i) => number * b[i]);
@jamesseanwright
jamesseanwright / react-usestate-hook-example.tsx
Last active December 20, 2018 18:25
React useState Hook Example
const MessageForm: React.FC<Props> = ({ submitMessage }) => {
const [message, setMessage] = React.useState('');
return (
<form onSubmit={e => {
e.preventDefault();
submitMessage(message);
}}>
<input
type="text"
@jamesseanwright
jamesseanwright / react-instance-backed-state-example.tsx
Created December 20, 2018 18:29
React State with Backing Instance Example
class MessageForm extends React.Component<Props, State> {
constructor(props: Props) {
super(props);
this.state = {
message: '',
};
}
render() {
@jamesseanwright
jamesseanwright / react-provider-example.tsx
Created December 21, 2018 11:23
React Provider Example
const UserContext = React.createContext(maybe<User>());
const App: React.FC<AppProps> = ({ user }) => (
<UserContext.Provider value={maybe(user)}>
<Header />
</UserContext.Provider>
);
const Header = () => (
<>
@jamesseanwright
jamesseanwright / react-props-example.tsx
Created December 21, 2018 12:04
React Props Example
const App: React.FC<AppProps> = ({ user }) => (
<Header user={maybe(user)} />
);
const Header: React.FC<UserProps> = ({ user }) => (
<>
<Logo user={user} />
<Account user={user} />
</>
);