Skip to content

Commit

Permalink
update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
Vadim Kudriavtcev committed Jan 15, 2019
1 parent 461723e commit 1e89b5a
Show file tree
Hide file tree
Showing 4 changed files with 240 additions and 3 deletions.
Empty file removed .gitignore
Empty file.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2019 Vadim
Copyright (c) 2019 Vadim Kudriavtcev

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
139 changes: 137 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,138 @@
# Redux-helper
# Redux Array Helper

Effortless operations with Reducers
Effortless operations with Reducers.

Motivation - make operations with redux store as simple as possible and keep data immutable.

If you currently doing update like this.

```javascript
export default function example(state = initialState, action) {

switch (action.type) {

case UPDATE:
return {
...state,
list: state.sections.map(item => {
if (item._id !== action.payload._id) {
return item
}
return {
...item,
...action.payload
}
})
}

default:
return state;

}

}
```

You can do this shorter:

```javascript

export default function example(state = initialState, action) {

switch (action.type) {

case UPDATE:
return {
...state,
list: ReduxArrayHelper.update(state.list, action.payload)
}

default:
return state;

}

}
```

And many other short operations of CRUD. More examples below.

### Installation

Copy ReduxHelper.js to your project. For example to src/utils/ folder.

### Importing

```javascript
import { ReduxArrayHelper } from '../utils/ReduxHelper'
```

### Methods

| Method | First Arguments | Second Arguments | Third Arguments | Description |
| ------ | ------ | ------ | ------ | ------ |
| append | list | payload | - | Insert to the end of the list |
| prepend | list | payload | - | Insert to the end of the list |
| insert | list | payload | index | Insert to position of the list |
| update | list | payload | (optional) comparator | Update item of list by id in the payload |
| updateByIndex | list | payload | index (int) | Update item of list by payload |
| delete | list | payload | (optional) comparator | Delete item from the list by id in the payload |
| deleteById | list | payload (id) | (optional) comparator | Delete item from the list using payload |
| deleteByIndex | list | index (int) | - | Delete item from the list using payload |
| find | list | payload | (optional) comparator | Return the item from the list by id in the payload |
| findById | list | payload (id) | (optional) comparator | Return the item from the list by payload|
| findIndexById | list | payload (id) | (optional) comparator | Return index of item by payload |

if you are using MongoDB, pass the third argument COMPORATOR as string key '_id'.

```javascript
ReduxArrayHelper.update(state.list, action.payload, '_id')
```

### Payload from action.

```javascript
{
id: 1,
title: 'Redux the best!',
size: 300,
}
```

### Reducer

```javascript
import {
CREATE_BOOK,
UPDATE_BOOK,
DELETE_BOOK,
} from '../var/Examples'

import { ReduxArrayHelper } from '../utils/ReduxHelper'

const initialState = {
list: [
{ id: 1, title: 'Redux awesome!'}
],
}

export default function examples(state = initialState, action) {

switch (action.type) {

case CREATE_BOOK:
return { ...state, list: ReduxArrayHelper.append(state.list, action.payload) }

case UPDATE_BOOK:
return { ...state, list: ReduxArrayHelper.update(state.list, action.payload) }

case DELETE_BOOK:
return { ...state, list: ReduxArrayHelper.delete(state.list, action.payload) }

default:
return state;

}

}
```
102 changes: 102 additions & 0 deletions ReduxHelper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
export const ReduxArrayHelper = {

append: (list, payload) => {
if(!list || !Array.isArray(list)) return [payload]
let newList = Array.from(list)
newList.push(payload)
return newList
},

prepend: (list, payload) => {
if(!list || !Array.isArray(list)) return [payload]
let newList = Array.from(list)
newList.unshift(payload)
return newList
},

insert: (list, payload, index) => {
if(!list || !Array.isArray(list)) return [payload]
let newList = Array.from(list)
newList.splice(index, 0, payload)
return newList
},

update: (list, payload, comparator = 'id') => {
if(!list || !Array.isArray(list) || !payload) return list
return Array.from(list).map(item => {
if (item[comparator] !== payload[comparator]) {
return item
}
return {
...item,
...payload
}
})
},

updateByIndex: (list, payload, index) => {
if(!list || !Array.isArray(list) || !payload) return list
return Array.from(list).map((item, i) => {
if (index !== i) {
return item
}
return {
...item,
...payload
}
})
},

delete: (list, payload, comparator = 'id') => {
if(!list || !Array.isArray(list) || !payload) return list
return list.filter((data) => data[comparator] !== payload[comparator])
},

deleteById: (list, payload, comparator = 'id') => {
if(!list || !Array.isArray(list) || !payload) return list
return list.filter((data) => data[comparator] !== payload)
},

deleteByIndex: (list, payload) => {
if(!list || !Array.isArray(list) || (!payload && payload !== 0)) return list
return list.splice(payload, 1)
},

find: (list, payload, compare = 'id') => {
if(!list || !Array.isArray(list)) return false
return list.find((data) => data[compare] === payload.id)
},

findById: (list, payload, compare = 'id') => {
if(!list || !Array.isArray(list)) return false
return list.find((data) => data[compare] === payload)
},

findIndexById: (list, payload, compare = 'id') => {
if(!list || !Array.isArray(list)) return false
return list.findIndex((data) => data[compare] === payload)
},

toObjectByKey: (list, key = 'id') => {
if(!list || !Array.isArray(list)) return list
let cache = {}
list.forEach(data => {
cache[data[key]] = data
})
return cache
},

groupedToObjectByKey: (list, key = 'id') => {
if(!list || !Array.isArray(list)) return list
let cache = {}
list.forEach(data => {
if(!cache[data[key]]) {
cache[data[key]] = [data]
} else {
cache[data[key]].push(data)
}
})
return cache
}

}

0 comments on commit 1e89b5a

Please sign in to comment.