4.0.2 ā€¢ Published 1 year ago

smart-array-filter v4.0.2

Weekly downloads
11
License
MIT
Repository
github
Last release
1 year ago

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

OptionTypeDefaultDescription
keywordsstring or string[][]The list of keywords to search for.
limitnumberInfinityThe maximum number of results to return.
caseSensitivebooleanfalseWhether the search should be case sensitive.
predicate"AND" or "OR""AND"The predicate to use to combine matches between keywords.
depthnumberInfinityThe depth to which the objects are inspected.
includePathsstring[][]The paths to include when searching for matches.
ignorePathsstring[][]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

4.0.1

1 year ago

4.0.2

1 year ago

3.2.0

2 years ago

4.0.0

2 years ago

3.1.3

2 years ago

3.1.2

2 years ago

3.1.1

2 years ago

3.1.0

2 years ago

3.0.0

2 years ago

2.3.0

3 years ago

2.2.0

3 years ago

2.1.0

3 years ago

2.0.3

4 years ago

2.0.0

4 years ago

1.1.0

8 years ago

0.5.0

8 years ago

0.4.1

8 years ago

0.4.0

8 years ago

0.3.0

8 years ago

0.2.2

8 years ago

0.2.0

9 years ago

0.1.1

9 years ago

0.1.0

9 years ago

0.0.2

9 years ago

0.0.1

9 years ago