This file contains 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
<?php | |
namespace App; | |
use Illuminate\Support\Arr; | |
use Illuminate\Support\Str; | |
use Illuminate\Database\Eloquent\Model; | |
use Illuminate\Database\Eloquent\Builder; | |
use Illuminate\Database\Eloquent\Relations\Relation; | |
use Illuminate\Database\Eloquent\Relations\BelongsTo; |
This file contains 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 {Multipart, MultipartFile, MultipartValue} from '@fastify/multipart'; | |
import {FastifyRequest} from 'fastify'; | |
function isMultipartFile(m: Multipart): m is MultipartFile { | |
return !!m['toBuffer']; | |
} | |
async function getValueOrBuffer(m: Multipart): Promise<string | Buffer> { | |
if (isMultipartFile(m)) { | |
return m.toBuffer(); |
This file contains 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 repeat(x: number, n: number): number[] { | |
return Array(n).fill(x); | |
} | |
/** | |
* Reimplemented Python's itertools.combinations_with_replacement | |
* https://docs.python.org/3/library/itertools.html#itertools.combinations_with_replacement | |
*/ | |
export function* combinationsWithReplacement<T>(pool: T[], r: number) { | |
const n = pool.length; |
This file contains 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 MapFn<T> = (x: T) => unknown; | |
type MapSchema<T> = { [K in keyof T]?: MapFn<T[K]> | MapSchema<T[K]> }; | |
type MapReturnType<T, TMap extends MapSchema<T>> = { | |
[K in keyof T]: TMap[K] extends Function | |
? ReturnType<Extract<TMap[K], MapFn<T[K]>>> | |
: MapReturnType<T[K], TMap[K]>; | |
}; | |
function isMapFn<T = any>(x: unknown): x is MapFn<T> { | |
return typeof x === "function"; |
This file contains 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
/** | |
* Reimplemented python's `random.choices` function | |
* https://github.com/python/cpython/blob/3.9/Lib/random.py#L473 | |
*/ | |
export function randomChoice<T>(population: T[], weights: number[], random: () => number): T { | |
let n = population.length; | |
let cum_weights = []; | |
for (let i = 0; i < weights.length; i++) { | |
cum_weights[i] = i === 0 ? weights[i] : weights[i] + cum_weights[i - 1]; | |
} |
This file contains 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
enum QuerySelectors { | |
Eq = "$eq", | |
Gt = "$gt", | |
Gte = "$gte", | |
Lt = "$lt", | |
Lte = "$lte", | |
Ne = "$ne" | |
} | |
enum LogicalOperators { |
This file contains 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 ReduceResult<T> { | |
done: true; | |
value: T; | |
} | |
export function reduceLargeArray<T, R>( | |
arr: T[], | |
processEachFn: (acc: R, x: T) => ReduceResult<R> | R, | |
initialValue: R, | |
onCompleteFn: (r: R) => void |
This file contains 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 exponentialRandomBackoff( | |
initialIntervalMillis: number, | |
multiplier: number, | |
randomizationFactor: number | |
) { | |
checkInterval(initialIntervalMillis); | |
checkMultiplier(multiplier); | |
checkRandomizationFactor(randomizationFactor); | |
return (attemptsMade: number) => { | |
const interval = Math.round(Math.pow(multiplier, attemptsMade) * initialIntervalMillis); |
This file contains 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 formatMoney(value: any, thousandSeparator: string | false = false, fractionDigits = 2): string { | |
let number = Number(value); | |
if (isNaN(number)) { | |
number = 0; | |
} | |
// Number#toFixed will throw runtime exception if fractionDigits isn't within 0-100 range | |
if (fractionDigits < 0 || fractionDigits > 100) { | |
fractionDigits = 0; | |
} |
This file contains 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 {Directive, Input, HostListener} from '@angular/core'; | |
@Directive({ | |
selector: 'img[fallback]' | |
}) | |
export class ImageFallbackDirective { | |
private _fallback = 'assets/images/404_image.png'; | |
@Input() set fallback(value: string) { | |
if (value && value.length > 0) { |
NewerOlder