Skip to content

Commit

Permalink
add combine-reducers
Browse files Browse the repository at this point in the history
  • Loading branch information
dominikstoetter committed Jan 26, 2018
0 parents commit d297c6a
Show file tree
Hide file tree
Showing 10 changed files with 4,238 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"presets": [
"env"
]
}
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules/
coverage/
1 change: 1 addition & 0 deletions dist/bundle.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions dist/bundle.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "combine-reducers",
"version": "1.0.0",
"main": "src/combine-reducers.js",
"license": "MIT",
"scripts": {
"test": "jest",
"build": "webpack"
},
"devDependencies": {
"babel": "^6.23.0",
"babel-loader": "^7.1.2",
"babel-preset-env": "^1.6.1",
"jest": "^22.1.4",
"uglifyjs-webpack-plugin": "^1.1.6",
"webpack": "^3.10.0"
}
}
24 changes: 24 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# combine-reducers

a standalone ES6 combine-reducers function. Unit tested. Babel pre-configured.

```
yarn run v1.3.2
$ jest --coverage
PASS test/combine-reducers.test.js
combineReducers
✓ should add a customer and a pony into eachs state (3ms)
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 0.895s, estimated 1s
Ran all test suites.
---------------------|----------|----------|----------|----------|----------------|
File | % Stmts | % Branch | % Funcs | % Lines |Uncovered Lines |
---------------------|----------|----------|----------|----------|----------------|
All files | 100 | 0 | 100 | 100 | |
combine-reducers.js | 100 | 0 | 100 | 100 | 1 |
---------------------|----------|----------|----------|----------|----------------|
✨ Done in 1.88s.
```
8 changes: 8 additions & 0 deletions src/combine-reducers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export default (reducers = {}) => (state = {}, action) =>
Object.assign(
{},
state,
...Object.keys(reducers).map(key => ({
[key]: reducers[key](state[key], action)
})
))
26 changes: 26 additions & 0 deletions test/combine-reducers.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import combineReducers from '../src/combine-reducers'

const reducerCustomers = (state = [], action) => action.type === 'SET_CUSTOMER' ? {
customer: action.customer
} : state
const reducerPonys = (state = [], action) => action.type === 'SET_PONY' ? {
pony: action.pony
} : state

describe('combineReducers', () => {
it('should add a customer and a pony into eachs state', () =>{
const customer = {name: `customer-${Math.random(100)}`}
const pony = {name: `pony-${Math.random(100)}`}

const combinedReducers = combineReducers({
reducerCustomers,
reducerPonys
});

const firstState = combinedReducers({}, { type: 'SET_CUSTOMER', customer })
expect(firstState.reducerCustomers.customer).toEqual(customer)

const secondState = combinedReducers(firstState, { type: 'SET_PONY', pony })
expect(secondState.reducerPonys.pony).toEqual(pony)
})
})
28 changes: 28 additions & 0 deletions webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const path = require('path');
const UglifyJsPlugin = require('uglifyjs-webpack-plugin')

module.exports = {
entry: './src/combine-reducers.js',
output: {
filename: 'bundle.js',
path: path.resolve(__dirname, 'dist')
},
module: {
loaders: [
{
test: /\.js$/,
loader: 'babel-loader',
query: {
presets: ['env']
}
}
]
},
stats: {
colors: true
},
devtool: 'source-map',
plugins: [
new UglifyJsPlugin()
]
};
Loading

0 comments on commit d297c6a

Please sign in to comment.