All files / src/base index.ts

100% Statements 6/6
100% Branches 0/0
100% Functions 2/2
100% Lines 6/6

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45                                                            9x   251x   251x   416x     251x     9x    
import type { DefaultRegistry, Methods, Registry } from "../registry";
import type { shim } from "./mixin";
 
export type Base<T> = T extends { [shim]: { value: infer U } }
	? { readonly value: U }
	: {
			readonly value: T;
		};
 
export type Fluent<
	T,
	Reg extends Registry = DefaultRegistry,
> = Base<T> & Methods<T, Reg> extends infer U
	? U &
			Pick<
				{
					/**
					 * Immediate casts and conversions to other types.
					 */
					to: unknown;
 
					[K: PropertyKey]: unknown;
				},
				keyof U
			>
	: never;
 
export function makeFluent<const Reg extends Registry>(
	registry: Reg,
) {
	const fluent = <const T>(value: T) => {
		// eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion
		const f = { value } as unknown as Fluent<T, Reg>;
 
		for (const mixin of registry) {
			// eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion
			mixin.fn(value, f, fluent as (value: unknown) => never);
		}
 
		return f;
	};
 
	return fluent;
}