Skip to content
This repository was archived by the owner on Feb 16, 2022. It is now read-only.

Commit ab28511

Browse files
author
Piotr Oleś
committed
feat: 🎸 Add condition detector support
Add condition detector support - added functions: `conditionDetector`, `mapState`, `mapNextState`, `mapPrevState`, `changed`, `changedFrom`, `changedFromFalsy`, `changedFromTruthy`, `changedTo`, `changedToFalsy`, `changedToTruthy`, `composeAnd`, `composeOr`, `mapFalsy`, `mapTruthy`. BREAKING CHANGE: 🧨 Renamed function `rmountDetector` to `mapState`.
1 parent f0084b8 commit ab28511

29 files changed

+544
-178
lines changed

.travis.yml

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,16 @@ script:
1212
- yarn test --ci
1313

1414
deploy:
15-
provider: script
16-
skip_cleanup: true
17-
script:
18-
- yarn exec semantic-release
19-
on:
20-
branch: master
15+
- provider: script
16+
skip_cleanup: true
17+
script:
18+
- yarn exec semantic-release
19+
on:
20+
branch: develop
21+
22+
- provider: script
23+
skip_cleanup: true
24+
script:
25+
- yarn exec semantic-release
26+
on:
27+
branch: master

package.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,16 @@
7070
}
7171
}
7272
},
73+
"release": {
74+
"branches": [
75+
"master",
76+
{
77+
"name": "develop",
78+
"channel": "beta",
79+
"prerelease": "rc"
80+
}
81+
]
82+
},
7383
"devDependencies": {
7484
"@commitlint/config-conventional": "^7.5.0",
7585
"@types/jest": "^24.0.0",

src/Detector.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import { Action, AnyAction } from "redux";
44
* Function that reduces previous and next state to single value.
55
*/
66
export type Detector<TState = any, TResult = any> = (
7-
prevState?: TState,
8-
nextState?: TState
7+
prevState: TState | undefined,
8+
nextState: TState | undefined
99
) => TResult;
1010

1111
/**

src/index.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,20 @@ export { createDetectorEnhancer } from "./createDetectorEnhancer";
99
// standard library
1010
export { composeDetectors } from "./lib/composeDetectors";
1111
export { combineDetectors } from "./lib/combineDetectors";
12-
export { mountDetector } from "./lib/mountDetector";
12+
export { conditionDetector } from "./lib/conditionDetector";
13+
export { mapState } from "./lib/mapState";
14+
export { mapNextState } from "./lib/mapNextState";
15+
export { mapPrevState } from "./lib/mapPrevState";
16+
export { composeAnd } from "./lib/composeAnd";
17+
export { composeOr } from "./lib/composeOr";
18+
export { mapFalsy } from "./lib/mapFalsy";
19+
export { mapTruthy } from "./lib/mapTruthy";
20+
21+
// standard library changed detectors
22+
export { changed } from "./lib/changed/changed";
23+
export { changedFrom } from "./lib/changed/changedFrom";
24+
export { changedFromFalsy } from "./lib/changed/changedFromFalsy";
25+
export { changedFromTruthy } from "./lib/changed/changedFromTruthy";
26+
export { changedTo } from "./lib/changed/changedTo";
27+
export { changedToFalsy } from "./lib/changed/changedToFalsy";
28+
export { changedToTruthy } from "./lib/changed/changedToTruthy";

src/lib/changed/changed.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/**
2+
* Condition detector that checks if state changed somehow using shallow equal compare
3+
*/
4+
export function changed<TState>(
5+
prevState?: TState,
6+
nextState?: TState
7+
): boolean {
8+
return prevState !== nextState;
9+
}

src/lib/changed/changedFrom.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { ConditionDetector } from "../../Detector";
2+
3+
/**
4+
* Condition detector that checks if state changed somehow and previous step was equal expected state.
5+
*/
6+
export function changedFrom<TState>(
7+
expectedPrevState: TState
8+
): ConditionDetector<TState> {
9+
return function changedFromDetector(prevState, nextState) {
10+
return prevState !== nextState && prevState === expectedPrevState;
11+
};
12+
}

src/lib/changed/changedFromFalsy.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/**
2+
* Condition detector that checks if state changed somehow and previous state was falsy
3+
*/
4+
export function changedFromFalsy<TState>(
5+
prevState?: TState,
6+
nextState?: TState
7+
): boolean {
8+
return prevState !== nextState && !prevState;
9+
}

src/lib/changed/changedFromTruthy.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/**
2+
* Condition detector that checks if state changed somehow and previous state was truthy
3+
*/
4+
export function changedFromTruthy<TState>(
5+
prevState?: TState,
6+
nextState?: TState
7+
): boolean {
8+
return prevState !== nextState && !!prevState;
9+
}

src/lib/changed/changedTo.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { ConditionDetector } from "../../Detector";
2+
3+
/**
4+
* Condition detector that checks if state changed somehow and next step was equal expected state.
5+
*/
6+
export function changedTo<TState>(
7+
expectedNextState: TState
8+
): ConditionDetector<TState> {
9+
return function changedFromDetector(prevState, nextState) {
10+
return prevState !== nextState && nextState === expectedNextState;
11+
};
12+
}

src/lib/changed/changedToFalsy.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/**
2+
* Condition detector that checks if state changed somehow and next state is falsy
3+
*/
4+
export function changedToFalsy<TState>(
5+
prevState?: TState,
6+
nextState?: TState
7+
): boolean {
8+
return prevState !== nextState && !nextState;
9+
}

0 commit comments

Comments
 (0)