From f58d5f20a570392398e99a5778dcf35028d77d4d Mon Sep 17 00:00:00 2001 From: Paul Elliott Date: Thu, 30 Nov 2023 13:43:49 -0500 Subject: [PATCH] refactor(functional): move pipe function, rename old pipe to flow --- src/core/remote/client.ts | 11 ++--- src/io/import/processors/restoreStateFile.ts | 3 +- src/utils/functional.ts | 42 ++++++++++++++++++++ src/utils/index.ts | 37 ----------------- src/utils/pipe.ts | 5 --- 5 files changed, 47 insertions(+), 51 deletions(-) create mode 100644 src/utils/functional.ts delete mode 100644 src/utils/pipe.ts diff --git a/src/core/remote/client.ts b/src/core/remote/client.ts index f13babdae..2ee649751 100644 --- a/src/core/remote/client.ts +++ b/src/core/remote/client.ts @@ -7,7 +7,7 @@ import { import { Maybe } from '@/src/types'; import { Deferred, defer } from '@/src/utils'; import { debug } from '@/src/utils/loggers'; -import pipe from '@/src/utils/pipe'; +import { flow } from '@/src/utils/functional'; import { nanoid } from 'nanoid'; import { Socket, io } from 'socket.io-client'; import { z } from 'zod'; @@ -160,13 +160,8 @@ export default class RpcClient { this.socket.on(STREAM_RESULT_EVENT, this.onStreamResultEvent); } - protected serialize = (obj: any) => { - return pipe(...this.serializers)(obj); - }; - - protected deserialize = (obj: any) => { - return pipe(...this.deserializers)(obj); - }; + protected serialize = flow(...this.serializers); + protected deserialize = flow(...this.deserializers); async connect(uri: string) { await this.disconnect(); diff --git a/src/io/import/processors/restoreStateFile.ts b/src/io/import/processors/restoreStateFile.ts index 093540b5c..ad6157c1c 100644 --- a/src/io/import/processors/restoreStateFile.ts +++ b/src/io/import/processors/restoreStateFile.ts @@ -14,7 +14,8 @@ import { fileToDataSource, } from '@/src/io/import/dataSource'; import { MANIFEST, isStateFile } from '@/src/io/state-file'; -import { ensureError, partition, pipe } from '@/src/utils'; +import { ensureError, partition } from '@/src/utils'; +import { pipe } from '@/src/utils/functional'; import Pipeline, { PipelineContext } from '@/src/core/pipeline'; import { Awaitable } from '@vueuse/core'; import doneWithDataSource from '@/src/io/import/processors/doneWithDataSource'; diff --git a/src/utils/functional.ts b/src/utils/functional.ts new file mode 100644 index 000000000..3f27df22b --- /dev/null +++ b/src/utils/functional.ts @@ -0,0 +1,42 @@ +type FlowFunction = (o: T) => T; + +export function flow(...fns: Array>) { + return (input: T) => fns.reduce((result, fn) => fn(result), input); +} + +// Pipe code from +// https://dev.to/ecyrbe/how-to-use-advanced-typescript-to-define-a-pipe-function-381h +// Changed second parameter to rest/spread argument. +type AnyFunc = (...arg: any) => any; + +type LastFnReturnType, Else = never> = F extends [ + ...any[], + (...arg: any) => infer R +] + ? R + : Else; + +type PipeArgs = F extends [ + (...args: infer A) => infer B +] + ? [...Acc, (...args: A) => B] + : F extends [(...args: infer A) => any, ...infer Tail] + ? Tail extends [(arg: infer B) => any, ...any[]] + ? PipeArgs B]> + : Acc + : Acc; + +// Example: +// const myNumber = pipe( +// "1", +// (a: string) => Number(a), +// (c: number) => c + 1, +// (d: number) => `${d}`, +// (e: string) => Number(e) +// ); +export function pipe( + arg: Parameters[0], + ...fns: PipeArgs extends F ? F : PipeArgs +): LastFnReturnType> { + return (fns.slice(1) as AnyFunc[]).reduce((acc, fn) => fn(acc), fns[0](arg)); +} diff --git a/src/utils/index.ts b/src/utils/index.ts index b4722bde0..28a83ac51 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -326,40 +326,3 @@ export function normalizeForStore(objects: T[], key: K) { return { order, byKey }; } - -// Pipe code from -// https://dev.to/ecyrbe/how-to-use-advanced-typescript-to-define-a-pipe-function-381h -type AnyFunc = (...arg: any) => any; - -type LastFnReturnType, Else = never> = F extends [ - ...any[], - (...arg: any) => infer R -] - ? R - : Else; - -type PipeArgs = F extends [ - (...args: infer A) => infer B -] - ? [...Acc, (...args: A) => B] - : F extends [(...args: infer A) => any, ...infer Tail] - ? Tail extends [(arg: infer B) => any, ...any[]] - ? PipeArgs B]> - : Acc - : Acc; - -// Example: -// const valid = pipe( -// "1", -// (a: string) => Number(a), -// (c: number) => c + 1, -// (d: number) => `${d}`, -// (e: string) => Number(e) -// ); -export function pipe( - arg: Parameters[0], - firstFn: FirstFn, - ...fns: PipeArgs extends F ? F : PipeArgs -): LastFnReturnType> { - return (fns as AnyFunc[]).reduce((acc, fn) => fn(acc), firstFn(arg)); -} diff --git a/src/utils/pipe.ts b/src/utils/pipe.ts deleted file mode 100644 index a41065412..000000000 --- a/src/utils/pipe.ts +++ /dev/null @@ -1,5 +0,0 @@ -export type PipeFunction = (o: T) => T; - -export default function pipe(...fns: Array>) { - return (input: T) => fns.reduce((result, fn) => fn(result), input); -}