Skip to content

Commit

Permalink
fix(shared-data): format rtp float and int choices to include suffix (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
jerader authored Apr 9, 2024
1 parent 3643bc7 commit cc084a4
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ describe('utils-formatRunTimeParameterDefaultValue', () => {
expect(result).toEqual('6.5 mL')
})

it('should return value with suffix when type is str', () => {
it('should return value when type is str', () => {
const mockData = {
value: 'left',
displayName: 'pipette mount',
variableName: 'mont',
variableName: 'mount',
description: 'pipette mount',
type: 'str',
choices: [
Expand All @@ -64,7 +64,59 @@ describe('utils-formatRunTimeParameterDefaultValue', () => {
expect(result).toEqual('Left')
})

it('should return value with suffix when type is boolean true', () => {
it('should return value when type is int choice with suffix', () => {
const mockData = {
value: 5,
displayName: 'num',
variableName: 'number',
description: 'its just number',
type: 'int',
suffix: 'mL',
min: 1,
max: 10,
choices: [
{
displayName: 'one',
value: 1,
},
{
displayName: 'six',
value: 6,
},
],
default: 5,
} as RunTimeParameter
const result = formatRunTimeParameterDefaultValue(mockData, mockTFunction)
expect(result).toEqual('5 mL')
})

it('should return value when type is float choice with suffix', () => {
const mockData = {
value: 5.0,
displayName: 'num',
variableName: 'number',
description: 'its just number',
type: 'float',
suffix: 'mL',
min: 1.0,
max: 10.0,
choices: [
{
displayName: 'one',
value: 1.0,
},
{
displayName: 'six',
value: 6.0,
},
],
default: 5.0,
} as RunTimeParameter
const result = formatRunTimeParameterDefaultValue(mockData, mockTFunction)
expect(result).toEqual('5 mL')
})

it('should return value when type is boolean true', () => {
const mockData = {
value: true,
displayName: 'Deactivate Temperatures',
Expand All @@ -77,7 +129,7 @@ describe('utils-formatRunTimeParameterDefaultValue', () => {
expect(result).toEqual('On')
})

it('should return value with suffix when type is boolean false', () => {
it('should return value when type is boolean false', () => {
const mockData = {
value: false,
displayName: 'Dry Run',
Expand Down
22 changes: 13 additions & 9 deletions shared-data/js/helpers/formatRunTimeParameterDefaultValue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,18 @@ export const formatRunTimeParameterDefaultValue = (
'suffix' in runTimeParameter && runTimeParameter.suffix != null
? runTimeParameter.suffix
: null

if ('choices' in runTimeParameter && runTimeParameter.choices != null) {
const choice = runTimeParameter.choices.find(
choice => choice.value === defaultValue
)
if (choice != null) {
return suffix != null
? `${choice.displayName} ${suffix}`
: choice.displayName
}
}

switch (type) {
case 'int':
case 'float':
Expand All @@ -21,15 +33,7 @@ export const formatRunTimeParameterDefaultValue = (
} else {
return Boolean(defaultValue) ? 'On' : 'Off'
}
case 'str':
if ('choices' in runTimeParameter && runTimeParameter.choices != null) {
const choice = runTimeParameter.choices.find(
choice => choice.value === defaultValue
)
if (choice != null) {
return choice.displayName
}
}
default:
break
}
return ''
Expand Down
21 changes: 12 additions & 9 deletions shared-data/js/helpers/formatRunTimeParameterValue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,17 @@ export const formatRunTimeParameterValue = (
'suffix' in runTimeParameter && runTimeParameter.suffix != null
? runTimeParameter.suffix
: null

if ('choices' in runTimeParameter && runTimeParameter.choices != null) {
const choice = runTimeParameter.choices.find(
choice => choice.value === value
)
if (choice != null) {
return suffix != null
? `${choice.displayName} ${suffix}`
: choice.displayName
}
}
switch (type) {
case 'int':
case 'float':
Expand All @@ -18,15 +29,7 @@ export const formatRunTimeParameterValue = (
case 'bool': {
return Boolean(value) ? t('on') : t('off')
}
case 'str':
if ('choices' in runTimeParameter && runTimeParameter.choices != null) {
const choice = runTimeParameter.choices.find(
choice => choice.value === value
)
if (choice != null) {
return choice.displayName
}
}
default:
break
}
return ''
Expand Down

0 comments on commit cc084a4

Please sign in to comment.