MCPcopy Index your code
hub / github.com/Effect-TS/effect / discriminator

Function discriminator

packages/effect/src/internal/matcher.ts:338–367  ·  view source on GitHub ↗
(field: D)

Source from the content-addressed store, hash-verified

336
337/** @internal */
338export const discriminator = <D extends string>(field: D) =>
339<
340 R,
341 P extends Types.Tags<D, R> & string,
342 Ret,
343 Fn extends (_: Extract<R, Record<D, P>>) => Ret
344>(
345 ...pattern: [
346 first: P,
347 ...values: Array<P>,
348 f: Fn
349 ]
350) => {
351 const f = pattern[pattern.length - 1]
352 const values: Array<P> = pattern.slice(0, -1) as any
353 const pred = values.length === 1
354 ? (_: any) => _ != null && _[field] === values[0]
355 : (_: any) => _ != null && values.includes(_[field])
356
357 return <I, F, A, Pr>(
358 self: Matcher<I, F, R, A, Pr, Ret>
359 ): Matcher<
360 I,
361 Types.AddWithout<F, Extract<R, Record<D, P>>>,
362 Types.ApplyFilters<I, Types.AddWithout<F, Extract<R, Record<D, P>>>>,
363 A | ReturnType<Fn>,
364 Pr,
365 Ret
366 > => (self as any).add(makeWhen(pred, f as any)) as any
367}
368
369/** @internal */
370export const discriminatorStartsWith = <D extends string>(field: D) =>

Callers 1

matcher.tsFile · 0.85

Calls 2

makeWhenFunction · 0.85
addMethod · 0.65

Tested by

no test coverage detected