From 6dd9ad12db85fc163e78e15e8c0f62c27f5480a9 Mon Sep 17 00:00:00 2001 From: loynoir Date: Sat, 7 Aug 2021 20:33:55 +0000 Subject: [PATCH] Add `useJsRegexpLookbehind` option (#105) --- index.d.ts | 7 +++++++ index.js | 24 +++++++++++++++++++++++- 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/index.d.ts b/index.d.ts index b2ab84e..e69a967 100644 --- a/index.d.ts +++ b/index.d.ts @@ -242,6 +242,13 @@ export interface Options { ``` */ readonly sortQueryParameters?: boolean; + + /** + prefer js-regexp-lookbehind{@link https://caniuse.com/js-regexp-lookbehind} + + @default true + */ + readonly preferJsRegexpLookbehind?: boolean; } /** diff --git a/index.js b/index.js index f8297f5..cd5c6d7 100644 --- a/index.js +++ b/index.js @@ -70,6 +70,8 @@ export default function normalizeUrl(urlString, options) { removeSingleSlash: true, removeDirectoryIndex: false, sortQueryParameters: true, + // ref: https://caniuse.com/js-regexp-lookbehind + preferJsRegexpLookbehind: true, ...options, }; @@ -121,7 +123,27 @@ export default function normalizeUrl(urlString, options) { // Remove duplicate slashes if not preceded by a protocol if (urlObject.pathname) { - urlObject.pathname = urlObject.pathname.replace(/(? { + if (/^(?!\/)/g.test(p1)) { + return `${p1}/`; + } + + return '/'; + }); + } } // Decode URI octets