perf(has/get): improve performance (#518)

This commit is contained in:
D-Sketon 2024-09-13 07:52:28 +08:00 committed by GitHub
parent 7258981909
commit b1bb77b602
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1,3 +1,21 @@
const ESCAPE_REGEXP = /\\(\\)?/g;
const PROPERTY_REGEXP = RegExp(
// Match anything that isn't a dot or bracket.
'[^.[\\]]+' +
'|' +
// Or match property names within brackets.
'\\[(?:' +
// Match a non-string expression.
'([^"\'][^[]*)' +
'|' +
// Or match strings (supports escaping characters).
'(["\'])((?:(?!\\2)[^\\\\]|\\\\.)*?)\\2' +
')\\]' +
'|' +
// Or match "" as the space between consecutive dots or empty brackets.
'(?=(?:\\.|\\[\\])(?:\\.|\\[\\]|$))',
'g'
);
/**
* Converts a deep key string into an array of path segments.
*
@ -16,25 +34,6 @@
* toPath('.a[b].c.d[e]["f.g"].h') // Returns ['', 'a', 'b', 'c', 'd', 'e', 'f.g', 'h']
*/
export function toPath(deepKey: string): string[] {
const ESCAPE_REGEXP = /\\(\\)?/g;
const PROPERTY_REGEXP = RegExp(
// Match anything that isn't a dot or bracket.
'[^.[\\]]+' +
'|' +
// Or match property names within brackets.
'\\[(?:' +
// Match a non-string expression.
'([^"\'][^[]*)' +
'|' +
// Or match strings (supports escaping characters).
'(["\'])((?:(?!\\2)[^\\\\]|\\\\.)*?)\\2' +
')\\]' +
'|' +
// Or match "" as the space between consecutive dots or empty brackets.
'(?=(?:\\.|\\[\\])(?:\\.|\\[\\]|$))',
'g'
);
const result: string[] = [];
if (deepKey[0] === '.') {