Skip to content

Commit 69ffa8e

Browse files
committed
ignore invalid units
1 parent 3bc86cb commit 69ffa8e

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

packages/core/src/attributes.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export type AttributeWithUnit = {
3939
/**
4040
* Unit of measurement that can be added to an attribute.
4141
*/
42-
type Units = 'ms' | 's' | 'bytes' | 'count' | 'percent';
42+
type Units = 'ms' | 's' | 'bytes' | 'count' | 'percent' | string;
4343

4444
/* If an attribute has either a 'value' or 'unit' property, we use the ValidAttributeObject type. */
4545
export type ValidatedAttributes<T> = {
@@ -86,7 +86,7 @@ export function isAttributeObject(maybeObj: unknown): maybeObj is AttributeWithU
8686
*/
8787
export function attributeValueToTypedAttributeValue(rawValue: unknown): TypedAttributeValue {
8888
const { value, unit } = isAttributeObject(rawValue) ? rawValue : { value: rawValue, unit: undefined };
89-
return { ...getTypedAttributeValue(value), ...(unit && { unit }) };
89+
return { ...getTypedAttributeValue(value), ...(unit && typeof unit === 'string' ? { unit } : {}) };
9090
}
9191

9292
// Disallow NaN, differentiate between integer and double

packages/core/test/lib/attributes.test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,17 @@ describe('attributeValueToTypedAttributeValue', () => {
250250
});
251251
});
252252
});
253+
254+
it.each([1, true, null, undefined, NaN, Symbol('test'), { foo: 'bar' }])(
255+
'ignores invalid (non-string) units (%s)',
256+
unit => {
257+
const result = attributeValueToTypedAttributeValue({ value: 'foo', unit });
258+
expect(result).toStrictEqual({
259+
value: 'foo',
260+
type: 'string',
261+
});
262+
},
263+
);
253264
});
254265

255266
describe('isAttributeObject', () => {

0 commit comments

Comments
 (0)