diff --git a/packages/core/src/composition/compose/index.ts b/packages/core/src/composition/compose/index.ts index 6811028f..118d3725 100644 --- a/packages/core/src/composition/compose/index.ts +++ b/packages/core/src/composition/compose/index.ts @@ -1,10 +1,24 @@ -import type { ExplicitAny, UnaryFunction } from '@/types'; +/* + * Copyright 2024 Hypergiant Galactic Systems Inc. All rights reserved. + * This file is licensed to you under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. You may obtain a copy + * of the License at https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under + * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS + * OF ANY KIND, either express or implied. See the License for the specific language + * governing permissions and limitations under the License. + */ + +/* eslint-disable @typescript-eslint/no-explicit-any */ +import type { UnaryFunction } from '@/types'; // https://stackoverflow.com/questions/49310886/typing-compose-function-in-typescript-flow-compose#answer-73082627 // If its a list of functions, last being Unary type ComposeParams = Fns extends readonly [ - ...ExplicitAny[], + // biome-ignore lint/suspicious/noExplicitAny: + ...any[], infer Last extends UnaryFunction, ] ? // Get Params of the last, which returns [...argTypes], so get the first one [0] @@ -20,17 +34,13 @@ type Composable = Fn extends readonly [UnaryFunction] ? Fn : // if its a list of Unary funcs (ignoring the first) - Fn extends readonly [ - ExplicitAny, - ...infer Rest extends readonly UnaryFunction[], - ] + // biome-ignore lint/suspicious/noExplicitAny: + Fn extends readonly [any, ...infer Rest extends readonly UnaryFunction[]] ? // Start building the list of func type by using the return type of the first in Rest // as the arg of the next in line and recursively spread the rest (doing the same thing) // The first is ignored but handled by the top level ComposeReturn - readonly [ - (arg: ComposeReturn) => ExplicitAny, - ...Composable, - ] + // biome-ignore lint/suspicious/noExplicitAny: + readonly [(arg: ComposeReturn) => any, ...Composable] : never; /** diff --git a/packages/core/src/composition/curry/index.ts b/packages/core/src/composition/curry/index.ts index 47b06d01..07de94f1 100644 --- a/packages/core/src/composition/curry/index.ts +++ b/packages/core/src/composition/curry/index.ts @@ -1,12 +1,22 @@ -// https://github.com/type-challenges/type-challenges/issues/15988 +/* + * Copyright 2024 Hypergiant Galactic Systems Inc. All rights reserved. + * This file is licensed to you under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. You may obtain a copy + * of the License at https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under + * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS + * OF ANY KIND, either express or implied. See the License for the specific language + * governing permissions and limitations under the License. + */ -import type { ExplicitAny } from '@/types'; +import type { Callable } from '@/types'; +// https://github.com/type-challenges/type-challenges/issues/15988 export type Curried =

>( ...args: P -) => ((...args: T) => ExplicitAny) extends ( - ...args: [...P, ...infer Args] -) => ExplicitAny + // biome-ignore lint/suspicious/noExplicitAny: +) => ((...args: T) => any) extends (...args: [...P, ...infer Args]) => any ? Args extends [] ? R : Curried @@ -22,9 +32,10 @@ export type Curried =

>( * curried(2)(3, 4); * curried(2, 3, 4); */ -export function autoCurry ExplicitAny>( +export function autoCurry( fn: T, - _args = [] as ExplicitAny[], + // biome-ignore lint/suspicious/noExplicitAny: + _args = [] as any[], ): Curried, ReturnType> { return (...__args) => ((rest) => (rest.length >= fn.length ? fn(...rest) : autoCurry(fn, rest)))([ diff --git a/packages/core/src/composition/pipe/index.ts b/packages/core/src/composition/pipe/index.ts index 3baacdef..a6cb8bf8 100644 --- a/packages/core/src/composition/pipe/index.ts +++ b/packages/core/src/composition/pipe/index.ts @@ -1,9 +1,23 @@ -import type { ExplicitAny, UnaryFunction } from '@/types'; +/* + * Copyright 2024 Hypergiant Galactic Systems Inc. All rights reserved. + * This file is licensed to you under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. You may obtain a copy + * of the License at https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under + * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS + * OF ANY KIND, either express or implied. See the License for the specific language + * governing permissions and limitations under the License. + */ + +/* eslint-disable @typescript-eslint/no-explicit-any */ +import type { UnaryFunction } from '@/types'; // If its a list of functions, last being Unary type PipeParams = Fns extends readonly [ infer First extends UnaryFunction, - ...ExplicitAny[], + // biome-ignore lint/suspicious/noExplicitAny: + ...any[], ] ? // Get Params of the first, which returns [...argTypes], so get the first one [0] // so that we have the true type of the arg @@ -14,7 +28,8 @@ type PipeParams = Fns extends readonly [ // have to spread and infer last so that it gets the right type for the last one // [-1] no bueno type PipeReturn = ReturnType< - Fns extends readonly [...ExplicitAny[], infer Last extends UnaryFunction] + // biome-ignore lint/suspicious/noExplicitAny: + Fns extends readonly [...any[], infer Last extends UnaryFunction] ? Last : never >; @@ -24,14 +39,13 @@ type Pipeable = Fn extends readonly [UnaryFunction] ? Fn : // if its a list of Unary funcs (ignoring the last) - Fn extends readonly [ - ...infer Head extends readonly UnaryFunction[], - ExplicitAny, - ] + // biome-ignore lint/suspicious/noExplicitAny: + Fn extends readonly [...infer Head extends readonly UnaryFunction[], any] ? // Start building the list of func type by using the return type of the last in Head // as the arg of the previous in line and recursively spread the rest (doing the same thing) // The last is ignored but handled by the top level FlowReturn - readonly [...Pipeable, (arg: PipeReturn) => ExplicitAny] + // biome-ignore lint/suspicious/noExplicitAny: + readonly [...Pipeable, (arg: PipeReturn) => any] : never; /** diff --git a/packages/core/src/types.ts b/packages/core/src/types.ts index 57c3f927..5ed58ab0 100644 --- a/packages/core/src/types.ts +++ b/packages/core/src/types.ts @@ -1,14 +1,27 @@ +/* + * Copyright 2024 Hypergiant Galactic Systems Inc. All rights reserved. + * This file is licensed to you under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. You may obtain a copy + * of the License at https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under + * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS + * OF ANY KIND, either express or implied. See the License for the specific language + * governing permissions and limitations under the License. + */ + export type Accumulator = (acc: R, x: T) => R; export type ArrayElementType = T extends (infer E)[] ? E : T; -export type Comparator = (x: T) => boolean; - // biome-ignore lint/suspicious/noExplicitAny: -export type ExplicitAny = any; +export type Callable = (...all: any) => any; + +export type Comparator = (x: T) => boolean; export type MapFn = (x: T, idx?: number) => R; export type Predicate = (x: T, idx?: number) => boolean; -export type UnaryFunction = (x: ExplicitAny) => ExplicitAny; +// biome-ignore lint/suspicious/noExplicitAny: +export type UnaryFunction = (x: any) => any; diff --git a/packages/core/src/utility/lookup/index.ts b/packages/core/src/utility/lookup/index.ts index 590d0244..e5e2b7bc 100644 --- a/packages/core/src/utility/lookup/index.ts +++ b/packages/core/src/utility/lookup/index.ts @@ -1,6 +1,8 @@ -import type { ExplicitAny } from '@/types'; +import type { Callable } from '@/types'; import { identity } from '../../combinators/i'; +type Cache = Record; + /** * Takes an object and an optional fallback function and returns a function that * takes a string and returns the lookup value or the result default fallback. @@ -17,15 +19,6 @@ import { identity } from '../../combinators/i'; * colorLookup(data.value); */ export const lookup = - < - A extends Record, - B extends (...args: ExplicitAny[]) => ExplicitAny, - >( - obj: A, - def?: B, - ) => - (prop: string | number | symbol): A[C] => { - const fn = def ?? identity; - - return fn(obj[prop]); - }; + (obj: A, def?: B) => + (prop: string | number | symbol): A[C] => + (def ?? identity)(obj[prop]); diff --git a/packages/core/src/utility/once/index.ts b/packages/core/src/utility/once/index.ts index 75511d58..5a7560d7 100644 --- a/packages/core/src/utility/once/index.ts +++ b/packages/core/src/utility/once/index.ts @@ -1,18 +1,16 @@ -import type { ExplicitAny } from '@/types'; - -// TS' `Function` type only models the object side of it, not whether it is callable. -type SomeFunction = (...args: ExplicitAny[]) => ExplicitAny; +import type { Callable } from '@/types'; /** * Ensures that the given function is only called once. */ -export const once = (fn: T) => { +export const once = (fn: T) => { let done = false; - // TODO: Better types, since it can return void? - // biome-ignore lint/suspicious/noConfusingVoidType: - return (...args: Parameters): ReturnType | void => - // biome-ignore lint/suspicious/noAssignInExpressions: Shhhh - // biome-ignore lint/style/noCommaOperator: Shhh - done ? void 0 : ((done = true), fn(args)); + return (...args: Parameters): ReturnType | undefined => { + if (!done) { + done = true; + + return fn(...args); + } + }; };