Skip to content

Instantly share code, notes, and snippets.

@ChrisDobby
ChrisDobby / Visitor.fs
Created August 28, 2015 09:31
Visitor in F#
namespace Visitor
type IPrescription =
abstract member Accept : IPrescriptionVisitor -> unit
and IPrescriptionVisitor =
abstract member Visit : PointInTime -> unit
abstract member Visit : Infusion -> unit
and PointInTime() =
@ChrisDobby
ChrisDobby / classNameFunc.js
Last active June 6, 2018 10:18
Creates a css class on the fly from an object
const ClassName = () => {
const createdClasses = [];
const stringToCss = str => str.split('').map(c => c.charCodeAt(0) >= 97
? c
: `-${String.fromCharCode(c.charCodeAt(0) + 32)}`).join('');
const cssString = css => Object.keys(css).map(key => `${stringToCss(key)}:${css[key]}`).join(';');
const addStyle = (name, css) => {
@ChrisDobby
ChrisDobby / lighthouse.js
Created January 23, 2019 11:55
Runs lighthouse against a web application - successful if everything scores 0.9 or above
require('colors');
const lighthouse = require('lighthouse');
const chromeLauncher = require('chrome-launcher');
const launchAndRun = (url, opts, config = null) =>
chromeLauncher.launch({ chromeFlags: opts.chromeFlags })
.then(chrome => {
opts.port = chrome.port;
return lighthouse(url, opts, config).then(results => chrome.kill().then(() => results.lhr));
});
@ChrisDobby
ChrisDobby / widthAndHeight.jsx
Created July 28, 2019 13:06
React component to display the width and height of the window
import React from "react";
function WidthAndHeight() {
const [width, setWidth] = React.useState(window.innerWidth);
const [height, setHeight] = React.useState(window.innerHeight);
React.useEffect(() => {
window.addEventListener("resize", updateWidthAndHeight);
return () => window.removeEventListener("resize", updateWidthAndHeight);
});
@ChrisDobby
ChrisDobby / canvasDraw.jsx
Last active March 9, 2023 09:23
React component to redraw a canvas when resized
import React from "react";
const scaleWidth = 500;
const scaleHeight = 500;
function draw(canvas, scaleX, scaleY) {
const context = canvas.getContext("2d");
context.scale(scaleX, scaleY);
context.clearRect(0, 0, canvas.clientWidth, canvas.clientHeight);
@ChrisDobby
ChrisDobby / hookRef.tsx
Created July 30, 2019 19:58
Example of React component in Typescript using the useRef hook
import * as React from "react";
function HookRef() {
const divRef = React.useRef<HTMLDivElement>(null);
React.useEffect(() => {
if (divRef.current) {
console.log(`hookRef div width: ${divRef.current.clientWidth}`);
}
}, []);
@ChrisDobby
ChrisDobby / createRef.tsx
Created July 30, 2019 20:00
Example of React component in Typescript using createRef
import * as React from "react";
class CreateRef extends React.Component {
divRef = React.createRef<HTMLDivElement>();
componentDidMount() {
if (this.divRef.current) {
console.log(`createRefRef div width: ${this.divRef.current.clientWidth}`);
}
}
@ChrisDobby
ChrisDobby / callbackRef.tsx
Created July 30, 2019 20:02
Example of React component in Typescript using a callback to set a ref
import * as React from "react";
class CallbackRef extends React.Component {
divRef: HTMLDivElement | null = null;
setDivRef = (element: HTMLDivElement) => {
this.divRef = element;
};
componentDidMount() {
@ChrisDobby
ChrisDobby / forwardRef.tsx
Created July 30, 2019 20:03
Example of React component in Typescript using forwarding refs
import * as React from "react";
const Forward = React.forwardRef((props, ref: React.Ref<HTMLDivElement>) => (
<div ref={ref} style={{ width: "100%", height: "30px", backgroundColor: "green" }} />
));
function ForwardRef() {
const divRef = React.useRef<HTMLDivElement>(null);
React.useEffect(() => {
function evenWord(word) {
let charCounts = [...word].reduce(
(charCount, char) => ({
...charCount,
[char]: charCount[char] ? charCount[char] + 1 : 1,
}),
{},
);
return Object.entries(charCounts).filter(([_, count]) => count % 2 !== 0).length;
}