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

Commit 40ad2a4

Browse files
Piotr Oleśpiotr-oles
authored andcommitted
feat: 🎸 Rename reduceDetectors to composeDetectors
Rename reduceDetectors to composeDetectors in order to follow community naming conventions (combine, compose, pipe). BREAKING CHANGE: 🧨 Renamed function `reduceDetectors` to `composeDetectors`.
1 parent 051880d commit 40ad2a4

File tree

4 files changed

+23
-23
lines changed

4 files changed

+23
-23
lines changed

README.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,13 @@ const store = createStore(rootReducer, createDetectorEnhancer(rootDetector));
3737
## Motivation
3838

3939
Redux Detector [enhancer](http://redux.js.org/docs/api/createStore.html) allows you to detect state changes in redux.
40-
A detector is a simple and pure function which compares two states and returns action or list of actions for some states transitions.
40+
An actions detector is a simple and pure function which compares two states and returns action or list of actions for some states transitions.
4141

4242
```typescript
43-
type Detector<S> = (
44-
prevState: S | undefined,
45-
nextState: S | undefined
46-
) => Action | Action[] | void;
43+
type ActionsDetector<TState, TAction extends Action = AnyAction> = (
44+
prevState: TState | undefined,
45+
nextState: TState | undefined
46+
) => TAction | TAction[] | void;
4747
```
4848

4949
For example detector that checks if number of rows exceed 100 looks like this:
@@ -61,12 +61,12 @@ Thanks to detectors purity they are predictable and easy to test. There is no pr
6161

6262
## Composition
6363

64-
Redux store can handle only one detector (and one reducer). But don't worry - you can combine and reduce them. To do this, use
65-
`combineDetectors` and `reduceDetectors` functions.
64+
Redux store can handle only one detector (and one reducer). But don't worry - you can combine and compose them. To do this, use
65+
`combineDetectors` and `composeDetectors` functions.
6666

6767
```js
6868
// ./detectors/rootDetector.js
69-
import { combineDetectors, reduceDetectors } from "redux-detector";
69+
import { combineDetectors, composeDetectors } from "redux-detector";
7070
import { fooDetector } from "./fooDetector";
7171
import { barDetector } from "./barDetector";
7272
import { anotherDetector } from "./anotherDetector";
@@ -81,7 +81,7 @@ import { anotherDetector } from "./anotherDetector";
8181
// and also `barDetector` to `state.bar` branch.
8282

8383
export default combineDetectors({
84-
foo: reduceDetectors(fooDetector, anotherDetector),
84+
foo: composeDetectors(fooDetector, anotherDetector),
8585
bar: barDetector
8686
});
8787
```

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ export { DetectableStoreExt } from "./DetectableStoreExt";
77
export { createDetectorEnhancer } from "./createDetectorEnhancer";
88

99
// standard library
10-
export { reduceDetectors } from "./lib/reduceDetectors";
10+
export { composeDetectors } from "./lib/composeDetectors";
1111
export { combineDetectors } from "./lib/combineDetectors";
1212
export { mountDetector } from "./lib/mountDetector";

src/lib/reduceDetectors.ts renamed to src/lib/composeDetectors.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { ActionsDetector } from "../Detector";
44
/**
55
* Compose many action detectors into one detector that aggregates actions returned by given detectors
66
*/
7-
export function reduceDetectors<TState, TAction extends Action = AnyAction>(
7+
export function composeDetectors<TState, TAction extends Action = AnyAction>(
88
...detectors: ActionsDetector<TState, TAction>[]
99
): ActionsDetector<TState, TAction> {
1010
// check detectors types in runtime
@@ -16,15 +16,15 @@ export function reduceDetectors<TState, TAction extends Action = AnyAction>(
1616
throw new Error(
1717
`Invalid ${invalidDetectorsIndexes.join(
1818
", "
19-
)} arguments in reduceDetectors function.\n` +
19+
)} arguments in composeDetectors function.\n` +
2020
`Detectors should be a 'function' type, ` +
2121
`'${invalidDetectorsIndexes
2222
.map(index => typeof detectors[index])
2323
.join(`', '`)}' types passed.`
2424
);
2525
}
2626

27-
return function reducedDetector(
27+
return function composedDetector(
2828
prevState: TState | undefined,
2929
nextState: TState | undefined
3030
): any[] {

test/reduceDetectors.spec.ts renamed to test/composeDetectors.spec.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { reduceDetectors } from "../src";
1+
import { composeDetectors } from "../src";
22

3-
describe("reduceDetectors", () => {
3+
describe("composeDetectors", () => {
44
it("should export composeDetectors function", () => {
5-
expect(reduceDetectors).toBeInstanceOf(Function);
5+
expect(composeDetectors).toBeInstanceOf(Function);
66
});
77

88
it("should return valid detector for composition of two detectors", () => {
@@ -14,7 +14,7 @@ describe("reduceDetectors", () => {
1414
return [{ type: "ACTION_C" }];
1515
}
1616

17-
const detectorAB = reduceDetectors(detectorA, detectorB);
17+
const detectorAB = composeDetectors(detectorA, detectorB);
1818

1919
expect(detectorAB).toBeInstanceOf(Function);
2020
expect(detectorAB({}, {})).toBeInstanceOf(Array);
@@ -49,7 +49,7 @@ describe("reduceDetectors", () => {
4949
return [];
5050
}
5151

52-
const detectorAB = reduceDetectors(detectorA, detectorB);
52+
const detectorAB = composeDetectors(detectorA, detectorB);
5353

5454
expect(detectorAB(-10, 50)).toEqual([{ type: "NEXT_STATE_GREATER" }]);
5555
expect(detectorAB(30, 20)).toEqual([{ type: "PREV_STATE_GREATER" }]);
@@ -68,7 +68,7 @@ describe("reduceDetectors", () => {
6868
}
6969
}
7070

71-
const detectorAB = reduceDetectors(detectorA, detectorB);
71+
const detectorAB = composeDetectors(detectorA, detectorB);
7272

7373
expect(detectorAB(-10, 50)).toEqual([{ type: "NEXT_STATE_GREATER" }]);
7474
expect(detectorAB(30, 20)).toEqual([{ type: "PREV_STATE_GREATER" }]);
@@ -83,7 +83,7 @@ describe("reduceDetectors", () => {
8383
return { type: "SINGLE_DETECTOR" };
8484
}
8585

86-
const detectorAB = reduceDetectors(detectorA, detectorB);
86+
const detectorAB = composeDetectors(detectorA, detectorB);
8787

8888
expect(detectorAB(undefined, undefined)).toEqual([
8989
{ type: "ARRAY_DETECTOR" },
@@ -92,18 +92,18 @@ describe("reduceDetectors", () => {
9292
});
9393

9494
it("should return valid composed detector for empty arguments", () => {
95-
const emptyDetector = reduceDetectors();
95+
const emptyDetector = composeDetectors();
9696

9797
expect(emptyDetector).toBeInstanceOf(Function);
9898
expect(emptyDetector(10, 20)).toEqual([]);
9999
});
100100

101101
it("should throw an exception for invalid arguments", () => {
102102
expect(() => {
103-
(reduceDetectors as any)({ foo: "bar" });
103+
(composeDetectors as any)({ foo: "bar" });
104104
}).toThrow();
105105
expect(() => {
106-
(reduceDetectors as any)(() => undefined, undefined);
106+
(composeDetectors as any)(() => undefined, undefined);
107107
}).toThrow();
108108
});
109109
});

0 commit comments

Comments
 (0)