Gathering detailed insights and metrics for fuzzy-predicate
Gathering detailed insights and metrics for fuzzy-predicate
Gathering detailed insights and metrics for fuzzy-predicate
Gathering detailed insights and metrics for fuzzy-predicate
Filter an array of objects (or an array of just about anything) down to only include those objects with properties somewhat matching the provided query.
npm install fuzzy-predicate
Typescript
Module System
Node Version
NPM Version
JavaScript (100%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
MIT License
2 Stars
5 Commits
2 Forks
1 Watchers
1 Branches
1 Contributors
Updated on Oct 08, 2017
Latest Version
1.0.1
Package Id
fuzzy-predicate@1.0.1
Size
6.47 kB
NPM Version
2.15.8
Node Version
4.4.7
Published on
Jan 15, 2017
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
4
Filter an array of objects (or an array of just about anything) down to only include those objects with properties somewhat matching the provided query.
1var fuzzy = require("fuzzy-predicate"); 2 3var data = [ 4 { 5 name: "Dan Smith" 6 }, 7 { 8 name: "Issac Long" 9 } 10]; 11 12var result = data.filter(fuzzy("dan")); 13 14console.log(result); 15// [{ 16// name: "Dan Smith" 17// }]
npm install fuzzy-predicate --save
1var fuzzy = require("fuzzy-predicate");
1// where "apple" is the data you're looking for 2var predicate = fuzzy("apple");
1var result = myArray.filter(predicate);
result
now contains only the elements in myArray
that somewhat match the query ("apple").
myArray
could have been an array of strings, an array of numbers, an array of objects, or even an array of arrays. fuzzy-predicate
will recursively search through all the data trying to find something that matches the original query.
fuzzy-predicate
is an ideal tool for using user input to filter a response from a Web service. Let's say you have an array of objects that each represent a user and you wanted to find user(s) named "John":
1var fuzzy = require("fuzzy-predicate"); 2 3var data = [ 4 { 5 id: "7128792", 6 name: "John Doe", 7 mail: "jdoe@example.com", 8 twitter: "john_doe" 9 }, 10 { 11 id: "1203922", 12 name: "Jane Doe", 13 mail: "jane.doe@example.com", 14 twitter: "grannysmithapple" 15 }, 16 { 17 id: "9189701", 18 name: "Dan Smith", 19 mail: "dan.smith@example.com", 20 twitter: "javascripz", 21 } 22]; 23 24var result = data.filter(fuzzy("john"));
In this scenario, result
would be an array containing a single element:
1{ 2 id: "7128792", 3 name: "John Doe", 4 mail: "jdoe@example.com", 5 twitter: "john_doe" 6}
But what if the query was "smith"?
1var result = data.filter(fuzzy("smith"));
result
would contain two elements:
1{ 2 id: "1203922", 3 name: "Jane Doe", 4 mail: "jane.doe@example.com", 5 twitter: "grannysmithapple" 6}, 7{ 8 id: "9189701", 9 name: "Dan Smith", 10 mail: "dan.smith@example.com", 11 twitter: "javascripz", 12}
When searching for "smith," fuzzy-predicate
found a match in the Twitter handle for Jane, and the name (and email) property for Dan.
Perhaps we only wanted to find people with a name matching "Smith":
1var result = data.filter(fuzzy("smith", ["name"]));
This time, result
would contain only one element:
1{ 2 id: "9189701", 3 name: "Dan Smith", 4 mail: "dan.smith@example.com", 5 twitter: "javascripz", 6}
fuzzy(query, keys)
Returns a filter predicate (function) suitable for passing to Array.prototype.filter
.
query
: The filter query to use to reduce an array down to objects matching the query. This can be a string or a number.keys
: Optionally restrict the search to a set of keys; only applied when filtering objects. This can be a string containing the name of a single key, or an array of keys.What makes this a "fuzzy" filter is that it is looking for values that somewhat match the query—not exact matches.
When comparing strings, the needle (the query) and the haystack value are both normalized following these rules:
\W
regex and underscores)Then, instead of checking string equality, it checks to see if the haystack value contains the needle value (using indexOf
). If it does, it's considered a match.
This process only applies when comparing strings; numbers must be exactly equal to be considered a match.
See LICENSE.
I welcome pull requests containing bug fixes and documentation improvements for fuzzy-predicate
. Be sure to run the tests before submitting any changes.
And although I consider fuzzy-predicate
to be mostly feature complete, I welcome discussion on how it could be a more useful tool (e.g. if callers could customize how normalization worked).
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
Reason
Found 0/5 approved changesets -- score normalized to 0
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no SAST tool detected
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Score
Last Scanned on 2025-07-07
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More