ordered-binary 1.5.0 → 1.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -125,11 +125,20 @@ function writeKey(key, target, position, inSequence) {
125
125
  let asFloat = Number(key);
126
126
  if (BigInt(asFloat) > key) {
127
127
  float64Array[0] = asFloat;
128
- if (int32Array[0])
129
- int32Array[0]--;
130
- else {
131
- int32Array[1]--;
132
- int32Array[0] = 0xffffffff;
128
+ if (asFloat > 0) {
129
+ if (int32Array[0])
130
+ int32Array[0]--;
131
+ else {
132
+ int32Array[1]--;
133
+ int32Array[0] = 0xffffffff;
134
+ }
135
+ } else {
136
+ if (int32Array[0] < 0xffffffff)
137
+ int32Array[0]++;
138
+ else {
139
+ int32Array[1]++;
140
+ int32Array[0] = 0;
141
+ }
133
142
  }
134
143
  asFloat = float64Array[0];
135
144
  }
@@ -138,7 +147,7 @@ function writeKey(key, target, position, inSequence) {
138
147
  return writeKey(asFloat, target, position, inSequence)
139
148
  writeKey(asFloat, target, position, inSequence);
140
149
  position += 9; // always increment by 9 if we are adding fractional bits
141
- let exponent = BigInt((int32Array[1] >> 20) - 1079);
150
+ let exponent = BigInt((int32Array[1] >> 20 & 0x7ff) - 1079);
142
151
  let nextByte = difference >> exponent;
143
152
  target[position - 1] |= Number(nextByte);
144
153
  difference -= nextByte << exponent;
@@ -185,7 +194,14 @@ function readKey(buffer, start, end, inSequence) {
185
194
  } else
186
195
  return Uint8Array.prototype.slice.call(buffer, start, end)
187
196
  } else {
188
- let dataView = buffer.dataView || (buffer.dataView = new DataView(buffer.buffer, buffer.byteOffset, ((buffer.byteLength + 3) >> 2) << 2));
197
+ let dataView;
198
+ try {
199
+ dataView = buffer.dataView || (buffer.dataView = new DataView(buffer.buffer, buffer.byteOffset, ((buffer.byteLength + 3) >> 2) << 2));
200
+ } catch(error) {
201
+ // if it is write at the end of the ArrayBuffer, we may need to retry with the exact remaining bytes
202
+ dataView = buffer.dataView || (buffer.dataView = new DataView(buffer.buffer, buffer.byteOffset, buffer.buffer.byteLength - buffer.byteOffset));
203
+ }
204
+
189
205
  let highInt = dataView.getInt32(position) << 4;
190
206
  let size = end - position;
191
207
  let lowInt;
@@ -214,8 +230,7 @@ function readKey(buffer, start, end, inSequence) {
214
230
  // convert the float to bigint, and then we will add precision as we enumerate through the
215
231
  // extra bytes
216
232
  value = BigInt(value);
217
- let exponent = highInt >> 20;
218
- // TODO: negatives?
233
+ let exponent = highInt >> 20 & 0x7ff;
219
234
  let next_byte = buffer[position - 1] & 0xf;
220
235
  value += BigInt(next_byte) << BigInt(exponent - 1079);
221
236
  while ((next_byte = buffer[position]) > 0 && position++ < end) {
package/index.js CHANGED
@@ -124,11 +124,20 @@ export function writeKey(key, target, position, inSequence) {
124
124
  let asFloat = Number(key)
125
125
  if (BigInt(asFloat) > key) {
126
126
  float64Array[0] = asFloat;
127
- if (int32Array[0])
128
- int32Array[0]--;
129
- else {
130
- int32Array[1]--;
131
- int32Array[0] = 0xffffffff;
127
+ if (asFloat > 0) {
128
+ if (int32Array[0])
129
+ int32Array[0]--;
130
+ else {
131
+ int32Array[1]--;
132
+ int32Array[0] = 0xffffffff;
133
+ }
134
+ } else {
135
+ if (int32Array[0] < 0xffffffff)
136
+ int32Array[0]++;
137
+ else {
138
+ int32Array[1]++;
139
+ int32Array[0] = 0;
140
+ }
132
141
  }
133
142
  asFloat = float64Array[0];
134
143
  }
@@ -137,7 +146,7 @@ export function writeKey(key, target, position, inSequence) {
137
146
  return writeKey(asFloat, target, position, inSequence)
138
147
  writeKey(asFloat, target, position, inSequence)
139
148
  position += 9; // always increment by 9 if we are adding fractional bits
140
- let exponent = BigInt((int32Array[1] >> 20) - 1079);
149
+ let exponent = BigInt((int32Array[1] >> 20 & 0x7ff) - 1079);
141
150
  let nextByte = difference >> exponent;
142
151
  target[position - 1] |= Number(nextByte);
143
152
  difference -= nextByte << exponent;
@@ -184,7 +193,14 @@ export function readKey(buffer, start, end, inSequence) {
184
193
  } else
185
194
  return Uint8Array.prototype.slice.call(buffer, start, end)
186
195
  } else {
187
- let dataView = buffer.dataView || (buffer.dataView = new DataView(buffer.buffer, buffer.byteOffset, ((buffer.byteLength + 3) >> 2) << 2))
196
+ let dataView;
197
+ try {
198
+ dataView = buffer.dataView || (buffer.dataView = new DataView(buffer.buffer, buffer.byteOffset, ((buffer.byteLength + 3) >> 2) << 2))
199
+ } catch(error) {
200
+ // if it is write at the end of the ArrayBuffer, we may need to retry with the exact remaining bytes
201
+ dataView = buffer.dataView || (buffer.dataView = new DataView(buffer.buffer, buffer.byteOffset, buffer.buffer.byteLength - buffer.byteOffset))
202
+ }
203
+
188
204
  let highInt = dataView.getInt32(position) << 4
189
205
  let size = end - position
190
206
  let lowInt
@@ -213,8 +229,7 @@ export function readKey(buffer, start, end, inSequence) {
213
229
  // convert the float to bigint, and then we will add precision as we enumerate through the
214
230
  // extra bytes
215
231
  value = BigInt(value);
216
- let exponent = highInt >> 20;
217
- // TODO: negatives?
232
+ let exponent = highInt >> 20 & 0x7ff;
218
233
  let next_byte = buffer[position - 1] & 0xf;
219
234
  value += BigInt(next_byte) << BigInt(exponent - 1079);
220
235
  while ((next_byte = buffer[position]) > 0 && position++ < end) {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "ordered-binary",
3
3
  "author": "Kris Zyp",
4
- "version": "1.5.0",
4
+ "version": "1.5.1",
5
5
  "description": "Conversion of JavaScript primitives to and from Buffer with binary order matching natural primitive order",
6
6
  "license": "MIT",
7
7
  "repository": {
package/tests/test.js CHANGED
@@ -47,6 +47,7 @@ suite('key buffers', () => {
47
47
  assertBufferComparison(toBufferKey(-5236532532532), toBufferKey(-5236532532531))
48
48
  })
49
49
  test('bigint equivalence', () => {
50
+ assert.strictEqual(fromBufferKey(toBufferKey(-35913040084491349n)), -35913040084491349n)
50
51
  assert.strictEqual(fromBufferKey(toBufferKey(6135421331404949076605986n)), 6135421331404949076605986n)
51
52
  assert.strictEqual(fromBufferKey(toBufferKey(0xfffffffffffffffffffffn)), 0xfffffffffffffffffffffn)
52
53
  assert.strictEqual(fromBufferKey(toBufferKey(12345678901234567890n)), 12345678901234567890n)
@@ -60,6 +61,7 @@ suite('key buffers', () => {
60
61
  num *= BigInt(Math.floor(random() * 3 + 1));
61
62
  num -= BigInt(Math.floor(random() * 1000));
62
63
  assert.strictEqual(BigInt(fromBufferKey(toBufferKey(num))), num)
64
+ assert.strictEqual(BigInt(fromBufferKey(toBufferKey(-num))), -num)
63
65
  }
64
66
  assert.strictEqual(fromBufferKey(toBufferKey(-352n)), -352)
65
67
  })