Adam Raine | 3442a3e | 2024-08-02 13:34:09 | [diff] [blame] | 1 | // 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 | |
| 5 | const ratings = { |
| 6 | good: 'is good', |
| 7 | 'needs-improvement': 'needs improvement', |
| 8 | poor: 'is poor', |
| 9 | }; |
| 10 | |
| 11 | const compares = { |
| 12 | better: 'significantly better than', |
| 13 | worse: 'significantly worse than', |
| 14 | similar: 'similar to', |
| 15 | }; |
| 16 | |
| 17 | function camelize(string) { |
| 18 | return string.replace(/-./g, x => x[1].toUpperCase()); |
| 19 | } |
| 20 | |
| 21 | function capitalizeFirstLetter(string) { |
| 22 | return string.charAt(0).toUpperCase() + string.slice(1); |
| 23 | } |
| 24 | |
| 25 | console.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 | |
| 29 | import * 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 | |
| 36 | const UIStrings = {`); |
| 37 | for (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 Raine | 17f93b8 | 2024-08-07 15:07:11 | [diff] [blame^] | 55 | for (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 Raine | 3442a3e | 2024-08-02 13:34:09 | [diff] [blame] | 70 | console.log(`}; |
| 71 | |
| 72 | const str_ = i18n.i18n.registerUIStrings('panels/timeline/components/MetricCompareStrings.ts', UIStrings);`); |
| 73 | |
| 74 | console.log(` |
| 75 | export 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 | |
| 82 | for (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 | } |
| 94 | console.log(` |
| 95 | throw new Error('Compare string not found'); |
| 96 | }`); |
Adam Raine | 17f93b8 | 2024-08-07 15:07:11 | [diff] [blame^] | 97 | |
| 98 | console.log(` |
| 99 | export 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 | |
| 107 | for (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 | } |
| 119 | console.log(` |
| 120 | throw new Error('Detailed compare string not found'); |
| 121 | }`); |