@@ -7,19 +7,37 @@ const createCsvWriter = require("csv-writer").createArrayCsvWriter;
77
88const StatDate = require ( "./statdate.js" ) ;
99
10+ /**
11+ * Enum for statistics grouping (year, month, day, null)
12+ * @readonly
13+ * @enum {string|null}
14+ */
1015const StatPeriod = new Enum (
1116 [ "year" , "month" , "day" , null ] ,
1217 { ignoreCase : true } ,
1318 { freeze : true }
1419) ;
1520
21+ /**
22+ * Class to collect, filter and save npm statistics to csv files
23+ * @property {string|null } [outDir] - path of the directory where
24+ * the gathered data will be saved into csv files
25+ * @property {StatPeriod } [datePeriod=year] - grouping of the statistics
26+ * @property {boolean } [mergeStoredData=true] - flag used to merge actual npm statistics with previously stored
27+ */
1628class WriteNpmStat {
1729 #packageName;
1830 outDir ;
1931
2032 #datePeriod;
2133 #mergeStoredData;
2234
35+ /**
36+ * Initialize WriteNpmStat class
37+ * @param {string } packageName - name of the target npm package
38+ * @param {string|null } [outDir] - path of the directory where
39+ * the gathered data will be saved into csv files
40+ */
2341 constructor ( packageName , outDir ) {
2442 if ( ! packageName ) {
2543 throw new Error ( "packageName is a required argument" ) ;
@@ -52,8 +70,32 @@ class WriteNpmStat {
5270 this . #mergeStoredData = Boolean ( mergeStoredData ) ;
5371 }
5472
55- getNpmStat ( startDay , endDay ) {
56- const statDate = new StatDate ( startDay , endDay ) ;
73+ /**
74+ * Returns npm statistics for a package
75+ * @param {string|null } [startDate] - start date of the statistics
76+ * should be in one of the following formats:
77+ *
78+ * <br> - "%Y", for example "2022", which means to be collected from "2022-01-01"
79+ *
80+ * <br> - "%Y-%m", for example "2022-01", which means to be collected from "2022-01-01"
81+ *
82+ * <br> - "%Y-%m-%d", for example "2022-01-01", which means to be collected from "2022-01-01"
83+ *
84+ * <br> - undefined, which means to be collected from the last half year
85+ * @param {string|null } [endDate] - end date of the statistics
86+ * should be in one of the following formats:
87+ *
88+ * <br> - "%Y", for example "2022", which means to be collected until "2022-12-31"
89+ *
90+ * <br> - "%Y-%m", for example "2022-12", which means to be collected until "2022-12-31"
91+ *
92+ * <br> - "%Y-%m-%d", for example "2022-12-31", which means to be collected until "2022-12-31"
93+ *
94+ * <br> - undefined, which means to be collected until the actual day
95+ * @returns {Promise } Promise object represents the npm statistics for a package
96+ */
97+ getNpmStat ( startDate , endDate ) {
98+ const statDate = new StatDate ( startDate , endDate ) ;
5799 const days = WriteNpmStat . getDays ( statDate . start , statDate . end ) ;
58100 return new Promise ( ( resolve ) => {
59101 const stats = [ ] ;
@@ -66,10 +108,10 @@ class WriteNpmStat {
66108 } ) ;
67109 }
68110
69- static getDays ( startDay , endDay ) {
111+ static getDays ( startDate , endDate ) {
70112 const arr = [ ] ;
71- const dt = new Date ( startDay ) ;
72- for ( dt ; dt <= new Date ( endDay ) ; dt . setDate ( dt . getDate ( ) + 1 ) ) {
113+ const dt = new Date ( startDate ) ;
114+ for ( dt ; dt <= new Date ( endDate ) ; dt . setDate ( dt . getDate ( ) + 1 ) ) {
73115 arr . push ( StatDate . formatDate ( new Date ( dt ) ) ) ;
74116 }
75117 return arr ;
@@ -91,26 +133,51 @@ class WriteNpmStat {
91133 } ) ;
92134 }
93135
94- writeNpmStat ( startDay , endDay , postfix = "npmstat" ) {
136+ /**
137+ * Writes npm statistics for a package
138+ * @param {string|null } [startDate] - start date of the statistics
139+ * should be in one of the following formats:
140+ *
141+ * <br> - "%Y", for example "2022", which means to be collected from "2022-01-01"
142+ *
143+ * <br> - "%Y-%m", for example "2022-01", which means to be collected from "2022-01-01"
144+ *
145+ * <br> - "%Y-%m-%d", for example "2022-01-01", which means to be collected from "2022-01-01"
146+ *
147+ * <br> - undefined, which means to be collected from the last half year
148+ * @param {string|null } [endDate] - end date of the statistics
149+ * should be in one of the following formats:
150+ *
151+ * <br> - "%Y", for example "2022", which means to be collected until "2022-12-31"
152+ *
153+ * <br> - "%Y-%m", for example "2022-12", which means to be collected until "2022-12-31"
154+ *
155+ * <br> - "%Y-%m-%d", for example "2022-12-31", which means to be collected until "2022-12-31"
156+ *
157+ * <br> - undefined, which means to be collected until the actual day
158+ * @param {string|null } [endDate=npmstat] - csv file's postfix
159+ * @returns {Promise } Promise object represents the npm statistics for a package
160+ */
161+ writeNpmStat ( startDate , endDate , postfix = "npmstat" ) {
95162 return new Promise ( ( resolve ) => {
96- const stats = this . getNpmStat ( startDay , endDay ) ;
163+ const stats = this . getNpmStat ( startDate , endDate ) ;
97164 stats . then ( ( stats ) => {
98165 const grouped = this . #groupStats(
99166 stats ,
100- startDay ,
101- endDay ,
167+ startDate ,
168+ endDate ,
102169 postfix
103170 ) ;
104171 this . #mergeStats( grouped ) . then ( ( merged ) => {
105172 this . #writeStats( merged ) ;
173+ return resolve ( merged ) ;
106174 } ) ;
107- return resolve ( ) ;
108175 } ) ;
109176 } ) ;
110177 }
111178
112- #groupStats( stats , startDay , endDay , postfix ) {
113- const statDate = new StatDate ( startDay , endDay ) ;
179+ #groupStats( stats , startDate , endDate , postfix ) {
180+ const statDate = new StatDate ( startDate , endDate ) ;
114181 const days = WriteNpmStat . getDays ( statDate . start , statDate . end ) ;
115182 const grouped = { } ;
116183 if ( this . datePeriod ) {
0 commit comments