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

smart-array-filter

v4.0.2

Published

Filter an array of objects

Downloads

4,089

Readme

smart-array-filter

NPM version build status Test coverage npm download

Filter an array of objects

Installation

npm install smart-array-filter

Documentation

Basic usage

const array = [
  {
    name: 'Cheetah',
    lifeExpectancy: 10,
    taxonomy: {
      phylum: 'Chordata',
      class: 'Mammalia',
      order: 'Carnivora',
      family: 'Felidae',
    },
    characteristics: {
      prey: 'Gazelle, Wildebeest, Hare',
      distinctiveFeature: 'Yellowish fur covered in small black spots',
    },
    locations: ['Africa', 'Asia', 'Eurasia'],
  },
  {
    name: 'Gorilla',
    lifeExpectancy: 40,
    taxonomy: {
      phylum: 'Chordata',
      class: 'Mammalia',
      order: 'Primates',
      family: 'Hominidae',
    },
    locations: ['Africa'],
  },
];
const filteredData = filter(array, {
  keywords: 'gorilla', // search for any field that contains the "gorilla" string
});

Options

| Option | Type | Default | Description | | --------------- | ---------------------- | ---------- | --------------------------------------------------------- | | keywords | string or string[] | [] | The list of keywords to search for. | | limit | number | Infinity | The maximum number of results to return. | | caseSensitive | boolean | false | Whether the search should be case sensitive. | | predicate | "AND" or "OR" | "AND" | The predicate to use to combine matches between keywords. | | depth | number | Infinity | The depth to which the objects are inspected. | | includePaths | string[] | [] | The paths to include when searching for matches. | | ignorePaths | string[] | [] | The paths to ignore when searching for matches. |

Recursivity

By default, objects are inspected recursively. This means that if your data has circular references, it might throw a RangeError: Maximum call stack size exceeded error.

To limit recursivity at a certain depth, you can set the depth option.

Search within specific fields

To search within a specific key, you can prefix your search term with the field's key followed by a colon.

name:Gorilla will match the name field of the Gorilla entry. order:Primates will match the taxonomy.order field of the Gorilla entry. taxonomy.order will match the taxonomy.order field of the Gorilla entry.

You can use the includePaths or the ignorePaths options to define which fields should be included or ignored when finding search matches.

Keywords

The keywords option can be a string or an array of strings.

If it's a string it will be split by space / newlines and each part will be considered as a keyword. When searching for content with spaces or newlines, the content must be enclosed in double quotes.

  • 🚫 { keywords: 'distinctiveFeature:black spots' } this creates 2 keywords
  • { keywords: 'distinctiveFeature:"black spots"' }

If keywords is an array of strings, each string will be considered as a keyword and there is no need for enclosing spaces in double quotes.

{ keywords: [ 'distinctiveFeature:black spots' ] }

Operators

  • No operator: name:Gori will match the Gorilla entry because the name contains "Gori".
  • Strict equality: name:=Gorilla will match the name, but not name:=Gorill.
  • Range operators
    • >, >=, <, <=. Example: lifeExpectancy:>=40
    • .. to define a range. Example: lifeExpectancy:10..40
    • The range operators also work with strings, using the ascii code.
  • Negation: -name:Gorilla will match all entries except the ones which match name:Gorilla. This operator can be combined with other operators: -name:=Gorilla.

Search within arrays

Arrays are traversed like objects, but inherit their parent's field name. For example in the Cheetah example, locations: ['Africa', 'Asia', 'Eurasia'] behaves as if there were 3 different properties but all with the locations name.

Any of those keywords will match the Cheetah entry: locations:Africa, locations:Asia, locations:Eurasia

Search predicate

By default, the search using the "AND" predicate, which is returning the intersection of the matches across all keywords. You can change this behavior by setting the predicate option to "OR".

filter(animals, {
  keywords: ['Gorilla', 'Cheetah'],
  predicate: 'OR', // will return the union of the matches on the "Gorilla" and "Cheetah" keywords
});

License

MIT