MCPcopy Index your code
hub / github.com/nodejs/node / set

Method set

lib/internal/url.js:597–647  ·  view source on GitHub ↗
(name, value)

Source from the content-addressed store, hash-verified

595 }
596
597 set(name, value) {
598 if (typeof this !== 'object' || this === null || !(#searchParams in this))
599 throw new ERR_INVALID_THIS('URLSearchParams');
600
601 if (arguments.length < 2) {
602 throw new ERR_MISSING_ARGS('name', 'value');
603 }
604
605 const list = this.#searchParams;
606 name = StringPrototypeToWellFormed(`${name}`);
607 value = StringPrototypeToWellFormed(`${value}`);
608 const { length } = list;
609
610 // If there are any name-value pairs whose name is `name`, in `list`, set
611 // the value of the first such name-value pair to `value` and remove the
612 // others.
613 let found = false;
614 let write = 0;
615 for (let i = 0; i < length; i += 2) {
616 const cur = list[i];
617 let keep = true;
618 if (cur === name) {
619 if (!found) {
620 list[write] = cur;
621 list[write + 1] = value;
622 found = true;
623 } else {
624 keep = false;
625 }
626 } else if (write !== i) {
627 list[write] = cur;
628 list[write + 1] = list[i + 1];
629 }
630 if (keep)
631 write += 2;
632 }
633
634 if (found && write !== length) {
635 list.length = write;
636 }
637
638 // Otherwise, append a new name-value pair whose name is `name` and value
639 // is `value`, to `list`.
640 if (!found) {
641 ArrayPrototypePush(list, name, value);
642 }
643
644 if (this.#context) {
645 setURLSearchParamsModified(this.#context);
646 }
647 }
648
649 sort() {
650 if (typeof this !== 'object' || this === null || !(#searchParams in this))

Callers 1

constructorMethod · 0.45

Calls

no outgoing calls

Tested by

no test coverage detected