ordered-binary 1.1.3 → 1.2.0
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/README.md +28 -2
- package/dist/index.cjs +18 -5
- package/index.js +13 -4
- package/package.json +1 -1
- package/tests/test.js +1 -0
package/README.md
CHANGED
|
@@ -1,8 +1,34 @@
|
|
|
1
|
+
[](https://www.npmjs.org/package/ordered-binary)
|
|
2
|
+
[](https://www.npmjs.org/package/ordered-binary)
|
|
3
|
+
[](LICENSE)
|
|
1
4
|
<a href="https://dev.doctorevidence.com/"><img src="./assets/powers-dre.png" width="203" /></a>
|
|
2
5
|
|
|
3
|
-
The ordered-binary provides a representation of JavaScript primitives
|
|
6
|
+
The ordered-binary package provides a representation of JavaScript primitives, serialized into binary format (NodeJS Buffers or Uint8Arrays), such that the binary values are naturally ordered such that it matches the natural ordering or values. For example, since -2.0321 > -2.04, then `toBufferKey(-2.0321)` will be greater than `toBufferKey(-2.04)` as a binary representation, in left-to-right evaluation. This is particular useful for storing keys as binaries with something like LMDB or LevelDB, to avoid any custom sorting.
|
|
4
7
|
|
|
5
|
-
The
|
|
8
|
+
The ordered-binary package supports strings, numbers, booleans, symbols, null, as well as an array of primitives. Here is an example of ordering of primitive values:
|
|
9
|
+
```
|
|
10
|
+
Buffer.from([0]) // buffers are left unchanged, and this is the minimum value
|
|
11
|
+
Symbol.for('even symbols')
|
|
12
|
+
-10 // negative supported
|
|
13
|
+
-1.1 // decimals supported
|
|
14
|
+
400
|
|
15
|
+
3E10
|
|
16
|
+
'Hello'
|
|
17
|
+
['Hello', 'World']
|
|
18
|
+
'World'
|
|
19
|
+
'hello'
|
|
20
|
+
['hello', 1, 'world']
|
|
21
|
+
['hello', 'world']
|
|
22
|
+
Buffer.from([0xff])
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
The main module exports these functions:
|
|
27
|
+
|
|
28
|
+
`writeKey(key: string | number | boolean | null | Array, target: Buffer, position: integer, inSequence?: boolean)` - Writes the provide key to the target buffer
|
|
29
|
+
|
|
30
|
+
`readKey(buffer, start, end, inSequence)` - Reads the key from the buffer, given the provided start and end, as a primitive value
|
|
6
31
|
|
|
7
32
|
`toBufferKey(jsPrimitive)` - This accepts a string, number, or boolean as the argument, and returns a `Buffer`.
|
|
33
|
+
|
|
8
34
|
`fromBufferKey(bufferKey, multiple)` - This accepts a Buffer and returns a JavaScript primitive value. This can also parse buffers that hold multiple values delimited by a byte `30`, by setting the second argument to true (in which case it will return an array).
|
package/dist/index.cjs
CHANGED
|
@@ -20,7 +20,14 @@ control character types:
|
|
|
20
20
|
|
|
21
21
|
const float64Array = new Float64Array(2);
|
|
22
22
|
const int32Array = new Int32Array(float64Array.buffer, 0, 4);
|
|
23
|
+
new Uint8Array(float64Array.buffer, 2, 6);
|
|
24
|
+
new Uint8Array(float64Array.buffer, 0, 8);
|
|
23
25
|
let nullTerminate = false;
|
|
26
|
+
let textEncoder;
|
|
27
|
+
try {
|
|
28
|
+
textEncoder = new TextEncoder();
|
|
29
|
+
} catch (error) {}
|
|
30
|
+
|
|
24
31
|
/*
|
|
25
32
|
* Convert arbitrary scalar values to buffer bytes with type preservation and type-appropriate ordering
|
|
26
33
|
*/
|
|
@@ -32,9 +39,9 @@ function writeKey(key, target, position, inSequence) {
|
|
|
32
39
|
case 'string':
|
|
33
40
|
let strLength = key.length;
|
|
34
41
|
let c1 = key.charCodeAt(0);
|
|
35
|
-
if (c1
|
|
42
|
+
if (!(c1 >= 28)) // escape character
|
|
36
43
|
target[position++] = 27;
|
|
37
|
-
if (strLength <
|
|
44
|
+
if (strLength < 0x40) {
|
|
38
45
|
let i, c2;
|
|
39
46
|
for (i = 0; i < strLength; i++) {
|
|
40
47
|
c1 = key.charCodeAt(i);
|
|
@@ -59,8 +66,10 @@ function writeKey(key, target, position, inSequence) {
|
|
|
59
66
|
target[position++] = c1 & 0x3f | 0x80;
|
|
60
67
|
}
|
|
61
68
|
}
|
|
62
|
-
} else {
|
|
69
|
+
} else if (target.utf8Write) {
|
|
63
70
|
position += target.utf8Write(key, position, 2000);
|
|
71
|
+
} else {
|
|
72
|
+
position += textEncoder.encodeInto(key, target.subarray(position)).written;
|
|
64
73
|
}
|
|
65
74
|
break
|
|
66
75
|
case 'number':
|
|
@@ -272,7 +281,7 @@ const enableNullTermination = () => nullTerminate = true;
|
|
|
272
281
|
const readString = eval(makeStringBuilder());
|
|
273
282
|
|
|
274
283
|
function compareKeys(a, b) {
|
|
275
|
-
// compare with type consistency that matches
|
|
284
|
+
// compare with type consistency that matches binary comparison
|
|
276
285
|
if (typeof a == 'object') {
|
|
277
286
|
if (!a) {
|
|
278
287
|
return b == null ? 0 : -1
|
|
@@ -319,8 +328,12 @@ const typeOrder = {
|
|
|
319
328
|
boolean: 2,
|
|
320
329
|
number: 3,
|
|
321
330
|
string: 4
|
|
322
|
-
};
|
|
331
|
+
};
|
|
332
|
+
const MINIMUM_KEY = null;
|
|
333
|
+
const MAXIMUM_KEY = Buffer.from([0xff]);
|
|
323
334
|
|
|
335
|
+
exports.MAXIMUM_KEY = MAXIMUM_KEY;
|
|
336
|
+
exports.MINIMUM_KEY = MINIMUM_KEY;
|
|
324
337
|
exports.compareKeys = compareKeys;
|
|
325
338
|
exports.enableNullTermination = enableNullTermination;
|
|
326
339
|
exports.encoder = encoder;
|
package/index.js
CHANGED
|
@@ -19,6 +19,11 @@ const int32Array = new Int32Array(float64Array.buffer, 0, 4)
|
|
|
19
19
|
const uint8Array6 = new Uint8Array(float64Array.buffer, 2, 6)
|
|
20
20
|
const uint8Array8 = new Uint8Array(float64Array.buffer, 0, 8)
|
|
21
21
|
let nullTerminate = false
|
|
22
|
+
let textEncoder
|
|
23
|
+
try {
|
|
24
|
+
textEncoder = new TextEncoder()
|
|
25
|
+
} catch (error) {}
|
|
26
|
+
|
|
22
27
|
/*
|
|
23
28
|
* Convert arbitrary scalar values to buffer bytes with type preservation and type-appropriate ordering
|
|
24
29
|
*/
|
|
@@ -30,9 +35,9 @@ export function writeKey(key, target, position, inSequence) {
|
|
|
30
35
|
case 'string':
|
|
31
36
|
let strLength = key.length
|
|
32
37
|
let c1 = key.charCodeAt(0)
|
|
33
|
-
if (c1
|
|
38
|
+
if (!(c1 >= 28)) // escape character
|
|
34
39
|
target[position++] = 27
|
|
35
|
-
if (strLength <
|
|
40
|
+
if (strLength < 0x40) {
|
|
36
41
|
let i, c2
|
|
37
42
|
for (i = 0; i < strLength; i++) {
|
|
38
43
|
c1 = key.charCodeAt(i)
|
|
@@ -57,8 +62,10 @@ export function writeKey(key, target, position, inSequence) {
|
|
|
57
62
|
target[position++] = c1 & 0x3f | 0x80
|
|
58
63
|
}
|
|
59
64
|
}
|
|
60
|
-
} else {
|
|
65
|
+
} else if (target.utf8Write) {
|
|
61
66
|
position += target.utf8Write(key, position, 2000)
|
|
67
|
+
} else {
|
|
68
|
+
position += textEncoder.encodeInto(key, target.subarray(position)).written
|
|
62
69
|
}
|
|
63
70
|
break
|
|
64
71
|
case 'number':
|
|
@@ -270,7 +277,7 @@ export const enableNullTermination = () => nullTerminate = true
|
|
|
270
277
|
const readString = eval(makeStringBuilder())
|
|
271
278
|
|
|
272
279
|
export function compareKeys(a, b) {
|
|
273
|
-
// compare with type consistency that matches
|
|
280
|
+
// compare with type consistency that matches binary comparison
|
|
274
281
|
if (typeof a == 'object') {
|
|
275
282
|
if (!a) {
|
|
276
283
|
return b == null ? 0 : -1
|
|
@@ -318,3 +325,5 @@ const typeOrder = {
|
|
|
318
325
|
number: 3,
|
|
319
326
|
string: 4
|
|
320
327
|
}
|
|
328
|
+
export const MINIMUM_KEY = null
|
|
329
|
+
export const MAXIMUM_KEY = Buffer.from([0xff])
|
package/package.json
CHANGED
package/tests/test.js
CHANGED
|
@@ -43,6 +43,7 @@ suite('key buffers', () => {
|
|
|
43
43
|
test('string equivalence', () => {
|
|
44
44
|
assert.strictEqual(fromBufferKey(toBufferKey('4')), '4')
|
|
45
45
|
assert.strictEqual(fromBufferKey(toBufferKey('hello')), 'hello')
|
|
46
|
+
assert.strictEqual(fromBufferKey(toBufferKey('')), '')
|
|
46
47
|
})
|
|
47
48
|
test('string comparison', () => {
|
|
48
49
|
assertBufferComparison(toBufferKey('4'), toBufferKey('5'))
|