Skip to content

gizt/selector

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

92 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

@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