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