Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

ordered-binary

Package Overview
Dependencies
Maintainers
1
Versions
26
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ordered-binary - npm Package Compare versions

Comparing version 1.4.1 to 1.5.0

118

index.js

@@ -17,2 +17,4 @@ /*

import exp from "constants"
const float64Array = new Float64Array(2)

@@ -114,3 +116,3 @@ const int32Array = new Int32Array(float64Array.buffer, 0, 4)

} else {
throw new Error('Unable to serialize object as a key')
throw new Error('Unable to serialize object as a key: ' + JSON.stringify(key))
}

@@ -124,3 +126,31 @@ } else // null

case 'bigint':
return writeKey(Number(key), target, position, inSequence)
let asFloat = Number(key)
if (BigInt(asFloat) > key) {
float64Array[0] = asFloat;
if (int32Array[0])
int32Array[0]--;
else {
int32Array[1]--;
int32Array[0] = 0xffffffff;
}
asFloat = float64Array[0];
}
let difference = key - BigInt(asFloat);
if (difference === 0n)
return writeKey(asFloat, target, position, inSequence)
writeKey(asFloat, target, position, inSequence)
position += 9; // always increment by 9 if we are adding fractional bits
let exponent = BigInt((int32Array[1] >> 20) - 1079);
let nextByte = difference >> exponent;
target[position - 1] |= Number(nextByte);
difference -= nextByte << exponent;
let first = true;
while (difference || first) {
first = false;
exponent -= 7n;
let nextByte = difference >> exponent;
target[position++] = Number(nextByte) | 0x80;
difference -= nextByte << exponent;
}
return position;
case 'undefined':

@@ -147,41 +177,53 @@ return position

if (controlByte < 24) {
if (controlByte < 8) {
position++
if (controlByte == 6) {
value = false
} else if (controlByte == 7) {
value = true
} else if (controlByte == 0) {
value = null
} else if (controlByte == 2) {
value = Symbol.for(readString(buffer))
} else
return Uint8Array.prototype.slice.call(buffer, start, end)
} else {
let dataView = buffer.dataView || (buffer.dataView = new DataView(buffer.buffer, buffer.byteOffset, ((buffer.byteLength + 3) >> 2) << 2))
let highInt = dataView.getInt32(position) << 4
let size = end - position
let lowInt
if (size > 4) {
lowInt = dataView.getInt32(position + 4)
highInt |= lowInt >>> 28
if (size <= 6) { // clear the last bits
lowInt &= -0x1000
if (controlByte < 8) {
position++
if (controlByte == 6) {
value = false
} else if (controlByte == 7) {
value = true
} else if (controlByte == 0) {
value = null
} else if (controlByte == 2) {
value = Symbol.for(readString(buffer))
} else
return Uint8Array.prototype.slice.call(buffer, start, end)
} else {
let dataView = buffer.dataView || (buffer.dataView = new DataView(buffer.buffer, buffer.byteOffset, ((buffer.byteLength + 3) >> 2) << 2))
let highInt = dataView.getInt32(position) << 4
let size = end - position
let lowInt
if (size > 4) {
lowInt = dataView.getInt32(position + 4)
highInt |= lowInt >>> 28
if (size <= 6) { // clear the last bits
lowInt &= -0x1000
}
lowInt = lowInt << 4
if (size > 8) {
lowInt = lowInt | buffer[position + 8] >> 4
}
} else
lowInt = 0
if (controlByte < 16) {
// negative gets negated
highInt = highInt ^ 0x7fffffff
lowInt = ~lowInt
}
lowInt = lowInt << 4
if (size > 8) {
lowInt = lowInt | buffer[position + 8] >> 4
int32Array[1] = highInt
int32Array[0] = lowInt
value = float64Array[0]
position += 9
if (size > 9 && buffer[position] > 0) {
// convert the float to bigint, and then we will add precision as we enumerate through the
// extra bytes
value = BigInt(value);
let exponent = highInt >> 20;
// TODO: negatives?
let next_byte = buffer[position - 1] & 0xf;
value += BigInt(next_byte) << BigInt(exponent - 1079);
while ((next_byte = buffer[position]) > 0 && position++ < end) {
value += BigInt(next_byte & 0x7f) << BigInt((start - position) * 7 + exponent - 1016);
}
}
} else
lowInt = 0
if (controlByte < 16) {
// negative gets negated
highInt = highInt ^ 0x7fffffff
lowInt = ~lowInt
}
int32Array[1] = highInt
int32Array[0] = lowInt
value = float64Array[0]
position += 9
}
} else {

@@ -188,0 +230,0 @@ if (controlByte == 27) {

{
"name": "ordered-binary",
"author": "Kris Zyp",
"version": "1.4.1",
"version": "1.5.0",
"description": "Conversion of JavaScript primitives to and from Buffer with binary order matching natural primitive order",

@@ -6,0 +6,0 @@ "license": "MIT",

@@ -16,2 +16,8 @@ import { assert } from 'chai'

//var inspector = require('inspector'); inspector.open(9330, null, true); debugger
let seed = 0;
export function random() {
seed++;
let a = seed * 15485863;
return ((a * a * a) % 2038074743) / 2038074743;
}

@@ -44,2 +50,24 @@ suite('key buffers', () => {

})
test('bigint equivalence', () => {
assert.strictEqual(fromBufferKey(toBufferKey(6135421331404949076605986n)), 6135421331404949076605986n)
assert.strictEqual(fromBufferKey(toBufferKey(0xfffffffffffffffffffffn)), 0xfffffffffffffffffffffn)
assert.strictEqual(fromBufferKey(toBufferKey(12345678901234567890n)), 12345678901234567890n)
assert.deepEqual(fromBufferKey(toBufferKey([12345678901234567890n, 44])), [12345678901234567890n, 44])
assert.deepEqual(fromBufferKey(toBufferKey(['hi', 12345678901234567890n])), ['hi', 12345678901234567890n])
assert.deepEqual(fromBufferKey(toBufferKey([6135421331404949076605986n, 'after'])), [6135421331404949076605986n, 'after'])
assert.strictEqual(fromBufferKey(toBufferKey(132923456789012345678903533235253252353211125n)), 132923456789012345678903533235253252353211125n)
assert.strictEqual(fromBufferKey(toBufferKey(352n)), 352)
let num = 5325n
for (let i = 0; i < 1100; i++) {
num *= BigInt(Math.floor(random() * 3 + 1));
num -= BigInt(Math.floor(random() * 1000));
assert.strictEqual(BigInt(fromBufferKey(toBufferKey(num))), num)
}
assert.strictEqual(fromBufferKey(toBufferKey(-352n)), -352)
})
test('bigint comparison', () => {
assertBufferComparison(toBufferKey(0xfffffffffffffffffffffn), toBufferKey(0x100fffffffffffffffffffn))
assertBufferComparison(toBufferKey(12345678901234567890), toBufferKey(12345678901234567890n))
})
test('string equivalence', () => {

@@ -46,0 +74,0 @@ assert.strictEqual(fromBufferKey(toBufferKey('4')), '4')

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc