Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve default transformer, Close #161 #162

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 5 additions & 40 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
* [Transform `Symbol()` action type to string](#transform-symbol-action-type-to-string)
* [Log everything except actions with certain type](#log-everything-except-actions-with-certain-type)
* [Collapse actions with certain type](#collapse-actions-with-certain-type)
* [Transform Immutable (without `combineReducers`)](#transform-immutable-without-combinereducers)
* [Transform Immutable (with `combineReducers`)](#transform-immutable-with-combinereducers)
* [Log batched actions](#log-batched-actions)
* [License](#license)

Expand Down Expand Up @@ -56,9 +54,9 @@ createLogger(options?: Object) => LoggerMiddleware
logErrors = true: Boolean, // Should the logger catch, log, and re-throw errors?
collapsed, // Takes a boolean or optionally a function that receives `getState` function for accessing current store state and `action` object as parameters. Returns `true` if the log group should be collapsed, `false` otherwise.
predicate, // If specified this function will be called before each action is processed with this middleware.
stateTransformer, // Transform state before print. Eg. convert Immutable object to plain JSON.
actionTransformer, // Transform state before print. Eg. convert Immutable object to plain JSON.
errorTransformer, // Transform state before print. Eg. convert Immutable object to plain JSON.
stateTransformer, // Transform state before print.
actionTransformer, // Transform state before print.
errorTransformer, // Transform state before print.
diff = false: Boolean, // Show diff between states.
diffPredicate // Filter function for showing states diff.'
}
Expand Down Expand Up @@ -126,12 +124,12 @@ Receives `getState` function for accessing current store state and `action` obj
*Default: `null` (always log)*

#### __stateTransformer = (state: Object) => state__
Transform state before print. Eg. convert Immutable object to plain JSON.
Transform state before print.

*Default: identity function*

#### __actionTransformer = (action: Object) => action__
Transform action before print. Eg. convert Immutable object to plain JSON.
Transform action before print.

*Default: identity function*

Expand Down Expand Up @@ -192,39 +190,6 @@ createLogger({
});
```

### Transform Immutable (without `combineReducers`)
```javascript
import { Iterable } from 'immutable';

const stateTransformer = (state) => {
if (Iterable.isIterable(state)) return state.toJS();
else return state;
};

const logger = createLogger({
stateTransformer,
});
```

### Transform Immutable (with `combineReducers`)
```javascript
const logger = createLogger({
stateTransformer: (state) => {
let newState = {};

for (var i of Object.keys(state)) {
if (Immutable.Iterable.isIterable(state[i])) {
newState[i] = state[i].toJS();
} else {
newState[i] = state[i];
}
};

return newState;
}
});
```

### Log batched actions
Thanks to [@smashercosmo](https://github.com/smashercosmo)
```javascript
Expand Down
10 changes: 7 additions & 3 deletions src/defaults.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
// Ensure object with toJSON method is converted to plain object. Useful for
// Immutable.js. https://github.com/evgenyrodionov/redux-logger/issues/161
const ensurePlainObject = object => JSON.parse(JSON.stringify(object));

export default {
level: `log`,
logger: console,
Expand All @@ -6,9 +10,9 @@ export default {
predicate: undefined,
duration: false,
timestamp: true,
stateTransformer: state => state,
actionTransformer: action => action,
errorTransformer: error => error,
stateTransformer: state => ensurePlainObject(state),
actionTransformer: action => ensurePlainObject(action),
errorTransformer: error => ensurePlainObject(error),
colors: {
title: () => `#000000`,
prevState: () => `#9E9E9E`,
Expand Down