From 6512635f043beee4ff51d524d1d72c671af385f7 Mon Sep 17 00:00:00 2001 From: Blake Embrey Date: Wed, 10 Jan 2024 15:13:18 -0500 Subject: [PATCH] Remove lookbehind from regex --- packages/change-case/src/index.spec.ts | 20 ++++++++++++++++++++ packages/change-case/src/index.ts | 5 +++-- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/packages/change-case/src/index.spec.ts b/packages/change-case/src/index.spec.ts index e4a5c905..14152e8a 100644 --- a/packages/change-case/src/index.spec.ts +++ b/packages/change-case/src/index.spec.ts @@ -208,6 +208,26 @@ const tests: [string, Result, Options?][] = [ separateNumbers: true, }, ], + [ + "𝒳123", + { + camelCase: "𝒳_123", + capitalCase: "𝒳 123", + constantCase: "𝒳_123", + dotCase: "𝒳.123", + kebabCase: "𝒳-123", + noCase: "𝒳 123", + pascalCase: "𝒳_123", + pascalSnakeCase: "𝒳_123", + pathCase: "𝒳/123", + sentenceCase: "𝒳 123", + snakeCase: "𝒳_123", + trainCase: "𝒳-123", + }, + { + separateNumbers: true, + }, + ], [ "1test", { diff --git a/packages/change-case/src/index.ts b/packages/change-case/src/index.ts index 6bee8053..a84701f2 100644 --- a/packages/change-case/src/index.ts +++ b/packages/change-case/src/index.ts @@ -3,7 +3,7 @@ const SPLIT_LOWER_UPPER_RE = /([\p{Ll}\d])(\p{Lu})/gu; const SPLIT_UPPER_UPPER_RE = /(\p{Lu})([\p{Lu}][\p{Ll}])/gu; // Used to iterate over the initial split result and separate numbers. -const SPLIT_SEPARATE_NUMBER_RE = /(?<=\d)(\p{Ll})|(?<=\p{L})(\d)/u; +const SPLIT_SEPARATE_NUMBER_RE = /(\d)\p{Ll}|(\p{L})\d/u; // Regexp involved with stripping non-word characters from the result. const DEFAULT_STRIP_REGEXP = /[^\p{L}\d]+/giu; @@ -72,7 +72,8 @@ export function splitSeparateNumbers(value: string) { const word = words[i]; const match = SPLIT_SEPARATE_NUMBER_RE.exec(word); if (match) { - words.splice(i, 1, word.slice(0, match.index), word.slice(match.index)); + const offset = match.index + (match[1] ?? match[2]).length; + words.splice(i, 1, word.slice(0, offset), word.slice(offset)); } } return words;