Skip to content
Merged
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
15 changes: 15 additions & 0 deletions packages/compass-components/src/components/bson-value.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,21 @@ describe('BSONValue', function () {
value: Binary.createFromHexString('3132303d', Binary.SUBTYPE_UUID),
expected: "UUID('3132303d')",
},
{
type: 'Binary',
value: Binary.fromInt8Array(new Int8Array([1, 2, 3])),
expected: 'Binary.fromInt8Array(new Int8Array([1, 2, 3]))',
},
{
type: 'Binary',
value: Binary.fromFloat32Array(new Float32Array([1.1, 2.2, 3.3])),
expected: 'Binary.fromFloat32Array(new Float32Array([1.1, 2.2, 3.3]))',
},
{
type: 'Binary',
value: Binary.fromPackedBits(new Uint8Array([1, 2, 3])),
expected: 'Binary.fromPackedBits(new Uint8Array([1, 2, 3]))',
},
{
type: 'Code',
value: new Code('var a = 1', { foo: 2 }),
Expand Down
31 changes: 31 additions & 0 deletions packages/compass-components/src/components/bson-value.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,37 @@ const BinaryValue: React.FunctionComponent<PropsByValueType<'Binary'>> = ({

return { stringifiedValue: `UUID('${uuid}')` };
}
if (value.sub_type === Binary.SUBTYPE_VECTOR) {
const vectorType = value.buffer[0];
if (vectorType === Binary.VECTOR_TYPE.Int8) {
const truncatedSerializedBuffer = truncate(
value.toInt8Array().slice(0, 100).join(', '),
100
);
return {
stringifiedValue: `Binary.fromInt8Array(new Int8Array([${truncatedSerializedBuffer}]))`,
};
} else if (vectorType === Binary.VECTOR_TYPE.Float32) {
const truncatedSerializedBuffer = truncate(
[...value.toFloat32Array().slice(0, 100)]
// Using a limited precision and removing trailing zeros for better displaying
.map((num) => num.toPrecision(8).replace(/\.?0+$/, ''))
.join(', '),
100
);
return {
stringifiedValue: `Binary.fromFloat32Array(new Float32Array([${truncatedSerializedBuffer}]))`,
};
} else if (vectorType === Binary.VECTOR_TYPE.PackedBit) {
const truncatedSerializedBuffer = truncate(
value.toPackedBits().slice(0, 100).join(', '),
100
);
return {
stringifiedValue: `Binary.fromPackedBits(new Uint8Array([${truncatedSerializedBuffer}]))`,
};
}
}
return {
stringifiedValue: `Binary.createFromBase64('${truncate(
value.toString('base64'),
Expand Down
Loading