Skip to content

Instantly share code, notes, and snippets.

View velosipedist's full-sized avatar

Nick Konev velosipedist

View GitHub Profile
@velosipedist
velosipedist / reduce-reducers.js
Created September 17, 2019 09:34
Simplified reduce-reducers version, without any extra arguments supported
const rootReducer = (...reducers) => {
return (stateInit, action) => {
return reducers.reduce(
(state, reducer, at) => {
return reducer(state, action) || state
},
stateInit
);
}
};
@velosipedist
velosipedist / when.tsx
Created March 18, 2020 13:40
Conditional factory to replace `switch(true){}` in functional style
type Predicate<T> = (x: T) => boolean;
/**
* Usage:
* ```typescript
* const factory = when(something)
* .match( (x) => someLogicReturningTrue )
* .then( () => anyLogicToProduceResult )
* .byDefault( () => defaultLogicWhenNoMatches )
*
// Available variables:
// - Machine
// - interpret
// - assign
// - send
// - sendParent
// - spawn
// - raise
// - actions