search-array

    1.4.1 • 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

    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 index keys
      • Defining case sensivitiy

    Install

    npm i search-array

    DownloadsWeekly Downloads

    13

    Version

    1.4.1

    License

    MIT

    Unpacked Size

    95.2 kB

    Total Files

    31

    Last publish

    Collaborators

    • ferrriii