@argos-ci/util
Version:
Set of utilities used across all Argos SDKs.
47 lines (46 loc) • 1.33 kB
JavaScript
//#region src/metadata.ts
/**
* Get metadata path from snapshot path.
*/
function getMetadataPath(snapshotPath) {
return snapshotPath + ".argos.json";
}
//#endregion
//#region src/name.ts
/**
* Build a screenshot name from a test name and options.
*/
function getScreenshotName(name, options = {}) {
return sanitizePath(`${name}${options.viewportWidth ? ` vw-${options.viewportWidth}` : ""}`);
}
/**
* Sanitize a path to be safe on all OSes.
*/
function sanitizePath(name) {
return name.split("/").map((filename) => sanitizeFilename(filename)).join("/");
}
function filenameReservedRegex() {
return /[<>:"/\\|?*\u0000-\u001F]|[. ]$/g;
}
function windowsReservedNameRegex() {
return /^(con|prn|aux|nul|com\d|lpt\d)$/i;
}
/**
* Sanitize a filename to be safe on all OSes.
*/
function sanitizeFilename(name) {
const replacement = "-";
const sanitized = name.replace(filenameReservedRegex(), replacement);
if (windowsReservedNameRegex().test(sanitized)) return `${sanitized}${replacement}`;
return sanitized;
}
//#endregion
//#region src/threshold.ts
/**
* Validates the threshold value.
*/
function validateThreshold(threshold) {
if (threshold < 0 || threshold > 1) throw new Error("The threshold must be between 0 and 1.");
}
//#endregion
export { getScreenshotName as n, getMetadataPath as r, validateThreshold as t };