Skip to content

Instantly share code, notes, and snippets.

function groupAnagrams(anagrams: string[]) {
const grouped: { [name: string]: string[] } = {};
for(const anagram of anagrams) {
const sorted = anagram.split('').sort().join('');
if(!grouped[sorted]) {
grouped[sorted] = [anagram];
} else {
grouped[sorted].push(anagram);
}
}
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;
}
}
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/>");
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 React from "react";
import { Dropdown, Checkbox, RadioButtons, FormElement } from "@emisgroup/ui-kit-react";
const testData = [
{
text: "Item one",
isChecked: false,
radioItem: "one",
},
{
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;
}
@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(() => {
@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 / 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 / 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}`);
}
}, []);