index.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import _ from 'lodash'
  2. export const randomString = e => {
  3. e = e || 32
  4. const t = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz'
  5. const a = t.length
  6. let n = ''
  7. for (let i = 0; i < e; i++) n += t.charAt(Math.floor(Math.random() * a))
  8. return n
  9. }
  10. export const resolveComponentType = type => {
  11. return `${_.upperFirst(type)}`
  12. }
  13. export function deepCompare (obj1, obj2, excludeKeys = []) {
  14. // eslint-disable-next-line eqeqeq
  15. if (obj1 == obj2) {
  16. return false
  17. }
  18. // 如果两个对象中的一个是基本类型,则它们不相等
  19. if (typeof obj1 !== 'object' || obj1 === null ||
  20. typeof obj2 !== 'object' || obj2 === null) {
  21. return true
  22. }
  23. // 如果两个对象的类型不相同,则它们不相等
  24. if (obj1.constructor !== obj2.constructor) {
  25. return true
  26. }
  27. // 递归地比较对象的属性
  28. for (const prop in obj1) {
  29. // 如果prop是要排除的key,则跳过
  30. if (excludeKeys.includes(prop)) {
  31. continue
  32. }
  33. if (obj1.hasOwnProperty(prop)) {
  34. if (!obj2.hasOwnProperty(prop)) {
  35. return true
  36. }
  37. if (deepCompare(obj1[prop], obj2[prop])) {
  38. return true
  39. }
  40. }
  41. }
  42. // 检查 obj2 中是否有 obj1 没有的属性
  43. for (const prop in obj2) {
  44. if (obj2.hasOwnProperty(prop) && !obj1.hasOwnProperty(prop)) {
  45. return true
  46. }
  47. }
  48. // 如果上面的检查都通过,则对象是相等的
  49. return false
  50. }