-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathindex.js
More file actions
845 lines (845 loc) 路 19.8 KB
/
Copy pathindex.js
File metadata and controls
845 lines (845 loc) 路 19.8 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
/**
* JS-PackTools. Is a little toolBox for developer specifically with webScraping and format date and text
* @module JS-PackTools | API Doc
* @author Jes煤s A. P茅rez S.
* {@version 2.3.17 | last Update: Mon Aug 17 2020 23:16:20 GMT-0500 (GMT-05:00)
* {@link https://github.com/jasp402/js-packtools}
*/
const constant = require(__dirname+'/constants');
const assert = require('assert');
const allEqual = require('./lib/allEqual');
const capitalLetter = require('./lib/capitalLetter');
const clearFolder = require('./lib/clearFolder');
const createFolders = require('./lib/createFolders');
const csvToJson = require('./lib/csvToJson');
const customDate = require('./lib/customDate');
const dayOfYear = require('./lib/dayOfYear');
const deleteFile = require('./lib/deleteFile');
const differenceDay = require('./lib/differenceDay');
const everyOrNone = require('./lib/everyOrNone');
const formatSeconds = require('./lib/formatSeconds');
const generateRageDate = require('./lib/generateRageDate');
const getFinalPath = require('./lib/getFinalPath');
const groupBy = require('./lib/groupBy');
const increaseDays = require('./lib/increaseDays');
const is = require('./lib/is');
const jsonToCsv = require('./lib/jsonToCsv');
const modEnd = require('./lib/modEnd');
const modStart = require('./lib/modStart');
const objectFilter = require('./lib/objectFilter');
const objectToDate = require('./lib/objectToDate');
const parseDate = require('./lib/parseDate');
const renameFiles = require('./lib/renameFiles');
const timeToDate = require('./lib/timeToDate');
const validateDir = require('./lib/validateDir');
const validateYear = require('./lib/validateYear');
const writeLog = require('./lib/writeLog');
const writeLogError = require('./lib/writeLogError');
const _log = require('./lib/_log');
const sourceLib = [
{
"name": "allEqual",
"category": "Arrays/Object",
"description": "This snippet checks whether all elements of the array are equal.",
"version": "2.0.0",
"example": [
"@example"
],
"arParams": [
{
"name": "arr",
"type": "array",
"description": "",
"default": ""
}
],
"returns": "boolean"
},
{
"name": "capitalLetter",
"category": "String",
"description": "capitalize the first letter of a sentence or all letters if second parameters is `true`.",
"version": "2.0.0",
"example": [
"@example https://gist.githubusercontent.com/jasp402/baad045d810c9c22450ff33bec3e8217/raw/77c42447ff5eca19f22042abaed1a0ea6bca439e/0-e.g.__capitalLetter.js",
"@example https://gist.githubusercontent.com/jasp402/2a72358a90bd433cd15e700d62190def/raw/fecdc9c3a790b7c0e93f53309cb5b3d9a0f9f08c/1-e.g.__capitalLetter.js"
],
"arParams": [
{
"name": "arg",
"type": "array/string/boolean",
"description": "any word or sentence",
"default": " string/boolean "
}
],
"returns": "string "
},
{
"name": "clearFolders",
"category": "Path/Files",
"description": "This simply clear the content a folder.",
"version": "1.0.0",
"example": [
"@example"
],
"arParams": [
{
"name": "path",
"type": "string",
"description": "",
"default": " "
}
],
"returns": "object "
},
{
"name": "createFolders",
"category": "Path/Files",
"description": "This create a folder whit sub-folder of date by default if param `withDate` is True.",
"version": "1.0.0",
"example": [
"@example"
],
"arParams": [
{
"name": "path",
"type": "string",
"description": "",
"default": ""
},
{
"name": "withDate",
"type": "boolean",
"description": "",
"default": ""
}
],
"returns": "boolean"
},
{
"name": "csvToJson",
"category": "convert",
"description": "This read a document CSV and convert in a Object Javascript (JSON).",
"version": "1.0.0",
"example": [
"@example"
],
"arParams": [
{
"name": "_csv",
"type": "string",
"description": "",
"default": ""
}
],
"returns": "string"
},
{
"name": "customDate",
"category": "Time/Date",
"description": "It's a date control. Without parameters give the current date, use the parameters to customize its functionality.",
"version": "1.0.0",
"example": [
"@example var jsPackTools = require(\"js-packtools\"); \\n let u = new jsPackTools(); \\n u.customDate('05/01/2019', 'yyyy-mm-dd', 15, false); // => 2019-05-15"
],
"arParams": [
{
"name": "_date",
"type": "string",
"description": "date or Object Date by Default is current date.",
"default": " new Date() "
},
{
"name": "_format",
"type": "string",
"description": "define the format that want to return.",
"default": " mm/dd/yyyy "
},
{
"name": "_addDay",
"type": "int",
"description": "count of days that add to date o current date",
"default": " 0 "
},
{
"name": "_type",
"type": "boolean",
"description": "method to return `string` or `Object Date`",
"default": " false "
}
],
"returns": "string|Object "
},
{
"name": "dayOfYear",
"category": "Time/Date",
"description": "This snippet gets the day of the year from a Date object.",
"version": "1.0.0",
"example": [
"@example"
],
"arParams": [
{
"name": "date",
"type": "string/date",
"description": "",
"default": ""
}
],
"returns": "number "
},
{
"name": "deleteFile",
"category": "Path/Files",
"description": "Check if the file exists before deleting",
"version": "1.0.0",
"example": [
"@example"
],
"arParams": [
{
"name": "path",
"type": "string",
"description": "",
"default": ""
}
],
"returns": "string "
},
{
"name": "differenceDay",
"category": "Time/Date",
"description": "This simply clear the content a folder.",
"version": "1.0.0",
"example": [
"@example"
],
"arParams": [
{
"name": "_startDate",
"type": "string",
"description": "",
"default": ""
},
{
"name": "_endDate",
"type": "string",
"description": "",
"default": ""
},
{
"name": "_initDate",
"type": "boolean",
"description": "",
"default": ""
}
],
"returns": "object "
},
{
"name": "everyOrNone",
"category": "Arrays/Object",
"description": "This snippet returns true if the predicate function returns true for all elements in a collection and false otherwise. \\n You can omit the second argument fn if you want to use Boolean as a default value.",
"version": "1.0.0",
"example": [
"@example"
],
"arParams": [
{
"name": "arr",
"type": "array",
"description": "",
"default": ""
},
{
"name": "fn",
"type": "function/boolean",
"description": "",
"default": ""
}
],
"returns": "boolean"
},
{
"name": "formatSeconds",
"category": "Time/Date",
"description": "This only format a value float",
"version": "1.0.1",
"example": [
"@example"
],
"arParams": [
{
"name": "time",
"type": "number",
"description": "",
"default": ""
}
],
"returns": "string"
},
{
"name": "generateRageDate",
"category": "Time/Date",
"description": "This function allows you to separate a given date in the number of days assigned to it.",
"version": "1.0.0",
"example": [
"@example"
],
"arParams": [
{
"name": "lastDate",
"type": "string",
"description": "",
"default": ""
},
{
"name": "splitDay",
"type": "number",
"description": "",
"default": ""
},
{
"name": "endDate",
"type": "string",
"description": "",
"default": ""
}
],
"returns": "array"
},
{
"name": "getFinalPath",
"category": "Path/Files",
"description": "Create structure of folders with parameters in constructor.",
"version": "1.0.0",
"example": [
"@example"
],
"arParams": [
{
"name": "",
"type": "empty",
"description": "",
"default": ""
}
],
"returns": "string "
},
{
"name": "groupBy",
"category": "Arrays/Object",
"description": "Sort the object by placing the value of the assigned property as key",
"version": "1.0.0",
"example": [
"@example"
],
"arParams": [
{
"name": "obj",
"type": "object",
"description": "",
"default": ""
},
{
"name": "key",
"type": "string",
"description": "",
"default": ""
}
],
"returns": "object"
},
{
"name": "increaseDays",
"category": "Time/Date",
"description": "Add days to date, can defined a maxDate.",
"version": "1.0.0",
"example": [
"@example"
],
"arParams": [
{
"name": "startDate",
"type": "string",
"description": "",
"default": ""
},
{
"name": "days",
"type": "number",
"description": "",
"default": ""
},
{
"name": "maxDate",
"type": "string",
"description": "",
"default": ""
}
],
"returns": "object "
},
{
"name": "is",
"category": "validate",
"description": "Is a function for evaluate to type of element",
"version": "1.0.1",
"example": [
"@example const {is} = require(\"js-packtools\");\\nis('array', [1]); //true \\nis('object', [1]); //false"
],
"arParams": [
{
"name": "type",
"type": "string",
"description": "Define type of element that want evaluate.",
"default": " array/object/number/string/boolean "
},
{
"name": "value",
"type": "any",
"description": "Element to evaluate.",
"default": " "
}
],
"returns": "boolean "
},
{
"name": "jsonToCsv",
"category": "convert",
"description": "Function for convert JSON to CSV",
"version": "1.0.1",
"example": [
"@example"
],
"arParams": [
{
"name": "dataJson",
"type": "array",
"description": "Array with dataSet.",
"default": " [] "
},
{
"name": "dirPath",
"type": "string",
"description": "Dir path for out file csv.",
"default": " "
},
{
"name": "delimiter",
"type": "string",
"description": "Character delimiter .",
"default": " , "
}
],
"returns": "string "
},
{
"name": "modEnd",
"category": "String",
"description": "Using a string to create a new string with new size inverse to modStart.",
"version": "1.0.1",
"example": [
"@example"
],
"arParams": [
{
"name": "str",
"type": "string",
"description": "",
"default": ""
},
{
"name": "size",
"type": "number",
"description": "",
"default": ""
},
{
"name": "_str",
"type": "string",
"description": "",
"default": ""
}
],
"returns": "string"
},
{
"name": "modStart",
"category": "String",
"description": "Using a string to create a new string with new size.",
"version": "1.0.1",
"example": [
"@example"
],
"arParams": [
{
"name": "str",
"type": "string",
"description": "",
"default": ""
},
{
"name": "size",
"type": "number",
"description": "",
"default": ""
},
{
"name": "_str",
"type": "string",
"description": "",
"default": ""
}
],
"returns": "string"
},
{
"name": "objectFilter",
"category": "Arrays/Object",
"description": "This fragment allows you to filter an object and return the key and its corresponding value",
"version": "1.1.2",
"example": [
"@example"
],
"arParams": [
{
"name": "dataSet",
"type": "array/object",
"description": "Object or array to object with dataSet",
"default": " "
},
{
"name": "array",
"type": "array",
"description": "Array of the key names you want to get in the filter",
"default": " "
}
],
"returns": "array|object"
},
{
"name": "objectToDate",
"category": "Time/Date",
"description": "Generate a object with date details.",
"version": "1.0.0",
"example": [
"@example"
],
"arParams": [
{
"name": "strTime",
"type": "string",
"description": "",
"default": ""
}
],
"returns": "object"
},
{
"name": "parseDate",
"category": "Time/Date",
"description": "Using a string and a format transform the string in date.",
"version": "1.0.1",
"example": [
"@example"
],
"arParams": [
{
"name": "_sDate",
"type": "string",
"description": "",
"default": ""
},
{
"name": "_sFormat",
"type": "string",
"description": "",
"default": ""
},
{
"name": "_bType",
"type": "boolean",
"description": "",
"default": ""
}
],
"returns": "object"
},
{
"name": "renameFiles",
"category": "Path/Files",
"description": "This function rename a batch of files with current date or token unique.",
"version": "1.0.0",
"example": [
"@example"
],
"arParams": [
{
"name": "path",
"type": "string",
"description": "",
"default": ""
},
{
"name": "name",
"type": "string",
"description": "",
"default": ""
},
{
"name": "opts",
"type": "number",
"description": "Unique identifier for each file in the folder null = self increasing",
"default": " date `or` token `or` null "
},
{
"name": "rand",
"type": "number",
"description": "1 = Suffix or 2 = Prefix",
"default": " 1 `or` 2 "
}
],
"returns": "void"
},
{
"name": "timeToDate",
"category": "Time/Date",
"description": "return the time between two dates, or a date and now",
"version": "1.0.1",
"example": [
"@example"
],
"arParams": [
{
"name": "_toDate",
"type": "string/date",
"description": "target date",
"default": " new Date() "
},
{
"name": "_sDate",
"type": "string/date",
"description": "start date (default: now)",
"default": " new Date() "
}
],
"returns": "string"
},
{
"name": "validateDir",
"category": "Path/Files",
"description": "Validate if there is a route. if not, create this route.",
"version": "1.0.0",
"example": [
"@example"
],
"arParams": [
{
"name": "path",
"type": "string",
"description": "",
"default": ""
}
],
"returns": "boolean"
},
{
"name": "validateYear",
"category": "Time/Date",
"description": "Can be current year or spend the year to validate",
"version": "1.0.0",
"example": [
"@example"
],
"arParams": [
{
"name": "date",
"type": "string/object",
"description": "",
"default": ""
},
{
"name": "yearCompare",
"type": "string/object",
"description": "",
"default": ""
},
{
"name": "operator",
"type": "string",
"description": "",
"default": ""
}
],
"returns": "boolean"
},
{
"name": "writeLog",
"category": "global",
"description": "write file .log in folder default of class.",
"version": "1.0.0",
"example": [
"@example"
],
"arParams": [
{
"name": "msg",
"type": "string",
"description": "",
"default": ""
},
{
"name": "color",
"type": "string",
"description": "",
"default": ""
}
],
"returns": "void"
},
{
"name": "writeLogError",
"category": "global",
"description": "write file .log in folder default of class.",
"version": "1.0.0",
"example": [
"@example"
],
"arParams": [
{
"name": "title",
"type": "string",
"description": "",
"default": ""
},
{
"name": "msg",
"type": "string",
"description": "",
"default": ""
},
{
"name": "color",
"type": "string",
"description": "",
"default": ""
}
],
"returns": "void"
},
{
"name": "log",
"category": "global",
"description": "It integrates the functions registry of errors and registry of execution for the document of registry in his project.",
"version": "1.0.0",
"example": [
"@example"
],
"arParams": [
{
"name": "_message",
"type": "string",
"description": "",
"default": ""
},
{
"name": "_fileName",
"type": "string",
"description": "",
"default": ""
},
{
"name": "_color",
"type": "string",
"description": "",
"default": ""
}
],
"returns": "string "
}
];
class parameters {
constructor(config = {})
{
assert.ok(typeof config === 'object', constant.ERROR_CONSTRUCTOR);
this.constant = constant;
this.folderName = ("folderName" in config) ? config.folderName : 'DATA'; //=> data_audit, data_download, docs, pdfs, trash...
this.folderWithDate = ("folderWithDate" in config) ? Boolean(config.folderWithDate) : true; //=> true => /2019_01_01/
this.folderFormatDate = ("folderFormatDate" in config) ? config.folderFormatDate : 'yyyy~_~mm~_~dd'; //=> 'yyyy_mm_dd';
this.nameWriteLog = ("nameWriteLog" in config) ? config.nameWriteLog + '.log' : 'logExecution.log';
this.nameWriteLogError = ("nameWriteLogError" in config) ? config.nameWriteLogError + '.log' : 'logError.log';
this.logFormatDate = ("logFormatDate" in config) ? config.logFormatDate : 'yyyy-mm-dd ~h:m:i'; //=> yyyy-mm-dd | hh:mm:sss
}
}
class jsPackTools extends parameters {
get allEqual() { return allEqual.bind(this) }
get capitalLetter() { return capitalLetter.bind(this) }
get clearFolder() { return clearFolder.bind(this) }
get createFolders() { return createFolders.bind(this) }
get csvToJson() { return csvToJson.bind(this) }
get customDate() { return customDate.bind(this) }
get dayOfYear() { return dayOfYear.bind(this) }
get deleteFile() { return deleteFile.bind(this) }
get differenceDay() { return differenceDay.bind(this) }
get everyOrNone() { return everyOrNone.bind(this) }
get formatSeconds() { return formatSeconds.bind(this) }
get generateRageDate() { return generateRageDate.bind(this) }
get getFinalPath() { return getFinalPath.bind(this) }
get groupBy() { return groupBy.bind(this) }
get increaseDays() { return increaseDays.bind(this) }
get is() { return is.bind(this) }
get jsonToCsv() { return jsonToCsv.bind(this) }
get modEnd() { return modEnd.bind(this) }
get modStart() { return modStart.bind(this) }
get objectFilter() { return objectFilter.bind(this) }
get objectToDate() { return objectToDate.bind(this) }
get parseDate() { return parseDate.bind(this) }
get renameFiles() { return renameFiles.bind(this) }
get timeToDate() { return timeToDate.bind(this) }
get validateDir() { return validateDir.bind(this) }
get validateYear() { return validateYear.bind(this) }
get writeLog() { return writeLog.bind(this) }
get writeLogError() { return writeLogError.bind(this) }
get _log() { return _log.bind(this) }
info() {
let arMethods = sourceLib;
return new Proxy(arMethods, {
get: function (obj, prop) {
let result, categories = {};
if (prop in obj) {
return obj[prop]
}
if (prop === 'number') {
return obj.length
}
for (let product of obj) {
if (product.name === prop) {
result = product
}
if (categories[product.category]) {
categories[product.category].push(product)
} else {
categories[product.category] = [product]
}
}
if (result) {
return result
}
if (prop === 'names') {
return Object.keys(obj).map(items => obj[items].name).filter(Boolean)
}
if (prop === 'tests') {
return Object.keys(obj).map(items => obj[items].test).filter(Boolean)
}
if (prop in categories) {
return categories[prop]
}
if (prop === 'categories') {
return Object.keys(categories)
}
return undefined;
}
});
}}
module.exports = (params) => new jsPackTools(params);