From b1bb77b6022665683db0e07d2f86dc88ba6a51a7 Mon Sep 17 00:00:00 2001 From: D-Sketon <2055272094@qq.com> Date: Fri, 13 Sep 2024 07:52:28 +0800 Subject: [PATCH] perf(has/get): improve performance (#518) --- src/compat/_internal/toPath.ts | 37 +++++++++++++++++----------------- 1 file changed, 18 insertions(+), 19 deletions(-) diff --git a/src/compat/_internal/toPath.ts b/src/compat/_internal/toPath.ts index 1fc87ed7..fd1adbe6 100644 --- a/src/compat/_internal/toPath.ts +++ b/src/compat/_internal/toPath.ts @@ -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] === '.') {