https://github.com/distributedlife/ok-selector-immutable
ok-selector that works with ImmutableJS
https://github.com/distributedlife/ok-selector-immutable
Last synced: 2 months ago
JSON representation
ok-selector that works with ImmutableJS
- Host: GitHub
- URL: https://github.com/distributedlife/ok-selector-immutable
- Owner: distributedlife
- Created: 2017-07-03T12:46:08.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2017-07-04T07:06:41.000Z (almost 9 years ago)
- Last Synced: 2025-12-31T17:46:46.714Z (6 months ago)
- Language: JavaScript
- Size: 7.81 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# ok-selector
A small library for plucking values out of Immutable objects.
`ok-selector` supports [immutablejs](https://github.com/facebook/immutable-js).
Given the following state:
```javascript
const state = Immutable.fromJS({
level1: {
level2: {
level3: {
value: 'yes'
}
}
},
array: [
{id: 1, value: 2},
{id: 2, value: 4},
{id: 3, value: 6},
{id: 4, value: 8},
]
});
```
These tests hold true
```javascript
it('should support dot.strings', () => {
expect(read(state, 'level1.level2.level3.value')).to.equal('yes');
expect(read(state, 'level1.level2.level3').toJS()).to.deep.equal({value: 'yes'});
expect(read(state, 'array').toJS()).to.deep.equal([
{id: 1, value: 2},
{id: 2, value: 4},
{id: 3, value: 6},
{id: 4, value: 8},
]);
});
it('should support addressing arrays by id', () => {
expect(read(state, 'array:3').toJS()).to.deep.equal({id: 3, value: 6});
expect(read(state, 'array:3.value')).to.deep.equal(6);
});
it('should support plucking all values from an array', () => {
expect(read(state, 'array*.value').toJS()).to.deep.equal([2, 4, 6, 8]);
});
```
# has
`has` is like `read` but returns a boolean response.
# Unwrapping
`read` always returns an object of based on the underlying implementation. Sometimes this is not what you want. `unwrap` will convert the response of read to a native object. For immutable objects it'll call `toJS`. Depending on the size and complexity of your state tree this can be relatively expensive.