blob: 4aa30d7553d8cd392a694e90da100ddb45786e52 [file] [log] [blame]
Copybara854996b2021-09-07 19:36:02 +00001// Copyright 2019 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/**
6 * @fileoverview Shared helpers for dealing with how <mr-cue> element instances
7 * are used.
8 */
9
10export const cueNames = Object.freeze({
11 CODE_OF_CONDUCT: 'code_of_conduct',
12 AVAILABILITY_MSGS: 'availability_msgs',
13 SWITCH_TO_PARENT_ACCOUNT: 'switch_to_parent_account',
14 SEARCH_FOR_NUMBERS: 'search_for_numbers',
15});
16
17export const AVAILABLE_CUES = Object.freeze(new Set(Object.values(cueNames)));
18
19export const CUE_DISPLAY_PREFIX = 'cue.';
20
21/**
22 * Converts a cue name to the format expected by components like <mr-metadata>
23 * for the purpose of ordering fields.
24 *
25 * @param {string} cueName The name of the cue.
26 * @return {string} A "cue.cue_name" formatted String used in ordering cues
27 * alongside field types (ie: Owner) in various field specs.
28 */
29export const cueNameToSpec = (cueName) => {
30 return CUE_DISPLAY_PREFIX + cueName;
31};
32
33/**
34 * Converts an issue field specifier to the name of the cue it references if
35 * it references a cue. ie: "cue.cue_name" would reference "cue_name".
36 *
37 * @param {string} spec A "cue.cue_name" format String specifying that a
38 * specific cue should be mixed alongside issue fields in a component like
39 * <mr-metadata>.
40 * @return {string} Name of the cue customized in the spec or an empty
41 * String if the spec does not reference a cue.
42 */
43export const specToCueName = (spec) => {
44 spec = spec.toLowerCase();
45 if (spec.startsWith(CUE_DISPLAY_PREFIX)) {
46 return spec.substring(CUE_DISPLAY_PREFIX.length);
47 }
48 return '';
49};