Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: objId was parsed as a number, and thus removed e.g. 00 #101

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions packages/helper/src/utils/Utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,20 @@ export function pad(n: string, width: number, z?: string): string {
}

export function xml2js(messageString: string): { [key: string]: any } {
/**
* objId should be handled as a string, not as a number
* so we need to force it as text in the messageString
* and reconvert it after it has been parsed by xmlParser
* all objects in this set will be forced to be strings:
*/
const forcedStringObject = new Set(['objId', 'objID'])
for (const object of forcedStringObject) {
const regex = new RegExp(`<${object}>(.*?)</${object}>`, 'g')
messageString = messageString.replace(regex, (_match, p1) => {
return `<${object}>_str_${p1}</${object}>`
})
}

const object = xmlParser(messageString, { compact: false, trim: true, nativeType: true })
// common tags we typically want to know the order of the contents of:
const orderedTags = new Set(['storyBody', 'mosAbstract', 'description', 'p', 'em', 'span', 'h1', 'h2', 'i', 'b'])
Expand Down Expand Up @@ -37,6 +51,15 @@ export function xml2js(messageString: string): { [key: string]: any } {

const childEl = element.elements[0]
const name = childEl.$name || childEl.$type || 'unknownElement'

/**
* forcedStringObjects has been forced to be strings, not numbers
* use the original value by removing _str_
*/
if (typeof childEl.text === 'string' && childEl.text.includes('_str_')) {
childEl.text = childEl.text.replace(/_str_/, '')
}

if (childEl.$type && childEl.$type === 'text') {
if (childEl.$name === undefined) {
// pure text node, hoist it up:
Expand Down Expand Up @@ -75,6 +98,14 @@ export function xml2js(messageString: string): { [key: string]: any } {
// make array compact:
const array: any = []
for (const childEl of element.elements) {
/**
* forcedStringObjects has been forced to be strings, not numbers
* use the original value by removing _str_
*/
if (typeof childEl.text === 'string' && childEl.text.includes('_str_')) {
childEl.text = childEl.text.replace(/_str_/, '')
}

if (childEl.$type && childEl.$type === 'text') {
if (Object.keys(childEl).length > 2) {
array.push(childEl)
Expand Down
22 changes: 22 additions & 0 deletions packages/helper/src/utils/__tests__/Utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,3 +146,25 @@ test('xml2js with simple data', () => {
})
}
})

test('xml2js handle objID with nubmerformatting as string', () => {
const o: any = xml2js(`
<content>
<navn>Jon Gelius</navn>
<objId>000987.6540</objId>
<tittel/>
<tematekst/>
<infotekst/>
<_valid>true</_valid>
</content>
`)

expect(o.content).toEqual({
navn: 'Jon Gelius',
objId: '000987.6540',
tittel: {},
tematekst: {},
infotekst: {},
_valid: true,
})
})