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
| // DisposableStack fork | |
| using parentStack = new DisposableStack(); | |
| const childStack = parentStack.use(new DisposableStack()); | |
| // DisposableStack register with dispose callback (should be cached) | |
| const stack = new DisposableStack(); | |
| stack.adopt(resource, (resource) => { | |
| // Dispose resource | |
| }); |
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
| const getFinishedState = async (s: [string], p: Promise<any>) => { | |
| try { | |
| await p; | |
| s[0] = 'resolved'; | |
| } catch (e) { | |
| s[0] = 'rejected'; | |
| // Don't swallow error | |
| return p; | |
| } |
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
| declare const _: unique symbol; | |
| export interface Redacted<out T> extends Symbol { | |
| [_]: T | |
| } | |
| const redactMap = new WeakMap(); | |
| export const init = <const T>(val: T): Redacted<T> => { | |
| const sym = Symbol('<redacted>'); | |
| redactMap.set(sym, val); |
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 { summary, bench, run, do_not_optimize } from 'mitata'; | |
| bench('noop', () => { }); | |
| bench('noop', () => { }); | |
| bench('noop', () => { }); | |
| summary(() => { | |
| { | |
| const fn = (a: number, b: number) => a * 2 + b + Math.random(); | |
| do_not_optimize(fn); |
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
| // Check whether email is somewhat valid (HTML5 input type=email) | |
| /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+?@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/ | |
| // Valid JS identifier | |
| /[$_\p{ID_Start}][$_\u200C\u200D\p{ID_Continue}]*/u |
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 { summary, run, bench, do_not_optimize } from "mitata"; | |
| import { LimitedBytesTransformStream } from "@std/streams"; | |
| const register = (name: string, fn: (list: Request[]) => any) => | |
| bench(name, function* () { | |
| yield { | |
| [0]: () => reqs.map((t) => t.clone()), | |
| bench: fn, | |
| }; | |
| }); |
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
| // Dependencies: ts-pattern, mitata | |
| import { match, P } from 'ts-pattern'; | |
| import { summary, run, bench, type k_state, do_not_optimize } from 'mitata'; | |
| summary(() => { | |
| type Data = | |
| | { type: 'text'; content: string } | |
| | { type: 'img'; src: string }; |
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
| // Dependencies: timsort2, mitata | |
| import { bench, do_not_optimize, run, summary, type k_state } from "mitata"; | |
| import { sort } from "timsort2"; | |
| const tests = [ | |
| function randomInt(n: number): number[] { | |
| const arr: number[] = new Array(n); | |
| for (let i = 0; i < n; i++) { | |
| arr[i] = (Math.random() * 9007199254740992) | 0; |
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 * as st from 'safe-throw'; | |
| import * as retry from 'safe-throw/retry'; | |
| const fetchDomainByNameForUser = retry.untilAsync( | |
| (res) => !st.isErr(res), | |
| async (domainName: string, userId: string) => { | |
| const authResult = await native.tryPromise( | |
| Auth.check({ | |
| action: 'query', | |
| resource: 'domains', |
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
| const std = @import("std"); | |
| const mem = std.mem; | |
| pub const Token = struct { | |
| pub const ArrayList = std.ArrayList(Token); | |
| const StringMap = std.StaticStringMap(Kind); | |
| pub const keywordsMap = StringMap.initComptime(.{ | |
| // Conditions | |
| .{ "if", Kind.keywordIf }, |
NewerOlder