Sasaki's Versions
System for applying incremental patches to a data structure.
Install
yarn add @strategies/versions
Example
Create your version patches
const versions = new Versions({
'1.0.0': file => {
// perform work on file. Mutating file itsself is okay since we always
// pass a fresh object into subsequent patches.
return file;
},
'2.0.0': file => {
// ...
return file;
},
// Patches can be declared out of order; that's okay.
'1.5.0': file => {
// ...
return file;
},
});
console.log(versions.latest) // -> '2.0.0'
console.log(versions.versions) // -> ['1.0.0', '1.5.0', '2.0.0']
Pass an object through
If the object does not contain a version
key, then the data starts at 0.0.0
.
const fileWithLatestPatch = versions.upgrade(file);
// fileWithLatestPatch.version === '2.0.0'
If you don't want to apply patches to the latest version, you can optionally specify which version as a second argument.
const fileAt1point5 = versions.upgrade(file, '1.5.0');