char/avril

(wip) why'd you have to go and make things so complicaaaaated?

git clone https://git.t4t.associates/char/avril

Charlotte Somport web extension to typescript44fe2c6

main
1.9 KiB65 linesraw
1import {
2  Error,
3  ObjectCreate,
4  ObjectSetPrototypeOf,
5  String,
6  StringPrototypeToWellFormed,
7  TypeError,
8  uncurryThis,
9  WeakMapPrototypeGet,
10} from "ext:core/primordials";
11
12const charCode = uncurryThis(String.prototype.charCodeAt);
13
14export function string(value: unknown): string {
15  if (typeof value === "symbol") {
16    throw new TypeError("Cannot convert a Symbol to a string");
17  }
18  return String(value);
19}
20export function usv(value: unknown): string {
21  return StringPrototypeToWellFormed(string(value));
22}
23export function byteString(value: unknown): string {
24  const result = string(value);
25  for (let i = 0; i < result.length; i++) {
26    if (charCode(result, i) > 255) {
27      throw new TypeError("Expected a ByteString");
28    }
29  }
30  return result;
31}
32export function dictionary(value: unknown): Record<PropertyKey, unknown> {
33  if (value === undefined || value === null) return ObjectCreate(null);
34  if (typeof value !== "object" && typeof value !== "function") {
35    throw new TypeError("Expected an options dictionary");
36  }
37  return value as Record<PropertyKey, unknown>;
38}
39export function isObject(
40  value: unknown,
41): value is Record<PropertyKey, unknown> {
42  return value !== null &&
43    (typeof value === "object" || typeof value === "function");
44}
45export function required(count: number, minimum: number): void {
46  if (count < minimum) {
47    throw new TypeError(`Expected at least ${minimum} argument(s)`);
48  }
49}
50export function state<K extends WeakKey, V>(
51  map: WeakMap<K, V>,
52  receiver: unknown,
53): NonNullable<V> {
54  const value = WeakMapPrototypeGet(map, receiver);
55  if (!value) throw new TypeError("Illegal invocation");
56  return value;
57}
58export function array<T = unknown>(): T[] {
59  const value: T[] = [];
60  ObjectSetPrototypeOf(value, null);
61  return value;
62}
63export function missing(feature: string): never {
64  throw new Error(`not yet implemented: ${feature}`);
65}