@@ -693,15 +693,17 @@ console.log(JSON.stringify(myURLs));
693693### Class: URLSearchParams
694694
695695The ` URLSearchParams ` API provides read and write access to the query of a
696- ` URL ` .
696+ ` URL ` . The ` URLSearchParams ` class can also be used standalone with one of the
697+ four following constructors.
697698
698699The WHATWG ` URLSearchParams ` interface and the [ ` querystring ` ] [ ] module have
699700similar purpose, but the purpose of the [ ` querystring ` ] [ ] module is more
700701general, as it allows the customization of delimiter characters (` & ` and ` = ` ).
701702On the other hand, this API is designed purely for URL query strings.
702703
703704``` js
704- const URL = require (' url' ).URL ;
705+ const { URL , URLSearchParams } = require (' url' );
706+
705707const myURL = new URL (' https://example.org/?abc=123' );
706708console .log (myURL .searchParams .get (' abc' ));
707709 // Prints 123
@@ -714,11 +716,125 @@ myURL.searchParams.delete('abc');
714716myURL .searchParams .set (' a' , ' b' );
715717console .log (myURL .href );
716718 // Prints https://example.org/?a=b
719+
720+ const newSearchParams = new URLSearchParams (myURL .searchParams );
721+ // The above is equivalent to
722+ // const newSearchParams = new URLSearchParams(myURL.search);
723+
724+ newSearchParams .append (' a' , ' c' );
725+ console .log (myURL .href );
726+ // Prints https://example.org/?a=b
727+ console .log (newSearchParams .toString ());
728+ // Prints a=b&a=c
729+
730+ // newSearchParams.toString() is implicitly called
731+ myURL .search = newSearchParams;
732+ console .log (myURL .href );
733+ // Prints https://example.org/?a=b&a=c
734+ newSearchParams .delete (' a' );
735+ console .log (myURL .href );
736+ // Prints https://example.org/?a=b&a=c
717737```
718738
719- #### Constructor: new URLSearchParams([ init] )
739+ #### Constructor: new URLSearchParams()
740+
741+ Instantiate a new empty ` URLSearchParams ` object.
742+
743+ #### Constructor: new URLSearchParams(string)
744+
745+ * ` string ` {string} A query string
746+
747+ Parse the ` string ` as a query string, and use it to instantiate a new
748+ ` URLSearchParams ` object. A leading ` '?' ` , if present, is ignored.
749+
750+ ``` js
751+ const { URLSearchParams } = require (' url' );
752+ let params;
753+
754+ params = new URLSearchParams (' user=abc&query=xyz' );
755+ console .log (params .get (' user' ));
756+ // Prints 'abc'
757+ console .log (params .toString ());
758+ // Prints 'user=abc&query=xyz'
759+
760+ params = new URLSearchParams (' ?user=abc&query=xyz' );
761+ console .log (params .toString ());
762+ // Prints 'user=abc&query=xyz'
763+ ```
764+
765+ #### Constructor: new URLSearchParams(obj)
766+
767+ * ` obj ` {Object} An object representing a collection of key-value pairs
720768
721- * ` init ` {String} The URL query
769+ Instantiate a new ` URLSearchParams ` object with a query hash map. The key and
770+ value of each property of ` obj ` are always coerced to strings.
771+
772+ * Note* : Unlike [ ` querystring ` ] [ ] module, duplicate keys in the form of array
773+ values are not allowed. Arrays are stringified using [ ` array.toString() ` ] [ ] ,
774+ which simply joins all array elements with commas.
775+
776+ ``` js
777+ const { URLSearchParams } = require (' url' );
778+ const params = new URLSearchParams ({
779+ user: ' abc' ,
780+ query: [' first' , ' second' ]
781+ });
782+ console .log (params .getAll (' query' ));
783+ // Prints ['first,second']
784+ console .log (params .toString ());
785+ // Prints 'user=abc&query=first%2Csecond'
786+ ```
787+
788+ #### Constructor: new URLSearchParams(iterable)
789+
790+ * ` iterable ` {Iterable} An iterable object whose elements are key-value pairs
791+
792+ Instantiate a new ` URLSearchParams ` object with an iterable map in a way that
793+ is similar to [ ` Map ` ] [ ] 's constructor. ` iterable ` can be an Array or any
794+ iterable object. That means ` iterable ` can be another ` URLSearchParams ` , in
795+ which case the constructor will simply create a clone of the provided
796+ ` URLSearchParams ` . Elements of ` iterable ` are key-value pairs, and can
797+ themselves be any iterable object.
798+
799+ Duplicate keys are allowed.
800+
801+ ``` js
802+ const { URLSearchParams } = require (' url' );
803+ let params;
804+
805+ // Using an array
806+ params = new URLSearchParams ([
807+ [' user' , ' abc' ],
808+ [' query' , ' first' ],
809+ [' query' , ' second' ]
810+ ]);
811+ console .log (params .toString ());
812+ // Prints 'user=abc&query=first&query=second'
813+
814+ // Using a Map object
815+ const map = new Map ();
816+ map .set (' user' , ' abc' );
817+ map .set (' query' , ' xyz' );
818+ params = new URLSearchParams (map);
819+ console .log (params .toString ());
820+ // Prints 'user=abc&query=xyz'
821+
822+ // Using a generator function
823+ function * getQueryPairs () {
824+ yield [' user' , ' abc' ];
825+ yield [' query' , ' first' ];
826+ yield [' query' , ' second' ];
827+ }
828+ params = new URLSearchParams (getQueryPairs ());
829+ console .log (params .toString ());
830+ // Prints 'user=abc&query=first&query=second'
831+
832+ // Each key-value pair must have exactly two elements
833+ new URLSearchParams ([
834+ [' user' , ' abc' , ' error' ]
835+ ]);
836+ // Throws TypeError: Each query pair must be a name/value tuple
837+ ```
722838
723839#### urlSearchParams.append(name, value)
724840
@@ -975,6 +1091,8 @@ console.log(myURL.origin);
9751091[ `require('url').format()` ] : #url_url_format_url_options
9761092[ `url.toString()` ] : #url_url_tostring
9771093[ Punycode ] : https://tools.ietf.org/html/rfc5891#section-4.4
1094+ [ `Map` ] : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map
1095+ [ `array.toString()` ] : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/toString
9781096[ WHATWG URL ] : #url_the_whatwg_url_api
9791097[ `new URL()` ] : #url_constructor_new_url_input_base
9801098[ `url.href` ] : #url_url_href
0 commit comments