From 16944e93b4031ed6d9c6e2b7e0f9e7ada04a0a3e Mon Sep 17 00:00:00 2001 From: Alec Helmturner Date: Sat, 25 Nov 2023 19:48:39 -0600 Subject: [PATCH] chore: cleanup (#88) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## PR Checklist - [ ] Addresses an existing open issue: fixes #000 - [ ] That issue was marked as [`status: accepting prs`](https://github.com/trpc/tupleson/issues?q=is%3Aopen+is%3Aissue+label%3A%22status%3A+accepting+prs%22) - [ ] Steps in [CONTRIBUTING.md](https://github.com/trpc/tupleson/blob/main/.github/CONTRIBUTING.md) were taken ## Overview --- src/async/asyncHelpers.ts | 31 ---------------- src/async/iterableUtils.ts | 75 -------------------------------------- src/iterableTypes.ts | 15 -------- src/sync/syncTypes.ts | 2 - src/tsonAssert.ts | 41 --------------------- 5 files changed, 164 deletions(-) delete mode 100644 src/async/asyncHelpers.ts diff --git a/src/async/asyncHelpers.ts b/src/async/asyncHelpers.ts deleted file mode 100644 index 2513b77..0000000 --- a/src/async/asyncHelpers.ts +++ /dev/null @@ -1,31 +0,0 @@ -export async function* mapIterable( - iterable: AsyncIterable, - fn: (v: T) => TValue, -): AsyncIterable { - for await (const value of iterable) { - yield fn(value); - } -} - -export async function reduceIterable< - T, - TInitialValue extends Promise = Promise, - TKey extends PropertyKey | bigint = bigint, - TKeyFn extends (prev?: TKey) => TKey = (prev?: TKey) => TKey, ->( - iterable: Iterable, - fn: (acc: Awaited, v: T, i: TKey) => Awaited, - initialValue: TInitialValue = Promise.resolve() as TInitialValue, - incrementKey: TKeyFn = ((prev?: bigint) => - prev === undefined ? 0n : prev + 1n) as TKeyFn, -): Promise> { - let acc = initialValue; - let i = incrementKey(); - - for await (const value of iterable) { - acc = fn(await acc, value, i); - i = incrementKey(i); - } - - return Promise.resolve(acc); -} diff --git a/src/async/iterableUtils.ts b/src/async/iterableUtils.ts index 8095410..4d099d1 100644 --- a/src/async/iterableUtils.ts +++ b/src/async/iterableUtils.ts @@ -156,93 +156,18 @@ export interface AsyncIterableEsque { [Symbol.asyncIterator](): AsyncIterator; } -export function isAsyncIterableEsque( - maybeAsyncIterable: unknown, -): maybeAsyncIterable is AsyncIterableEsque { - return ( - !!maybeAsyncIterable && - (typeof maybeAsyncIterable === "object" || - typeof maybeAsyncIterable === "function") && - Symbol.asyncIterator in maybeAsyncIterable - ); -} - export interface IterableEsque { [Symbol.iterator](): Iterator; } -export function isIterableEsque( - maybeIterable: unknown, -): maybeIterable is IterableEsque { - return ( - !!maybeIterable && - (typeof maybeIterable === "object" || - typeof maybeIterable === "function") && - Symbol.iterator in maybeIterable - ); -} type SyncOrAsyncGeneratorFnEsque = AsyncGeneratorFnEsque | GeneratorFnEsque; -export function isMaybeAsyncGeneratorFn( - maybeAsyncGeneratorFn: unknown, -): maybeAsyncGeneratorFn is SyncOrAsyncGeneratorFnEsque { - return ( - typeof maybeAsyncGeneratorFn === "function" && - ["AsyncGeneratorFunction", "GeneratorFunction"].includes( - maybeAsyncGeneratorFn.constructor.name, - ) - ); -} - export type GeneratorFnEsque = () => Generator; -export function isGeneratorFnEsque( - maybeGeneratorFn: unknown, -): maybeGeneratorFn is GeneratorFnEsque { - return ( - typeof maybeGeneratorFn === "function" && - maybeGeneratorFn.constructor.name === "GeneratorFunction" - ); -} - export type AsyncGeneratorFnEsque = () => AsyncGenerator; -export function isAsyncGeneratorFnEsque( - maybeAsyncGeneratorFn: unknown, -): maybeAsyncGeneratorFn is AsyncGeneratorFnEsque { - return ( - typeof maybeAsyncGeneratorFn === "function" && - maybeAsyncGeneratorFn.constructor.name === "AsyncGeneratorFunction" - ); -} - export type PromiseEsque = PromiseLike; -export function isPromiseEsque( - maybePromise: unknown, -): maybePromise is PromiseEsque { - return ( - !!maybePromise && - typeof maybePromise === "object" && - "then" in maybePromise && - typeof maybePromise.then === "function" - ); -} - export type ThunkEsque = () => unknown; -export function isThunkEsque(maybeThunk: unknown): maybeThunk is ThunkEsque { - return ( - !!maybeThunk && typeof maybeThunk === "function" && maybeThunk.length === 0 - ); -} - -export type Thunkable = - | AsyncIterableEsque - | IterableEsque - | PromiseEsque - | SyncOrAsyncGeneratorFnEsque - | ThunkEsque; - -export type MaybePromise = Promise | T; diff --git a/src/iterableTypes.ts b/src/iterableTypes.ts index ebf6b6b..0c33b88 100644 --- a/src/iterableTypes.ts +++ b/src/iterableTypes.ts @@ -13,21 +13,6 @@ /* eslint-disable @typescript-eslint/no-empty-interface */ -/** - * A stronger type for Iterator - */ -export interface Iterator - extends globalThis.Iterator {} - -/** - * A stronger type for AsyncIterator - */ -export interface AsyncIterator< - T = unknown, - TReturn = unknown, - TNextArg = unknown, -> extends globalThis.AsyncIterator {} - /** * A stronger type for Generator */ diff --git a/src/sync/syncTypes.ts b/src/sync/syncTypes.ts index b9503f0..447e275 100644 --- a/src/sync/syncTypes.ts +++ b/src/sync/syncTypes.ts @@ -68,8 +68,6 @@ export interface TsonTypeTesterCustom { test: (v: unknown) => boolean; } -export type TsonTypeTester = TsonTypeTesterCustom | TsonTypeTesterPrimitive; - export type TsonType< /** * The type of the value diff --git a/src/tsonAssert.ts b/src/tsonAssert.ts index efff045..c9ebc78 100644 --- a/src/tsonAssert.ts +++ b/src/tsonAssert.ts @@ -1,44 +1,3 @@ -export const asserts = Symbol("asserted"); -export type Not = T extends true ? false : true; - -const secret = Symbol("secret"); -type Secret = typeof secret; - -export type IsNever = [T] extends [never] ? true : false; -export type IsAny = [T] extends [Secret] ? Not> : false; -export type IsUnknown = [unknown] extends [T] ? Not> : false; -/** - * The more I think about these semantics, the less they make sense. - * What is this API, really? What is the goal? - * Is it to provide a way to assert that a value is of a certain type? I think - * this only makes sense in a few limited cases. - * - * At what point in the pipeline would this occur? Would this happen for all - * values? If so, well... you've asserted that your parser only handles - * the type you're asserting. I don't know why you'd want to predetermine - * that at the time of configuration. Alternatively, if this isn't called - * for each value, then one of the following must be true: - * - * - you have also specified, somehow, *when* to apply this assertion - * - it is part of an array of operations that are attempted in order, - * and the first one that succeeds is used - * - * The first point could easily be accomplished by a conditional inside the - * assertion function (e.g. for the number guard, If not of type 'number' - * then it's definitely not NaN)... so no need for anything special like an - * array of type handler keys associated with the assertion. - * - * The second point is an option, since that's how custom tson types work. - * But is there really any utility to that? We're not zod validating... - * we're marshalling. We assume the type layer is accurate, without - * enforcing it. If we want to integrate runtime type validation, that - * seems like a feature request, potentially warranting it's own API. - * - * Ultimately, I feel like this functionality is easily served by a simple - * assertion function that throws for invalid values. For most (all?) except - * for the unknown object guard they would come first in the array, while - * the unknown object guard would come last. - */ interface TsonGuardBase { key: string; }