1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import { isObject, has, every } from 'lodash/fp';
7 export interface Vocabulary {
9 tags: Record<string, Tag>;
12 export interface Label {
17 export interface TagValue {
21 export interface Tag {
24 values?: Record<string, TagValue>;
27 export interface PropFieldSuggestion {
32 const VOCABULARY_VALIDATORS = [
38 export const isVocabulary = (value: any) =>
39 every(validator => validator(value), VOCABULARY_VALIDATORS);
41 export const isStrictTag = (tagKeyID: string, vocabulary: Vocabulary) => {
42 const tag = vocabulary.tags[tagKeyID];
43 return tag ? tag.strict : false;
46 export const getTagValueID = (tagKeyID:string, tagValueLabel:string, vocabulary: Vocabulary) =>
47 (tagKeyID && vocabulary.tags[tagKeyID] && vocabulary.tags[tagKeyID].values)
48 ? Object.keys(vocabulary.tags[tagKeyID].values!).find(
49 k => vocabulary.tags[tagKeyID].values![k].labels.find(
50 l => l.label === tagValueLabel) !== undefined) || ''
53 export const getTagValueLabel = (tagKeyID:string, tagValueID:string, vocabulary: Vocabulary) =>
54 vocabulary.tags[tagKeyID] &&
55 vocabulary.tags[tagKeyID].values &&
56 vocabulary.tags[tagKeyID].values![tagValueID] &&
57 vocabulary.tags[tagKeyID].values![tagValueID].labels.length > 0
58 ? vocabulary.tags[tagKeyID].values![tagValueID].labels[0].label
61 const compare = (a: PropFieldSuggestion, b: PropFieldSuggestion) => {
62 if (a.label < b.label) {return -1;}
63 if (a.label > b.label) {return 1;}
67 export const getTagValues = (tagKeyID: string, vocabulary: Vocabulary) => {
68 const tag = vocabulary.tags[tagKeyID];
69 const ret = tag && tag.values
70 ? Object.keys(tag.values).map(
71 tagValueID => tag.values![tagValueID].labels && tag.values![tagValueID].labels.length > 0
72 ? tag.values![tagValueID].labels.map(
73 lbl => Object.assign({}, {"id": tagValueID, "label": lbl.label}))
74 : [{"id": tagValueID, "label": tagValueID}])
75 .reduce((prev, curr) => [...prev, ...curr], [])
81 export const getTags = ({ tags }: Vocabulary) => {
82 const ret = tags && Object.keys(tags)
83 ? Object.keys(tags).map(
84 tagID => tags[tagID].labels && tags[tagID].labels.length > 0
85 ? tags[tagID].labels.map(
86 lbl => Object.assign({}, {"id": tagID, "label": lbl.label}))
87 : [{"id": tagID, "label": tagID}])
88 .reduce((prev, curr) => [...prev, ...curr], [])
94 export const getTagKeyID = (tagKeyLabel:string, vocabulary: Vocabulary) =>
95 Object.keys(vocabulary.tags).find(
96 k => vocabulary.tags[k].labels.find(
97 l => l.label === tagKeyLabel) !== undefined
100 export const getTagKeyLabel = (tagKeyID:string, vocabulary: Vocabulary) =>
101 vocabulary.tags[tagKeyID] && vocabulary.tags[tagKeyID].labels.length > 0
102 ? vocabulary.tags[tagKeyID].labels[0].label