question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Error with number filtering

See original GitHub issue

Hi. I have found possible bug when trying to filter an array with number type fields containing the same values

  • match-sorter version: 3.5.1
  • node version: 10.15.3
  • npm version: 6.9.0

Code Example:

let p = [{ name: 'samuel', likes: 0 }, { name: 'pedro', likes: 0 }];

const result = matchSorter(p, '0', {
	keys: ['name', 'likes'],
	threshold: rankings.WORD_STARTS_WITH
});

console.log(result);

Error: TypeError aRankedItem.localeCompare is not a function

Live Example

Problem: If I’m not mistaken, the problem is that the match-sorter try to call the function <localeCompare> of the String class from a variable of Number type. My solution was simply, when trying to compare, I cast the variable to String in any case.

Possible Solution:

function sortRankedItems(a, b) {
	var aFirst = -1;
	var bFirst = 1;
	var aRankedItem = a.rankedItem,
		aRank = a.rank,
		aKeyIndex = a.keyIndex;
	var bRankedItem = b.rankedItem,
		bRank = b.rank,
		bKeyIndex = b.keyIndex;

	if (aRank === bRank) {
		if (aKeyIndex === bKeyIndex) {
			return String(aRankedItem).localeCompare(bRankedItem);
		} else {
			return aKeyIndex < bKeyIndex ? aFirst : bFirst;
		}
	} else {
		return aRank > bRank ? aFirst : bFirst;
	}
}

I dont know if it’s good solution but works perfect for now.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
kentcdoddscommented, Jun 12, 2019

That suggested solution sounds great to me. Would you be willing to implement it?

0reactions
kentcdoddscommented, Jun 13, 2019

Maybe this will help: https://kcd.im/pull-request

Read more comments on GitHub >

github_iconTop Results From Across the Web

Error (Filtering using a number on a column which
Filtering using a number on a column which contains both strings and numbers is not currently supported. Its a very simple flow.
Read more >
Error when global filter called for numeric columns · Issue #4210
Using the latest ColumDefs documentation, I updated my numeric columns to display a string instead of a number (since it doesn't really make ......
Read more >
How to use Excel filtering to resolve file format errors
Right-click (or Control-click on a Mac) on the cell containing the data in error. On Windows, select Filter > Filter by Selected Cell's...
Read more >
ELMAH—Error Filtering Examples
To learn more about how to filter harmless or noisy errors from your logs, see “Error Filtering”. Filter requests for favicon.ico that end...
Read more >
FIltering on blank number returns error - DNNSharp Helpcenter
When using the filter on a column containing only numbers (including blanks), filtering on a blank number returns an error (Cannot perform '='...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found