Commit 2726921c authored by Guillaume Cornut's avatar Guillaume Cornut
Browse files

fix: Simplify KeyValueObject converter.

parent 681e1024
......@@ -2,7 +2,7 @@ import { Component, OnInit } from '@angular/core';
import { BrapiService } from '../brapi.service';
import { ActivatedRoute } from '@angular/router';
import { BrapiLocation } from '../models/brapi.model';
import { KeyValueObject } from '../utils';
import { KeyValueObject, toKeyValueObjects } from '../utils';
import { DataDiscoverySource } from '../models/data-discovery.model';
import { GnpisService } from '../gnpis.service';
......@@ -30,7 +30,7 @@ export class SiteCardComponent implements OnInit {
this.location = response.result;
this.additionalInfos = [];
if (this.location.additionalInfo) {
this.manageAdditionalInfo(KeyValueObject.fromObject(this.location.additionalInfo).sort());
this.manageAdditionalInfo(toKeyValueObjects(this.location.additionalInfo).sort());
}
const sourceURI = location['schema:includedInDataCatalog'];
// TODO Remove the condition when the field includedInDataCatalog will be added to URGI study.
......
......@@ -5,7 +5,7 @@ import { BrapiGermplasm, BrapiObservationVariable, BrapiStudy, BrapiTrial } from
import { GnpisService } from '../gnpis.service';
import { DataDiscoverySource } from '../models/data-discovery.model';
import { KeyValueObject } from '../utils';
import { KeyValueObject, toKeyValueObjects } from '../utils';
@Component({
selector: 'gpds-study-card',
......@@ -43,7 +43,7 @@ export class StudyCardComponent implements OnInit {
this.additionalInfos = [];
if (this.study.additionalInfo) {
this.additionalInfos = KeyValueObject.fromObject(this.study.additionalInfo).sort();
this.additionalInfos = toKeyValueObjects(this.study.additionalInfo).sort();
}
// Get study trials
......
import { KeyValueObject } from './utils';
import { KeyValueObject, toKeyValueObjects } from './utils';
describe('KeyValueObject', () => {
it('should convert JS object to array of KeyValueObject', () => {
const actual = KeyValueObject.fromObject({
const actual = toKeyValueObjects({
'a': '1',
'b': '2',
'c': null,
......@@ -13,10 +13,10 @@ describe('KeyValueObject', () => {
'f': '3'
});
const expected = [
new KeyValueObject('a', '1'),
new KeyValueObject('b', '2'),
new KeyValueObject('f', '3'),
const expected: KeyValueObject[] = [
{ key: 'a', value: '1' },
{ key: 'b', value: '2' },
{ key: 'f', value: '3' },
];
expect(actual).toEqual(expected);
......
......@@ -8,19 +8,18 @@ export function asArray<T>(obj: T | T[]): T[] {
return [obj];
}
export class KeyValueObject {
public key: string;
public value: string;
constructor(key: string, value: string) {
this.key = key;
this.value = value;
}
export interface KeyValueObject {
key: string;
value: string;
}
static fromObject(o: { [key: string]: string }): KeyValueObject[] {
return Object.entries(o)
/**
* Transform an object with string keys and values to a list of `KeyValueObject`.
* Also makes sure the keys and values are truthy.
*/
export function toKeyValueObjects(object: Record<string, string>): KeyValueObject[] {
return Object.entries(object)
.filter(([key, value]) => !!key && !!value)
.map(([key, value]) => new KeyValueObject(key, value));
}
.map(([key, value]) => ({ key, value }));
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment