blob: 535d79bca6e251161bacc1197c0da00f670681c5 [file] [log] [blame]
Adam Raine3442a3e2024-08-02 13:34:091// Copyright 2024 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5const ratings = {
6 good: 'is good',
7 'needs-improvement': 'needs improvement',
8 poor: 'is poor',
9};
10
11const compares = {
12 better: 'significantly better than',
13 worse: 'significantly worse than',
14 similar: 'similar to',
15};
16
17function camelize(string) {
18 return string.replace(/-./g, x => x[1].toUpperCase());
19}
20
21function capitalizeFirstLetter(string) {
22 return string.charAt(0).toUpperCase() + string.slice(1);
23}
24
25console.log(`// Copyright 2024 The Chromium Authors. All rights reserved.
26// Use of this source code is governed by a BSD-style license that can be
27// found in the LICENSE file.
28
29import * as i18n from '../../../core/i18n/i18n.js';
30
31// This file is auto-generated by scripts/generate_metric_compare_strings.js.
32//
33// If you need to update one or more of these strings, it is preferable to modify the script
34// and write stdout to this file (Minor formatting differences may apply).
35
36const UIStrings = {`);
37for (const [rating, ratingText] of Object.entries(ratings)) {
38 for (const [compare, compareText] of Object.entries(compares)) {
39 const tag = camelize(rating) + capitalizeFirstLetter(compare) + 'Compare';
40 console.log(` /**
41 * @description Text block that compares a local metric value to real user experiences.
42 * @example {LCP} PH1
43 * @example {500 ms} PH2
44 */
45 ${tag}: 'Your local {PH1} {PH2} ${ratingText}, and is ${compareText} your users’ experience.',`);
46 }
47 const tag = camelize(rating) + 'Summarized';
48 console.log(` /**
49 * @description Text block that summarize a local metric value.
50 * @example {LCP} PH1
51 * @example {500 ms} PH2
52 */
53 ${tag}: 'Your local {PH1} {PH2} ${ratingText}.',`);
54}
Adam Raine17f93b82024-08-07 15:07:1155for (const [localRating, localRatingText] of Object.entries(ratings)) {
56 for (const [fieldRating, fieldRatingText] of Object.entries(ratings)) {
57 const tag = camelize(localRating) + capitalizeFirstLetter(camelize(fieldRating)) + 'DetailedCompare';
58 const transition = localRating === fieldRating ? 'Additionally' : 'However';
59 console.log(` /**
60 * @description Text block that compares a local metric value to real user experiences.
61 * @example {LCP} PH1
62 * @example {500 ms} PH2
63 * @example {400 ms} PH3
64 * @example {40%} PH4
65 */
66 ${tag}: 'Your local {PH1} {PH2} ${localRatingText} and is rated the same as {PH4} of real-user {PH1} experiences. ${
67 transition}, the field data 75th percentile {PH1} {PH3} ${fieldRatingText}.',`);
68 }
69}
Adam Raine3442a3e2024-08-02 13:34:0970console.log(`};
71
72const str_ = i18n.i18n.registerUIStrings('panels/timeline/components/MetricCompareStrings.ts', UIStrings);`);
73
74console.log(`
75export function renderCompareText(rating: ${Object.keys(ratings).map(c => `'${c}'`).join('|')}, compare?: ${
76 Object.keys(compares).map(c => `'${c}'`).join('|')}, values?: Record<string, Object>): Element {
77 if (!values) {
78 values = {};
79 }
80`);
81
82for (const rating of Object.keys(ratings)) {
83 for (const compare of Object.keys(compares)) {
84 const tag = camelize(rating) + capitalizeFirstLetter(compare) + 'Compare';
85 console.log(` if (rating === '${rating}' && compare === '${compare}') {
86 return i18n.i18n.getFormatLocalizedString(str_, UIStrings.${tag}, values);
87 }`);
88 }
89 const tag = camelize(rating) + 'Summarized';
90 console.log(` if (rating === '${rating}' && !compare) {
91 return i18n.i18n.getFormatLocalizedString(str_, UIStrings.${tag}, values);
92 }`);
93}
94console.log(`
95 throw new Error('Compare string not found');
96}`);
Adam Raine17f93b82024-08-07 15:07:1197
98console.log(`
99export function renderDetailedCompareText(localRating: ${
100 Object.keys(ratings).map(c => `'${c}'`).join('|')}, fieldRating?: ${
101 Object.keys(ratings).map(c => `'${c}'`).join('|')}, values?: Record<string, Object>): Element {
102 if (!values) {
103 values = {};
104 }
105`);
106
107for (const localRating of Object.keys(ratings)) {
108 for (const fieldRating of Object.keys(ratings)) {
109 const tag = camelize(localRating) + capitalizeFirstLetter(camelize(fieldRating)) + 'DetailedCompare';
110 console.log(` if (localRating === '${localRating}' && fieldRating === '${fieldRating}') {
111 return i18n.i18n.getFormatLocalizedString(str_, UIStrings.${tag}, values);
112 }`);
113 }
114 const tag = camelize(localRating) + 'Summarized';
115 console.log(` if (localRating === '${localRating}' && !fieldRating) {
116 return i18n.i18n.getFormatLocalizedString(str_, UIStrings.${tag}, values);
117 }`);
118}
119console.log(`
120 throw new Error('Detailed compare string not found');
121}`);