@gizt/selector
TypeScript icon, indicating that this package has built-in type declarations

1.0.11 • Public • Published

@gizt/selector - Fast & Simple JSON Selector

Node CI

@gizt/selector is a fast and intuitive JSON selector (the syntax is based on JSON notation and glob).

You can try it online here playground

Quick start

npm install @gizt/selector

Usage

// es6/browser
import select from '@gizt/selector'

// node
const select = require('@gizt/selector')

Example

let data = {
  "title": "Awesome",
  "users": [
    { "name": "John", "family": "Doe", "friends": [{ "name": "F1" }] },
    { "name": "Joe",  "family": "Dae", "friends": [{ "name": "F2" }] }
    ...
  ]
}

Selectors

Simple selector

Simple property/name selector

// Property selector
select('users[].name', data) // ['John', 'Joe', ...]

// Nested array
select('users[].friends[].name', data) // ['F1', 'F2']

Wildcard selector

Support prefix, suffix, middle and star

// prefix
select('users[].na*', data)  // ['John', 'Joe']

// suffix
select('users[].*ly', data)  // ['Doe', 'Dae']

// middle
select('users[].*am*', data) // ['John', 'Doe', 'Joe', 'Dae']

// star
select('users[].friends[].*', data) // ['F1', 'F2']

Array selector

Array index & slice

// index
select('users[1].name', data) // 'Joe'

// all elements start from index `1` same as `data.slice(1)`
select('users[1:].name', data) // ['Joe', 'Ja', ...]

// first 2 elements same a `data.slice(0, 2)`
select('users[:2].name', data) // ['John', 'Joe']

Array input

Also support array input

select('[].name', [ { name: 'a'}, { name: 'b' } ]) // ['a', 'b']

For more example, please see test directory

Readme

Keywords

Package Sidebar

Install

npm i @gizt/selector

Weekly Downloads

43

Version

1.0.11

License

MIT

Unpacked Size

64.7 kB

Total Files

27

Last publish

Collaborators

  • rezigned