30-seconds-of-code: getURLParameters can be wrote in this way

Created on 1 Feb 2019  路  9Comments  路  Source: 30-seconds/30-seconds-of-code

const getURLParameters = url => (url.match(/([^?=&]+)(=([^&]*))/g) || [])
.filter(a => a.includes('='))
.map(b => b.split('='))
.reduce((prev, curr) => ({ ...prev, [curr[0]]: curr[1] }), {});

getURLParameters wrote in this way can be easily understood

All 9 comments

https://developer.mozilla.org/en-US/docs/Web/API/UR

If anything we should just rewrite getURLParameters using the URL class and avoid the regex.

const getURLParameters = url => new URL(url).search
  .slice(1)
  .split('=')
  .reduce((acc, [key, value]) => key && (acc[key] = value, acc) || acc, {})

https://developer.mozilla.org/en-US/docs/Web/API/UR

If anything we should just rewrite getURLParameters using the URL class and avoid the regex.

const getURLParameters = url => new URL(url).search
  .slice(1)
  .split('=')
  .reduce((acc, [key, value]) => key && (acc[key] = value, acc) || acc, {})

image

https://developer.mozilla.org/en-US/docs/Web/API/UR
If anything we should just rewrite getURLParameters using the URL class and avoid the regex.

const getURLParameters = url => new URL(url).search
  .slice(1)
  .split('=')
  .reduce((acc, [key, value]) => key && (acc[key] = value, acc) || acc, {})

image

const getURLParameters = url => new URL(url)
      .search.slice(1)
      .split('&')
      .map(itm => itm.split('='))
      .reduce((prev,[ key, value]) => ({ ...prev, [key]: value}), {});

URL class and avoid the regex
A summary :

// 閫氳繃鍒涘缓URL瀵硅薄鑾峰彇鏌ヨ鍙傛暟
// get search by URL class
const getURLParameters = url => new URL(url).search
  .slice(1)
  .split('&')
  .map(itm => itm.split('='))
  .reduce((prev, [key, value]) => ({ ...prev, [key]: value }), {});

// 閫氳繃姝e垯鑾峰彇鏌ヨ鍙傛暟
// get search by regex
const getURLParameters = url => (url.match(/([^?=&]+)(=([^&]*))/g) || [])
  .filter(a => a.includes('='))
  .map(b => b.split('='))
  .reduce((prev, [key, value]) => ({ ...prev, [key]: value }), {});

https://developer.mozilla.org/en-US/docs/Web/API/UR

If anything we should just rewrite getURLParameters using the URL class and avoid the regex.

const getURLParameters = url => new URL(url).search
  .slice(1)
  .split('=')
  .reduce((acc, [key, value]) => key && (acc[key] = value, acc) || acc, {})

This is not a viable option, as the URL API is not available in Node.js. The snippet might be used in a server-side context, not only a client-side one (e.g. parsing a request URL in Express), so this would be a breaking change.

Based on this perf, the existing version performs better than the suggested change both on Chrome and Firefox. Therefore, I am closing the issue as the readability improvements to the snippet are negligible.

@Chalarangelo https://nodejs.org/docs/latest-v4.x/api/url.html#url_url and if you scroll down search is available. This has been available since NodeJS 4LTS

I did however manage to delete the part that made it actually work in an edit... go me -.-

const getURLParameters = url => new URL(url).search
  .slice(1)
  .split('&')
  .map(s =>s.split('='))
  .reduce((acc, [key, value]) => key && (acc[key] = value, acc) || acc, {})

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for any follow-up tasks.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

larrybotha picture larrybotha  路  3Comments

henrycjchen picture henrycjchen  路  4Comments

Chalarangelo picture Chalarangelo  路  5Comments

fuchao2012 picture fuchao2012  路  4Comments

fplgusmao picture fplgusmao  路  4Comments