Proversion
Upgrade versionable objects via immer produce
Install
npm i -S proversion
prerequisite
immer 6.0.0
Usage
Provides a simple method to upgrade objects according to version rules.
Example
A simple upgrade of an object from version 1 to version 2.
const upgrades = ;upgrades; const original = version: 1 ;const applied result = upgrades; // Applied is an array of applied upgradesconsole;applied 2 // result is the upgraded objectconsole;result version: 2 myAddedParam: true
Note: Original is untouched thanks to Immer
Example 2
Multiple upgrades
const upgrades = ;upgrades;upgrades;upgrades;upgrades;upgrades; const original = version: 1 ;const applied result = upgrades; // Applied is an array of applied upgradesconsole;applied 2 3 4 5 6 // result is the upgraded objectconsole;result version: 6 myAddedParam: false hi: 'not_today'
But notice what happens if we pass in a version 3 object.
const original = version: 3 myAddedParam: false hi: 'There' ;const applied result = upgrades; // Applied is an array of applied upgradesconsole;applied 4 5 6 // result is the upgraded objectconsole;result version: 6 myAddedParam: false hi: 'not_today'
we get the same result, but the version 1->2 and 2->3 upgrades are ignored.
Immer
Why use immer produce?
- Safe mutation of objects without affecting original
- Clean mutation API via
draft
object - Converters are pure functions
Immer
is well maintained and has excellent documentation