Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

appendQueryString should combine duplicates or error if different

未关闭
#37 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
30/100
Issue 类型
功能
描述清晰度
基本清楚
活跃度
停滞
技术栈
javascript
领域
tooling

调研方向

The proposed changes are in js/QueryStringMachine.js and js/QueryStringMachineTests.js; first read the existing appendQueryString implementation and run the current test file. Define the duplicate-key behavior for identical and differing values, then add tests covering it and finish when the documented behavior passes without regressing existing URL appending cases.

由索引模型根据 Issue 内容生成。

描述

type:misc

@samreid and I thought that this would be a nice improvement since QSM errors out if you supply a query parameter twice. We got a bit stuck though and have to give up right now. Here is a patch!

Index: js/QueryStringMachine.js
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- js/QueryStringMachine.js	(revision d0435aa45cc13c17aa1c8c9c21882539123249db)
+++ js/QueryStringMachine.js	(date 1581717644317)
@@ -311,7 +311,32 @@
        * // Limit to the second screen
        * simURL = QueryStringMachine.appendQueryString( simURL, 'screens=2' );
        */
-      appendQueryString: function( url, queryParameters ) {
+      appendQueryString: function( url, queryParameters, options ) {
+
+        options = options || {
+          combineDuplicates: true
+        };
+
+
+        const convertToSearchURI = queryString => {
+          if ( queryString.indexOf( '?' ) === 0 ) {
+            return queryString;
+          }
+          else if ( queryString( '&' ) === 0 ) {
+            return `?${queryString.substring( 1 )}`;
+          }
+          else {
+            return `?${queryString}`;
+          }
+        };
+
+        let allParametersForURL = [];
+        if ( url.indexOf( '?' ) > 0 ) {
+          debugger;
+          allParametersForURL = getKeysForString( `?${url.split( '?' )[ 1 ]}` );
+        }
+        let allParametersFromQueryParameters = getKeysForString( convertToSearchURI( queryParameters ) );
+
         if ( queryParameters.indexOf( '?' ) === 0 || queryParameters.indexOf( '&' ) === 0 ) {
           queryParameters = queryParameters.substring( 1 );
         }
@@ -400,6 +425,23 @@
       return values;
     };
 
+    /**
+     * Query strings may show the same key appearing multiple times, such as ?value=2&value=3.
+     * This method recovers all of the string values.  For this example, it would be ['2','3'].
+     *
+     * @param {string} string - the parameters string (including first '?')
+     * @returns {Array.<string>} -
+     */
+    const getKeysForString = function( string ) {
+      const keys = [];
+      const params = string.slice( 1 ).split( '&' );
+      for ( let i = 0; i < params.length; i++ ) {
+        const splitByEquals = params[ i ].split( '=' );
+        keys.push( splitByEquals[ 0 ] );
+      }
+      return keys;
+    };
+
     // Schema validation ===============================================================================================
 
     /**
@@ -667,7 +709,7 @@
      * validValues - array of the valid values for the query parameter
      * isValidValue - function that takes a parsed Object (not string) and checks if it is acceptable
      * elementSchema - specifies the schema for elements in an array
-     * separator -  array elements are separated by this string, defaults to `,`
+     * separator -  array elements are separated by this string, defaults to `, `
      * parse - a function that takes a string and returns an Object
      */
     const TYPES = {
Index: js/QueryStringMachineTests.js
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- js/QueryStringMachineTests.js	(revision d0435aa45cc13c17aa1c8c9c21882539123249db)
+++ js/QueryStringMachineTests.js	(date 1581715948637)
@@ -202,5 +202,20 @@
     test( 'http://localhost.com/hello.html?abc', '', 'http://localhost.com/hello.html?abc' );
     test( 'http://localhost.com/hello.html?abc', '?123', 'http://localhost.com/hello.html?abc&123' );
     test( 'http://localhost.com/hello.html?abc', '&123', 'http://localhost.com/hello.html?abc&123' );
+
+
+    const combineDuplicates = {
+      combineDuplicates: true
+    };
+
+    test( 'http://localhost.com/hello.html?abc&123', '&123', 'http://localhost.com/hello.html?abc&123', combineDuplicates );
+
+
+    assert.throws( () => {
+      QueryStringMachine.appendQueryString( 'http://localhost.com/hello.html?abc', 'abc=3', combineDuplicates )
+    }, 'duplicate parameter is not identical' );
+
+    // without combineDuplicates option, just allow multiple query parameters.
+    test( 'http://localhost.com/hello.html?abc', 'abc=3', 'http://localhost.com/hello.html?abc&abc=3' );
   } );
 } );
\ No newline at end of file

主要语言
TypeScript
星标
3
派生
5
PR 合并指标
30 天内没有已合并 PR

环境准备

这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

phetsims/query-string-machine 的其他 Issue

查看 phetsims/query-string-machine 的全部 Issue

相似的 Issue

更多 TypeScript Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。