Skip to content

Instantly share code, notes, and snippets.

View Visitor.fs
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 Jun 6, 2018
Creates a css class on the fly from an object
View classNameFunc.js
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 Jan 23, 2019
Runs lighthouse against a web application - successful if everything scores 0.9 or above
View lighthouse.js
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 Jul 28, 2019
React component to display the width and height of the window
View widthAndHeight.jsx
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 Jun 7, 2020
React component to redraw a canvas when resized
View canvasDraw.jsx
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 Jul 30, 2019
Example of React component in Typescript using the useRef hook
View hookRef.tsx
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 Jul 30, 2019
Example of React component in Typescript using createRef
View createRef.tsx
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 Jul 30, 2019
Example of React component in Typescript using a callback to set a ref
View callbackRef.tsx
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 Jul 30, 2019
Example of React component in Typescript using forwarding refs
View forwardRef.tsx
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(() => {
View evenWord.js
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;
}
You can’t perform that action at this time.