Inspired by ES6 deconstruct. A safe extract of js object properties into a new object
npm install revampjsInspired by ES6 deconstruct. A safe extract of js object properties into a new object
I am still working on the ergonomics of this helper library so breaking changes are to be expected at the moment.
Please feel free to open an issue if you have any suggestion.
javascript
// do not try this, it will not work!
const myNewObject = {a, b, c} = mySourceObject
`So I ended up writing helper functions to do the result object wrapping. As it was becoming repetitive, I tried to wrap it up into this library. I hope it can useful for others as well.
Features
- [x] Safely extract any value(s) (nested or not) from a source object
- [x] Rename extracted properties in destination object
- [x] Ligh-version for simply picking properties- [x] Travis CI to run test suite
Changelog (0.2.0)
- [x] Support for ImmutableJS objectsPlan
- [ ] Finaliwe syntax of parameter for values extraction
- [ ] Add test cases to demo the usage with ImmutableJS
- [ ] Support of an equivalent of {...rest} from ES6 (maybe)
- [ ] Add warning when attempting to access a palsy keyGetting started
$3
- with npm
`
npm install --save revampjs
`
- or with yarn
`
yarn add revampjs
`$3
- using require
`javascript
const revamp = require('revampjs');
`
- or using ES6 import (not tested yet)
`javascript
import * as revamp from 'revampjs';
``javascript
const sourceObject = {
a: 42,
b: 'hello',
c: 'world',
d: '!',
e: {
f: 'nested value'
}
}const myNewObject = revamp.pick(sourceObject, ['b', 'c']);
const myNewObjectRevamped = revamp.extract(sourceObject, {
newKey: 'b',
newKeyForNested: 'e.f'
});
`$3
Both functions pick and extract accept a third optional parameter isSrcImmutable.
If you pass true to this value, then the functions will consider the src parameter as an ImmutableJS object.Please note that:
- this feature has just been added and test coverage has not been updated yet.
- this library itself does not use ImmutableJS. Therefore the returned object is never an ImmutableJS object (though each extracted value is).
- the
extract` function assumes that the whole src object and its properties are immutable.