npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

search-array

v1.6.0

Published

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

Downloads

535

Readme

JsonSearch

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