['**']
(combining haystack and context)
@@ -522,8 +528,13 @@ Default: `undefined`
When defined, this function is called after traversal as `afterFn(state = { result, haystack, context })`.
-If a value other than `undefined` is returned, that value is returned as
-the final result instead of `state.result`.
+Additional information written to `state` in `beforeFn` is available in `afterFn`.
+
+The content of `state` can be modified in the function. In particular the key `state.result` can be updated.
+
+If a value other than `undefined` is returned from `afterFn`, that value is written to `state.result`.
+
+After `beforeFn` has executed, the key `state.result` is returned as the final result.
_Examples_:
['**']
(returning count plus context)
@@ -550,6 +561,18 @@ objectScan(['**'], {
// => [ 4 ]
```
+ ['**']
(pass data from beforeFn to afterFn)
+
+
+```js
+const haystack = {};
+objectScan(['**'], {
+ beforeFn: (state) => { /* eslint-disable no-param-reassign */ state.custom = 7; },
+ afterFn: (state) => state.custom
+})(haystack);
+// => 7
+```
+
#### compareFn
diff --git a/src/core/find.js b/src/core/find.js
index f794a2c4..6ec59b53 100644
--- a/src/core/find.js
+++ b/src/core/find.js
@@ -205,7 +205,7 @@ export default (haystack_, searches_, ctx) => {
if (ctx.afterFn !== undefined) {
const r = ctx.afterFn(state);
if (r !== undefined) {
- return r;
+ state.result = r;
}
}
return state.result;
diff --git a/test/readme/README.template.md b/test/readme/README.template.md
index 45c3bfc6..c993c88e 100644
--- a/test/readme/README.template.md
+++ b/test/readme/README.template.md
@@ -356,10 +356,16 @@ breakFn: ({ key }) => key === 'a.b'
Type: `function`
Default: `undefined`
-When defined, this function is called before traversal as `beforeFn(state = { haystack, context })`
-and `state.haystack` is subsequently traversed using `state.context`.
+When defined, this function is called before traversal as `beforeFn(state = { haystack, context })`.
-If a value other than `undefined` is returned, that value is written to `state.haystack` before traversal.
+If a value other than `undefined` is returned from `beforeFn`,
+that value is written to `state.haystack` before traversal.
+
+The content of `state` can be modified in the function.
+After `beforeFn` has executed, the traversal happens using `state.haystack` and `state.context`.
+
+The content in `state` can be accessed in `afterFn`.
+Note however that the key `result` is being overwritten.
_Examples_:
@@ -385,8 +391,13 @@ Default: `undefined`
When defined, this function is called after traversal as `afterFn(state = { result, haystack, context })`.
-If a value other than `undefined` is returned, that value is returned as
-the final result instead of `state.result`.
+Additional information written to `state` in `beforeFn` is available in `afterFn`.
+
+The content of `state` can be modified in the function. In particular the key `state.result` can be updated.
+
+If a value other than `undefined` is returned from `afterFn`, that value is written to `state.result`.
+
+After `beforeFn` has executed, the key `state.result` is returned as the final result.
_Examples_:
@@ -406,6 +417,14 @@ afterFn: ({ result }) => result.filter((v) => v > 3)
rtn: 'value'
joined: false
+
+haystack: {}
+needles: ['**']
+comment: pass data from beforeFn to afterFn
+beforeFn: (state) => { /* eslint-disable no-param-reassign */ state.custom = 7; }
+afterFn: (state) => state.custom
+joined: false
+
#### compareFn