Skip to content

Commit 72c906f

Browse files
committed
Some refactoring and added description comments
1 parent 238a9fa commit 72c906f

File tree

1 file changed

+34
-23
lines changed

1 file changed

+34
-23
lines changed

src/scripts/baseline-browser-versions.ts

Lines changed: 34 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -359,16 +359,27 @@ type Options = {
359359
includeDownstreamBrowsers?: boolean;
360360
/**
361361
* Pass a date in the format 'YYYY-MM-DD' to get versions compatible with Widely available on the specified date.
362-
* If left undefined and a `targetYear` is not passed, defaults to Widely Available as of the current date.
362+
* If left undefined and a `targetYear` is not passed, defaults to Widely available as of the current date.
363+
* > NOTE: cannot be used with `targetYear`.
363364
*/
364365
widelyAvailableOnDate?: string | number;
365366
/**
366367
* Pass a year between 2016 and the current year to get browser versions compatible with all
367368
* Newly Available features as of the end of the year specified.
369+
* > NOTE: cannot be used with `widelyAvailableOnDate`.
368370
*/
369371
targetYear?: number;
370372
};
371373

374+
/**
375+
* Function that returns browser versions compatible with specified Baseline targets.
376+
* Defaults to returning the minimum versions of the core browser set that support Baseline Widely available.
377+
* Takes an optional configuraation object `Object` with four optional properties:
378+
* - `listAllCompatibleVersions`: `false` (default) or `false`
379+
* - `includeDownstreamBrowsers`: `false` (default) or `false`
380+
* - `widelyAvailableOnDate`: date in format `YYYY-MM-DD`
381+
* - `targetYear`: year in format `YYYY`
382+
*/
372383
export function getCompatibleVersions(userOptions?: Options): BrowserVersion[] {
373384
let incomingOptions = userOptions ?? {};
374385

@@ -423,8 +434,8 @@ export function getCompatibleVersions(userOptions?: Options): BrowserVersion[] {
423434

424435
type AllVersionsOptions = {
425436
/**
426-
* Whether to return the output as a Javascript Object (`object`) or a CSV string (`csv`).
427-
* Defaults to `object`.
437+
* Whether to return the output as a Javascript `Array` (`array`) or a CSV string (`csv`).
438+
* Defaults to `array`.
428439
*/
429440
outputFormat?: string;
430441
/**
@@ -434,34 +445,35 @@ type AllVersionsOptions = {
434445
includeDownstreamBrowsers?: boolean;
435446
};
436447

448+
/**
449+
* A function that returns all known browser versions with their level of Baseline support.
450+
* Takes an object as an argument with two optional properties:
451+
* - `includeDownstreamBrowsers`: `true` (default) or `false`
452+
* - `outputFormat`: `array` (default) or `csv`
453+
*/
437454
export function getAllVersions(
438455
userOptions?: AllVersionsOptions,
439456
): AllBrowsersBrowserVersion[] | string {
440457
let incomingOptions = userOptions ?? {};
441458

442459
let options: AllVersionsOptions = {
443-
outputFormat: incomingOptions.outputFormat ?? "object",
460+
outputFormat: incomingOptions.outputFormat ?? "array",
444461
includeDownstreamBrowsers:
445462
incomingOptions.includeDownstreamBrowsers ?? false,
446463
};
447464

448465
let nextYear = new Date().getFullYear() + 1;
449466

450467
const yearArray = [...Array(nextYear).keys()].slice(2016);
451-
console.log(yearArray);
452-
const yearObject: YearVersions = {};
468+
const yearMinimumVersions: YearVersions = {};
453469
yearArray.forEach((year: number) => {
454-
let yearString = year.toString();
455-
yearObject[yearString] = {};
470+
yearMinimumVersions[year] = {};
456471
getCompatibleVersions({ targetYear: year }).forEach((version) => {
457-
if (yearObject[yearString]) {
458-
yearObject[yearString][version.browser] = version;
459-
}
472+
if (yearMinimumVersions[year])
473+
yearMinimumVersions[year][version.browser] = version;
460474
});
461475
});
462476

463-
console.log(yearObject);
464-
465477
const waMinimumVersions = getCompatibleVersions({});
466478
const waObject: versionsObject = {};
467479
waMinimumVersions.forEach((version: BrowserVersion) => {
@@ -485,8 +497,8 @@ export function getAllVersions(
485497
let waVersion = waObject[browserName]?.version ?? "0";
486498

487499
yearArray.forEach((year) => {
488-
if (yearObject[year]) {
489-
let minBrowserVersionInfo = yearObject[year][browserName] ?? {
500+
if (yearMinimumVersions[year]) {
501+
let minBrowserVersionInfo = yearMinimumVersions[year][browserName] ?? {
490502
version: "0",
491503
};
492504
let minBrowserVersion = minBrowserVersionInfo.version;
@@ -503,13 +515,13 @@ export function getAllVersions(
503515
subArray.forEach((version) => {
504516
let isWaCompatible =
505517
compareVersions(version.version, waVersion) >= 0 ? true : false;
506-
let versionToPush: AllBrowsersBrowserVersion = {
518+
outputArray.push({
507519
...version,
508520
year: year - 1,
509521
waCompatible: isWaCompatible,
510-
};
511-
outputArray.push(versionToPush);
522+
});
512523
});
524+
513525
thisBrowserAllVersions = thisBrowserAllVersions.slice(
514526
sliceIndex,
515527
thisBrowserAllVersions.length,
@@ -528,12 +540,11 @@ export function getAllVersions(
528540
upstreamVersion.version === version.engine_version,
529541
);
530542
if (correspondingChromiumVersion) {
531-
let versionToPush: AllBrowsersBrowserVersion = {
543+
outputArray.push({
532544
...version,
533545
year: correspondingChromiumVersion.year,
534546
waCompatible: correspondingChromiumVersion.waCompatible,
535-
};
536-
outputArray.push(versionToPush);
547+
});
537548
}
538549
});
539550
}
@@ -549,7 +560,7 @@ export function getAllVersions(
549560
});
550561

551562
if (options.outputFormat === "csv") {
552-
let outputString = `"browser","version","year","waCompatible","release_date","engine","engine_version"\n`;
563+
let outputString = `"browser","version","year","waCompatible","release_date","engine","engine_version"`;
553564

554565
outputArray.forEach((version) => {
555566
let outputs = {
@@ -561,7 +572,7 @@ export function getAllVersions(
561572
engine: version.engine ?? "NULL",
562573
engine_version: version.engine_version ?? "NULL",
563574
};
564-
outputString += `"${outputs.browser}","${outputs.version}","${outputs.year}","${outputs.waCompatible}","${outputs.release_date}","${outputs.engine}","${outputs.engine_version}"\n`;
575+
outputString += `\n"${outputs.browser}","${outputs.version}","${outputs.year}","${outputs.waCompatible}","${outputs.release_date}","${outputs.engine}","${outputs.engine_version}"`;
565576
});
566577

567578
return outputString;

0 commit comments

Comments
 (0)