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 / 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() {
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;
}
import React from "react";
import { Dropdown, Checkbox, RadioButtons, FormElement } from "@emisgroup/ui-kit-react";
const testData = [
{
text: "Item one",
isChecked: false,
radioItem: "one",
},
{
import React from "react";
import { Tabs, ButtonGroup, Button } from "@emisgroup/ui-kit-react";
const TabComponent = () => {
const tabsList = [{ text: "Tab1" }, { text: "Tab2" }, { text: "Tab3" }];
const [selectedTab, setSelectedTab] = React.useState(0);
return (
<>
<Tabs tabsList={tabsList} activeTab={selectedTab} onTabSelect={setSelectedTab} />
import * as React from "react";
import { render, RenderResult } from "@testing-library/react";
import fetch from "jest-fetch-mock";
import { useGetAnnotation } from "./useGetAnnotation";
import { act } from "react-dom/test-utils";
fetch.enableMocks();
const presignedUrl = "https:/a-presigned-url";
const testXml = btoa("<something/>");
function longestPrefix(strings: string[]) {
const { 0: first, [strings.length - 1]: last } = [...strings].sort();
let prefixLength = -1;
for (let i = 0; i < Math.min(first.length, last.length); i++) {
if (first[i] === last[i]) {
prefixLength = i;
} else {
break;
}
}