Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion scripts/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const entries = [
enabled: config.values['vscode-typescript'] ?? config.values.vscode,
async apply() {
await new JsonFile('.vscode/settings.json').setProperty(
'typescript.tsdk',
'js/ts.tsdk.path',
'node_modules/typescript/lib',
);
},
Expand Down
59 changes: 59 additions & 0 deletions src/misc/__test__/pipe.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { describe, test } from 'node:test';
import { expect } from '@std/expect';
import { pipe } from '../pipe.ts';

describe('pipe', () => {
test('should work correctly for zero functions', () => {
const flow = pipe();
const result: void = flow();

expect(result).toBe(undefined);
});

test('should work correctly for single function', () => {
const flow = pipe(() => 123);
const result: number = flow();

expect(result).toBe(123);
});

test('should work correctly for two functions', () => {
const flow = pipe(
//
(a: number, b: number) => a + b,
(value: number) => value * 2,
);

expect(flow(2, 3)).toBe(10);
});

test('should work correctly for two functions', () => {
const flow = pipe(
//
(a: number, b: number) => a + b,
(value: number) => value * 2,
(value: number) => `value is ${value}`,
);

const cases: Array<{ input: [number, number]; output: string }> = [
{
input: [1, 1],
output: 'value is 4',
},
{
input: [2, 3],
output: 'value is 10',
},
{
input: [3, 4],
output: 'value is 14',
},
];

for (const { input, output } of cases) {
const result: string = flow(...input);

expect(result).toBe(output);
}
});
});
11 changes: 6 additions & 5 deletions src/misc/mod.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
export * from './debounce.ts';
export * from './identity.ts';
export * from './is-object.ts';
export * from './is-shallow-equal.ts';
export * from './keys.ts';
export * from './noop.ts';
export * from './once.ts';
export * from './pairs.ts';
export * from './pipe.ts';
export * from './plural.ts';
export * from './throttle.ts';
export * from './wait.ts';
export * from './noop.ts';
export * from './timer-pool.ts';
export * from './identity.ts';
export * from './keys.ts';
export * from './once.ts';
export * from './wait.ts';
61 changes: 61 additions & 0 deletions src/misc/pipe.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import type { AnyFunction } from '../mod.ts';
import { noop } from './noop.ts';

export type FunctionPipe<List extends AnyFunction[]> = List extends [
infer F1 extends AnyFunction,
infer F2 extends AnyFunction,
...infer Rest extends AnyFunction[],
]
? [ReturnType<F1>] extends [Parameters<F2>[0]]
? [F1, ...FunctionPipe<[F2, ...Rest]>]
: never
: List;

export type FirstItemParameters<List extends AnyFunction[]> = List extends [
infer FirstFn extends AnyFunction,
...AnyFunction[],
]
? Parameters<FirstFn>
: never;

export type LastItemReturnType<List extends AnyFunction[]> = List extends [
...AnyFunction[],
infer LastFn extends AnyFunction,
]
? ReturnType<LastFn>
: never;

export function pipe(): () => void;

export function pipe<F extends AnyFunction>(func: F): (...args: Parameters<F>) => ReturnType<F>;

export function pipe<Funcs extends AnyFunction[]>(
...funcs: FunctionPipe<Funcs> & Funcs
): (...args: FirstItemParameters<Funcs>) => LastItemReturnType<Funcs>;

/**
* Returns function that applies given functions from left to right.
* @param funcs Functions.
* @returns Function.
*/
export function pipe<Funcs extends AnyFunction[]>(
...funcs: FunctionPipe<Funcs> & Funcs
): (...args: FirstItemParameters<Funcs>) => LastItemReturnType<Funcs> {
if (funcs.length === 0) {
return noop as AnyFunction;
}

if (funcs.length === 1) {
return (...args: any[]) => funcs[0](...args);
}

return (...args: any[]): any => {
let acc: any = funcs[0]!(...args);

for (let i = 1; i < funcs.length; i++) {
acc = funcs[i]!(acc);
}

return acc;
};
}
4 changes: 3 additions & 1 deletion src/misc/throttle.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { AnyFunction } from '../mod.ts';

export interface ThrottleOptions {
leading?: boolean;
trailing?: boolean;
Expand All @@ -10,7 +12,7 @@ export interface ThrottleOptions {
* @param options Options.
* @returns Throttled function.
*/
export function throttle<T extends (...args: any[]) => any>(
export function throttle<T extends AnyFunction>(
fn: T,
timeout = 0,
{ leading = true, trailing = false }: ThrottleOptions = {},
Expand Down
5 changes: 3 additions & 2 deletions src/react/use-stable-callback.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useRef } from 'react';
import type { AnyFunction } from '../mod.ts';

interface HookState<T extends (...args: any[]) => any> {
interface HookState<T extends AnyFunction> {
callback: T;
stable: (...args: Parameters<T>) => ReturnType<T>;
}
Expand All @@ -15,7 +16,7 @@ interface HookState<T extends (...args: any[]) => any> {
* @param callback Callback.
* @returns Stable callback.
*/
export function useStableCallback<T extends (...args: any[]) => any>(
export function useStableCallback<T extends AnyFunction>(
callback: T,
): (...args: Parameters<T>) => ReturnType<T> {
const stateRef = useRef<HookState<T>>(null);
Expand Down
1 change: 1 addition & 0 deletions src/types/functions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type AnyFunction = (...args: any[]) => any;
1 change: 1 addition & 0 deletions src/types/mod.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export type * from './distributive-omit.ts';
export type * from './remote-data.ts';
export type * from './functions.ts';
Loading