Socket
Socket
Sign inDemoInstall

immer

Package Overview
Dependencies
Maintainers
1
Versions
173
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

immer - npm Package Compare versions

Comparing version 0.2.0 to 0.2.1

es5.js

4

changelog.md
# Changelog
### 0.2.1
* Fixed: `immer/es5.js` was not packed into the package. PR [#28](https://github.com/mweststrate/immer/pull/28) by [Nicolas Lepage](https://github.com/nlepage)
### 0.2.0

@@ -4,0 +8,0 @@

3

package.json
{
"name": "immer",
"version": "0.2.0",
"version": "0.2.1",
"description": "Create your next immutable state by mutating the current one",

@@ -32,2 +32,3 @@ "main": "immer.js",

"immer.js",
"es5.js",
"index.d.ts"

@@ -34,0 +35,0 @@ ],

@@ -173,2 +173,65 @@ # Immer

---
Here are some typical reducer examples, take from the Redux [Immutable Update Patterns](https://redux.js.org/docs/recipes/reducers/ImmutableUpdatePatterns.html) page, and their immer counter part.
These examples are semantically equivalent and produce the exact same state.
```javascript
// Plain reducer
function insertItem(array, action) {
return [
...array.slice(0, action.index),
action.item,
...array.slice(action.index)
]
}
// With immer
function insertItem(array, action) {
return immer(array, draft => {
draft.splice(action.index, 0, action.item)
})
}
// Plain reducer
function removeItem(array, action) {
return [
...array.slice(0, action.index),
...array.slice(action.index + 1)
];
}
// With immer
function removeItem(array, action) {
return immer(array, draft => {
draft.splice(action.index, 1)
})
}
// Plain reducer
function updateObjectInArray(array, action) {
return array.map( (item, index) => {
if(index !== action.index) {
// This isn't the item we care about - keep it as-is
return item;
}
// Otherwise, this is the one we want - return an updated value
return {
...item,
...action.item
};
});
}
// With immer
function updateObjectInArray(array, action) {
return immer(array, draft => {
draft[action.index] = { ...item, ...action.item}
// Alternatively, since arbitrarily deep updates are supported:
// Object.assign(draft[action.index], action.item)
})
}
```
## Performance

@@ -175,0 +238,0 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc