This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface Success<T> { | |
type: 'success'; | |
data: T; | |
} | |
interface Failure<E> { | |
type: 'failure'; | |
error: E; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class FullTextSearch<T> { | |
// token -> (item -> frequência do token no item) | |
private index: Map<string, Map<T, number>> = new Map(); | |
constructor(items: T[], private textExtractor: (item: T) => string) { | |
for (const item of items) { | |
this.addToIndex(item); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Eagerly evaluated | |
export default class Pipe<T> { | |
constructor(private readonly _value: T) { } | |
to<U>(fn: (value: T) => U) { | |
return new Pipe(fn(this._value)); | |
} | |
get value() { | |
return this._value; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useEffect, useState } from "react"; | |
import { Input } from "./ui/input"; | |
type Props = { | |
suggestions: string[]; | |
}; | |
export function AutosuggestionInput({ suggestions }: Props) { | |
const [input, setInput] = useState(""); | |
const [suggestion, setSuggestion] = useState(""); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type Result<T> = readonly [T, null] | readonly [null, Error] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useState } from "react"; | |
export function useArrayState<T>(initialState: T[] = []) { | |
const [state, setState] = useState<T[]>(initialState) | |
const add = (newValue: T) => { | |
setState((currentState) => [...currentState, newValue]) | |
} | |
const remove = (index: number) => { | |
setState( | |
(currentState) => currentState.filter((_, i) => i !== index) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export function download(blob: Blob, fileName: string) { | |
const blobURL = URL.createObjectURL(blob) | |
const link = document.createElement('a') | |
link.href = blobURL | |
link.download = fileName | |
link.click() | |
URL.revokeObjectURL(blobURL) | |
link.remove() | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export function parseCSV<T = Record<string, string>>({ | |
csv, | |
headers, | |
delimiter = "," | |
}: { | |
csv: string | |
delimiter?: string | |
headers?: string[] | |
}) { | |
const header = headers ?? csv.slice(0, csv.indexOf("\n")).split(delimiter) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Led { | |
private: | |
int _pin; | |
int _state; | |
public: | |
Led(int pin, int state = LOW){ | |
_pin = pin; | |
_state = state; | |
pinMode(_pin, OUTPUT); | |
digitalWrite(_pin, _state); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export default class AsyncQueue<T> { | |
private _items: Array<T> = [] | |
private _pending: ((value: T) => void) | null = null; | |
private wait() { | |
return new Promise<T>(resolve => { this._pending = resolve }) | |
} | |
enqueue(item: T) { | |
if (!this._pending) { | |
this._items.push(item) |
NewerOlder