1.6.1 • Published 5 months ago

@nodescript/pointer v1.6.1

Weekly downloads
-
License
ISC
Repository
github
Last release
5 months ago

NodeScript Pointer

Simple library for getting and setting object values via JSON Pointer or dot-delimited formats.

Highlights

  • 🔥 Zero dependencies
  • 💻 Works in browser
  • 🗜 Tidy and compact, only ~500 bytes minified uncompressed
  • ⚡️ Blazing fast

Usage

import * as pointer from '@nodescript/pointer';

const object = {
    foo: {
        items: [
            { bar: 'one' },
            { bar: 'two' },
            { bar: 'three' },
        ]
    }
};

// If pointer starts with /, then it's interpreted
// as RFC6901 compliant JSON Pointer
pointer.get(object, '/foo/items/1/bar'); // 'two'

// Otherwise it's a dot-delimited path
pointer.get(object, 'foo.items.0'); // { bar: 'one' }

// Set modifies the object, creating additional objects and arrays as needed
const newObj = {}
pointer.set(newObj, 'foo.items.0.bar', '123');
// newObj is now { foo: { items: [{ bar: '123' }] } }

Spec

NodeScript Pointer aims to implement all the features from RFC6901 whilst also extending it to provide the following features:

  1. Dot-delimited paths:

    get({ foo: { bar: { baz: 123 }}}, 'foo.bar.baz')
    // 123
  2. Wildcard syntax to access array content:

    // Single level
    get({
        users: [
            { name: 'Joe' },
            { name: 'Jane' },
        ]
    }, 'users.*.name');
    // ['Joe', 'Jane']
    
    // Multiple levels
    get({
        items: [
            {
                tags: [{ value: 1 }, { value: 2 }],
            },
            {
                tags: [{ value: 3 }],
            },
        ]
    }, 'items.*.tags.*.value');
    // [ [ 1, 2 ], [ 3 ] ]
  3. Push values into array:

    const obj = {};
    set(obj, '/items/-', 'foo');
    // { items: ['foo' ] }
1.6.1

5 months ago

1.2.0

12 months ago

1.1.0

12 months ago

1.6.0

11 months ago

1.5.0

12 months ago

1.4.0

12 months ago

1.3.0

12 months ago

1.0.3

1 year ago