Skip to content

Commit

Permalink
report: add Util.prepareReportResult method
Browse files Browse the repository at this point in the history
  • Loading branch information
paulirish committed Aug 3, 2018
1 parent d939fd2 commit ff070b4
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 31 deletions.
35 changes: 4 additions & 31 deletions lighthouse-core/report/html/renderer/report-renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,30 +40,17 @@ class ReportRenderer {
}

/**
* @param {LH.ReportResult} report
* @param {LH.ReportResult} result
* @param {Element} container Parent element to render the report into.
*/
renderReport(report, container) {
// If any mutations happen to the report within the renderers, we want the original object untouched
const clone = /** @type {LH.ReportResult} */ (JSON.parse(JSON.stringify(report)));
renderReport(result, container) {
// Mutate the UIStrings if necessary (while saving originals)
const originalUIStrings = JSON.parse(JSON.stringify(Util.UIStrings));
// If LHR is older (≤3.0.3), it has no locale setting. Set default.
if (!clone.configSettings.locale) {
clone.configSettings.locale = 'en-US';
}
Util.setNumberDateLocale(clone.configSettings.locale);
if (clone.i18n && clone.i18n.rendererFormattedStrings) {
ReportRenderer.updateAllUIStrings(clone.i18n.rendererFormattedStrings);
}

// TODO(phulce): we all agree this is technical debt we should fix
if (typeof clone.categories !== 'object') throw new Error('No categories provided.');
clone.reportCategories = Object.values(clone.categories);
ReportRenderer.smooshAuditResultsIntoCategories(clone.audits, clone.reportCategories);
const report = Util.prepareReportResult(result);

container.textContent = ''; // Remove previous report.
container.appendChild(this._renderReport(clone));
container.appendChild(this._renderReport(report));

// put the UIStrings back into original state
ReportRenderer.updateAllUIStrings(originalUIStrings);
Expand Down Expand Up @@ -230,20 +217,6 @@ class ReportRenderer {
return reportFragment;
}

/**
* Place the AuditResult into the auditDfn (which has just weight & group)
* @param {Object<string, LH.Audit.Result>} audits
* @param {Array<LH.ReportResult.Category>} reportCategories
*/
static smooshAuditResultsIntoCategories(audits, reportCategories) {
for (const category of reportCategories) {
category.auditRefs.forEach(auditMeta => {
const result = audits[auditMeta.id];
auditMeta.result = result;
});
}
}

/**
* @param {LH.I18NRendererStrings} rendererFormattedStrings
*/
Expand Down
49 changes: 49 additions & 0 deletions lighthouse-core/report/html/renderer/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,55 @@ class Util {
return `%10d${NBSP}ms`;
}

/**
*
* @param {LH.Result} result
* @return {LH.ReportResult}
*/
static prepareReportResult(result) {
// If any mutations happen to the report within the renderers, we want the original object untouched
const clone = /** @type {LH.ReportResult} */ (JSON.parse(JSON.stringify(result)));

// If LHR is older (≤3.0.3), it has no locale setting. Set default.
if (!clone.configSettings.locale) {
clone.configSettings.locale = 'en-US';
}
Util.setNumberDateLocale(clone.configSettings.locale);
if (clone.i18n && clone.i18n.rendererFormattedStrings) {
Util.rendererFormattedStrings = clone.i18n.rendererFormattedStrings;
}

// TODO(phulce): we all agree this is technical debt we should fix
if (typeof clone.categories !== 'object') throw new Error('No categories provided.');
clone.reportCategories = Object.values(clone.categories);
Util.smooshAuditResultsIntoCategories(clone.audits, clone.reportCategories);
return clone;
}

/**
* Place the AuditResult into the auditDfn (which has just weight & group)
* @param {Object<string, LH.Audit.Result>} audits
* @param {Array<LH.ReportResult.Category>} reportCategories
*/
static smooshAuditResultsIntoCategories(audits, reportCategories) {
for (const category of reportCategories) {
category.auditRefs.forEach(auditMeta => {
const result = audits[auditMeta.id];
auditMeta.result = result;
});
}
}

/**
* @param {LH.I18NRendererStrings} rendererFormattedStrings
*/
static updateAllUIStrings(rendererFormattedStrings) {
// TODO(i18n): don't mutate these here but on the LHR and pass that around everywhere
for (const [key, value] of Object.entries(rendererFormattedStrings)) {
Util.UIStrings[key] = value;
}
}

/**
* @param {string|Array<string|number>=} displayValue
* @return {string}
Expand Down

0 comments on commit ff070b4

Please sign in to comment.