Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Polyfill for URL.prototype.searchParams #12

Open
saschanaz opened this issue Sep 7, 2017 · 8 comments
Open

Polyfill for URL.prototype.searchParams #12

saschanaz opened this issue Sep 7, 2017 · 8 comments

Comments

@saschanaz
Copy link

saschanaz commented Sep 7, 2017

URL instance supports searchParams member which is URLSearchParams. It will be great to support this here.

@jerrybendy
Copy link
Owner

Maybe you can do like this:

if (window.URL) {
  Object.defineProperty(URL.prototype, 'searchParams', {
    get: function () {
      return new URLSearchParams(this.search)
    }
  })
}

@iFwu
Copy link

iFwu commented Sep 26, 2017

@jerrybendy but I can't do something like params.set by such way.

@jerrybendy
Copy link
Owner

@xekri You are right. If you do that, every time you get the searchParams object, it'll return a new object.

So,

var u = new URL('http://github.com')
u.searchParams === u.searchParams  // should be `true`, but `false` returned

It causes a bug. To fix this bug, we should add a private variable to URL.prototype to save the searchParams object temporarily. Thus, we have to do a lot of work here.

It is not necessary for most of users, so I didn't put it in the index.js file. I'll try to fix it, and if it works well, I'll add a new file to this repo as an optional choice.

@jhenry9636
Copy link

Thanks for this amazing tool. Was this feature added?

@jerrybendy
Copy link
Owner

@jhenry9636 Not yet.

But you can use a workaround like above. I didn't write this solution into this library, because I don't want to make it fatter.

@xyy94813
Copy link

It is my solution. This can still be improved.

if (URL) {
  Object.defineProperty(URL.prototype, 'searchParams', {
    get (context) {
      const searchParams = new URLSearchParams(this.search)
      searchParams.set = (key, value) => {
        URLSearchParams.prototype.set.call(searchParams, key, value)
        this.search = `?${searchParams.toString()}`
      }
      searchParams.delete = (key, value) => {
        URLSearchParams.prototype.delete.call(searchParams, key, value)
        this.search = `?${searchParams.toString()}`
      }
      return searchParams
    }
  })
}

@jerrybendy
Copy link
Owner

@xyy94813 Your solution is better. Based on your code, We'd better rewrite all methods which can modify the URLSearchParams object, like append, delete, set and sort.

Although URL.searchParams.set !== URL.searchParams.prototype.set, I think it can be accepted for most of scenes.

👍

@xyy94813
Copy link

@jerrybendy

I found that core-js already supports URL and URLSearchParmas compatibility.

see:
https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/web.url.js
https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/web.url-search-params.js

We can use it in the browser. And learn from its implementation

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants