Skip to content
David Vegh edited this page May 4, 2022 · 2 revisions

Tutorial

write-jsdelivrstat makes it easy to collect, filter and save jsdelivr statistics to csv files.

Visit our documentation site for code reference.

Initilaize a new WriteJsdelivrStat class

In these examples we initilaize a WriteJsdelivrStat class in order to collect statistics about npm-stat-api npm package.

Parameters:

  • packageName: name of the target npm package
  • outDir: path of the directory where the gathered data will be saved into csv files

Example - Initilaize without outDir

Because outDir is null the gathered statistics will be only printed to the console.

const WriteJsdelivrStat = require("write-jsdelivrstat").default;

const targetPackage = "npm-stat-api";
const writejsdelivrstat = new WriteJsdelivrStat(targetPackage);

Example - Initilaize with outDir

Because outDir is not null the gathered statistics will be saved into csv files too.

const WriteJsdelivrStat = require("write-jsdelivrstat").default;

const targetPackage = "npm-stat-api";
const csvDir = "stats/npm-stat-api";
const writejsdelivrstat = new WriteJsdelivrStat(targetPackage, csvDir);

Properties of the WriteJsdelivrStat class

Properties:

  • outDir: path of the directory where the gathered data will be saved into csv files
  • datePeriod: grouping of the statistics
  • writePackageName: flag used to write the name of the package into a csv column
  • mergeStoredData: flag used to merge actual jsdelivr statistics with previously stored

Example - Change outDir

outDir can be changed or set at anytime.

const WriteJsdelivrStat = require("write-jsdelivrstat").default;

const targetPackage = "npm-stat-api";
const writejsdelivrstat = new WriteJsdelivrStat(targetPackage);

writejsdelivrstat.outDir = "stats/npm-stat-api";

Example - Change datePeriod

const WriteJsdelivrStat = require("write-jsdelivrstat").default;

const targetPackage = "npm-stat-api";
const writejsdelivrstat = new WriteJsdelivrStat(targetPackage);

writejsdelivrstat.datePeriod = "month";

Example - Change writePackageName

const WriteJsdelivrStat = require("write-jsdelivrstat").default;

const targetPackage = "npm-stat-api";
const writejsdelivrstat = new WriteJsdelivrStat(targetPackage);

writejsdelivrstat.writePackageName = true;

Example - Change mergeStoredData

const WriteJsdelivrStat = require("write-jsdelivrstat").default;

const targetPackage = "npm-stat-api";
const writejsdelivrstat = new WriteJsdelivrStat(targetPackage);

writejsdelivrstat.mergeStoredData = false;

Methods of the WriteJsdelivrStat class

getJsdelivrStat

Parameters:

  • startDate: start date of the statistics
  • endDate: end date of the statistics

Example - Get jsdelivr statistics between 2022-03-01 and 2022-04-10

const WriteJsdelivrStat = require("write-jsdelivrstat").default;

const targetPackage = "npm-stat-api";
const writejsdelivrstat = new WriteJsdelivrStat(targetPackage);

writejsdelivrstat.getJsdelivrStat("2022-03", "2022-04-10").then(stats => {
    console.log(stats);
});

writeJsdelivrStat

Parameters:

  • startDate: start date of the statistics
  • endDate: end date of the statistics
  • postfix: postfix of the csv file

Example - Write jsdelivr statistics between 2022-01-01 and 2022-03-31, grouped by month

const WriteJsdelivrStat = require("write-jsdelivrstat").default;

const targetPackage = "npm-stat-api";
const csvDir = "stats/npm-stat-api";
const writejsdelivrstat = new WriteJsdelivrStat(targetPackage, csvDir);

writejsdelivrstat.datePeriod = "month";
writejsdelivrstat.writeJsdelivrStat("2022", "2022-03");