@lhci/utils
Advanced tools
Comparing version 0.1.1-alpha.2 to 0.1.1-alpha.3
{ | ||
"name": "@lhci/utils", | ||
"version": "0.1.1-alpha.2", | ||
"version": "0.1.1-alpha.3", | ||
"license": "Apache-2.0", | ||
@@ -15,3 +15,3 @@ "repository": { | ||
}, | ||
"gitHead": "88b6f085e30e871155454a2969e6e80d9f235f1a" | ||
"gitHead": "4da32a1c9070c11322976f450d93debebff03fff" | ||
} |
@@ -58,5 +58,8 @@ /** | ||
const delta = compareValue - baseValue; | ||
const absoluteDelta = Math.abs(compareValue - baseValue); | ||
const absoluteDelta = Math.abs(delta); | ||
// Handle the 0 case to avoid messy NaN handling. | ||
if (delta === 0) return {delta, absoluteDelta, percentDelta: 0, percentAbsoluteDelta: 0}; | ||
// Percent delta is `delta / baseValue` unless `baseValue == 0`. | ||
// Then `percentDelta` is 100% by arbitrary convention. | ||
// Then `percentDelta` is 100% by arbitrary convention (instead of Infinity/NaN). | ||
const percentDelta = baseValue ? delta / baseValue : 1; | ||
@@ -99,2 +102,3 @@ const percentAbsoluteDelta = Math.abs(percentDelta); | ||
type: 'error', | ||
attemptedType: type, | ||
baseValue: baseValue || NaN, | ||
@@ -204,2 +208,37 @@ compareValue: compareValue || NaN, | ||
/** | ||
* @param {LH.AuditResult} audit | ||
*/ | ||
function normalizeScore(audit) { | ||
if (audit.scoreDisplayMode === 'notApplicable') { | ||
// notApplicable should be treated as passing. | ||
return 1; | ||
} | ||
if (audit.scoreDisplayMode === 'informative') { | ||
// informative should be treated as failing. | ||
return 0; | ||
} | ||
return audit.score; | ||
} | ||
/** | ||
* @param {LH.AuditResult} audit | ||
*/ | ||
function normalizeNumericValue(audit) { | ||
if (audit.scoreDisplayMode === 'notApplicable') { | ||
return 0; | ||
} | ||
return audit.numericValue; | ||
} | ||
/** | ||
* @param {LH.AuditResult} audit | ||
*/ | ||
function normalizeDetailsItems(audit) { | ||
return (audit.details && audit.details.items) || []; | ||
} | ||
/** | ||
* @param {LH.AuditResult} baseAudit | ||
@@ -214,18 +253,22 @@ * @param {LH.AuditResult} compareAudit | ||
/** @type {Array<LHCI.AuditDiff>} */ | ||
const diffs = [ | ||
createAuditDiff({ | ||
auditId, | ||
type: 'score', | ||
baseValue: baseAudit.score, | ||
compareValue: compareAudit.score, | ||
}), | ||
]; | ||
const diffs = []; | ||
if (typeof baseAudit.numericValue === 'number') { | ||
if (typeof baseAudit.score === 'number' || typeof compareAudit.score === 'number') { | ||
diffs.push( | ||
createAuditDiff({ | ||
auditId, | ||
type: 'score', | ||
baseValue: normalizeScore(baseAudit), | ||
compareValue: normalizeScore(compareAudit), | ||
}) | ||
); | ||
} | ||
if (typeof baseAudit.numericValue === 'number' || typeof compareAudit.numericValue === 'number') { | ||
diffs.push( | ||
createAuditDiff({ | ||
auditId, | ||
type: 'numericValue', | ||
baseValue: baseAudit.numericValue, | ||
compareValue: compareAudit.numericValue, | ||
baseValue: normalizeNumericValue(baseAudit), | ||
compareValue: normalizeNumericValue(compareAudit), | ||
}) | ||
@@ -236,9 +279,7 @@ ); | ||
if ( | ||
baseAudit.details && | ||
baseAudit.details.items && | ||
compareAudit.details && | ||
compareAudit.details.items | ||
(baseAudit.details && baseAudit.details.items) || | ||
(compareAudit.details && compareAudit.details.items) | ||
) { | ||
const baseItems = baseAudit.details.items; | ||
const compareItems = compareAudit.details.items; | ||
const baseItems = normalizeDetailsItems(baseAudit); | ||
const compareItems = normalizeDetailsItems(compareAudit); | ||
@@ -245,0 +286,0 @@ diffs.push( |
@@ -12,2 +12,7 @@ /** | ||
// To include in next LHR update... | ||
// - Not applicable audits | ||
// - Informative audits | ||
// - 0-value numericValue | ||
/** @typedef {import('./lhr-generator').AuditGenDef} AuditGenDef */ | ||
@@ -14,0 +19,0 @@ |
81055
1837