Skip to content

Instantly share code, notes, and snippets.

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;
}
}
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);
}
}
const addZerosCount = ({ str, zeroCount }: { str: string; zeroCount: number }) => `${str}${zeroCount || ''}`
const replaceZeros = (strIncludingZeros: string) =>
addZerosCount(
strIncludingZeros.split('').reduce((acc, ch) => (ch === '0' ? { ...acc, zeroCount: (acc.zeroCount += 1) } : { str: `${addZerosCount(acc)}${ch}`, zeroCount: 0 }), {
str: '',
zeroCount: 0,
})
)
@ChrisDobby
ChrisDobby / maxSubArray.ts
Last active January 2, 2023 10:10
Given an array of integers arr and an integer n, return a subarray of arr of length n where the sum is the largest. Make sure you maintain the order of the original array
const maxSubArray = (arr: number[], len: number) =>
arr.length < len
? []
: arr
.map((_, index, a) => a.slice(index, index + len))
.filter(a => a.length === len)
.sort((arr1, arr2) => Math.max(...arr2) - Math.max(...arr1))[0]
@ChrisDobby
ChrisDobby / sumEveryOther.ts
Created January 9, 2023 07:41
Given a number, sum every second digit in that number.
const sumEveryOther = (num: number) =>
num
.toString()
.split('')
.reduce((total, digit, index) => ((index + 1) % 2 === 0 ? total + parseInt(digit) : total), 0)
@ChrisDobby
ChrisDobby / missingBits.ts
Created January 25, 2023 09:19
You are given a list of positive integers which represents some range of integers which has been truncated. Find the missing bits, insert ellipses to show that that part has been truncated, and print it. If the consecutive values differ by exactly two, then insert the missing value.
const fillGap = (num: number, prev: number) => (num - prev === 2 ? `${num - 1},${num}` : `...,${num}`)
const missingBits = (numbers: number[]) =>
`[${numbers
.map((num, index, arr) => ({ num, prev: arr[index - 1] }))
.map(({ num, prev }) => (num - prev === 1 ? num : fillGap(num, prev)))
.join(',')}]`
@ChrisDobby
ChrisDobby / generateArrays.ts
Created January 31, 2023 19:19
Given a positive integer, generate an array in which every element is an array that goes from 1 to the index of that array.
const generateArrays = (n: number) => Array.from({ length: n }, (_, index) => Array.from({ length: index + 1 }, (_, innerIndex) => innerIndex + 1))