utils.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. /**
  2. * @param {string} url
  3. * @returns {Object}
  4. */
  5. function param2Obj(url) {
  6. const search = decodeURIComponent(url.split('?')[1]).replace(/\+/g, ' ')
  7. if (!search) {
  8. return {}
  9. }
  10. const obj = {}
  11. const searchArr = search.split('&')
  12. searchArr.forEach(v => {
  13. const index = v.indexOf('=')
  14. if (index !== -1) {
  15. const name = v.substring(0, index)
  16. obj[name] = v.substring(index + 1, v.length)
  17. }
  18. })
  19. return obj
  20. }
  21. /**
  22. * This is just a simple version of deep copy
  23. * Has a lot of edge cases bug
  24. * If you want to use a perfect deep copy, use lodash's _.cloneDeep
  25. * @param {Object} source
  26. * @returns {Object}
  27. */
  28. function deepClone(source) {
  29. if (!source && typeof source !== 'object') {
  30. throw new Error('error arguments', 'deepClone')
  31. }
  32. const targetObj = source.constructor === Array ? [] : {}
  33. Object.keys(source).forEach(keys => {
  34. if (source[keys] && typeof source[keys] === 'object') {
  35. targetObj[keys] = deepClone(source[keys])
  36. } else {
  37. targetObj[keys] = source[keys]
  38. }
  39. })
  40. return targetObj
  41. }
  42. module.exports = {
  43. param2Obj,
  44. deepClone
  45. }