tools_dj 1.0.0

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 (4) hide show
  1. package/LICENCE.md +23 -0
  2. package/README.md +16 -0
  3. package/index.js +552 -0
  4. package/package.json +18 -0
package/LICENCE.md ADDED
@@ -0,0 +1,23 @@
1
+ The HDJ.
2
+
3
+ Copyright (c) 2023 Michael Mclaughlin
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ 'Software'), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
+
package/README.md ADDED
@@ -0,0 +1,16 @@
1
+ # element-plus 主题自定义
2
+
3
+ ## 介绍
4
+ tools工具库
5
+
6
+ ## 安装
7
+ npm i set_el_theme
8
+
9
+ ## 使用
10
+ import {setThemeData} from 'set_el_theme'
11
+ setThemeData({themeColor:'#38bd14', brightColor:'#0036ea', darkColor:'#9f450a'})
12
+
13
+ {themeColor:'#38bd14', brightColor:'#0036ea', darkColor:'#9f450a'},
14
+ themeColor: 主体颜色
15
+ brightColor: 亮处颜色
16
+ darkColor: 暗处颜色 不给黑色到亮色前一个色阶
package/index.js ADDED
@@ -0,0 +1,552 @@
1
+ /*删除数组重复的元素
2
+ *let newArr = del_repeat(Arr)
3
+ */
4
+ export const del_repeat=(obj)=>{//新建一个去重的方法
5
+ let unique={};
6
+ obj.forEach(function (item){
7
+ unique[JSON.stringify(item)]=item;
8
+ })
9
+ obj=Object.keys(unique).map(function (u){
10
+ if (u!='undefined') return JSON.parse(u);
11
+ })
12
+ return obj;
13
+ }
14
+
15
+ //格式化时间
16
+ //格式化参数format=[ year month day hour minute],type=- /
17
+ export const formatDate= (time,format='day',type='-')=> {
18
+ let year =time.getFullYear()//年
19
+ let month =(time.getMonth()+1).toString().padStart(2,'0')//月
20
+ let day =(time.getDate()).toString().padStart(2,'0')//日
21
+ let hour =(time.getHours()).toString().padStart(2,'0')//时
22
+ let minute =(time.getMinutes()).toString().padStart(2,'0')//分
23
+ let types={
24
+ year:1,
25
+ month:2,
26
+ day:3,
27
+ hour:4,
28
+ minute:5
29
+ }
30
+ let formDates="";
31
+ for (let i=0; i<types[format]; i++){
32
+ if(i==0)formDates+=year
33
+ if(i==1)formDates+=month
34
+ if(i==2)formDates+=day
35
+ if(i==3)formDates+=hour
36
+ if(i==4)formDates+=minute
37
+ if (i!=types[format]-1)formDates+=type
38
+ }
39
+ return formDates
40
+ }
41
+
42
+ //时间格式yyyy-MM-dd HH:mm 返回时间戳
43
+ export const time_change=(data)=>{
44
+ return Date.parse(data);
45
+ }
46
+
47
+
48
+ /** 2022/8/11 9:14 User: DJ
49
+ * content: 时间转换
50
+ * time 时间格式 YYYY-MM-DD hh:mm:ss week time_slot
51
+ */
52
+ export const formatTime= (time,format='YYYY-MM-DD hh:mm:ss',type='-')=> {
53
+ try {
54
+ time=time.replaceAll('-','/')
55
+ }catch (e) {}
56
+ time=new Date(time)
57
+ let year =time.getFullYear()//年
58
+ let month =(time.getMonth()+1).toString().padStart(2,'0')//月
59
+ let day =(time.getDate()).toString().padStart(2,'0')//日
60
+ let hour =(time.getHours()).toString().padStart(2,'0')//时
61
+ let minute =(time.getMinutes()).toString().padStart(2,'0')//分
62
+ let seconds =(time.getSeconds()).toString().padStart(2,'0')//秒
63
+ let weeks={
64
+ 0:'周日',
65
+ 1:'周一',
66
+ 2:'周二',
67
+ 3:'周三',
68
+ 4:'周四',
69
+ 5:'周五',
70
+ 6:'周六',
71
+ }
72
+ let week =weeks[time.getDay()]
73
+ let time_slot=hour>12?'下午':'上午'
74
+ let arrayDate=[]
75
+ arrayDate=[
76
+ {group:1,connect:'',format:'YYYY',data:year},
77
+ {group:1,connect:'',format:'yyyy',data:year},
78
+ {group:1,connect:type,format:'MM',data:month},
79
+ {group:1,connect:type,format:'DD',data:day},
80
+ {group:1,connect:type,format:'dd',data:day},
81
+ {group:2,connect:' ',format:'hh',data:hour},
82
+ {group:2,connect:' ',format:'HH',data:hour},
83
+ {group:2,connect:':',format:'mm',data:minute},
84
+ {group:2,connect:':',format:'ss',data:seconds},
85
+ {group:3,connect:' ',format:'week',data:week},
86
+ {group:4,connect:' ',format:'time_slot',data:time_slot},
87
+ ]
88
+ let indexItem=0
89
+ let formDates="";
90
+ arrayDate.forEach(function (item,index) {
91
+ let isIn =format.indexOf(item.format)
92
+ if (isIn!=-1){//在
93
+ let connect=''
94
+ if (indexItem!=0){
95
+ connect=item.connect
96
+ }
97
+ formDates+=(connect+item.data)
98
+ indexItem++
99
+ }
100
+ })
101
+ return formDates
102
+ }
103
+
104
+ /**
105
+ * 计算两个日期之间的天数
106
+ * date1 开始日期 yyyy-MM-dd
107
+ * date2 结束日期 yyyy-MM-dd
108
+ * 如果日期相同 返回一天 开始日期大于结束日期,返回0
109
+ */
110
+ export function getDaysBetween(date1, date2 ,negative=false) {
111
+ var startDate = Date.parse(date1);
112
+ var endDate = Date.parse(date2);
113
+ var days = 0;
114
+ if (startDate > endDate) {
115
+ days = (startDate - endDate) / (24 * 60 * 60 * 1000);
116
+ }
117
+ if (startDate < endDate) {
118
+ days = (endDate - startDate) / (24 * 60 * 60 * 1000);
119
+ if (negative){
120
+ days=-days
121
+ }
122
+ }
123
+ return days;
124
+ }
125
+
126
+ /*判断是否为空
127
+ * 传入一个值
128
+ * 为空返回true非空返回false*/
129
+ export const is_empty=(obj)=>{
130
+ const old=Object.prototype.toString.call(obj)
131
+ let res=false
132
+ switch (old.slice(8,-1)){
133
+ case 'Undefined':
134
+ res=true;
135
+ break;
136
+ case 'Null':
137
+ res=true;
138
+ break;
139
+ case 'String':
140
+ res=obj.replace(/(^[ \t\n\r]*)|([ \t\n\r]*$)/g, '').length === 0
141
+ break;
142
+ case 'Boolean':
143
+ res=(!obj)
144
+ break;
145
+ case 'Array':
146
+ res=obj.length === 0
147
+ break;
148
+ case 'Number':
149
+ res=(0 === obj || isNaN(obj))
150
+ break;
151
+ case 'Object':
152
+ const arr=Object.keys(obj)
153
+ res=(arr.length === 0)
154
+ break;
155
+ default:
156
+ break;
157
+ }
158
+ return res
159
+ }
160
+
161
+ /*字符串替换传入
162
+ str:字符串,index:位置第几位下标,char:替换的内容
163
+ 返回一个字符串*/
164
+ export const repalce=(str,index,char)=>{
165
+ return str.substring(0,index)+char+str.substring(index+1);
166
+ }//字符串替换
167
+
168
+
169
+ /*url组装
170
+ * 传入一个对象,isPrefix不写 默认前缀带问号 如果isPrefix等于false就是不带前缀?
171
+ * return一个url格式的字符串*/
172
+ export const param=(data,isPrefix=true)=>{//组装成为url
173
+ if (is_empty(data) == false) {
174
+ let prefix = isPrefix ? '?' : ''
175
+ let _result = []
176
+ for (let key in data) {
177
+ let value = data[key]
178
+ if (['', undefined, null].includes(value)) {// 去掉为空的参数
179
+ continue
180
+ }
181
+ if (value.constructor === Array) {
182
+ value.forEach(_value => {
183
+ _result.push(encodeURIComponent(key) + '[]=' + encodeURIComponent(_value))
184
+ })
185
+ } else {
186
+ _result.push(encodeURIComponent(key) + '=' + encodeURIComponent(value))
187
+ }
188
+ }
189
+ console.log(_result.length ? prefix + _result.join('&') : '')
190
+ return _result.length ? prefix + _result.join('&') : ''
191
+ }else {
192
+ console.log(false)
193
+ return false
194
+ }
195
+ }
196
+
197
+
198
+ /*合并追加数组或者对象
199
+ * 传入两个数组或者对象
200
+ * return合并之后的数组或是对象*/
201
+ export const merge=(obj1,obj2)=>{//合并追加数组对象
202
+ const st=Object.prototype.toString.call(obj1)
203
+ if (st.slice(8,-1)=='Array'){
204
+ obj1.push.apply(obj1,obj2)//合并数组
205
+ return (new del_repeat(obj1))//数组去重
206
+ }if (st.slice(8,-1)=='Object'){
207
+ return Object.assign(obj1,obj2)
208
+ }
209
+ }
210
+
211
+ /*判断类型
212
+ * 传入任意一个参数
213
+ * 返回他的类型*/
214
+ export const is=(obj)=>{//判断数据类型需要判断所有的类型
215
+ const st=Object.prototype.toString.call(obj)
216
+ return st.slice(8,-1)
217
+ }
218
+
219
+ /*去除数组中的重复元素
220
+ * 传入一个数组
221
+ * return 一个数组出去*/
222
+ export const unique= (obj) => {//去除数组对象中的重复元素
223
+ if (is_empty(obj) == false) {
224
+ let res = new del_repeat(obj)
225
+ console.log(res)
226
+ return res
227
+ } else {
228
+ console.log(false)
229
+ return false
230
+ }
231
+ }
232
+
233
+ /*输出数组对象的第一个元素
234
+ * 传入一个数组对象
235
+ * return一个对象或是数组
236
+ * 为空返回false*/
237
+ export const first= (obj)=> {//输出对象中的第一个元素
238
+ if (is_empty(obj) == false) {
239
+ const st = Object.prototype.toString.call(obj)
240
+ if (st === '[object Object]') {
241
+ new fora(obj, 1)
242
+ for (const objElement of obj) {
243
+ console.log(objElement)
244
+ return(objElement)
245
+ }
246
+ } else {
247
+ console.log(obj[0])
248
+ return log[0]
249
+ }
250
+ }else {
251
+ console.log(false)
252
+ return false
253
+ }
254
+ }
255
+
256
+ /*输出数组对象的最后一个元素
257
+ * 传入一个数组对象
258
+ * return一个对象或是数组
259
+ * 为空返回false*/
260
+ export const last= (obj)=> {//输出对象中的最后一个元素
261
+ if (is_empty(obj) == false) {
262
+ const st = Object.prototype.toString.call(obj)
263
+ if (st === '[object Object]') {
264
+ new fora(obj, 2)
265
+ for (const objElement of obj) {
266
+ console.log(objElement)
267
+ // return(objElement)
268
+ }
269
+ } else {
270
+ console.log(obj[obj.length - 1])
271
+ }
272
+ } else {
273
+ console.log(false)
274
+ return false
275
+ }
276
+ }
277
+ /** 2022/7/15 16:45 User: DJ
278
+ * content: 二维数组查找返回他所在的下标
279
+ */
280
+ export const findByIndex= (array,key,data) =>{
281
+ return (array|| []).findIndex((profile) => profile[key] === data);
282
+ }
283
+
284
+ /** 2022/8/16 14:44 User: DJ
285
+ * content: 设置二维数据的值
286
+ * @source array /源数据
287
+ * @sourceKey string/源key
288
+ * @sourceData string/key值
289
+ * @nowKey string/目标key
290
+ * @nowData any/设置的数据
291
+ */
292
+ export function setObjData(source , sourceKey , sourceData, nowKey, nowData) {
293
+ try {
294
+ var p = source.find(function (curr) {
295
+ return curr[sourceKey] == sourceData
296
+ })
297
+ p[nowKey] = nowData
298
+ return true
299
+ } catch (e) {
300
+ return false
301
+ }
302
+ }
303
+ /** 2022/8/18 9:20 User: DJ
304
+ * content: 查找二维数组数据
305
+ */
306
+ export function selectObjData(source , sourceKey , sourceData) {
307
+ try {
308
+ return source.find(function (curr) {
309
+ return curr[sourceKey] == sourceData
310
+ })
311
+ } catch (e) {
312
+ return ''
313
+ }
314
+ }
315
+
316
+ /**
317
+ * 构造树型结构数据
318
+ * @param {*} data 数据源
319
+ * @param {*} id id字段 默认 'id'
320
+ * @param {*} parentId 父节点字段 默认 'parentId'
321
+ * @param {*} children 孩子节点字段 默认 'children'
322
+ */
323
+ export function handleTree(data, id, parentId, children) {
324
+ let config = {
325
+ id: id || 'id',
326
+ parentId: parentId || 'parentId',
327
+ childrenList: children || 'children'
328
+ };
329
+
330
+ var childrenListMap = {};
331
+ var nodeIds = {};
332
+ var tree = [];
333
+
334
+ for (let d of data) {
335
+ let parentId = d[config.parentId];
336
+ if (childrenListMap[parentId] == null) {
337
+ childrenListMap[parentId] = [];
338
+ }
339
+ nodeIds[d[config.id]] = d;
340
+ childrenListMap[parentId].push(d);
341
+ }
342
+
343
+ for (let d of data) {
344
+ let parentId = d[config.parentId];
345
+ if (nodeIds[parentId] == null) {
346
+ tree.push(d);
347
+ }
348
+ }
349
+
350
+ for (let t of tree) {
351
+ adaptToChildrenList(t);
352
+ }
353
+
354
+ function adaptToChildrenList(o) {
355
+ if (childrenListMap[o[config.id]] !== null) {
356
+ o[config.childrenList] = childrenListMap[o[config.id]];
357
+ }
358
+ if (o[config.childrenList]) {
359
+ for (let c of o[config.childrenList]) {
360
+ adaptToChildrenList(c);
361
+ }
362
+ }
363
+ }
364
+
365
+ return tree;
366
+ }
367
+
368
+ //将base64转换为blob
369
+ function dataURLtoBlob(dataurl) {
370
+ var arr = dataurl.split(','),
371
+ mime = arr[0].match(/:(.*?);/)[1],
372
+ bstr = atob(arr[1]),
373
+ n = bstr.length,
374
+ u8arr = new Uint8Array(n);
375
+ while (n--) {
376
+ u8arr[n] = bstr.charCodeAt(n);
377
+ }
378
+ return new Blob([u8arr], { type: mime });
379
+ }
380
+
381
+
382
+ //将blob转换为file
383
+ function blobToFile(theBlob, fileName){
384
+ return new File([theBlob], fileName);
385
+ // theBlob.lastModifiedDate = new Date();
386
+ // theBlob.name = fileName;
387
+ // return theBlob;
388
+ }
389
+ export function base64Img(base64,imgName){
390
+ let blob = dataURLtoBlob(base64);
391
+ return blobToFile(blob, imgName);
392
+ }
393
+
394
+ /** 2022/8/27 10:19 User: DJ
395
+ * content: 排序时间
396
+ */
397
+ export function compareDate(prop,type='up') {
398
+
399
+ return function(a, b) {
400
+ if (type=='up'){
401
+ return Date.parse(a[prop]) - Date.parse(b[prop]);
402
+ }else {
403
+ return Date.parse(b[prop]) - Date.parse(a[prop]);
404
+ }
405
+ }
406
+ }
407
+ /** 2022/8/27 10:19 User: DJ
408
+ * content: 排序
409
+ */
410
+ export function compare(prop,type='up') {
411
+ return function(a, b) {
412
+ if (type=='up'){
413
+ return a[prop] - b[prop] // 升序
414
+ }else {
415
+ return b[prop] - a[prop] // 降序
416
+ }
417
+ }
418
+ }
419
+
420
+
421
+ /** 2023/2/16 14:18 User: DJ
422
+ * content: 前置补位
423
+ * str,字符或数字
424
+ * n 长度
425
+ * k 补位字符
426
+ * d 是否超出保留 false 截取
427
+ */
428
+ function numberRepair(str='',n=2,k='0',d=false){
429
+ let s = "";
430
+ let sLen=(''+str).length
431
+ if (sLen>n && d==true){
432
+ return sLen
433
+ }else {
434
+ let diffLen=n-sLen
435
+ if (diffLen>0){
436
+ for (let i = 0; i < diffLen; i++) {
437
+ s += k;
438
+ }
439
+ }
440
+ return (s + str).slice(-1 * n);
441
+ }
442
+ }
443
+
444
+ /** 2023/2/16 14:20 User: DJ
445
+ * content: 数字补位
446
+ */
447
+ Number.prototype.repair = function(n = 2) {
448
+ let s = "";
449
+ for (let i = 1; i < n; i++) {
450
+ s += '0';
451
+ }
452
+ return (s + this).slice(-1 * n);
453
+ }
454
+
455
+ /** 2023/3/2 10:47 User: DJ
456
+ * content: 返回是否移动设备
457
+ */
458
+ export function isMobile() {
459
+ let userAgent=navigator.userAgent
460
+ let flag = userAgent.match(
461
+ /(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i
462
+ );
463
+ return !is_empty(flag);
464
+ }
465
+ /** 2023/3/2 15:53 User: DJ
466
+ * content: 设置头部
467
+ * 顺便设置了title
468
+ * cZoom 自定义缩放 没有不会生效
469
+ * mobile 移动端
470
+ * pc 电脑端
471
+ * isScale 是否缩放
472
+ * meta: {title: "简历",cZoom:{mobile:0.6,pc:1,isScale:'yes'}},
473
+ */
474
+ export function setMeta(meta) {
475
+ if (!is_empty(meta)) {
476
+ let metaArr = [];
477
+ document.head.childNodes.forEach((item) => {
478
+ switch (item.tagName) {
479
+ case "META":
480
+ metaArr.push(item);
481
+ break;
482
+ case "TITLE":
483
+ //顺便设置title
484
+ document.title = meta.title || "模板测试";
485
+ break;
486
+ }
487
+ });
488
+
489
+ const metas = document.createElement("META");//原始头//添加想要的meta(全局)
490
+ let minScale = 1
491
+ let isScale = 'yes' //允许视图缩放
492
+ if (meta.cZoom) {
493
+ if (isMobile()) {
494
+ if (meta.cZoom.mobile) {
495
+ minScale = meta.cZoom.mobile
496
+ }
497
+ } else {
498
+ if (meta.cZoom.pc) {
499
+ minScale = meta.cZoom.pc
500
+ }
501
+ }
502
+ if (meta.cZoom.isScale) {
503
+ isScale = meta.cZoom.isScale
504
+ }
505
+ }
506
+ let contentStr = `width=device-width,minimum-scale=${minScale},initial-scale=${minScale},user-scalable=${isScale}`
507
+ if (isScale == 'no') {
508
+ contentStr += `,maximum-scale=${minScale}`
509
+ }
510
+ const seat = [
511
+ {charset: "utf-8"},
512
+ {
513
+ name: "viewport",
514
+ content: contentStr,
515
+ },
516
+ ];
517
+ const creatFrag = document.createDocumentFragment();
518
+ seat.forEach((ele) => {
519
+ creatFrag.append(metas.cloneNode());
520
+ Object.entries(ele).forEach((item) => {
521
+ creatFrag.lastChild.setAttribute(item[0], item[1]);
522
+ });
523
+ });
524
+
525
+ metaArr.forEach((item) => {//删除原来的meta
526
+ document.head.removeChild(item);
527
+ });
528
+ document.head.prepend(creatFrag); //将文档片段写入head
529
+ }
530
+ }
531
+
532
+ /** 2023/3/15 15:00 User: DJ
533
+ * content: 色阶取值
534
+ * 第一个颜色到第二个颜色
535
+ * 色阶宽度
536
+ */
537
+ export const colorMix = (color1, color2, weight) => {
538
+ weight = Math.max(Math.min(Number(weight), 1), 0)
539
+ let r1 = parseInt(color1.substring(1, 3), 16)
540
+ let g1 = parseInt(color1.substring(3, 5), 16)
541
+ let b1 = parseInt(color1.substring(5, 7), 16)
542
+ let r2 = parseInt(color2.substring(1, 3), 16)
543
+ let g2 = parseInt(color2.substring(3, 5), 16)
544
+ let b2 = parseInt(color2.substring(5, 7), 16)
545
+ let r = Math.round(r1 * (1 - weight) + r2 * weight)
546
+ let g = Math.round(g1 * (1 - weight) + g2 * weight)
547
+ let b = Math.round(b1 * (1 - weight) + b2 * weight)
548
+ r = ("0" + (r || 0).toString(16)).slice(-2)
549
+ g = ("0" + (g || 0).toString(16)).slice(-2)
550
+ b = ("0" + (b || 0).toString(16)).slice(-2)
551
+ return "#" + r + g + b;
552
+ }
package/package.json ADDED
@@ -0,0 +1,18 @@
1
+ {
2
+ "name": "tools_dj",
3
+ "version": "1.0.0",
4
+ "description": "dj tools 工具库",
5
+ "main": "index.js",
6
+ "scripts": {
7
+ "test": "echo \"Error: no test specified\" && exit 1"
8
+ },
9
+ "author": "hdj",
10
+ "license": "ISC",
11
+ "keywords": [
12
+ "tools",
13
+ "tool"
14
+ ],
15
+ "files": [
16
+ "index.js"
17
+ ]
18
+ }