utils.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /** 未选 */
  2. export const unCheckedStatus = 0
  3. /** 半选 */
  4. export const halfCheckedStatus = 1
  5. /** 选中 */
  6. export const isCheckedStatus = 2
  7. /**
  8. * 深拷贝内容
  9. * @param originData 拷贝对象
  10. * @author crlang(https://crlang.com)
  11. */
  12. export function deepClone(originData) {
  13. const type = Object.prototype.toString.call(originData)
  14. let data
  15. if (type === '[object Array]') {
  16. data = []
  17. for (let i = 0; i < originData.length; i++) {
  18. data.push(deepClone(originData[i]))
  19. }
  20. } else if (type === '[object Object]') {
  21. data = {}
  22. for (const prop in originData) {
  23. // eslint-disable-next-line no-prototype-builtins
  24. if (originData.hasOwnProperty(prop)) { // 非继承属性
  25. data[prop] = deepClone(originData[prop])
  26. }
  27. }
  28. } else {
  29. data = originData
  30. }
  31. return data
  32. }
  33. /**
  34. * 获取所有指定的节点
  35. * @param type
  36. * @param value
  37. * @author crlang(https://crlang.com)
  38. */
  39. export function getAllNodes(list, type, value, packDisabledkey = true) {
  40. if (!list || list.length === 0) {
  41. return []
  42. }
  43. const res = []
  44. for (let i = 0; i < list.length; i++) {
  45. const item = list[i]
  46. if (item[type] === value) {
  47. if ((packDisabledkey && item.disabled) || !item.disabled) {
  48. res.push(item)
  49. }
  50. }
  51. }
  52. return res
  53. }
  54. /**
  55. * 获取所有指定的key值
  56. * @param type
  57. * @param value
  58. * @author crlang(https://crlang.com)
  59. */
  60. export function getAllNodeKeys(list, type, value, packDisabledkey = true) {
  61. if (!list || list.length === 0) {
  62. return null
  63. }
  64. const res = []
  65. for (let i = 0; i < list.length; i++) {
  66. const item = list[i]
  67. if (item[type] === value) {
  68. if ((packDisabledkey && item.disabled) || !item.disabled) {
  69. res.push(item.key)
  70. }
  71. }
  72. }
  73. return res.length ? res : null
  74. }
  75. /**
  76. * 错误输出
  77. *
  78. * @param msg
  79. */
  80. export function logError(msg, ...args) {
  81. console.error(`DaTree: ${msg}`, ...args)
  82. }
  83. const toString = Object.prototype.toString
  84. export function is(val, type) {
  85. return toString.call(val) === `[object ${type}]`
  86. }
  87. /**
  88. * 是否对象(Object)
  89. * @param val
  90. */
  91. export function isObject(val) {
  92. return val !== null && is(val, 'Object')
  93. }
  94. /**
  95. * 是否数字(Number)
  96. * @param val
  97. */
  98. export function isNumber(val) {
  99. return is(val, 'Number')
  100. }
  101. /**
  102. * 是否字符串(String)
  103. * @param val
  104. */
  105. export function isString(val) {
  106. return is(val, 'String')
  107. }
  108. /**
  109. * 是否函数方法(Function)
  110. * @param val
  111. */
  112. export function isFunction(val) {
  113. return typeof val === 'function'
  114. }
  115. /**
  116. * 是否布尔(Boolean)
  117. * @param val
  118. */
  119. export function isBoolean(val) {
  120. return is(val, 'Boolean')
  121. }
  122. /**
  123. * 是否数组(Array)
  124. * @param val
  125. */
  126. export function isArray(val) {
  127. return val && Array.isArray(val)
  128. }