Skip to content

Commit

Permalink
More tests and fixes for defs.
Browse files Browse the repository at this point in the history
  • Loading branch information
uhop committed Sep 16, 2024
1 parent 23fe823 commit 20d61fe
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 8 deletions.
29 changes: 22 additions & 7 deletions src/defs.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,27 +25,42 @@ export declare function many<T>(values: T[]): Many<T>;
export declare function getManyValues<T>(o: Many<T>): T[];

export interface Flushable<I = any, O = unknown> {
[flushSymbol]: 1;
(value: I, ...rest: any[]): O;
[flushSymbol]: 1;
}
export declare function isFlushable<I, O>(o: (value: I, ...rest: any[]) => O): o is Flushable<I, O>;
export declare function isFlushable<I = any, O = unknown>(
o: (value: I, ...rest: any[]) => O
): o is Flushable<I, O>;
export declare function flushable<I, O>(
write: (value: I, ...rest: any[]) => O,
final?: () => O
): Flushable<I, O>;

export interface FunctionList<T extends (...args: readonly any[]) => unknown> {
export interface FunctionList<
T extends (...args: readonly any[]) => unknown,
I = any,
O = unknown
> {
(value: I, ...rest: any[]): O;
[fListSymbol]: 1;
fList: T[];
}
export declare function isFunctionList(o: object): o is FunctionList;
export declare function setFunctionList<T extends (...args: readonly any[]) => unknown>(
o: any,
export declare function isFunctionList<I, O>(
o: (value: I, ...rest: readonly any[]) => O
): o is FunctionList<(...args: readonly any[]) => unknown, I, O>;
export declare function setFunctionList<
T extends (...args: readonly any[]) => unknown,
F extends (...args: readonly any[]) => unknown
>(
o: F,
fns: T[]
): FunctionList<T>;
): F extends (value: infer I, ...rest: any[]) => infer O ? FunctionList<T, I, O> : never;
export declare function getFunctionList<T extends (...args: readonly any[]) => unknown>(
o: FunctionList<T>
): T[];
export declare function clearFunctionList<F>(
o: F
): F extends (value: infer I, ...rest: any[]) => infer O ? (value: I, ...rest: any[]) => O : never;

// generic utilities: unpacking types

Expand Down
7 changes: 7 additions & 0 deletions src/defs.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ const setFunctionList = (o, fns) => {
return o;
}

const clearFunctionList = o => {
delete o.fList;
delete o[fListSymbol];
return o;
}

class Stop extends Error {}

// old aliases
Expand Down Expand Up @@ -63,3 +69,4 @@ module.exports.fListSymbol = fListSymbol;
module.exports.isFunctionList = isFunctionList;
module.exports.getFunctionList = getFunctionList;
module.exports.setFunctionList = setFunctionList;
module.exports.clearFunctionList = clearFunctionList;
75 changes: 74 additions & 1 deletion ts-check/defs.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
import {
isFinalValue,
finalValue,
getFinalValue
getFinalValue,

isMany,
many,
getManyValues,

isFlushable,
flushable,

isFunctionList,
getFunctionList,
setFunctionList,
clearFunctionList
} from 'stream-chain/defs.js';

{
Expand All @@ -21,3 +33,64 @@ import {
// const v = getFinalValue(w);
// void v;
}

{
const x = many([1, 2, 3]);
if (isMany(x)) {
const t = getManyValues(x);
void t;
}
const z = getManyValues(x);
void z;

const w = {};
if (isMany(w)) {
const t = getManyValues(w);
void t;
}
// const v = getManyValues(w);
// void v;
}

{
const x = flushable((x: number) => x * x);
if (isFlushable(x)) {
const t = x(42);
void t;
}
const z = x(42);
void z;

const w = (x: string) => x + 'x';
if (isFlushable(w)) {
const t = w('42');
void t;
}
}

{
const x = setFunctionList((x: number) => x * x, [() => 42]);
if (isFunctionList(x)) {
const t = getFunctionList(x);
void t;
}
const z = getFunctionList(x);
void z;

const y = (x: string) => x + 'x';
if (isFunctionList(y)) {
const t = getFunctionList(y);
void t;
}

const w = clearFunctionList(x);
if (isFunctionList(w)) {
const t = getFunctionList(w);
void t;
}

// const v = getFunctionList(w);
// void v;

void w;
}

0 comments on commit 20d61fe

Please sign in to comment.