X-Git-Url: https://git.arvados.org/arvados-workbench2.git/blobdiff_plain/939593fbdd53bcca1c9c5e2247b3f279fdcf1523..6522c126265875505b3f616c867ef7d655a4da80:/src/models/vocabulary.ts diff --git a/src/models/vocabulary.ts b/src/models/vocabulary.ts index 55525c22..c913bd65 100644 --- a/src/models/vocabulary.ts +++ b/src/models/vocabulary.ts @@ -2,6 +2,7 @@ // // SPDX-License-Identifier: AGPL-3.0 +import { escapeRegExp } from 'common/regexp'; import { isObject, has, every } from 'lodash/fp'; export interface Vocabulary { @@ -27,7 +28,7 @@ export interface Tag { export interface PropFieldSuggestion { id: string; label: string; - description?: string; + synonyms?: string[]; } const VOCABULARY_VALIDATORS = [ @@ -44,12 +45,17 @@ export const isStrictTag = (tagKeyID: string, vocabulary: Vocabulary) => { return tag ? tag.strict : false; }; -export const getTagValueID = (tagKeyID:string, tagValueLabel:string, vocabulary: Vocabulary) => - (tagKeyID && vocabulary.tags[tagKeyID] && vocabulary.tags[tagKeyID].values) - ? Object.keys(vocabulary.tags[tagKeyID].values!).find( - k => vocabulary.tags[tagKeyID].values![k].labels.find( - l => l.label.toLowerCase() === tagValueLabel.toLowerCase()) !== undefined) || '' - : ''; +export const getTagValueID = (tagKeyID:string, tagValueLabel:string, vocabulary: Vocabulary) => { + if (tagKeyID && vocabulary.tags[tagKeyID] && vocabulary.tags[tagKeyID].values) { + const values = vocabulary.tags[tagKeyID].values!; + return Object.keys(values).find(k => + (k.toLowerCase() === tagValueLabel.toLowerCase()) + || values[k].labels.find( + l => l.label.toLowerCase() === tagValueLabel.toLowerCase()) !== undefined) + || ''; + }; + return ''; +}; export const getTagValueLabel = (tagKeyID:string, tagValueID:string, vocabulary: Vocabulary) => vocabulary.tags[tagKeyID] && @@ -78,19 +84,22 @@ export const getTagValues = (tagKeyID: string, vocabulary: Vocabulary): PropFiel : []; }; -export const getPreferredTagValues = (tagKeyID: string, vocabulary: Vocabulary, withSynonyms?: boolean): PropFieldSuggestion[] => { +export const getPreferredTagValues = (tagKeyID: string, vocabulary: Vocabulary, withMatch?: string): PropFieldSuggestion[] => { const tag = vocabulary.tags[tagKeyID]; + const regex = !!withMatch ? new RegExp(escapeRegExp(withMatch), 'i') : undefined; return tag && tag.values ? Object.keys(tag.values).map( tagValueID => tag.values![tagValueID].labels && tag.values![tagValueID].labels.length > 0 ? { "id": tagValueID, "label": tag.values![tagValueID].labels[0].label, - "description": tag.values![tagValueID].labels[0].label + ( - withSynonyms && tag.values![tagValueID].labels.length > 1 - ? ` (${tag.values![tagValueID].labels.slice(1).map(l => l.label).join(', ')})` - : '')} - : {"id": tagValueID, "label": tagValueID}) + "synonyms": !!withMatch && tag.values![tagValueID].labels.length > 1 + ? tag.values![tagValueID].labels.slice(1) + .filter(l => !!regex ? regex.test(l.label) : true) + .map(l => l.label) + : [] + } + : {"id": tagValueID, "label": tagValueID, "synonyms": []}) .sort(compare) : []; }; @@ -107,28 +116,30 @@ export const getTags = ({ tags }: Vocabulary): PropFieldSuggestion[] => { : []; }; -export const getPreferredTags = ({ tags }: Vocabulary, withSynonyms?: boolean): PropFieldSuggestion[] => { +export const getPreferredTags = ({ tags }: Vocabulary, withMatch?: string): PropFieldSuggestion[] => { + const regex = !!withMatch ? new RegExp(escapeRegExp(withMatch), 'i') : undefined; return tags && Object.keys(tags) ? Object.keys(tags).map( tagID => tags[tagID].labels && tags[tagID].labels.length > 0 ? { "id": tagID, "label": tags[tagID].labels[0].label, - "description": tags[tagID].labels[0].label + ( - withSynonyms && tags[tagID].labels.length > 1 - ? ` (${tags[tagID].labels.slice(1).map(lbl => lbl.label).join(', ')})` - : '' - )} - : {"id": tagID, "label": tagID}) + "synonyms": !!withMatch && tags[tagID].labels.length > 1 + ? tags[tagID].labels.slice(1) + .filter(l => !!regex ? regex.test(l.label) : true) + .map(lbl => lbl.label) + : [] + } + : {"id": tagID, "label": tagID, "synonyms": []}) .sort(compare) : []; }; -export const getTagKeyID = (tagKeyLabel:string, vocabulary: Vocabulary) => - Object.keys(vocabulary.tags).find( - k => vocabulary.tags[k].labels.find( - l => l.label.toLowerCase() === tagKeyLabel.toLowerCase()) !== undefined - ) || ''; +export const getTagKeyID = (tagKeyLabel: string, vocabulary: Vocabulary) => + Object.keys(vocabulary.tags).find(k => (k.toLowerCase() === tagKeyLabel.toLowerCase()) + || vocabulary.tags[k].labels.find( + l => l.label.toLowerCase() === tagKeyLabel.toLowerCase()) !== undefined) + || ''; export const getTagKeyLabel = (tagKeyID:string, vocabulary: Vocabulary) => vocabulary.tags[tagKeyID] && vocabulary.tags[tagKeyID].labels.length > 0