Re: [whatwg/url] Simplify constructing URLSearchParams (#175)

The behavior here is governed by [the definiton for how to convert a JS value into a union type](https://heycam.github.io/webidl/#es-union). I think it ends up looking basically like this:

```js
if (arg === null || arg === undefined) {
  // record<USVString, USVString>
  
  // add zero key/value pairs to the URLSearchParams
} else if (typeof arg === "object") {
  const iterator = arg[Symbol.iterator];
  if (iterator === undefined || iterator === null) {
    // record<USVString, USVString>
    const map = new Map();
    for (const key of Object.getOwnPropertyNames(arg)) {
      const desc = Object.getOwnPropertyDescriptor(arg, key);
      if (desc !== undefined && desc.enumerable) {
        const typedKey = toUSVString(key);
        const typedValue = toUSVString(arg[key]);
        map.set(typedKey, typedValue);
      }
    }
    
    // Use `map` to add key/value pairs to the URLSearchParams
  } else {
    // sequence<sequence<USVString>>
    const pairs = [];
    
    // Note: per spec we have to first exhaust the lists (convert
    // from JS value to sequence<sequence<USVString>>) then process them
    for (const x of iterator) {
      const gotten = [];
      pairs.push(gotten);
      for (const y of x) {
        gotten.push(toUSVString(y));
      }
    }
    
    for (const pair of pairs) {
      if (pair.length !== 2) {
        throw new TypeError();
      }
      
      // Add (`pair[0]`, `pair[1]`) key/value pair to URLSearchParams
    }
  }
} else {
  // USVString
  const string = toUSVString(arg);
  // parse and process the string to add key/value pairs to the URLSearchParams
}
```

-- 
You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub:
https://github.com/whatwg/url/pull/175#issuecomment-270685303

Received on Thursday, 5 January 2017 16:21:08 UTC