Skip to content

Instantly share code, notes, and snippets.

View rain84's full-sized avatar
🐳
Believe in yourself!

rain84 rain84

🐳
Believe in yourself!
View GitHub Profile
export const compose = (...fns) => (...args) =>
fns.reduceRight((args, fn) => fn(...(Array.isArray(args) ? args : [args])), args)
export const composeRecursive = (...fns) => {
const last = fns.pop()
if (!fns.length) return (...args) => last(...args)
const prev = fns.pop()
fns.push((...args) => {
let res = last(...args)
@rain84
rain84 / max-sub-array.airbnb.test.ts
Last active March 8, 2021 14:23
Question from "Airbnb": Given an array of integers, return the largest range, inclusive, of integers that are all included in the array. For example, given the array [9, 6, 1, 3, 8, 10, 12, 11], return (8, 12) since 8, 9, 10, 11, and 12 are all in the array.
import { maxSubArray, addToSet } from './max-sub-array.airbnb'
describe('MaxSubArray', () => {
let arr = []
test('should return on empty array', () => {
expect(maxSubArray(arr)).toBeUndefined()
})
test('should work on array.length === 1', () => {
@rain84
rain84 / equalChunks.test.ts
Created March 8, 2021 19:05
Question from F8. Given a list of strictly positive integers, partition the list into 3 contiguous partitions which each sum up to the same value. If not possible, return null. example, given the following list: [3, 5, 8, 0, 8] Return the following 3 partitions: [ [3, 5], [8, 0], [8] ] Which each add up to 8.
import { equalChunks, getChunkIndexes, extractByIndexes, ResultType } from './equalChunks'
describe('equalChunks', () => {
const helpers = {
sum: (arr: number[]) => arr.reduce((a, b) => a + b),
have3equalChunks: (arr: ResultType) => arr.every((item) => helpers.sum(item) === helpers.sum(arr[0])),
}
test('should return null on empty array', () => {
expect(typeof equalChunks).toBe('function')
import { asyncParallel } from './asyncParallel'
const fakedFetch = (timeout: number) => new Promise((resolve) => setTimeout(() => resolve(timeout), timeout * 1000))
describe('fakedFetch', () => {
test('should retun Promise', () => {
expect(fakedFetch(1) instanceof Promise).toBeTruthy()
})
test('should asyncronously retun value after delay', async () => {
@rain84
rain84 / component.js
Last active March 18, 2021 15:52
React-tree invalidation with <ErrorBoundary />
import {useState, useEffect, Component} from 'react'
const ErrorBlock = ({error: e, onReload}) => (
<div>
<div role="alert">
There was an rejected: {e?.message && <pre style={{whiteSpace: 'normal'}}>{e.message}</pre>}
</div>
<button onClick={onReload}>Reload page</button>
</div>
)