From 0447ec0d5ce2085a9d5caec2818bc979f2e3fab2 Mon Sep 17 00:00:00 2001 From: anasdevv Date: Sun, 28 Dec 2025 00:42:47 +0500 Subject: [PATCH 1/2] [Fix]: reset key state on type switch when duplicates is last Fixes #514 --- lib/parse.js | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/lib/parse.js b/lib/parse.js index bfba0fe1..8b33e931 100644 --- a/lib/parse.js +++ b/lib/parse.js @@ -60,6 +60,8 @@ var charsetSentinel = 'utf8=%E2%9C%93'; // encodeURIComponent('✓') var parseValues = function parseQueryStringValues(str, options) { var obj = { __proto__: null }; + var keysByRoot = options.duplicates === 'last' ? { __proto__: null } : undefined; + var cleanStr = options.ignoreQueryPrefix ? str.replace(/^\?/, '') : str; cleanStr = cleanStr.replace(/%5B/gi, '[').replace(/%5D/gi, ']'); @@ -131,10 +133,27 @@ var parseValues = function parseQueryStringValues(str, options) { } if (key !== null) { + if (options.duplicates === 'last') { + var keyIsArray = key.indexOf('[') !== -1; + var root = keyIsArray ? key.split('[')[0] : key; + var existingKeys = keysByRoot[root]; + if (!existingKeys) { + keysByRoot[root] = [key]; + } else if (existingKeys[0].indexOf('[') !== -1 === keyIsArray) { + existingKeys.push(key); + } else { + for (var j = 0; j < existingKeys.length; j++) { + delete obj[existingKeys[j]]; + } + keysByRoot[root] = [key]; + } + } + var existing = has.call(obj, key); - if (existing && options.duplicates === 'combine') { + if (existing && (options.duplicates === 'combine' || + (options.duplicates === 'last' && key !== null && key.indexOf('[]') !== -1))) { obj[key] = utils.combine(obj[key], val); - } else if (!existing || options.duplicates === 'last') { + } else if (!existing || options.duplicates === 'last' ) { obj[key] = val; } } From e72192330706de2b54bc95f35d00e4222bdb8d7e Mon Sep 17 00:00:00 2001 From: anasdevv Date: Tue, 30 Dec 2025 07:08:46 +0500 Subject: [PATCH 2/2] chore(lint): fix ESLint issues in parse.js --- lib/parse.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/parse.js b/lib/parse.js index 2a80e9e9..e3c382ca 100644 --- a/lib/parse.js +++ b/lib/parse.js @@ -150,10 +150,10 @@ var parseValues = function parseQueryStringValues(str, options) { } var existing = has.call(obj, key); - if (existing && (options.duplicates === 'combine' || - (options.duplicates === 'last' && key !== null && key.indexOf('[]') !== -1))) { + if (existing && (options.duplicates === 'combine' + || (options.duplicates === 'last' && key !== null && key.indexOf('[]') !== -1))) { obj[key] = utils.combine(obj[key], val); - } else if (!existing || options.duplicates === 'last' ) { + } else if (!existing || options.duplicates === 'last') { obj[key] = val; } }