A decentralized music tracking and discovery platform built on AT Protocol 馃幍
at main 78 lines 2.2 kB view raw
1/** 2 * GENERATED CODE - DO NOT MODIFY 3 */ 4 5import type { ValidationResult } from "@atp/lexicon"; 6 7export type OmitKey<T, K extends keyof T> = { 8 [K2 in keyof T as K2 extends K ? never : K2]: T[K2]; 9}; 10 11export type $Typed<V, T extends string = string> = V & { $type: T }; 12export type Un$Typed<V extends { $type?: string }> = OmitKey<V, "$type">; 13 14export type $Type<Id extends string, Hash extends string> = Hash extends "main" 15 ? Id 16 : `${Id}#${Hash}`; 17 18function isObject<V>(v: V): v is V & object { 19 return v != null && typeof v === "object"; 20} 21 22function is$type<Id extends string, Hash extends string>( 23 $type: unknown, 24 id: Id, 25 hash: Hash, 26): $type is $Type<Id, Hash> { 27 return hash === "main" 28 ? $type === id 29 // $type === `${id}#${hash}` 30 : typeof $type === "string" && 31 $type.length === id.length + 1 + hash.length && 32 $type.charCodeAt(id.length) === 35 /* '#' */ && 33 $type.startsWith(id) && 34 $type.endsWith(hash); 35} 36 37export type $TypedObject<V, Id extends string, Hash extends string> = V extends 38 { 39 $type: $Type<Id, Hash>; 40 } ? V 41 : V extends { $type?: string } 42 ? V extends { $type?: infer T extends $Type<Id, Hash> } ? V & { $type: T } 43 : never 44 : V & { $type: $Type<Id, Hash> }; 45 46export function is$typed<V, Id extends string, Hash extends string>( 47 v: V, 48 id: Id, 49 hash: Hash, 50): v is $TypedObject<V, Id, Hash> { 51 return isObject(v) && "$type" in v && is$type(v.$type, id, hash); 52} 53 54export function maybe$typed<V, Id extends string, Hash extends string>( 55 v: V, 56 id: Id, 57 hash: Hash, 58): v is V & object & { $type?: $Type<Id, Hash> } { 59 return ( 60 isObject(v) && 61 ("$type" in v ? v.$type === undefined || is$type(v.$type, id, hash) : true) 62 ); 63} 64 65export type Validator<R = unknown> = (v: unknown) => ValidationResult<R>; 66export type ValidatorParam<V extends Validator> = V extends Validator<infer R> 67 ? R 68 : never; 69 70/** 71 * Utility function that allows to convert a "validate*" utility function into a 72 * type predicate. 73 */ 74export function asPredicate<V extends Validator>(validate: V) { 75 return function <T>(v: T): v is T & ValidatorParam<V> { 76 return validate(v).success; 77 }; 78}