A list of naming conventions based on this excelent article:
https://betterprogramming.pub/useful-tips-for-naming-your-variables-8139cc8d44b5
const somethingCount // itemsCount (preferable)
const numberOfSomething // numberOfItems
A list of naming conventions based on this excelent article:
https://betterprogramming.pub/useful-tips-for-naming-your-variables-8139cc8d44b5
const somethingCount // itemsCount (preferable)
const numberOfSomething // numberOfItems
/** | |
* Picks a random item from a list. | |
* | |
* For example: | |
* | |
* pick(['a', 'b', 'c'], [0.7, 0.2]) | |
* // outputs 'a' with 70% chance | |
* // outputs 'b' with 20% chance | |
* // outputs 'c' with 10% chance (the rest) | |
* |
/** | |
* Sorts a list list of records by fields. | |
* | |
* | |
* Example 1: | |
* | |
* const items = [ | |
* { currency: 'EUR', amount: 101.45 }, | |
* { currency: 'EUR', amount: 33.23 }, | |
* { currency: 'EUR', amount: 55.00 }, |
import { Db } from 'mongodb' | |
// Increments the `count` field of the `codes` collection and returns it. | |
// This function is "thread safety". | |
// | |
// It's important to update and find the document at the same time. | |
// Otherwise a "race condition" may arise. | |
const newCode = async (db: Db, name: string) => { | |
const { value: { count } } = await db.collection('codes').findOneAndUpdate({ name }, { $inc: { count: 1 } }, { | |
upsert: true, |
extension String { | |
func chunks(_ size: Int) -> [String] { | |
stride(from: 0, to: self.count, by: size).map { | |
let i0 = self.index(self.startIndex, offsetBy: $0) | |
let i1 = self.index(self.startIndex, offsetBy: min($0 + size, self.count)) | |
return "\(self[i0..<i1])" | |
} | |
} | |
} |
extension String { | |
func leftTrimmed() -> Self { | |
guard let i = self.firstIndex(where: { !$0.isWhitespace }) else { | |
return "" | |
} | |
return "\(self[i...])" | |
} | |
func rightTrimmed() -> Self { | |
guard let i = self.lastIndex(where: { !$0.isWhitespace }) else { |
extension Array where Element: Collection { | |
// Calculates the size of a matrix. | |
func size() -> (cols: Int, rows: Int) { | |
let numRows = self.count | |
let numCols = self.reduce(0, { Swift.max($0, $1.count) }) | |
return (numCols, numRows) | |
} | |
// Transposes a matrix. | |
// |
import { useCallback, useMemo, useState } from 'react'; | |
const useValidator = <T extends Record<string, string>>(validator: () => T) => { | |
const [errors, setErrors] = useState<Partial<T>>({}) | |
return useMemo(() => ({ | |
validate: () => { | |
setErrors(validator()) | |
return Object.values(errors).every((x) => !x) | |
}, |
import { useCallback, useMemo, useEffect } from 'react' | |
export const useBreakpoints = <T extends Record<string, number>>(breakpoints: T): keyof T | undefined => { | |
const searchBreakpoint = useCallback((breakpoints: { key: string, value: number }[]) => { | |
return breakpoints.find((x) => window.innerWidth < x.value)?.key | |
}, []) | |
const entries = useMemo(() => { | |
return Object | |
.entries(breakpoints) |