Skip to content

Commit

Permalink
Merge pull request #305 from Element84/ba/bug/metadata-config-excepti…
Browse files Browse the repository at this point in the history
…on-error

metadata config exception error
  • Loading branch information
bradleyandrick authored Dec 27, 2023
2 parents 2223354 + 2465488 commit cebd9dd
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 34 deletions.
9 changes: 6 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

46 changes: 31 additions & 15 deletions src/utils/dataHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,21 +71,37 @@ export function setScenesForCartLayer() {
}

export function processDisplayFieldValues(value) {
if (typeof value === 'boolean') {
return value.toString()
} else if (Array.isArray(value)) {
return value.map(processDisplayFieldValues).join(', ')
} else if (typeof value === 'number') {
return value.toString()
} else if (typeof value === 'string') {
return value
} else if (typeof value === 'object' && value !== null) {
const processedObject = {}
for (const key in value) {
processedObject[key] = processDisplayFieldValues(value[key])
switch (true) {
case value === null:
case value === undefined:
return 'No Data'
case typeof value === 'boolean':
case typeof value === 'number':
case typeof value === 'string':
return value.toString()
case Array.isArray(value):
return value.map(processDisplayFieldValues).join(', ')
case typeof value === 'object':
return formatNestedObjectDisplayFieldValues(value)
default:
return 'Unsupported Type'
}
}

function formatNestedObjectDisplayFieldValues(inputObject) {
function formatValue(value) {
if (Array.isArray(value)) {
return `[${value.map(formatValue).join(', ')}]`
} else if (typeof value === 'object') {
return `{${formatObject(value)}}`
} else {
return value
}
return processedObject
} else {
return 'Unsupported Type'
}
function formatObject(obj) {
return Object.entries(obj)
.map(([key, value]) => `${key}: ${formatValue(value)}`)
.join(', ')
}
return `${formatObject(inputObject)}`
}
25 changes: 9 additions & 16 deletions src/utils/dataHelper.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,29 +149,22 @@ describe('dataHelper', () => {
expect(actual).toEqual(expected)
})

it('should recursively process object values', () => {
it('should process nested object values into string', () => {
const input = {
bool: true,
arr: [1, 2, 3],
num: 42,
str: 'Hello, world!',
nestedObj: {
nestedBool: false,
nestedArr: [4, 5, 6]
nestedArr: [4, 5, 6],
platform: {
eq: 'landsat-8'
},
collections: ['landsat-c2-l2']
}
}

const result = processDisplayFieldValues(input)
expect(result).toEqual({
bool: 'true',
arr: '1, 2, 3',
num: '42',
str: 'Hello, world!',
nestedObj: {
nestedBool: 'false',
nestedArr: '4, 5, 6'
}
})
expect(result).toEqual(
'nestedObj: {nestedBool: false, nestedArr: [4, 5, 6], platform: {eq: landsat-8}, collections: [landsat-c2-l2]}'
)
})

it('returns Unsupported Type for other types', () => {
Expand Down

0 comments on commit cebd9dd

Please sign in to comment.