Skip to content

Commit

Permalink
feat(converter): factorize individual name utils in common file for iso
Browse files Browse the repository at this point in the history
  • Loading branch information
jahow committed Apr 12, 2024
1 parent 73be4e4 commit 5ec96b6
Show file tree
Hide file tree
Showing 12 changed files with 63 additions and 28 deletions.
4 changes: 2 additions & 2 deletions libs/api/metadata-converter/src/lib/gn4/atomic-operations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import {
Individual,
Organization,
} from '@geonetwork-ui/common/domain/model/record'
import { getRoleFromRoleCode } from '../iso19139/codelists/role.mapper'
import { getRoleFromRoleCode } from '../iso19139/utils/role.mapper'
import { Thesaurus } from './types'
import { getKeywordTypeFromKeywordTypeCode } from '../iso19139/codelists/keyword.mapper'
import { getKeywordTypeFromKeywordTypeCode } from '../iso19139/utils/keyword.mapper'

export type SourceWithUnknownProps = { [key: string]: unknown }

Expand Down
4 changes: 2 additions & 2 deletions libs/api/metadata-converter/src/lib/gn4/gn4.field.mapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import {
} from './atomic-operations'
import { MetadataUrlService } from './metadata-url.service'
import { Injectable } from '@angular/core'
import { getStatusFromStatusCode } from '../iso19139/codelists/status.mapper'
import { getUpdateFrequencyFromFrequencyCode } from '../iso19139/codelists/update-frequency.mapper'
import { getStatusFromStatusCode } from '../iso19139/utils/status.mapper'
import { getUpdateFrequencyFromFrequencyCode } from '../iso19139/utils/update-frequency.mapper'
import {
CatalogRecord,
Constraint,
Expand Down
6 changes: 2 additions & 4 deletions libs/api/metadata-converter/src/lib/iso19115-3/read-parts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import {
Role,
} from '@geonetwork-ui/common/domain/model/record'
import { matchMimeType } from '../common/distribution.mapper'
import { fullNameToParts } from '../iso19139/utils/individual-name'

export function readKind(rootEl: XmlElement): RecordKind {
return pipe(
Expand Down Expand Up @@ -97,10 +98,7 @@ export function extractIndividual(
extractCharacterString(),
map((fullName) => {
if (!fullName) return []
const parts = fullName.split(/\s+/)
if (!parts.length) return [fullName, null]
const first = parts.shift()
return [first, parts.join(' ')]
return fullNameToParts(fullName)
})
)
const getContact = findNestedElement('cit:contactInfo', 'cit:CI_Contact')
Expand Down
10 changes: 4 additions & 6 deletions libs/api/metadata-converter/src/lib/iso19115-3/write-parts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import {
writeLinkage,
} from '../iso19139/write-parts'
import { findIdentification } from '../iso19139/read-parts'
import { namePartsToFull } from '../iso19139/utils/individual-name'

export function writeUniqueIdentifier(
record: CatalogRecord,
Expand Down Expand Up @@ -241,17 +242,14 @@ export function writeOwnerOrganization(
}

export function appendResponsibleParty(contact: Individual) {
const name =
contact.lastName && contact.firstName
? `${contact.firstName} ${contact.lastName}`
: contact.lastName || contact.firstName || null
const fullName = namePartsToFull(contact.firstName, contact.lastName)

const createIndividual = pipe(
createElement('cit:individual'),
createChild('cit:CI_Individual'),
name
fullName
? appendChildren(
pipe(createElement('cit:name'), writeCharacterString(name))
pipe(createElement('cit:name'), writeCharacterString(fullName))
)
: noop,
contact.position
Expand Down
14 changes: 6 additions & 8 deletions libs/api/metadata-converter/src/lib/iso19139/read-parts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import {
UpdateFrequency,
UpdateFrequencyCustom,
} from '@geonetwork-ui/common/domain/model/record'
import { getStatusFromStatusCode } from './codelists/status.mapper'
import { getUpdateFrequencyFromFrequencyCode } from './codelists/update-frequency.mapper'
import { getStatusFromStatusCode } from './utils/status.mapper'
import { getUpdateFrequencyFromFrequencyCode } from './utils/update-frequency.mapper'
import {
findChildElement,
findChildrenElement,
Expand All @@ -37,9 +37,10 @@ import {
mapArray,
pipe,
} from '../function-utils'
import { getRoleFromRoleCode } from './codelists/role.mapper'
import { getRoleFromRoleCode } from './utils/role.mapper'
import { matchMimeType, matchProtocol } from '../common/distribution.mapper'
import { getKeywordTypeFromKeywordTypeCode } from './codelists/keyword.mapper'
import { getKeywordTypeFromKeywordTypeCode } from './utils/keyword.mapper'
import { fullNameToParts } from './utils/individual-name'

export function extractCharacterString(): ChainableFunction<
XmlElement,
Expand Down Expand Up @@ -143,10 +144,7 @@ export function extractIndividual(): ChainableFunction<XmlElement, Individual> {
extractCharacterString(),
map((fullName) => {
if (!fullName) return []
const parts = fullName.split(/\s+/)
if (!parts.length) return [fullName, null]
const first = parts.shift()
return [first, parts.join(' ')]
return fullNameToParts(fullName)
})
)
const getOrganization = extractOrganization()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { fullNameToParts, namePartsToFull } from './individual-name'

describe('individual name utils', () => {
it('fullNameToParts', () => {
expect(fullNameToParts('John Doe')).toEqual(['John', 'Doe'])
expect(fullNameToParts('John')).toEqual(['John', null])
expect(fullNameToParts(' John Jim Doe Blah ')).toEqual([
'John',
'Jim Doe Blah',
])
})
it('namePartsToFull', () => {
expect(namePartsToFull('John', 'Doe')).toEqual('John Doe')
expect(namePartsToFull('John', null)).toEqual('John')
expect(namePartsToFull(null, 'Doe')).toEqual('Doe')
expect(namePartsToFull(null, null)).toEqual(null)
expect(namePartsToFull('', ' ')).toEqual(null)
expect(namePartsToFull('John', 'Doe Blah')).toEqual('John Doe Blah')
expect(namePartsToFull(' John Jim ', ' Doe Blah ')).toEqual(
'John Jim Doe Blah'
)
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* Parts are [firstName, lastName]
* Second part will be null if no separation could be done
* @param fullName
*/
export function fullNameToParts(fullName: string): [string, string | null] {
const parts = fullName.trim().split(/\s+/)
const first = parts.shift()
return [first, parts.join(' ').trim() || null]
}

export function namePartsToFull(
firstName: string | null,
lastName: string | null
): string | null {
const first = firstName?.trim()
const last = lastName?.trim()
if (!first && !last) return null
return last && first ? `${first} ${last}` : last || first
}
10 changes: 4 additions & 6 deletions libs/api/metadata-converter/src/lib/iso19139/write-parts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import {
} from '../function-utils'
import format from 'date-fns/format'
import { readKind } from './read-parts'
import { namePartsToFull } from './utils/individual-name'

export function writeCharacterString(
text: string
Expand Down Expand Up @@ -235,10 +236,7 @@ export function getISODuration(updateFrequency: UpdateFrequencyCustom): string {
}

export function appendResponsibleParty(contact: Individual) {
const name =
contact.lastName && contact.firstName
? `${contact.firstName} ${contact.lastName}`
: contact.lastName || contact.firstName || null
const fullName = namePartsToFull(contact.firstName, contact.lastName)

const createAddress = pipe(
createElement('gmd:address'),
Expand Down Expand Up @@ -287,11 +285,11 @@ export function appendResponsibleParty(contact: Individual) {
return appendChildren(
pipe(
createElement('gmd:CI_ResponsibleParty'),
name
fullName
? appendChildren(
pipe(
createElement('gmd:individualName'),
writeCharacterString(name)
writeCharacterString(fullName)
)
)
: noop,
Expand Down

0 comments on commit 5ec96b6

Please sign in to comment.