@ivujs/i-utils 1.1.1 → 1.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (140) hide show
  1. package/README.md +17 -19
  2. package/dist/cjs/id/index.cjs +26 -43
  3. package/dist/cjs/index.cjs +8 -398
  4. package/dist/es/id/index.d.ts +16 -0
  5. package/dist/es/id/index.mjs +28 -42
  6. package/dist/es/index.d.ts +2 -0
  7. package/dist/es/index.mjs +8 -58
  8. package/dist/index.d.ts +21 -0
  9. package/dist/lib/index.full.cjs.js +38 -0
  10. package/dist/lib/index.full.cjs.min.js +2 -0
  11. package/dist/lib/index.full.cjs.min.js.map +1 -0
  12. package/dist/lib/index.full.esm.js +35 -15822
  13. package/dist/lib/index.full.esm.min.js +2 -58
  14. package/dist/lib/index.full.esm.min.js.map +1 -0
  15. package/dist/lib/index.full.umd.js +39 -16176
  16. package/dist/lib/index.full.umd.min.js +2 -0
  17. package/dist/lib/index.full.umd.min.js.map +1 -0
  18. package/package.json +84 -54
  19. package/dist/cjs/ID-card/index.cjs +0 -122
  20. package/dist/cjs/array/index.cjs +0 -465
  21. package/dist/cjs/clipboard/index.cjs +0 -135
  22. package/dist/cjs/color/index.cjs +0 -210
  23. package/dist/cjs/constants/date.cjs +0 -186
  24. package/dist/cjs/constants/lang.cjs +0 -13
  25. package/dist/cjs/constants/math.cjs +0 -13
  26. package/dist/cjs/constants/regexp.cjs +0 -57
  27. package/dist/cjs/constants/sort.cjs +0 -15
  28. package/dist/cjs/cookie/index.cjs +0 -71
  29. package/dist/cjs/crypto/aes/aes.cjs +0 -533
  30. package/dist/cjs/crypto/aes/index.cjs +0 -28
  31. package/dist/cjs/crypto/base32/base32.cjs +0 -385
  32. package/dist/cjs/crypto/base32/index.cjs +0 -44
  33. package/dist/cjs/crypto/base64/base64.cjs +0 -433
  34. package/dist/cjs/crypto/base64/index.cjs +0 -64
  35. package/dist/cjs/crypto/des/des.cjs +0 -1054
  36. package/dist/cjs/crypto/des/index.cjs +0 -30
  37. package/dist/cjs/crypto/md5/index.cjs +0 -25
  38. package/dist/cjs/crypto/md5/md5.cjs +0 -944
  39. package/dist/cjs/crypto/sha/sha1/index.cjs +0 -25
  40. package/dist/cjs/crypto/sha/sha1/sha1.cjs +0 -602
  41. package/dist/cjs/crypto/sha/sha256/index.cjs +0 -46
  42. package/dist/cjs/crypto/sha/sha256/sha256.cjs +0 -654
  43. package/dist/cjs/crypto/sha/sha3/index.cjs +0 -44
  44. package/dist/cjs/crypto/sha/sha3/sha3.cjs +0 -747
  45. package/dist/cjs/crypto/sha/sha512/index.cjs +0 -88
  46. package/dist/cjs/crypto/sha/sha512/sha512.cjs +0 -1186
  47. package/dist/cjs/crypto/sm/lib/asn1.cjs +0 -167
  48. package/dist/cjs/crypto/sm/lib/ec.cjs +0 -352
  49. package/dist/cjs/crypto/sm/lib/jsbn.cjs +0 -1633
  50. package/dist/cjs/crypto/sm/lib/sm3.cjs +0 -173
  51. package/dist/cjs/crypto/sm/lib/utils.cjs +0 -198
  52. package/dist/cjs/crypto/sm/sm2/index.cjs +0 -122
  53. package/dist/cjs/crypto/sm/sm2/sm2.cjs +0 -269
  54. package/dist/cjs/crypto/sm/sm3/index.cjs +0 -15
  55. package/dist/cjs/crypto/sm/sm3/sm3.cjs +0 -103
  56. package/dist/cjs/crypto/sm/sm4/index.cjs +0 -28
  57. package/dist/cjs/crypto/sm/sm4/sm4.cjs +0 -362
  58. package/dist/cjs/crypto/tea/index.cjs +0 -26
  59. package/dist/cjs/crypto/tea/tea.cjs +0 -215
  60. package/dist/cjs/date/index.cjs +0 -1349
  61. package/dist/cjs/desensitized/index.cjs +0 -74
  62. package/dist/cjs/device/index.cjs +0 -159
  63. package/dist/cjs/dom/index.cjs +0 -176
  64. package/dist/cjs/file/index.cjs +0 -339
  65. package/dist/cjs/function/index.cjs +0 -67
  66. package/dist/cjs/keycode/index.cjs +0 -142
  67. package/dist/cjs/math/index.cjs +0 -284
  68. package/dist/cjs/number/index.cjs +0 -14
  69. package/dist/cjs/object/index.cjs +0 -216
  70. package/dist/cjs/pagination/index.cjs +0 -140
  71. package/dist/cjs/random/index.cjs +0 -25
  72. package/dist/cjs/regexp/index.cjs +0 -109
  73. package/dist/cjs/storage/index.cjs +0 -11
  74. package/dist/cjs/storage/localStorage.cjs +0 -40
  75. package/dist/cjs/storage/sessionStorage.cjs +0 -40
  76. package/dist/cjs/string/index.cjs +0 -338
  77. package/dist/cjs/url/index.cjs +0 -279
  78. package/dist/cjs/validate/index.cjs +0 -487
  79. package/dist/cjs/weapp/index.cjs +0 -151
  80. package/dist/es/ID-card/index.mjs +0 -116
  81. package/dist/es/array/index.mjs +0 -437
  82. package/dist/es/clipboard/index.mjs +0 -129
  83. package/dist/es/color/index.mjs +0 -200
  84. package/dist/es/constants/date.mjs +0 -184
  85. package/dist/es/constants/lang.mjs +0 -11
  86. package/dist/es/constants/math.mjs +0 -11
  87. package/dist/es/constants/regexp.mjs +0 -55
  88. package/dist/es/constants/sort.mjs +0 -13
  89. package/dist/es/cookie/index.mjs +0 -65
  90. package/dist/es/crypto/aes/aes.mjs +0 -529
  91. package/dist/es/crypto/aes/index.mjs +0 -25
  92. package/dist/es/crypto/base32/base32.mjs +0 -381
  93. package/dist/es/crypto/base32/index.mjs +0 -39
  94. package/dist/es/crypto/base64/base64.mjs +0 -427
  95. package/dist/es/crypto/base64/index.mjs +0 -57
  96. package/dist/es/crypto/des/des.mjs +0 -1050
  97. package/dist/es/crypto/des/index.mjs +0 -27
  98. package/dist/es/crypto/md5/index.mjs +0 -22
  99. package/dist/es/crypto/md5/md5.mjs +0 -941
  100. package/dist/es/crypto/sha/sha1/index.mjs +0 -22
  101. package/dist/es/crypto/sha/sha1/sha1.mjs +0 -599
  102. package/dist/es/crypto/sha/sha256/index.mjs +0 -41
  103. package/dist/es/crypto/sha/sha256/sha256.mjs +0 -649
  104. package/dist/es/crypto/sha/sha3/index.mjs +0 -39
  105. package/dist/es/crypto/sha/sha3/sha3.mjs +0 -742
  106. package/dist/es/crypto/sha/sha512/index.mjs +0 -79
  107. package/dist/es/crypto/sha/sha512/sha512.mjs +0 -1177
  108. package/dist/es/crypto/sm/lib/asn1.mjs +0 -164
  109. package/dist/es/crypto/sm/lib/ec.mjs +0 -349
  110. package/dist/es/crypto/sm/lib/jsbn.mjs +0 -1630
  111. package/dist/es/crypto/sm/lib/sm3.mjs +0 -170
  112. package/dist/es/crypto/sm/lib/utils.mjs +0 -186
  113. package/dist/es/crypto/sm/sm2/index.mjs +0 -111
  114. package/dist/es/crypto/sm/sm2/sm2.mjs +0 -258
  115. package/dist/es/crypto/sm/sm3/index.mjs +0 -13
  116. package/dist/es/crypto/sm/sm3/sm3.mjs +0 -99
  117. package/dist/es/crypto/sm/sm4/index.mjs +0 -25
  118. package/dist/es/crypto/sm/sm4/sm4.mjs +0 -359
  119. package/dist/es/crypto/tea/index.mjs +0 -23
  120. package/dist/es/crypto/tea/tea.mjs +0 -211
  121. package/dist/es/date/index.mjs +0 -1262
  122. package/dist/es/desensitized/index.mjs +0 -69
  123. package/dist/es/device/index.mjs +0 -145
  124. package/dist/es/dom/index.mjs +0 -164
  125. package/dist/es/file/index.mjs +0 -323
  126. package/dist/es/function/index.mjs +0 -63
  127. package/dist/es/keycode/index.mjs +0 -139
  128. package/dist/es/math/index.mjs +0 -274
  129. package/dist/es/number/index.mjs +0 -12
  130. package/dist/es/object/index.mjs +0 -203
  131. package/dist/es/pagination/index.mjs +0 -134
  132. package/dist/es/random/index.mjs +0 -22
  133. package/dist/es/regexp/index.mjs +0 -98
  134. package/dist/es/storage/index.mjs +0 -9
  135. package/dist/es/storage/localStorage.mjs +0 -35
  136. package/dist/es/storage/sessionStorage.mjs +0 -35
  137. package/dist/es/string/index.mjs +0 -321
  138. package/dist/es/url/index.mjs +0 -261
  139. package/dist/es/validate/index.mjs +0 -451
  140. package/dist/es/weapp/index.mjs +0 -140
@@ -1,465 +0,0 @@
1
- 'use strict';
2
-
3
- var sort = require('../constants/sort.cjs');
4
- var index = require('../validate/index.cjs');
5
-
6
- /* 数组计算 */
7
- /**
8
- * 数组最小值
9
- * @param {Array} array 数组
10
- * @returns {Number} 返回最小值
11
- */
12
- function arrayMin(array) {
13
- return Math.min.apply(null, array);
14
- }
15
-
16
- /**
17
- * 数组最大值
18
- * @param {Array} array 数组
19
- * @returns {Number} 返回最大值
20
- */
21
- function arrayMax(array) {
22
- return Math.max.apply(null, array);
23
- }
24
-
25
- /**
26
- * 数组求和
27
- * @param {Array} array 数组
28
- * @returns {Number} 返回和
29
- */
30
- function arraySum(array) {
31
- return array.reduce(function (pre, cur) {
32
- return pre + cur;
33
- });
34
- }
35
-
36
- /**
37
- * 数组求平均值
38
- * @param {Array} array 数组
39
- * @returns {Number} 返回平均数
40
- */
41
- function arrayAvg(array) {
42
- return arraySum(array) / array.length;
43
- }
44
-
45
- /* 数组比较 */
46
- /**
47
- * 数组中是否包含指定的元素
48
- * @param {String|Number} value 元素
49
- * @param {Array} array 查找的数组
50
- * @returns {Boolean} 返回结果
51
- */
52
- function inArray(value, array) {
53
- if (index.isNull(value)) return false;
54
- return array.includes(value);
55
- }
56
-
57
- /**
58
- * 比较两个数组是否相等
59
- * @param {Array} array1 数组1
60
- * @param {Array} array2 数组2
61
- * @returns {Boolean} 返回结果
62
- */
63
- function arrayEquals(array1, array2) {
64
- if (array1 === array2) return true;
65
- if (array1.length !== array2.length) return false;
66
- return array1.every((v, i) => v === array2[i]);
67
- }
68
-
69
- /* 数组操作 */
70
- /**
71
- * 生成指定长度的数组
72
- * @param {Number} length 长度,默认 0
73
- * @returns {Array} 返回数组
74
- */
75
- function arrayCreate(length = 0) {
76
- return [...Array(length).keys()];
77
- }
78
-
79
- /**
80
- * 数组指定位置添加元素
81
- * @description 如果数组为空,则在0位置添加元素
82
- * @param {Array} array 数组
83
- * @param {Number} index 下标位置,默认0
84
- * @param {*} value 添加的元素
85
- * @returns {Array} 返回操作后的数组
86
- */
87
- function arrayInsert(array = [], index = 0, value = undefined) {
88
- if (index < 0) return array;
89
-
90
- if (array.length === 0) {
91
- array.push(value);
92
- } else {
93
- if (index > array.length - 1) {
94
- return array;
95
- }
96
- array.splice(index, 0, value);
97
- }
98
-
99
- return array;
100
- }
101
-
102
- /**
103
- * 数组指定位置前面添加元素
104
- * @description 如果数组为空,则在0位置添加元素
105
- * @param {Array} array 数组
106
- * @param {Number} index 下标位置,默认0
107
- * @param {*} value 添加的元素
108
- * @returns {Array} 返回操作后的数组
109
- */
110
- function arrayInsertBefore(array = [], index = 0, value = undefined) {
111
- if (index < 0) return array;
112
-
113
- if (array.length === 0) {
114
- array.push(value);
115
- } else {
116
- if (index > array.length - 1) {
117
- return array;
118
- }
119
- array.splice(index, 0, value);
120
- }
121
- return array;
122
- }
123
-
124
- /**
125
- * 数组指定位置后面添加元素
126
- * @description 如果数组为空,则在0位置添加元素
127
- * @param {Array} array 数组
128
- * @param {Number} index 下标位置,默认0
129
- * @param {*} value 添加的元素
130
- * @returns {Array} 返回操作后的数组
131
- */
132
- function arrayInsertAfter(array = [], index = 0, value = undefined) {
133
- if (index < 0) return array;
134
-
135
- if (array.length === 0) {
136
- array.push(value);
137
- } else {
138
- if (index > array.length - 1) {
139
- return array;
140
- }
141
- array.splice(index, 0, array.splice(index, 1, value)[0]);
142
- }
143
-
144
- return array;
145
- }
146
-
147
- /**
148
- * 数组指定位置删除元素
149
- * @param {Array} array 数组
150
- * @param {Number} index 下标位置,默认0
151
- * @returns {Array} 返回操作后的数组
152
- */
153
- function arrayRemove(array = [], index = 0) {
154
- if (index < 0 || index > array.length - 1) return array;
155
-
156
- array.splice(index, 1);
157
- return array;
158
- }
159
-
160
- /**
161
- * 数组指定位置前面删除元素
162
- * @param {Array} array 数组
163
- * @param {Number} index 下标位置,默认0
164
- * @returns {Array} 返回操作后的数组
165
- */
166
- function arrayRemoveBefore(array = [], index = 0) {
167
- if (index <= 0 || index > array.length - 1) return array;
168
-
169
- array.splice(index - 1, 1);
170
- return array;
171
- }
172
-
173
- /**
174
- * 数组指定位置后面删除元素
175
- * @param {Array} array 数组
176
- * @param {Number} index 下标位置,默认0
177
- * @returns {Array} 返回操作后的数组
178
- */
179
- function arrayRemoveAfter(array = [], index = 0) {
180
- if (index < 0 || index > array.length - 1) return array;
181
-
182
- array.splice(index + 1, 1);
183
- return array;
184
- }
185
-
186
- /**
187
- * 数组置顶
188
- * @param {Array} array 数组
189
- * @param {Number} index 下标位置,默认0
190
- * @returns {Array} 返回操作后的数组
191
- */
192
- function arrayTop(array = [], index = 0) {
193
- if (index < 0 || index > array.length - 1) return array;
194
-
195
- array.unshift(array.splice(index, 1)[0]);
196
- return array;
197
- }
198
-
199
- /**
200
- * 数组置尾
201
- * @param {Array} array 数组
202
- * @param {Number} index 下标位置,默认0
203
- * @returns {Array} 返回操作后的数组
204
- */
205
- function arrayBottom(array = [], index = 0) {
206
- if (index < 0 || index > array.length - 1) return array;
207
-
208
- array.push(array.splice(index, 1)[0]);
209
- return array;
210
- }
211
-
212
- /**
213
- * 数组向上移动
214
- * @param {Array} array 数组
215
- * @param {Number} index 下标位置,默认0
216
- * @returns {Array} 返回操作后的数组
217
- */
218
- function arrayUp(array = [], index = 0) {
219
- if (index < 0 || index > array.length - 1) return array;
220
-
221
- if (index > 0) {
222
- array.splice(index - 1, 0, array.splice(index, 1)[0]);
223
- } else {
224
- array.push(array.splice(index, 1)[0]);
225
- }
226
- return array;
227
- }
228
-
229
- /**
230
- * 数组向下移动
231
- * @param {Array} array 数组
232
- * @param {Number} index 下标位置,默认0
233
- * @returns {Array} 返回操作后的数组
234
- */
235
- function arrayDown(array = [], index = 0) {
236
- if (index < 0 || index > array.length - 1) return array;
237
-
238
- if (index < array.length - 1) {
239
- array.splice(index + 1, 0, array.splice(index, 1)[0]);
240
- } else {
241
- array.unshift(array.splice(index, 1)[0]);
242
- }
243
- return array;
244
- }
245
-
246
- /**
247
- * 数组交换元素
248
- * @param {Array} array 数组
249
- * @param {Number} sourceIndex 原索引
250
- * @param {Number} targetIndex 目标索引
251
- * @returns {Array} 返回操作后的数组
252
- */
253
- function arraySwap(array, sourceIndex, targetIndex) {
254
- if (sourceIndex < 0 || targetIndex < 0 || sourceIndex > array.length - 1 || targetIndex > array.length - 1) {
255
- return array;
256
- }
257
-
258
- [array[targetIndex], array[sourceIndex]] = [array[sourceIndex], array[targetIndex]];
259
- return array;
260
- }
261
-
262
- /**
263
- * 数组排序
264
- * @param {Array} array 数组
265
- * @param {Number} mode 排序模式,参考常量集合中 数组常量,默认是升序
266
- * @returns {Array} 返回操作后的数组
267
- */
268
- function arraySort(array, mode = sort.SORT.ASC) {
269
- return array.sort((a, b) => {
270
- switch (mode) {
271
- // 升序
272
- case sort.SORT.ASC:
273
- return a - b;
274
- // 降序
275
- case sort.SORT.DESC:
276
- return b - a;
277
- // 随机
278
- case sort.SORT.RANDOM:
279
- return Math.random() - 0.5;
280
- // 默认
281
- default:
282
- return array;
283
- }
284
- });
285
- }
286
-
287
- /**
288
- * 数组元素去重
289
- * @param {Array} array 数组
290
- * @returns {Array} 返回操作后的数组
291
- */
292
- function arrayUnique(array) {
293
- if (index.isEmpty(array)) return [];
294
-
295
- return Array.from(new Set(array));
296
- }
297
-
298
- /**
299
- * 数组打乱元素
300
- * @description 可以适用于一些抽奖人员列表打乱顺序
301
- * @param {Array} array 数组
302
- * @returns {Array} 返回操作后的数组
303
- */
304
- function arrayShuffle(array) {
305
- for (let i = 1; i < array.length; i++) {
306
- const random = Math.floor(Math.random() * (i + 1));
307
- [array[random], array[i]] = [array[i], array[random]];
308
- }
309
- return array;
310
- }
311
-
312
- /* 数组转换 */
313
- /**
314
- * 普通数组转树形结构
315
- * @description 包含id和pid属性关系的一维数组,转为children的树形结构
316
- * @param {Array} array 数组
317
- * @param {Object} setting 配置项
318
- * @returns {Array} 返回树形节点
319
- */
320
- function arrayToTree(array, setting = { key: "id", parentKey: "pid", childrenKey: "children" }) {
321
- let key = setting.key,
322
- parentKey = setting.parentKey,
323
- childrenKey = setting.childrenKey;
324
-
325
- // 数组或者key是否为空
326
- if (!array || array.length === 0 || !key || key === "") return [];
327
-
328
- // 获得子节点方法
329
- const nodeChildren = function (node, childrenKey, newChildren) {
330
- if (!node) {
331
- return null;
332
- }
333
- if (typeof newChildren !== "undefined") {
334
- node[childrenKey] = newChildren;
335
- }
336
- return node[childrenKey];
337
- };
338
-
339
- // 声明变量
340
- let result = [];
341
- let tempMap = {};
342
- for (let i = 0; i < array.length; i++) {
343
- // 如果源数据数组中有children,则需要删除掉,否则会导致和之前的children合并
344
- array[i][childrenKey] && delete array[i][childrenKey];
345
- tempMap[array[i][key]] = array[i];
346
- }
347
- for (let i = 0; i < array.length; i++) {
348
- let parent = tempMap[array[i][parentKey]];
349
- if (parent && array[i][key] !== array[i][parentKey]) {
350
- let children = nodeChildren(parent, childrenKey);
351
-
352
- if (!children) {
353
- children = nodeChildren(parent, childrenKey, []);
354
- }
355
- children.push(array[i]);
356
- } else {
357
- result.push(array[i]);
358
- }
359
- }
360
-
361
- // 返回结果
362
- return result;
363
- }
364
-
365
- /**
366
- * 树形结构转普通数组
367
- * @param {Array} nodes 树形节点
368
- * @param {Object} setting 配置项
369
- * @returns {Array} 返回普通数组
370
- */
371
- function treeToArray(nodes, setting = { childrenKey: "children" }) {
372
- let childrenKey = setting.childrenKey;
373
- let result = [];
374
- for (let node of nodes) {
375
- // 删除掉多余空的children
376
- if (node[childrenKey] && !node[childrenKey].length) {
377
- delete node[childrenKey];
378
- }
379
- result.push(node);
380
- // 继续执行
381
- if (node[childrenKey] && node[childrenKey].length) {
382
- let array = treeToArray(node[childrenKey], setting);
383
- array && result.push(...array);
384
- }
385
- }
386
-
387
- // 返回结果
388
- return result;
389
- }
390
-
391
- /* 数组求并集,交集,差集等 */
392
- /**
393
- * 数组求并集
394
- * @description 数组1 和 数组2 合并一起的元素集合
395
- * @param {Array} array1 数组1
396
- * @param {Array} array2 数组2
397
- * @returns {Array} 返回数组
398
- */
399
- function arrayUnion(array1, array2) {
400
- return [...new Set(array1.concat(array2))];
401
- }
402
-
403
- /**
404
- * 数组求交集
405
- * @description 数组1 和 数组2 相同的元素集合
406
- * @param {Array} array1 数组1
407
- * @param {Array} array2 数组2
408
- * @returns {Array} 返回数组
409
- */
410
- function arrayIntersect(array1, array2) {
411
- return [...new Set(array1)].filter((item) => array2.includes(item));
412
- }
413
-
414
- /**
415
- * 数组求差集
416
- * @description 数组1 中不包含 数组2 的元素集合
417
- * @param {Array} array1 数组1
418
- * @param {Array} array2 数组2
419
- * @returns {Array} 返回数组
420
- */
421
- function arrayDifference(array1, array2) {
422
- return [...new Set(array1)].filter((item) => !array2.includes(item));
423
- }
424
-
425
- /**
426
- * 数组求补集
427
- * @description 数组1 和 数组2 不相同的元素集合
428
- * @param {Array} array1 数组1
429
- * @param {Array} array2 数组2
430
- * @returns {Array} 返回数组
431
- */
432
- function arrayComplement(array1, array2) {
433
- return [
434
- ...[...new Set(array1)].filter((item) => !array2.includes(item)),
435
- ...[...new Set(array2)].filter((item) => !array1.includes(item)),
436
- ];
437
- }
438
-
439
- exports.arrayAvg = arrayAvg;
440
- exports.arrayBottom = arrayBottom;
441
- exports.arrayComplement = arrayComplement;
442
- exports.arrayCreate = arrayCreate;
443
- exports.arrayDifference = arrayDifference;
444
- exports.arrayDown = arrayDown;
445
- exports.arrayEquals = arrayEquals;
446
- exports.arrayInsert = arrayInsert;
447
- exports.arrayInsertAfter = arrayInsertAfter;
448
- exports.arrayInsertBefore = arrayInsertBefore;
449
- exports.arrayIntersect = arrayIntersect;
450
- exports.arrayMax = arrayMax;
451
- exports.arrayMin = arrayMin;
452
- exports.arrayRemove = arrayRemove;
453
- exports.arrayRemoveAfter = arrayRemoveAfter;
454
- exports.arrayRemoveBefore = arrayRemoveBefore;
455
- exports.arrayShuffle = arrayShuffle;
456
- exports.arraySort = arraySort;
457
- exports.arraySum = arraySum;
458
- exports.arraySwap = arraySwap;
459
- exports.arrayToTree = arrayToTree;
460
- exports.arrayTop = arrayTop;
461
- exports.arrayUnion = arrayUnion;
462
- exports.arrayUnique = arrayUnique;
463
- exports.arrayUp = arrayUp;
464
- exports.inArray = inArray;
465
- exports.treeToArray = treeToArray;
@@ -1,135 +0,0 @@
1
- 'use strict';
2
-
3
- var index = require('../file/index.cjs');
4
-
5
- /**
6
- * 获得剪切板数据
7
- * @description 获得的剪切板的数据是会返回多个可用的MIME类型,比如是纯文本就返回一个['text/plain'],是复制的html则会返回两个可用的['text/plain','text/html'],是复制的图片则是['image/png']
8
- * @returns {Promise} 返回剪切板的数据,是数组形式,如果是文本则是字符串,否则是blob数据
9
- */
10
- function getClipboard() {
11
- return new Promise((resolve, reject) => {
12
- if (window.navigator.clipboard) {
13
- window.navigator.clipboard.read().then((clipboardItems) => {
14
- let list = [];
15
- for (let item of clipboardItems) {
16
- for (let type of item.types) {
17
- item.getType(type).then(async (blob) => {
18
- // 文本类型,可以直接输出为字符串
19
- if (blob.type.includes("text")) {
20
- let text = await index.blobToText(blob);
21
- list.push({ type: blob.type, data: text });
22
- }
23
- // 图片和其他文件类型,返回blob对象
24
- else {
25
- list.push({ type: blob.type, data: blob });
26
- }
27
- });
28
- }
29
- }
30
- resolve(list);
31
- });
32
- } else {
33
- console.warn("not support navigator clipboard!");
34
- }
35
- });
36
- }
37
-
38
- /**
39
- * 获得剪切板文本
40
- * @returns {Promise} 返回剪切板文本
41
- */
42
- function getClipboardText() {
43
- return new Promise((resolve, reject) => {
44
- if (window.navigator.clipboard) {
45
- window.navigator.clipboard
46
- .readText()
47
- .then((text) => {
48
- resolve(text);
49
- })
50
- .catch((error) => {
51
- reject(error);
52
- });
53
- } else {
54
- console.warn("not support navigator clipboard!");
55
- }
56
- });
57
- }
58
-
59
- /**
60
- * 设置剪切板数据
61
- * @description 可以设置文本或者blob类型的数据
62
- * @param {String,Blob} data 写入的数据,可以是文本或blob数据
63
- * @returns {Promise} 返回结果
64
- */
65
- function setClipboard(data) {
66
- return new Promise((resolve, reject) => {
67
- // 现代浏览器
68
- if (window.navigator.clipboard) {
69
- let clipboardItem = null;
70
- // 是文本类型
71
- if (typeof data === "string") {
72
- let blob = new Blob([data], { type: "text/plain" });
73
- clipboardItem = new window.ClipboardItem({
74
- ["text/plain"]: blob,
75
- });
76
- }
77
- // 其他文件类型
78
- else {
79
- clipboardItem = new window.ClipboardItem({ [data.type]: data });
80
- }
81
-
82
- // 写入到剪切板中
83
- window.navigator.clipboard.write([clipboardItem]).then(
84
- (success) => {
85
- resolve(true);
86
- },
87
- (failed) => {
88
- reject(false);
89
- },
90
- );
91
- } else {
92
- console.warn("not support navigator clipboard!");
93
- }
94
- });
95
- }
96
-
97
- /**
98
- * 设置剪切板文本
99
- * @param {String} text 写入的文本
100
- * @returns {Promise} 返回结果
101
- */
102
- function setClipboardText(text) {
103
- return new Promise((resolve, reject) => {
104
- // 现代浏览器
105
- if (window.navigator.clipboard) {
106
- window.navigator.clipboard.writeText(text).then(
107
- (success) => {
108
- resolve(true);
109
- },
110
- (failed) => {
111
- reject(false);
112
- },
113
- );
114
- } else {
115
- console.warn("not support navigator clipboard!");
116
- }
117
- });
118
- }
119
-
120
- /**
121
- * 清空剪切板
122
- * @returns {Promise} 返回结果
123
- */
124
- function clearClipboard() {
125
- return new Promise(async (resolve, reject) => {
126
- await setClipboardText("");
127
- resolve(true);
128
- });
129
- }
130
-
131
- exports.clearClipboard = clearClipboard;
132
- exports.getClipboard = getClipboard;
133
- exports.getClipboardText = getClipboardText;
134
- exports.setClipboard = setClipboard;
135
- exports.setClipboardText = setClipboardText;