2.0.0 • Published 7 years ago

ok-selector-immutable v2.0.0

Weekly downloads
1
License
BSD-2-Clause
Repository
-
Last release
7 years ago

ok-selector

A small library for plucking values out of Immutable objects.

ok-selector supports immutablejs.

Given the following state:

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

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.