1
1
/**
2
2
* @author jdeseva
3
- * @date 2019.10.24
3
+ * @date 2022.11.29
4
4
* @description 自用工具类函数,包含常用的自定义工具类函数,不定期整理更新。欢迎提出issue
5
5
* @homePage https://github.com/jsmini/util-tools
6
6
*/
@@ -74,17 +74,15 @@ export function __Throttle(method: Function, delay: number = 500): Function {
74
74
* @param idKey {string} 主键
75
75
* @returns {Array<object> } 返回的树形结构数据
76
76
*/
77
- export function __ToTree (
78
- data : object [ ] ,
79
- parentIdKey : string ,
80
- idKey : string = 'id'
81
- ) : object [ ] {
77
+ export function __ToTree < T > ( data : T [ ] , parentIdKey : string , idKey : string = 'id' ) : T [ ] {
82
78
let _idMap = Object . create ( null )
83
- data . forEach ( ( row : object ) : void => {
79
+
80
+ data . forEach ( ( row : T ) : void => {
84
81
_idMap [ row [ idKey ] ] = row
85
82
} )
86
- const result : object [ ] = [ ]
87
- data . forEach ( ( row : object ) : void => {
83
+ const result : T [ ] = [ ]
84
+
85
+ data . forEach ( ( row : T ) : void => {
88
86
let parent = _idMap [ row [ parentIdKey ] ]
89
87
if ( parent ) {
90
88
let v = parent . children || ( parent . children = [ ] )
@@ -103,11 +101,7 @@ export function __ToTree(
103
101
* @param {String } [val = value] 键值
104
102
* @returns {Array<Object> } 返回数组
105
103
*/
106
- export function __MapToArray (
107
- map : object ,
108
- key : string = 'name' ,
109
- val : string = 'value'
110
- ) : object [ ] {
104
+ export function __MapToArray < T > ( map : Record < string , T > , key : string = 'name' , val : string = 'value' ) : T [ ] {
111
105
let res = [ ]
112
106
for ( let k in map ) {
113
107
var temp = Object . create ( null )
@@ -125,13 +119,10 @@ export function __MapToArray(
125
119
* @param {String } [val = value] 键值
126
120
* @returns {Object } 返回map对象字典
127
121
*/
128
- export function __ArrayToMap (
129
- array : object [ ] ,
130
- key : string = 'name' ,
131
- val : string = 'value'
132
- ) : object {
122
+ export function __ArrayToMap < T > ( array : Record < string , T > [ ] , key : string = 'name' , val : string = 'value' ) : Record < string , T > {
133
123
var res = Object . create ( null ) // 新建一个纯粹对象
134
- array . forEach ( ( row : object ) : void => {
124
+
125
+ array . forEach ( ( row : Record < string , T > ) : void => {
135
126
res [ row [ key ] ] = row [ val ]
136
127
} )
137
128
return res
0 commit comments