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
// Write an interface here | |
interface Directory { | |
addFile: (name: string) => void; | |
} | |
class DesktopDirectory implements Directory { | |
addFile(name: string) { | |
console.log(`Adding file: ${name}`); | |
} |
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
//a dynamic version where you can send schema from backend via API and form is rendered on frontend | |
import { useId } from 'react'; | |
import { FormSchema } from './form'; | |
import { useForm } from 'react-hook-form'; | |
import { zodResolver } from '@hookform/resolvers/zod'; | |
import * as z from 'zod'; | |
type Props = {}; | |
type FormField = Array<Record<string, string | undefined>>; |
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 { useFormFields } from '../lib/hooks/useFormFields'; | |
type Props = {}; | |
export default function Form({}: Props) { | |
const [fields, handleFieldChange] = useFormFields({ | |
email: '', | |
password: '', | |
}); |
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
function maxVowels(s: string, k: number): number { | |
const vowels = new Set(['a', 'e', 'i', 'o', 'u']); | |
let maxVowels = 0; | |
let currVowels = 0; | |
for(let i = 0; i < k; i++){ | |
if(vowels.has(s[i])) currVowels++ | |
} | |
maxVowels = currVowels; |
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
function parseJSON(jsonString: string): Record<string, any> { | |
const result: Record<string, any> = {}; | |
jsonString = jsonString.substring(1, jsonString.length - 1); | |
const pairs = jsonString.split(','); | |
for (const pair of pairs) { | |
const keyValue = pair.split(':'); | |
let key = keyValue[0].trim(); | |
let value = keyValue[1].trim(); | |
key = key.substring(1, key.length - 1); |
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 function curry(func: Function): Function { | |
return function curried(this: any, ...args: any[]) { | |
if (args.length >= func.length) { | |
return func.apply(this, args); | |
} else { | |
return function (this: any, ...args1: any[]) { | |
return curried.apply(this, args.concat(args1)); | |
}; | |
} | |
}; |
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
/** | |
* Throttling is a technique used to control how many times we allow a function to be executed over time. | |
* When a JavaScript function is said to be throttled with a wait time of X milliseconds, it can only be invoked at most once every X milliseconds. | |
* The callback is invoked immediately and cannot be invoked again for the rest of the wait duration. | |
*/ | |
type ThrottleFunction<T extends any[]> = (...args: T) => any; | |
export default function throttle<T extends any[]>( | |
func: ThrottleFunction<T>, | |
wait: number |
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
function validParentheses(str: string): boolean { | |
const stack: string[] = []; | |
const map: Record<string, string> = { | |
'}': '{', | |
')': '(', | |
']': '[', | |
}; | |
for (let index = 0; index < str.length; index++) { | |
const char = str[index]; | |
if (map[char]) { |
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 MyComponentProps { | |
// Your component props here | |
} | |
const MyComponent = forwardRef<HTMLInputElement, MyComponentProps>( | |
(props, ref) => { | |
// Render your component with the ref attached to the desired element | |
return <input ref={ref} {...props} />; | |
} | |
); |
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
function debounce( | |
func: (...args: any[]) => any, | |
delay: number | |
): (...args: any[]) => void { | |
let timeoutId: ReturnType<typeof setTimeout> | null; | |
return function debouncedFunction(...args: any[]): void { | |
clearTimeout(timeoutId!); | |
timeoutId = setTimeout(() => { |
NewerOlder