Skip to content

Instantly share code, notes, and snippets.

View nberlette's full-sized avatar
🧱
make, break, patch, repeat ad infinitum

Nicholas Berlette nberlette

🧱
make, break, patch, repeat ad infinitum
View GitHub Profile
@nberlette
nberlette / dbc.wip.pegjs
Created July 11, 2023 05:40
[WIP] DBC Language Grammar
/*!
* DBC Language Grammar, for PEG.js
* ---------------------------------------------------------------------------
* Copyright (c) 2021-2023+ Nicholas Berlette. All rights reserved.
* ---------------------------------------------------------------------------
* More info: [DBC Language Documentation](https://canbus-org.github.io/dbc)
*/
// #region Internal Parser Functions
{
const TYPES_TO_PROPERTY_NAMES = {
@nberlette
nberlette / dbc.pegjs
Last active July 11, 2023 07:09
DBC Language Grammar - PEG.js
/*!
* DBC Language Grammar, for PEG.js
* ---------------------------------------------------------------------------
* Copyright (c) 2021-2023+ Nicholas Berlette. All rights reserved.
* ---------------------------------------------------------------------------
* More info: [DBC Language Documentation](https://canbus-org.github.io/dbc)
*/
// #region Internal Parser Functions
{
const TYPES_TO_PROPERTY_NAMES = {
@nberlette
nberlette / math.ts
Last active June 25, 2023 11:28
TypeScript: type-level arithmetic operations
//
// Type-Level Arithmetic Operations in TypeScript
//
// Implemented by Nicholas Berlette (@nberlette)
// Inspired by the work of many others :)
//
export type ADD<A extends number, B extends number> =
| Internal.ADD<A, B> extends infer R extends number
? [R] extends [never] ? number : R : number;
@nberlette
nberlette / deserialize.ts
Last active July 1, 2023 19:51
🥣 dessert · deserialize and serialize, with type-safety.
import {
assert,
type SerializedValue,
TypedArray,
TypedArrayTypes,
} from "./helpers.ts";
import { StructuredReader, StructuredSyncReader } from "./reader.ts";
const binaryInputTypes = [ArrayBuffer, Uint8Array] as const;
const streamInputTypes = [ReadableStream, ...binaryInputTypes] as const;
@nberlette
nberlette / murmur128.ts
Created June 23, 2023 17:31
[TS] murmur128
/**
* MurmurHash3 128-bit implementation in TypeScript. Supports a custom seed for
* its first parameter. For another variant that assumes a default seed of 0,
* see {@link murmur128} instead.
*
* @param seed The seed to use for this hash.
* @param keys The keys to hash.
* @returns 128-bit hash as a `bigint`.
*
* Based on bryc's JavaScript hash functions:
@nberlette
nberlette / long.ts
Last active June 28, 2023 05:52
Math.long - based on bryc's implementation
declare interface LongStatic {
readonly prototype: Long;
new (low: number, high: number): Long;
fromInt(value: number): Long;
fromBigInt(value: bigint): Long;
fromString(value: string, radix?: number): Long;
fromNumber(value: number): Long;
fromBits(lowBits: number, highBits: number): Long;
@nberlette
nberlette / deno_install.sh
Created June 5, 2023 04:30 — forked from LukeChannings/deno_install.sh
Deno installation script
#!/bin/sh
# A modification of the standard Deno installation script (https://deno.land/install.sh)
# updated to support downloading a Linux arm64 binary from LukeChannings/deno-arm64
set -e
if ! command -v unzip >/dev/null; then
echo "Error: unzip is required to install Deno (see: https://github.com/denoland/deno_install#unzip-is-required)." 1>&2
exit 1
fi
@nberlette
nberlette / dasync.ts
Last active July 7, 2023 12:13
Dasync: conditionally trigger the event loop; inspired by deasync (obviously)
// "deasync" by Nicholas Berlette - https://github.com/nberlette
// Inspired by deasync for node.js. Obviously, this is not a
// piece of software you should ever consider using in production.
// (....or even in development for that matter)
export type Conditional = () => boolean;
import { SpinWait } from "./utils.ts";
// Having `null` as the superclass effectively disables both the
@nberlette
nberlette / types.d.ts
Last active May 31, 2023 23:51
TS utilities for asserting structural equality between types
// Adapted from https://stackoverflow.com/a/53808212/10873797
export type Equal<T, U> = (<G>() => G extends T ? 1 : 2) extends
(<G>() => G extends U ? 1 : 2) ? true
: false;
export type AssertEqual<
Expected extends $,
Got extends $$,
$ = [Equal<Got, Expected>] extends [true] ? Expected
: ([Expected] extends [Got] ? never : Got),
@nberlette
nberlette / comparable.ts
Last active July 1, 2023 18:17
[TS] portable fork of @esfx/equatable, for deno.
import { isFunction, isMissing } from "./guards.ts";
/**
* Represents a value that can compare itself relationally with another value.
*/
export interface Comparable {
/**
* Compares this value with another value, returning a value indicating one of the following conditions:
*
* - A negative value indicates this value is lesser.