Skip to content

Commit 1e5f8b0

Browse files
author
GuustMetz
committed
fix edge case in formatNamedValue.js
1 parent 560585f commit 1e5f8b0

2 files changed

Lines changed: 11 additions & 3 deletions

File tree

lib/public/utilities/formatting/formatNamedValue.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,9 @@
1919
* @returns {string} The name or id or a '-' if null
2020
*/
2121
export function formatNamedValue(value) {
22-
return value?.name ?? value ?? '-';
22+
if (['string', 'number', 'boolean'].includes(typeof value)) {
23+
return value;
24+
}
25+
26+
return value?.name ?? '-';
2327
}

test/lib/public/utilities/formatting/formatNamedValue.test.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,22 @@ module.exports = () => {
2525
expect(formatNamedValue({ name: 'Test', id: 123 })).to.equal('Test');
2626
});
2727

28-
it('should return value when value has no name property', () => {
28+
it('should return value when value is a string, number or a boolean', () => {
2929
expect(formatNamedValue('Plain String')).to.equal('Plain String');
3030
expect(formatNamedValue(42)).to.equal(42);
3131
expect(formatNamedValue(true)).to.equal(true);
32-
expect(formatNamedValue({ id: 1 })).to.deep.equal({ id: 1 });
3332
});
3433

3534
it('should return "-" when value is null or undefined', () => {
3635
expect(formatNamedValue(null)).to.equal('-');
3736
expect(formatNamedValue(undefined)).to.equal('-');
3837
});
3938

39+
it('should return "-" when value.name is null or undefined', () => {
40+
expect(formatNamedValue({name: undefined})).to.equal('-');
41+
expect(formatNamedValue({name: null})).to.equal('-');
42+
});
43+
4044
it('should correctly handle falsy but defined values', () => {
4145
expect(formatNamedValue('')).to.equal('');
4246
expect(formatNamedValue(0)).to.equal(0);

0 commit comments

Comments
 (0)