1.0.0 • Published 2 years ago

@trs/accessor v1.0.0

Weekly downloads
-
License
MIT
Repository
-
Last release
2 years ago

@trs/accessor

Deeply access the path of a Javascript object using a JSONPath-like syntax with support for sub-selects.

Getting Started

import {access} from '@trs/accessor';

const value = {
  a: {
    b: [1, 2],
  },
};

const path = 'a.b[1]';

const result = access(path, value);

assert(result === 2);

Usage

Paths can be resolved using sub-paths surrounded by curly braces ({}). These are resolved outward.

const value = {
  a: {
    a1: [3, 4]
  },
  b: {
    b2: 'c'
  }
  c: 1,
}

const path = 'a.a1[{{b.b2}}]';

const result = access(path, value);

assert(result === 4);

Here we resolve b.b2 first, giving us 'c'. As this is also wrapped in curly braces, we resolve c giving us 1. We then resolve a.a1[1] giving us 4.