Skip to content

Commit 6a577db

Browse files
committed
docs: for int32LE helper
1 parent 2ee93ab commit 6a577db

File tree

2 files changed

+25
-5
lines changed

2 files changed

+25
-5
lines changed

test/node/tools/utils.js

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,13 +128,33 @@ const bufferFromHexArray = array => {
128128

129129
exports.bufferFromHexArray = bufferFromHexArray;
130130

131-
function int32ToHex(int32) {
131+
/**
132+
* A companion helper to bufferFromHexArray to help with constructing bson bytes manually.
133+
* When creating a BSON Binary you need a leading little endian int32 followed by a sequence of bytes
134+
* of that length.
135+
*
136+
* @example
137+
* ```js
138+
* const binAsHex = '000000';
139+
* const serializedUUID = bufferFromHexArray([
140+
* '05', // binData type
141+
* '6100', // 'a' & null
142+
* int32ToHex(binAsHex.length / 2), // binary starts with int32 length
143+
* '7F', // user subtype
144+
* binAsHex // uuid bytes
145+
* ]);
146+
* ```
147+
*
148+
* @param {number | Int32} int32 -
149+
* @returns
150+
*/
151+
function int32LEToHex(int32) {
132152
const buf = Buffer.alloc(4);
133-
buf.writeInt32LE(int32, 0);
153+
buf.writeInt32LE(+int32, 0);
134154
return buf.toString('hex');
135155
}
136156

137-
exports.int32ToHex = int32ToHex;
157+
exports.int32LEToHex = int32LEToHex;
138158

139159
/**
140160
* A helper to calculate the byte size of a string (including null)

test/node/uuid.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { BSON, BSONError } from '../register-bson';
55
const BSON_DATA_BINARY = BSON.BSONType.binData;
66
import { BSON_BINARY_SUBTYPE_UUID_NEW } from '../../src/constants';
77
import { expect } from 'chai';
8-
import { bufferFromHexArray, int32ToHex } from './tools/utils';
8+
import { bufferFromHexArray, int32LEToHex } from './tools/utils';
99

1010
// Test values
1111
const UPPERCASE_DASH_SEPARATED_UUID_STRING = 'AAAAAAAA-AAAA-4AAA-AAAA-AAAAAAAAAAAA';
@@ -158,7 +158,7 @@ describe('UUID', () => {
158158
const serializedUUID = bufferFromHexArray([
159159
'05', // binData type
160160
'6100', // 'a' & null
161-
int32ToHex(nullUUID.length / 2), // binary starts with int32 length
161+
int32LEToHex(nullUUID.length / 2), // binary starts with int32 length
162162
'04', // uuid subtype
163163
nullUUID // uuid bytes
164164
]);

0 commit comments

Comments
 (0)