MCPcopy
hub / github.com/gvergnaud/ts-pattern / reducer

Function reducer

tests/select.test.ts:102–145  ·  view source on GitHub ↗
(state: State, event: Event)

Source from the content-addressed store, hash-verified

100 };
101
102 const reducer = (state: State, event: Event): State =>
103 match<[State, Event], State>([state, event])
104 .with(
105 [
106 { status: 'loading' },
107 {
108 type: 'success',
109 data: P.select('data'),
110 requestTime: P.select('time'),
111 },
112 ],
113 ({ data, time }) => {
114 type t = Expect<Equal<typeof time, number | undefined>>;
115
116 return {
117 status: 'success',
118 data,
119 };
120 }
121 )
122 .with(
123 [{ status: 'loading' }, { type: 'success', data: P.select('data') }],
124 ({ data }) => ({
125 status: 'success',
126 data,
127 })
128 )
129 .with(
130 [{ status: 'loading' }, { type: 'error', error: P.select('error') }],
131 ({ error }) => ({
132 status: 'error',
133 error,
134 })
135 )
136 .with([{ status: 'loading' }, { type: 'cancel' }], () => initState)
137 .with([{ status: P.not('loading') }, { type: 'fetch' }], () => ({
138 status: 'loading',
139 }))
140 .with([P.select('state'), P.select('event')], ({ state, event }) => {
141 type t = Expect<Equal<typeof state, State>>;
142 type t2 = Expect<Equal<typeof event, Event>>;
143 return state;
144 })
145 .run();
146
147 expect(reducer(initState, { type: 'cancel' })).toEqual(initState);
148

Callers 1

select.test.tsFile · 0.70

Calls 4

matchFunction · 0.90
runMethod · 0.80
withMethod · 0.80
selectMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…