Merge branch '15781-multi-value-property-edit'
[arvados-workbench2.git] / src / lib / resource-properties.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 export const deleteProperty = (properties: any, key: string, value: string) => {
6     if (Array.isArray(properties[key])) {
7         properties[key] = properties[key].filter((v: string) => v !== value);
8         if (properties[key].length === 1) {
9             properties[key] = properties[key][0];
10         } else if (properties[key].length === 0) {
11             delete properties[key];
12         }
13     } else if (properties[key] === value) {
14         delete properties[key];
15     }
16     return properties;
17 }
18
19 export const addProperty = (properties: any, key: string, value: string) => {
20     if (properties[key]) {
21         if (Array.isArray(properties[key])) {
22             properties[key] = [...properties[key], value];
23         } else {
24             properties[key] = [properties[key], value];
25         }
26         // Remove potential duplicate and save as single value if needed
27         properties[key] = Array.from(new Set(properties[key]));
28         if (properties[key].length === 1) {
29             properties[key] = properties[key][0];
30         }
31     } else {
32         properties[key] = value;
33     }
34     return properties;
35 }