Hacktoberfest 2026: le issue che i maintainer hanno segnato per ottobre, aperte e adatte ai principianti. Sfoglia le issue Hacktoberfest

appendQueryString should combine duplicates or error if different

Aperta
#37 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
30/100
Tipo di issue
Funzionalità
Chiarezza
Abbastanza chiara
Stato di attività
Ferma
Stack tecnologico
javascript
Ambito
tooling

Direzione di ricerca

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.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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

Lingua principale
TypeScript
Stelle
3
Fork
5
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Preparare l'ambiente

Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di phetsims/query-string-machine

Tutte le issue di phetsims/query-string-machine

Issue simili

Altre issue su TypeScript

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.