Skip to content

Instantly share code, notes, and snippets.

@wulucxy
Created July 9, 2018 01:18
Show Gist options
  • Save wulucxy/f5628821fa838516cfe52497a7c2287e to your computer and use it in GitHub Desktop.
Save wulucxy/f5628821fa838516cfe52497a7c2287e to your computer and use it in GitHub Desktop.
Url #snippet

Url

composeUrl

合并 url

import qs from 'qs'

const composeUrl = (url, params) => {
  if (_.isEmpty(params)) {
    return url
  }

  const serializedParams = qs.stringify(params)

  return url + (url.includes('?') ? '&' : '?') + serializedParams
}

Demo:

var url = 'http://abc.com?a=1'

composeUrl(url, { b: 2, c: 3 }) // 'http://abc.com?a=1&b=2&c=3'

getUrlQuery

获取 url query参数

import qs from 'qs'

const getQuery = () => {
  return qs.parse(location.search.substring(1))
}

Demo:

var url = 'http://abc.com?a=1'

getUrlQuery(url) // { a: 1}

isUrl

根据是否携带 http(s) 判断是否为超链接

const isUrl = (url) => {
  return /^https?:\/\//.test(url)
}

Demo:

var url = 'http://abc.com?a=1'

isUrl(url) // true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment