search-array

1.6.0 • Public • Published

JsonSearch

downloads version issues package size forks stars license programming language test status

Easy and lightweight search library for finding items inside an array of objects

Query options

You can use any of the below syntaxes to quey items inside arrays. A combination of these items is also possible.

  • [keywords]: Returns items that contain any of the keywords. Example, test, free text search
  • "[keyword]": Returns items that contain the exact keyword. It preserves white spaces. Example, Back End
  • -[keyword]: Items not containing the keyword.
    • You can use exact match syntax as well like, -"Query with space"
  • [key]:[keyword]: Returns items that have the [keyword] in the [key] property of the object. Example, name:bob
    • you can use exact match or not containing query syntaxes as well. Example, name:"Bob Rich" or, -name:bob (name should not contain bob)

Usage

Node

Installation:

npm i search-array

Usage:

const JsonSearch = require('search-array').default

const objectArray = [
  {id:1, title: 'Good book', author: 'Jim', colors: 'red'},
  {id:2, title: 'Interesting Movie', author: 'Bob', colors: 'dark red'},
  {id:3, title: 'Good Series', author: 'Alex', colors: 'dark blue'},
  {id:4, title: 'Something', author: 'Feri', colors: ['red', 'blue']}
]

const searcher = new JsonSearch(objectArray)
let foundObjects = searcher.query('good')
console.log(foundObjects) // prints items with id 1 and 3

foundObjects = searcher.query('good -red')
console.log(foundObjects) // prints item 3

foundObjects = searcher.query('good -colors:"dark blue"')
console.log(foundObjects) // prints item 1

foundObjects = searcher.query('red')
console.log(foundObjects) // prints item 1, 2 and 4

Enale sorting

By default, the ordering of the original array is preserved in the results. But it's possible to sort results based on their match strength, example:

const searcher = new JsonSearch(objectArray, {
  sort: true
})

So, the first item in the query() results would be a better match for the query compared to the last item in the results.

Defining search indice

By default all object keys of the first item in the objectArray will be used for finding the items in the whole array. But you can specify search object keys by passing an object of indice as options. The syntax is as followings:

const searcher = new JsonSearch(objectArray, {
  indice: {
    'key used in query': 'corresponsding key in the object'
  }
})

Example:

const JsonSearch = require('search-array').default

const objectArray = [
  {id:1, title: 'Good book', author: 'Jim', colors: 'red'},
  {id:2, title: 'Interesting Movie', author: 'Bob', colors: 'dark red'},
  {id:3, title: 'Good Series', author: 'Alex', colors: 'dark blue'},
  {id:4, title: 'Something', author: 'Feri', colors: ['red', 'blue']}
]

const searcher = new JsonSearch(objectArray, {
  indice: {
    'title': 'title', // search the `title`
    'name': 'author' // search the `author` but it's renamed as `name` in queries
  }
})
let foundObjects = searcher.query('good')
console.log(foundObjects) // prints items with id 1 and 3

let foundObjects = searcher.query('name:Jim')
console.log(foundObjects) // prints item with id 1

let foundObjects = searcher.query('author:Jim')
console.log(foundObjects) // finds nothing, the index `author` has not been defined in the options

foundObjects = searcher.query('red')
console.log(foundObjects) // finds nothing because the `red` does not exist in the 'title' or 'author' properties of items

Browser

Use as module:

<script type="module">
  import JsonSearch from 'https://unpkg.com/search-array/dist/esm/min/JsonSearch.js'

  const objectArray = [/*your objects here*/]
  const searcher = new JsonSearch(objectArray)
  let foundObjects = searcher.query('good')
</script>

Or, classic:

<script src="https://unpkg.com/search-array"></script>

<script>
  // the JsonSearch class is available here

  const objectArray = [/*your objects here*/]
  const searcher = new JsonSearch(objectArray)
  let foundObjects = searcher.query('good')
</script>

NPM

Installation:

npm i search-array
import JsonSearch from 'search-array'

...

TODO

  • Add support for nested objects
  • Add support for wild card query
  • Add options for:
    • Defining case sensivitiy

Package Sidebar

Install

npm i search-array

Weekly Downloads

150

Version

1.6.0

License

MIT

Unpacked Size

137 kB

Total Files

36

Last publish

Collaborators

  • ferrriii