CollectionUtils.ts
4.64 KB
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import LinkList from '@ohos.util.List';
import HashMap from '@ohos.util.HashMap';
/**
* ArrayUtils class.
*/
export class CollectionUtils {
/**
* The Array utils tag.
*/
private static readonly TAG: string = 'ArrayUtils';
static isArray(value: any): boolean {
if (typeof Array.isArray === 'function') {
return Array.isArray(value);
} else {
return Object.prototype.toString.call(value) === '[object Array]';
}
}
/**
* Check collection is empty or not.
* @param collection any[]
* @returns {boolean} true(empty)
*/
static isEmpty(collection?: any[]): boolean {
return!collection || collection.length === 0;
}
static isEmptyList<T>(list1?: LinkList<T>): boolean {
return!list1 || list1.length === 0;
}
static isEmptyHashMap(obj?: HashMap<any, any>): boolean {
if (!obj) {
return true;
}
return obj.isEmpty();
}
static isEmptyMap(obj?: Map<any, any>): boolean {
if (!obj) {
return true;
}
return Object.keys(obj).length === 0 && obj.constructor === Object;
}
static isEmptyRecord(obj?: Record<string, string>): boolean {
if (!obj) {
return true;
}
return Object.keys(obj).length === 0 && obj.constructor === Object;
}
/**
* Check collection is empty or not.
* @param collection any[]
* @returns {boolean} true(not empty)
*/
static isNotEmpty(collection?: any[]): boolean {
if (!collection) {
return false
}
return collection.length > 0;
}
static getElement(collection?: any[], index?: number): any {
if (CollectionUtils.isEmpty(collection) || index === undefined) {
return null;
}
return index >= 0 && index < collection.length ? collection[index] : null;
}
static getListSize(collection?: any[]): number {
return CollectionUtils.isEmpty(collection) ? 0 : collection.length;
}
static getListElement(collection?: any[], index?: number): any {
if (CollectionUtils.isEmpty(collection) || index === undefined) {
return null;
}
return index >= 0 && index < collection.length ? collection[index] : null;
}
static convertArray<T>(objectList: T[] | T): T[] {
if (CollectionUtils.isArray(objectList)) {
return objectList as T[];
} else {
return [objectList as T];
}
}
/**
* 把list2合入list1后
* @param list1
* @param list2
* @returns
*/
static addAll<T>(list1: LinkList<T>, list2: LinkList<T>): LinkList<T> {
if (!list1) {
list1 = new LinkList<T>();
}
if (!list2) {
return list1;
}
for (let index = 0; index < list2.length; index++) {
list1.add(list2[index])
}
return list1
}
static deepCopy(objectList: any[]): any[] {
const list: any[] = [];
for (const objectItem of objectList) {
if (typeof objectItem !== 'object') {
list.push(objectItem);
continue;
}
if (objectItem.constructor === Date) {
list.push(new Date(objectItem));
continue;
}
if (objectItem.constructor === RegExp) {
list.push(new RegExp(objectItem));
continue;
}
if (objectItem.clone) {
list.push(objectItem.clone());
continue;
}
const newObj = new objectItem.constructor();
for (const key in objectItem) {
if (Object.hasOwnProperty.call(objectItem, key)) {
const val = objectItem[key];
newObj[key] = val;
}
}
list.push(newObj);
}
return list;
}
static deepCopyNumber(values: Set<number>): Set<number> {
const newSet: Set<number> = new Set();
values.forEach((value => {
newSet.add(value);
}));
return newSet;
}
/**
* Uint8Array to string
* @param fileData Uint8Array
*/
static uint8ArrayToString(fileData: Uint8Array): string {
return decodeURIComponent(escape(String.fromCharCode(...fileData)));
}
/**
* string to Uint8Array
* @param str string
*/
static stringToUint8Array(str: string): Uint8Array {
// spilt('') Each character is divided between them
const arr = unescape(encodeURIComponent(str)).split('').map(val => val.charCodeAt(0));
return new Uint8Array(arr);
}
/**
* 截取集合部分数据。
* start:0 - end:20 截取0-19,共20个数据
*/
static getSubElements(collection?: any[], start?: number, end?: number): any[] {
if (CollectionUtils.isEmpty(collection) || start === undefined || end === undefined) {
return null;
}
if (start < 0 || end < start) {
return null;
}
if (end > collection.length) {
return null;
}
let ss = collection.slice(start, end);
ss;
return collection.slice(start, end);
}
}