Skip to content

Commit

Permalink
Implement enum to feature ID conversion, pending tests
Browse files Browse the repository at this point in the history
  • Loading branch information
KyleJu committed Feb 27, 2025
1 parent 7427f53 commit 3dbcead
Showing 1 changed file with 28 additions and 1 deletion.
29 changes: 28 additions & 1 deletion client-src/elements/chromedash-form-field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,9 @@ export class ChromedashFormField extends LitElement {
<td colspan="2" class="webdx">
See web feature
<a
href="https://webstatus.dev/features/${this.value.toLowerCase()}"
href="https://webstatus.dev/features/${enumLabelToFeatureKey(
this.value
)}"
target="_blank"
>
${this.value}
Expand All @@ -493,6 +495,31 @@ export class ChromedashFormField extends LitElement {
}
}

/**
* enumLabelToFeatureKey converts Webdx enum labels to
* web feature IDs. This function is rewritten from
* https://github.com/GoogleChrome/webstatus.dev/blob/6ae7ecf7c26e2563a6e1685cc6d92fc12bcc7941/lib/gcpspanner/spanneradapters/chromium_historgram_enum_consumer.go#L102-L125
*/
function enumLabelToFeatureKey(label: string) {
let result = '';
for (let i = 0; i < label.length; i++) {
const char = label[i];
if (i === 0) {
result += char.toLowerCase();
continue;
}
if (char === char.toUpperCase()) {
result += '-' + char.toLowerCase();
continue;
}
if (i > 0 && /[a-zA-Z]/.test(label[i - 1]) && char >= '0' && char <= '9') {
result += '-';
}
result += char;
}
return result;
}

/**
* Gets the value of a field from a feature entry form, or from the feature.
* Looks up the field name in the provided form field values, using the stageOrId
Expand Down

0 comments on commit 3dbcead

Please sign in to comment.