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'; ...@@ -2,7 +2,7 @@ import { Component, OnInit } from '@angular/core';
import { BrapiService } from '../brapi.service'; import { BrapiService } from '../brapi.service';
import { ActivatedRoute } from '@angular/router'; import { ActivatedRoute } from '@angular/router';
import { BrapiLocation } from '../models/brapi.model'; import { BrapiLocation } from '../models/brapi.model';
import { KeyValueObject } from '../utils'; import { KeyValueObject, toKeyValueObjects } from '../utils';
import { DataDiscoverySource } from '../models/data-discovery.model'; import { DataDiscoverySource } from '../models/data-discovery.model';
import { GnpisService } from '../gnpis.service'; import { GnpisService } from '../gnpis.service';
...@@ -30,7 +30,7 @@ export class SiteCardComponent implements OnInit { ...@@ -30,7 +30,7 @@ export class SiteCardComponent implements OnInit {
this.location = response.result; this.location = response.result;
this.additionalInfos = []; this.additionalInfos = [];
if (this.location.additionalInfo) { if (this.location.additionalInfo) {
this.manageAdditionalInfo(KeyValueObject.fromObject(this.location.additionalInfo).sort()); this.manageAdditionalInfo(toKeyValueObjects(this.location.additionalInfo).sort());
} }
const sourceURI = location['schema:includedInDataCatalog']; const sourceURI = location['schema:includedInDataCatalog'];
// TODO Remove the condition when the field includedInDataCatalog will be added to URGI study. // TODO Remove the condition when the field includedInDataCatalog will be added to URGI study.
......
...@@ -5,7 +5,7 @@ import { BrapiGermplasm, BrapiObservationVariable, BrapiStudy, BrapiTrial } from ...@@ -5,7 +5,7 @@ import { BrapiGermplasm, BrapiObservationVariable, BrapiStudy, BrapiTrial } from
import { GnpisService } from '../gnpis.service'; import { GnpisService } from '../gnpis.service';
import { DataDiscoverySource } from '../models/data-discovery.model'; import { DataDiscoverySource } from '../models/data-discovery.model';
import { KeyValueObject } from '../utils'; import { KeyValueObject, toKeyValueObjects } from '../utils';
@Component({ @Component({
selector: 'gpds-study-card', selector: 'gpds-study-card',
...@@ -43,7 +43,7 @@ export class StudyCardComponent implements OnInit { ...@@ -43,7 +43,7 @@ export class StudyCardComponent implements OnInit {
this.additionalInfos = []; this.additionalInfos = [];
if (this.study.additionalInfo) { if (this.study.additionalInfo) {
this.additionalInfos = KeyValueObject.fromObject(this.study.additionalInfo).sort(); this.additionalInfos = toKeyValueObjects(this.study.additionalInfo).sort();
} }
// Get study trials // Get study trials
......
import { KeyValueObject } from './utils'; import { KeyValueObject, toKeyValueObjects } from './utils';
describe('KeyValueObject', () => { describe('KeyValueObject', () => {
it('should convert JS object to array of KeyValueObject', () => { it('should convert JS object to array of KeyValueObject', () => {
const actual = KeyValueObject.fromObject({ const actual = toKeyValueObjects({
'a': '1', 'a': '1',
'b': '2', 'b': '2',
'c': null, 'c': null,
...@@ -13,10 +13,10 @@ describe('KeyValueObject', () => { ...@@ -13,10 +13,10 @@ describe('KeyValueObject', () => {
'f': '3' 'f': '3'
}); });
const expected = [ const expected: KeyValueObject[] = [
new KeyValueObject('a', '1'), { key: 'a', value: '1' },
new KeyValueObject('b', '2'), { key: 'b', value: '2' },
new KeyValueObject('f', '3'), { key: 'f', value: '3' },
]; ];
expect(actual).toEqual(expected); expect(actual).toEqual(expected);
......
...@@ -8,19 +8,18 @@ export function asArray<T>(obj: T | T[]): T[] { ...@@ -8,19 +8,18 @@ export function asArray<T>(obj: T | T[]): T[] {
return [obj]; return [obj];
} }
export class KeyValueObject { export interface KeyValueObject {
public key: string; key: string;
public value: string; value: string;
}
constructor(key: string, value: string) {
this.key = key;
this.value = value;
}
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) .filter(([key, value]) => !!key && !!value)
.map(([key, value]) => new KeyValueObject(key, value)); .map(([key, value]) => ({ key, value }));
}
} }
Supports Markdown
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