import { TinyType } from '../TinyType.js'; import { ConstructorAbstractOrInstance } from '../types/index.js'; import { PatternMatcher } from './PatternMatcher.js'; import { MatcherRule, MatchesEqualTinyType, MatchesIdentical, MatchesObjectsWithCommonPrototype } from './rules/index.js'; /** * @access private */ export class ObjectMatcher extends PatternMatcher, TinyType | Input_Type, Output_Type> { when(pattern: ConstructorAbstractOrInstance, transformation: (v: MT) => Output_Type): ObjectMatcher; when(pattern: TinyType, transformation: (v: TinyType) => Output_Type): ObjectMatcher; when(pattern: Input_Type, transformation: (v: Input_Type) => Output_Type): ObjectMatcher; when(pattern: any, transformation: (v: any) => Output_Type) { // eslint-disable-line @typescript-eslint/explicit-module-boundary-types return new ObjectMatcher( this.value, this.rules.concat(this.rule(pattern, transformation)), ); } private rule(pattern: any, transformation: (v: any) => Output_Type): MatcherRule { switch (true) { case pattern instanceof TinyType: return new MatchesEqualTinyType(pattern as TinyType, transformation); case typeof pattern === 'function': return new MatchesObjectsWithCommonPrototype(pattern, transformation); default: return new MatchesIdentical(pattern, transformation); } } }