Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add big endian methods #11

Merged
merged 1 commit into from
Feb 23, 2024
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
78 changes: 77 additions & 1 deletion browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,74 @@ function readInt32LE (buffer, offset) {
return view.getInt32(offset, true)
}

function writeDoubleBE (buffer, value, offset) {
if (offset === undefined) offset = 0

const view = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength)
view.setFloat64(offset, value, false)

return offset + 8
}

function writeFloatBE (buffer, value, offset) {
if (offset === undefined) offset = 0

const view = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength)
view.setFloat32(offset, value, false)

return offset + 4
}

function writeUInt32BE (buffer, value, offset) {
if (offset === undefined) offset = 0

const view = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength)
view.setUint32(offset, value, false)

return offset + 4
}

function writeInt32BE (buffer, value, offset) {
if (offset === undefined) offset = 0

const view = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength)
view.setInt32(offset, value, false)

return offset + 4
}

function readDoubleBE (buffer, offset) {
if (offset === undefined) offset = 0

const view = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength)

return view.getFloat64(offset, false)
}

function readFloatBE (buffer, offset) {
if (offset === undefined) offset = 0

const view = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength)

return view.getFloat32(offset, false)
}

function readUInt32BE (buffer, offset) {
if (offset === undefined) offset = 0

const view = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength)

return view.getUint32(offset, false)
}

function readInt32BE (buffer, offset) {
if (offset === undefined) offset = 0

const view = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength)

return view.getInt32(offset, false)
}

module.exports = exports = {
isBuffer,
isEncoding,
Expand Down Expand Up @@ -495,5 +563,13 @@ module.exports = exports = {
readDoubleLE,
readFloatLE,
readUInt32LE,
readInt32LE
readInt32LE,
writeDoubleBE,
writeFloatBE,
writeUInt32BE,
writeInt32BE,
readDoubleBE,
readFloatBE,
readUInt32BE,
readInt32BE
}
43 changes: 42 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,38 @@ function readInt32LE (buffer, offset) {
return toBuffer(buffer).readInt32LE(offset)
}

function writeDoubleBE (buffer, value, offset) {
return toBuffer(buffer).writeDoubleBE(value, offset)
}

function writeFloatBE (buffer, value, offset) {
return toBuffer(buffer).writeFloatBE(value, offset)
}

function writeUInt32BE (buffer, value, offset) {
return toBuffer(buffer).writeUInt32BE(value, offset)
}

function writeInt32BE (buffer, value, offset) {
return toBuffer(buffer).writeInt32BE(value, offset)
}

function readDoubleBE (buffer, offset) {
return toBuffer(buffer).readDoubleBE(offset)
}

function readFloatBE (buffer, offset) {
return toBuffer(buffer).readFloatBE(offset)
}

function readUInt32BE (buffer, offset) {
return toBuffer(buffer).readUInt32BE(offset)
}

function readInt32BE (buffer, offset) {
return toBuffer(buffer).readInt32BE(offset)
}

module.exports = {
isBuffer,
isEncoding,
Expand Down Expand Up @@ -144,5 +176,14 @@ module.exports = {
readDoubleLE,
readFloatLE,
readUInt32LE,
readInt32LE
readInt32LE,
writeDoubleBE,
writeFloatBE,
writeUInt32BE,
writeInt32BE,
readDoubleBE,
readFloatBE,
readUInt32BE,
readInt32BE

}
189 changes: 189 additions & 0 deletions test/browser.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -406,3 +406,192 @@ test('readInt32LE', (t) => {
t.is(actual, expected)
})
})

test('writeFloatBE', (t) => {
t.test('offset 0', (t) => {
const buffer = b.alloc(4)
const updatedOffset = b.writeFloatBE(buffer, 123.5)

t.is(updatedOffset, 4)
t.is(new DataView(buffer.buffer).getFloat32(0, false), 123.5)
})

t.test('other offset', (t) => {
const buffer = b.alloc(8)
const updatedOffset = b.writeFloatBE(buffer, 123.5, 4)

t.is(updatedOffset, 8)
t.is(new DataView(buffer.buffer).getFloat32(4, false), 123.5)
})

t.test('sub-buffer', (t) => {
const buffer = b.alloc(8)
const sub = buffer.subarray(2)

b.writeFloatBE(sub, 123.5)

t.is(new DataView(buffer.buffer).getFloat32(2, false), 123.5)
})
})

test('writeUInt32BE', (t) => {
t.test('offset 0', (t) => {
const buffer = b.alloc(4)
const updatedOffset = b.writeUInt32BE(buffer, 123)

t.is(updatedOffset, 4)
t.is(new DataView(buffer.buffer).getUint32(0, false), 123)
})

t.test('other offset', (t) => {
const buffer = b.alloc(8)
const updatedOffset = b.writeUInt32BE(buffer, 123, 4)

t.is(updatedOffset, 8)
t.is(new DataView(buffer.buffer).getUint32(4, false), 123)
})

t.test('sub-buffer', (t) => {
const buffer = b.alloc(8)
const sub = buffer.subarray(2)

b.writeUInt32BE(sub, 123)

t.is(new DataView(buffer.buffer).getUint32(2, false), 123)
})
})

test('writeInt32BE', (t) => {
t.test('offset 0', (t) => {
const buffer = b.alloc(4)
const updatedOffset = b.writeInt32BE(buffer, 123)

t.is(updatedOffset, 4)
t.is(new DataView(buffer.buffer).getInt32(0, false), 123)
})

t.test('other offset', (t) => {
const buffer = b.alloc(8)
const updatedOffset = b.writeInt32BE(buffer, 123, 4)

t.is(updatedOffset, 8)
t.is(new DataView(buffer.buffer).getInt32(4, false), 123)
})

t.test('sub-buffer', (t) => {
const buffer = b.alloc(8)
const sub = buffer.subarray(2)

b.writeInt32BE(sub, 123)

t.is(new DataView(buffer.buffer).getInt32(2, false), 123)
})
})

test('readDoubleBE', (t) => {
const expected = 5.447603722011605e-270

t.test('offset 0', (t) => {
const buffer = new Uint8Array([8, 7, 6, 5, 4, 3, 2, 1])

const actual = b.readDoubleBE(buffer)
t.is(actual, expected)
})

t.test('other offset', (t) => {
const buffer = new Uint8Array(
[...new Array(8).fill(0), ...[8, 7, 6, 5, 4, 3, 2, 1]]
)

const actual = b.readDoubleBE(buffer, 8)
t.is(actual, expected)
})

t.test('sub-buffer', (t) => {
const buffer = new Uint8Array(
[...new Array(4).fill(0), ...[8, 7, 6, 5, 4, 3, 2, 1]]
)
const sub = buffer.subarray(4)

const actual = b.readDoubleBE(sub)
t.is(actual, expected)
})
})

test('readFloatBE', (t) => {
const expected = 1.539989614439558e-36

t.test('offset 0', (t) => {
const buffer = new Uint8Array([4, 3, 2, 1])

const actual = b.readFloatBE(buffer)
t.is(actual, expected)
})

t.test('other offset', (t) => {
const buffer = new Uint8Array([0, 0, 0, 0, 4, 3, 2, 1])

const actual = b.readFloatBE(buffer, 4)
t.is(actual, expected)
})

t.test('sub-buffer', (t) => {
const buffer = new Uint8Array([0, 0, 4, 3, 2, 1])
const sub = buffer.subarray(2)

const actual = b.readFloatBE(sub)
t.is(actual, expected)
})
})

test('readUInt32BE', (t) => {
const expected = '78563412'

t.test('Offset 0', (t) => {
const buffer = new Uint8Array([0x78, 0x56, 0x34, 0x12])

const actual = b.readUInt32BE(buffer).toString(16)
t.is(actual, expected)
})

t.test('other offset', (t) => {
const buffer = new Uint8Array([0, 0, 0, 0, 0x78, 0x56, 0x34, 0x12])

const actual = b.readUInt32BE(buffer, 4).toString(16)
t.is(actual, expected)
})

t.test('sub-buffer', (t) => {
const buffer = new Uint8Array([0, 0, 0x78, 0x56, 0x34, 0x12])
const sub = buffer.subarray(2)

const actual = b.readUInt32BE(sub).toString(16)
t.is(actual, expected)
})
})

test('readInt32BE', (t) => {
const expected = 83886080

t.test('Offset 0', (t) => {
const buffer = new Uint8Array([5, 0, 0, 0])

const actual = b.readInt32BE(buffer)
t.is(actual, expected)
})

t.test('other offset', (t) => {
const buffer = new Uint8Array([0, 0, 0, 0, 5, 0, 0, 0])

const actual = b.readInt32BE(buffer, 4)
t.is(actual, expected)
})

t.test('sub-buffer', (t) => {
const buffer = new Uint8Array([0, 0, 5, 0, 0, 0])
const sub = buffer.subarray(2)

const actual = b.readInt32BE(sub)
t.is(actual, expected)
})
})
Loading
Loading