Skip to content
Open
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
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ npm install ieee754

## methods

`var ieee754 = require('ieee754')`
`import {read, write} from 'ieee754'`

The `ieee754` object has the following functions:

```
ieee754.read = function (buffer, offset, isLE, mLen, nBytes)
ieee754.write = function (buffer, value, offset, isLE, mLen, nBytes)
read = function (buffer, offset, isLE, mLen, nBytes)
write = function (buffer, value, offset, isLE, mLen, nBytes)
```

The arguments mean the following:
Expand Down
20 changes: 18 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
/*! ieee754. BSD-3-Clause License. Feross Aboukhadijeh <https://feross.org/opensource> */
exports.read = function (buffer, offset, isLE, mLen, nBytes) {

/**
* @param {Uint8Array} buffer the buffer
* @param {number} offset offset into the buffer
* @param {Boolean} isLE is little endian?
* @param {number} mLen mantissa length
* @param {number} nBytes number of bytes
*/
export function read (buffer, offset, isLE, mLen, nBytes) {
let e, m
const eLen = (nBytes * 8) - mLen - 1
const eMax = (1 << eLen) - 1
Expand Down Expand Up @@ -40,7 +48,15 @@ exports.read = function (buffer, offset, isLE, mLen, nBytes) {
return (s ? -1 : 1) * m * Math.pow(2, e - mLen)
}

exports.write = function (buffer, value, offset, isLE, mLen, nBytes) {
/**
* @param {Uint8Array} buffer the buffer
* @param {number} value offset into the buffer
* @param {number} offset value to set
* @param {boolean} isLE is little endian?
* @param {number} mLen mantissa length
* @param {number} nBytes number of bytes
*/
export function write (buffer, value, offset, isLE, mLen, nBytes) {
let e, m, c
let eLen = (nBytes * 8) - mLen - 1
const eMax = (1 << eLen) - 1
Expand Down
8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@
"contributors": [
"Romain Beauxis <toots@rastageeks.org>"
],
"type": "module",
"devDependencies": {
"airtap": "^3.0.0",
"airtap": "^4.0.3",
"standard": "*",
"tape": "^5.0.1"
"tape": "^5.3.1"
},
"keywords": [
"IEEE 754",
Expand All @@ -22,6 +23,9 @@
"floating point",
"ieee754"
],
"engines": {
"node": ">=12.20"
},
"license": "BSD-3-Clause",
"main": "index.js",
"types": "index.d.ts",
Expand Down
12 changes: 6 additions & 6 deletions test/basic.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const ieee754 = require('../')
const test = require('tape')
import test from 'tape'
import { read, write } from '../index.js'

const EPSILON = 0.00001

Expand All @@ -8,7 +8,7 @@ test('read float', function (t) {
const buf = Buffer.alloc(4)

buf.writeFloatLE(val, 0)
const num = ieee754.read(buf, 0, true, 23, 4)
const num = read(buf, 0, true, 23, 4)

t.ok(Math.abs(num - val) < EPSILON)
t.end()
Expand All @@ -18,7 +18,7 @@ test('write float', function (t) {
const val = 42.42
const buf = Buffer.alloc(4)

ieee754.write(buf, val, 0, true, 23, 4)
write(buf, val, 0, true, 23, 4)
const num = buf.readFloatLE(0)

t.ok(Math.abs(num - val) < EPSILON)
Expand All @@ -30,7 +30,7 @@ test('read double', function (t) {
const buf = Buffer.alloc(8)

buf.writeDoubleLE(value, 0)
const num = ieee754.read(buf, 0, true, 52, 8)
const num = read(buf, 0, true, 52, 8)

t.ok(Math.abs(num - value) < EPSILON)
t.end()
Expand All @@ -40,7 +40,7 @@ test('write double', function (t) {
const value = 12345.123456789
const buf = Buffer.alloc(8)

ieee754.write(buf, value, 0, true, 52, 8)
write(buf, value, 0, true, 52, 8)
const num = buf.readDoubleLE(0)

t.ok(Math.abs(num - value) < EPSILON)
Expand Down