Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

match-sorter

Package Overview
Dependencies
Maintainers
1
Versions
46
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

match-sorter - npm Package Compare versions

Comparing version 1.0.0 to 1.1.0

46

dist/index.js

@@ -19,13 +19,15 @@ (function (global, factory) {

});
var matchRankMap = {
equals: 5,
startsWith: 4,
wordStartsWith: 3,
contains: 2,
acronym: 1,
matches: 0,
noMatch: -1
var rankings = {
EQUAL: 6,
STARTS_WITH: 5,
WORD_STARTS_WITH: 4,
CONTAINS: 3,
ACRONYM: 2,
MATCHES: 1,
NO_MATCH: 0
};
matchSorter.rankings = rankings;
exports.default = matchSorter;
exports.rankings = rankings;

@@ -43,2 +45,4 @@

var keys = options.keys;
var _options$threshold = options.threshold;
var threshold = _options$threshold === undefined ? rankings.MATCHES : _options$threshold;

@@ -57,3 +61,3 @@ var matchedItems = items.reduce(reduceItemsToRanked, []);

if (rank > matchRankMap.noMatch) {
if (rank >= threshold) {
matches.push({ item: item, rank: rank, index: index, keyIndex: keyIndex });

@@ -79,7 +83,7 @@ }

return { rank: rank, keyIndex: keyIndex };
}, { rank: matchRankMap.noMatch, keyIndex: -1 });
}, { rank: rankings.NO_MATCH, keyIndex: -1 });
}
/**
* Gives a matchRankMap score based on how well the two strings match.
* Gives a rankings score based on how well the two strings match.
* @param {String} testString - the string to test against

@@ -96,3 +100,3 @@ * @param {String} stringToRank - the string to rank

if (stringToRank.length > testString.length) {
return matchRankMap.noMatch;
return rankings.NO_MATCH;
}

@@ -102,3 +106,3 @@

if (testString === stringToRank) {
return matchRankMap.equals;
return rankings.EQUAL;
}

@@ -108,3 +112,3 @@

if (testString.indexOf(stringToRank) === 0) {
return matchRankMap.startsWith;
return rankings.STARTS_WITH;
}

@@ -114,3 +118,3 @@

if (testString.indexOf(' ' + stringToRank) !== -1) {
return matchRankMap.wordStartsWith;
return rankings.WORD_STARTS_WITH;
}

@@ -120,3 +124,3 @@

if (testString.indexOf(stringToRank) !== -1) {
return matchRankMap.contains;
return rankings.CONTAINS;
} else if (stringToRank.length === 1) {

@@ -126,3 +130,3 @@ // If the only character in the given stringToRank

// it's definitely not a match.
return matchRankMap.noMatch;
return rankings.NO_MATCH;
}

@@ -132,3 +136,3 @@

if (getAcronym(testString).indexOf(stringToRank) !== -1) {
return matchRankMap.acronym;
return rankings.ACRONYM;
}

@@ -158,3 +162,3 @@

/**
* Returns a matchRankMap.matches or noMatch score based on whether
* Returns a rankings.matches or noMatch score based on whether
* the characters in the stringToRank are found in order in the

@@ -186,6 +190,6 @@ * testString

if (!found) {
return matchRankMap.noMatch;
return rankings.NO_MATCH;
}
}
return matchRankMap.matches;
return rankings.MATCHES;
}

@@ -192,0 +196,0 @@

{
"name": "match-sorter",
"version": "1.0.0",
"version": "1.1.0",
"description": "Simple, expected, and deterministic best-match sorting of an array in JavaScript",

@@ -5,0 +5,0 @@ "main": "dist/index.js",

@@ -64,2 +64,4 @@ # match-sorter

// You can also pass an options object:
// **keys** (defaults to undefined and just uses the value itself as above)
const objList = [

@@ -73,4 +75,27 @@ {name: 'Janice', color: 'Green'},

matchSorter(objList, 're', {keys: ['color', 'name']}) // [{name: 'Jen', color: 'Red'}, {name: 'Janice', color: 'Green'}, {name: 'Fred', color: 'Orange'}]
// **threshold** (defaults to MATCH)
const fruit = ['orange', 'apple', 'grape', 'banana']
matchSorter(fruit, 'ap', {threshold: matchSorter.rankings.NO_MATCH}) // ['apple', 'grape', 'orange', 'banana'] (returns all items, just sorted by best match)
const things = ['google', 'airbnb', 'apple', 'apply', 'app'],
matchSorter(things, 'app', {threshold: matchSorter.rankings.EQUAL}) // ['app'] (only items that are equal)
const otherThings = ['fiji apple', 'google', 'app', 'crabapple', 'apple', 'apply']
matchSorter(otherThings, 'app', {threshold: matchSorter.rankings.WORD_STARTS_WITH}) // ['app', 'apple', 'apply', 'fiji apple'] (everything that matches with "word starts with" or better)
/*
* Available thresholds (from top to bottom) are:
* - EQUAL
* - STARTS_WITH
* - WORD_STARTS_WITH
* - CONTAINS
* - ACRONYM
* - MATCHES
* - NO_MATCH
*/
```
> In the examples above, we're using CommonJS. If you're using ES6 modules, then you can do:
>
> `import matchSorter, {rankings} from 'match-sorter'`
## Inspiration

@@ -77,0 +102,0 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc