Get wrong results when use regexp or Symbol as function arguments
See original GitHub issueBecause use JSON.stringify(args)
in defaultCacheKey
function, it will get wrong results when using regexp or Symbol as function arguments.
The reasons are:
console.log(JSON.stringify(/Sindre Sorhus/));
// => '{}'
console.log(JSON.stringify(/Elvin Peng/));
// => '{}'
console.log(JSON.stringify({foo: Symbol('Sindre Sorhus')}));
// => '{}'
console.log(JSON.stringify({foo: Symbol('Elvin Peng')}));
// =>'{}'
Issue Analytics
- State:
- Created 5 years ago
- Reactions:3
- Comments:9 (5 by maintainers)
Top Results From Across the Web
Regex for matching Functions and Capturing their Arguments
I have a function that searches the expression for math functions using Regex, retrieves the arguments, looks up the function name, and ...
Read more >Regular Expressions :: Eloquent JavaScript
A regular expression is a type of object. It can be either constructed with the RegExp constructor or written as a literal value...
Read more >RegExp - JavaScript - MDN Web Docs - Mozilla
Chrome Edge
RegExp Full support. Chrome1. Toggle history Full support. Edge12. Toggle history
@@match Full support. Chrome50. Toggle history Full support. Edge13. Toggle history
@@matchAll Full...
Read more >Methods of RegExp and String - The Modern JavaScript Tutorial
In this article we'll cover various methods that work with regexps in-depth. str.match(regexp). The method str.match(regexp) finds matches ...
Read more >re — Regular expression operations — Python 3.11.1 ...
The solution is to use Python's raw string notation for regular expression patterns; backslashes are not handled in any special way in a...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I just realized something, order matters:
This means that the cache is hit even less than we realize
I’m open to suggestions on how to better handle this. A
Symbol
cannot really be properly stringified as it doesn’t have any value. Even if you give two symbols the same description, per definition, they are supposed to be unique, but there’s no way (as far as I know) for us to stringify them as unique values. A regex could be handled somehow though, since it can be stringified with.toString()
. So maybe if instead of JSON.stringifying the whole arguments object, we stringify each item and also include its type and also specially handle regex? That would still not solve the Symbol case.// @maxwellgerber @keithamus @LinusU