ordered-binary 1.3.0 → 1.4.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/.idea/modules.xml +8 -0
- package/.idea/ordered-binary.iml +14 -0
- package/.nyc_output/5b6bd808-0399-4ee6-9395-344688761dfa.json +1 -0
- package/.nyc_output/processinfo/5b6bd808-0399-4ee6-9395-344688761dfa.json +1 -0
- package/.nyc_output/processinfo/index.json +1 -0
- package/LICENSE +21 -21
- package/README.md +39 -39
- package/dist/index.cjs +37 -15
- package/index.d.ts +22 -22
- package/index.js +37 -15
- package/package.json +37 -37
- package/rollup.config.js +11 -11
- package/tests/test.js +5 -2
- package/uuid.js +0 -110
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
|
2
|
+
<project version="4">
|
|
3
|
+
<component name="ProjectModuleManager">
|
|
4
|
+
<modules>
|
|
5
|
+
<module fileurl="file://$PROJECT_DIR$/.idea/ordered-binary.iml" filepath="$PROJECT_DIR$/.idea/ordered-binary.iml" />
|
|
6
|
+
</modules>
|
|
7
|
+
</component>
|
|
8
|
+
</project>
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
|
2
|
+
<module type="WEB_MODULE" version="4">
|
|
3
|
+
<component name="NewModuleRootManager">
|
|
4
|
+
<content url="file://$MODULE_DIR$">
|
|
5
|
+
<excludeFolder url="file://$MODULE_DIR$/temp" />
|
|
6
|
+
<excludeFolder url="file://$MODULE_DIR$/.tmp" />
|
|
7
|
+
<excludeFolder url="file://$MODULE_DIR$/tmp" />
|
|
8
|
+
<excludeFolder url="file://$MODULE_DIR$/.idea" />
|
|
9
|
+
<excludeFolder url="file://$MODULE_DIR$/dist" />
|
|
10
|
+
</content>
|
|
11
|
+
<orderEntry type="inheritedJdk" />
|
|
12
|
+
<orderEntry type="sourceFolder" forTests="false" />
|
|
13
|
+
</component>
|
|
14
|
+
</module>
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"parent":null,"pid":240712,"argv":["/home/kzyp/.nvm/versions/node/v20.0.0/bin/node","/home/kzyp/dev/ordered-binary/node_modules/mocha/bin/mocha","--ui","tdd","--reporter","/home/kzyp/.local/share/JetBrains/Toolbox/apps/CLion/ch-0/222.4167.35.plugins/NodeJS/js/mocha-intellij/lib/mochaIntellijReporter.js","/home/kzyp/dev/ordered-binary/tests/test.js"],"execArgv":[],"cwd":"/home/kzyp/dev/ordered-binary","time":1688411366364,"ppid":240701,"coverageFilename":"/home/kzyp/dev/ordered-binary/.nyc_output/5b6bd808-0399-4ee6-9395-344688761dfa.json","externalId":"","uuid":"5b6bd808-0399-4ee6-9395-344688761dfa","files":[]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"processes":{"5b6bd808-0399-4ee6-9395-344688761dfa":{"parent":null,"children":[]}},"files":{},"externalIds":{}}
|
package/LICENSE
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2018 Kris Zyp
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
|
13
|
-
copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
-
SOFTWARE.
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2018 Kris Zyp
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,39 +1,39 @@
|
|
|
1
|
-
[](https://www.npmjs.org/package/ordered-binary)
|
|
2
|
-
[](https://www.npmjs.org/package/ordered-binary)
|
|
3
|
-
[](LICENSE)
|
|
4
|
-
<a href="https://dev.doctorevidence.com/"><img src="./assets/powers-dre.png" width="203" /></a>
|
|
5
|
-
|
|
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.
|
|
7
|
-
|
|
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
|
|
31
|
-
|
|
32
|
-
`toBufferKey(jsPrimitive)` - This accepts a string, number, or boolean as the argument, and returns a `Buffer`.
|
|
33
|
-
|
|
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).
|
|
35
|
-
|
|
36
|
-
And these constants:
|
|
37
|
-
|
|
38
|
-
`MINIMUM_KEY` - The minimum key supported (`null`, which is represented as single zero byte)
|
|
39
|
-
`MAXIMUM_KEY` - A maximum key larger than any supported primitive (single 0xff byte)
|
|
1
|
+
[](https://www.npmjs.org/package/ordered-binary)
|
|
2
|
+
[](https://www.npmjs.org/package/ordered-binary)
|
|
3
|
+
[](LICENSE)
|
|
4
|
+
<a href="https://dev.doctorevidence.com/"><img src="./assets/powers-dre.png" width="203" /></a>
|
|
5
|
+
|
|
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.
|
|
7
|
+
|
|
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
|
|
31
|
+
|
|
32
|
+
`toBufferKey(jsPrimitive)` - This accepts a string, number, or boolean as the argument, and returns a `Buffer`.
|
|
33
|
+
|
|
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).
|
|
35
|
+
|
|
36
|
+
And these constants:
|
|
37
|
+
|
|
38
|
+
`MINIMUM_KEY` - The minimum key supported (`null`, which is represented as single zero byte)
|
|
39
|
+
`MAXIMUM_KEY` - A maximum key larger than any supported primitive (single 0xff byte)
|
package/dist/index.cjs
CHANGED
|
@@ -43,7 +43,10 @@ function writeKey(key, target, position, inSequence) {
|
|
|
43
43
|
let i, c2;
|
|
44
44
|
for (i = 0; i < strLength; i++) {
|
|
45
45
|
c1 = key.charCodeAt(i);
|
|
46
|
-
if (c1
|
|
46
|
+
if (c1 <= 4) {
|
|
47
|
+
target[position++] = 4;
|
|
48
|
+
target[position++] = c1;
|
|
49
|
+
} else if (c1 < 0x80) {
|
|
47
50
|
target[position++] = c1;
|
|
48
51
|
} else if (c1 < 0x800) {
|
|
49
52
|
target[position++] = c1 >> 6 | 0xc0;
|
|
@@ -66,7 +69,7 @@ function writeKey(key, target, position, inSequence) {
|
|
|
66
69
|
}
|
|
67
70
|
} else {
|
|
68
71
|
if (target.utf8Write)
|
|
69
|
-
position += target.utf8Write(key, position);
|
|
72
|
+
position += target.utf8Write(key, position, 0xffffffff);
|
|
70
73
|
else
|
|
71
74
|
position += textEncoder.encodeInto(key, target.subarray(position)).written;
|
|
72
75
|
if (position > target.length - 4)
|
|
@@ -254,10 +257,14 @@ function makeStringBuilder() {
|
|
|
254
257
|
let v = fromCharCode((i & 0xf) + 97) + fromCharCode((i >> 4) + 97);
|
|
255
258
|
stringBuildCode += `
|
|
256
259
|
let ${v} = source[position++]
|
|
257
|
-
if (
|
|
258
|
-
|
|
259
|
-
else
|
|
260
|
-
|
|
260
|
+
if (${v} > 4) {
|
|
261
|
+
if (${v} >= 0x80) ${v} = finishUtf8(${v}, source)
|
|
262
|
+
} else {
|
|
263
|
+
if (${v} === 4)
|
|
264
|
+
${v} = source[position++]
|
|
265
|
+
else
|
|
266
|
+
return fromCharCode(${previous})
|
|
267
|
+
}
|
|
261
268
|
`;
|
|
262
269
|
previous.push(v);
|
|
263
270
|
if (i == 1000000) // this just exists to prevent rollup from doing dead code elimination on finishUtf8
|
|
@@ -281,20 +288,19 @@ function finishUtf8(byte1, src) {
|
|
|
281
288
|
} else if ((byte1 & 0xf8) === 0xf0) {
|
|
282
289
|
// 4 bytes
|
|
283
290
|
if (pendingSurrogate) {
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
291
|
+
byte1 = pendingSurrogate;
|
|
292
|
+
pendingSurrogate = null;
|
|
293
|
+
position += 3;
|
|
294
|
+
return byte1
|
|
288
295
|
}
|
|
289
296
|
const byte2 = src[position++] & 0x3f;
|
|
290
297
|
const byte3 = src[position++] & 0x3f;
|
|
291
298
|
const byte4 = src[position++] & 0x3f;
|
|
292
299
|
let unit = ((byte1 & 0x07) << 0x12) | (byte2 << 0x0c) | (byte3 << 0x06) | byte4;
|
|
293
300
|
if (unit > 0xffff) {
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
position -= 4; // reset so we can return the next part of the surrogate pair
|
|
301
|
+
pendingSurrogate = 0xdc00 | (unit & 0x3ff);
|
|
302
|
+
unit = (((unit - 0x10000) >>> 10) & 0x3ff) | 0xd800;
|
|
303
|
+
position -= 4; // reset so we can return the next part of the surrogate pair
|
|
298
304
|
}
|
|
299
305
|
return unit
|
|
300
306
|
} else {
|
|
@@ -302,7 +308,23 @@ function finishUtf8(byte1, src) {
|
|
|
302
308
|
}
|
|
303
309
|
}
|
|
304
310
|
|
|
305
|
-
const readString =
|
|
311
|
+
const readString =
|
|
312
|
+
typeof process !== 'undefined' && process.isBun ? // the eval in bun doesn't properly closure on position, so we
|
|
313
|
+
// have to manually update it
|
|
314
|
+
(function(reading) {
|
|
315
|
+
let { setPosition, getPosition, readString } = reading;
|
|
316
|
+
return (source) => {
|
|
317
|
+
setPosition(position);
|
|
318
|
+
let value = readString(source);
|
|
319
|
+
position = getPosition();
|
|
320
|
+
return value;
|
|
321
|
+
};
|
|
322
|
+
})((new Function('fromCharCode', 'let position; let readString = ' + makeStringBuilder() +
|
|
323
|
+
';return {' +
|
|
324
|
+
'setPosition(p) { position = p },' +
|
|
325
|
+
'getPosition() { return position },' +
|
|
326
|
+
'readString }'))(fromCharCode)) :
|
|
327
|
+
eval(makeStringBuilder());
|
|
306
328
|
|
|
307
329
|
function compareKeys(a, b) {
|
|
308
330
|
// compare with type consistency that matches binary comparison
|
package/index.d.ts
CHANGED
|
@@ -1,23 +1,23 @@
|
|
|
1
|
-
type Key = Key[] | string | symbol | number | boolean | Uint8Array;
|
|
2
|
-
/** Writes a key (a primitive value) to the target buffer, starting at the given position */
|
|
3
|
-
export function writeKey(key: Key, target: Uint8Array, position: number, inSequence?: boolean): number;
|
|
4
|
-
/** Reads a key from the provided buffer, from the given range */
|
|
5
|
-
export function readKey(buffer: Uint8Array, start: number, end?: number, inSequence?: boolean): Key;
|
|
6
|
-
/** Converts key to a Buffer. This is generally much slower than using writeKey since it involves a full buffer allocation, and should be avoided for performance sensitive code. */
|
|
7
|
-
export function toBufferKey(key: Key): Buffer;
|
|
8
|
-
/** Converts Buffer to Key */
|
|
9
|
-
export function fromBufferKey(source: Buffer): Key;
|
|
10
|
-
/** Compares two keys, returning -1 if `a` comes before `b` in the ordered binary representation of the keys, or 1 if `a` comes after `b`, or 0 if they are equivalent */
|
|
11
|
-
export function compareKeys(a: Key, b: Key): number;
|
|
12
|
-
/** The minimum key, with the "first" binary representation (one byte of zero) */
|
|
13
|
-
export const MINIMUM_KEY: null
|
|
14
|
-
/** A maximum key, with a binary representation after all other JS primitives (one byte of 0xff) */
|
|
15
|
-
export const MAXIMUM_KEY: Uint8Array
|
|
16
|
-
/** Enables null termination, ensuring that writing keys to buffers will end with a padding of zeros at the end to complete the following 32-bit word */
|
|
17
|
-
export function enableNullTermination(): void;
|
|
18
|
-
/** An object that holds the functions for encapsulation as a single encoder */
|
|
19
|
-
export const encoder: {
|
|
20
|
-
writeKey: typeof writeKey,
|
|
21
|
-
readKey: typeof readKey,
|
|
22
|
-
enableNullTermination: typeof enableNullTermination,
|
|
1
|
+
type Key = Key[] | string | symbol | number | boolean | Uint8Array;
|
|
2
|
+
/** Writes a key (a primitive value) to the target buffer, starting at the given position */
|
|
3
|
+
export function writeKey(key: Key, target: Uint8Array, position: number, inSequence?: boolean): number;
|
|
4
|
+
/** Reads a key from the provided buffer, from the given range */
|
|
5
|
+
export function readKey(buffer: Uint8Array, start: number, end?: number, inSequence?: boolean): Key;
|
|
6
|
+
/** Converts key to a Buffer. This is generally much slower than using writeKey since it involves a full buffer allocation, and should be avoided for performance sensitive code. */
|
|
7
|
+
export function toBufferKey(key: Key): Buffer;
|
|
8
|
+
/** Converts Buffer to Key */
|
|
9
|
+
export function fromBufferKey(source: Buffer): Key;
|
|
10
|
+
/** Compares two keys, returning -1 if `a` comes before `b` in the ordered binary representation of the keys, or 1 if `a` comes after `b`, or 0 if they are equivalent */
|
|
11
|
+
export function compareKeys(a: Key, b: Key): number;
|
|
12
|
+
/** The minimum key, with the "first" binary representation (one byte of zero) */
|
|
13
|
+
export const MINIMUM_KEY: null
|
|
14
|
+
/** A maximum key, with a binary representation after all other JS primitives (one byte of 0xff) */
|
|
15
|
+
export const MAXIMUM_KEY: Uint8Array
|
|
16
|
+
/** Enables null termination, ensuring that writing keys to buffers will end with a padding of zeros at the end to complete the following 32-bit word */
|
|
17
|
+
export function enableNullTermination(): void;
|
|
18
|
+
/** An object that holds the functions for encapsulation as a single encoder */
|
|
19
|
+
export const encoder: {
|
|
20
|
+
writeKey: typeof writeKey,
|
|
21
|
+
readKey: typeof readKey,
|
|
22
|
+
enableNullTermination: typeof enableNullTermination,
|
|
23
23
|
}
|
package/index.js
CHANGED
|
@@ -39,7 +39,10 @@ export function writeKey(key, target, position, inSequence) {
|
|
|
39
39
|
let i, c2
|
|
40
40
|
for (i = 0; i < strLength; i++) {
|
|
41
41
|
c1 = key.charCodeAt(i)
|
|
42
|
-
if (c1
|
|
42
|
+
if (c1 <= 4) {
|
|
43
|
+
target[position++] = 4
|
|
44
|
+
target[position++] = c1
|
|
45
|
+
} else if (c1 < 0x80) {
|
|
43
46
|
target[position++] = c1
|
|
44
47
|
} else if (c1 < 0x800) {
|
|
45
48
|
target[position++] = c1 >> 6 | 0xc0
|
|
@@ -62,7 +65,7 @@ export function writeKey(key, target, position, inSequence) {
|
|
|
62
65
|
}
|
|
63
66
|
} else {
|
|
64
67
|
if (target.utf8Write)
|
|
65
|
-
position += target.utf8Write(key, position)
|
|
68
|
+
position += target.utf8Write(key, position, 0xffffffff)
|
|
66
69
|
else
|
|
67
70
|
position += textEncoder.encodeInto(key, target.subarray(position)).written
|
|
68
71
|
if (position > target.length - 4)
|
|
@@ -250,10 +253,14 @@ function makeStringBuilder() {
|
|
|
250
253
|
let v = fromCharCode((i & 0xf) + 97) + fromCharCode((i >> 4) + 97)
|
|
251
254
|
stringBuildCode += `
|
|
252
255
|
let ${v} = source[position++]
|
|
253
|
-
if (
|
|
254
|
-
|
|
255
|
-
else
|
|
256
|
-
|
|
256
|
+
if (${v} > 4) {
|
|
257
|
+
if (${v} >= 0x80) ${v} = finishUtf8(${v}, source)
|
|
258
|
+
} else {
|
|
259
|
+
if (${v} === 4)
|
|
260
|
+
${v} = source[position++]
|
|
261
|
+
else
|
|
262
|
+
return fromCharCode(${previous})
|
|
263
|
+
}
|
|
257
264
|
`
|
|
258
265
|
previous.push(v)
|
|
259
266
|
if (i == 1000000) // this just exists to prevent rollup from doing dead code elimination on finishUtf8
|
|
@@ -277,20 +284,19 @@ function finishUtf8(byte1, src) {
|
|
|
277
284
|
} else if ((byte1 & 0xf8) === 0xf0) {
|
|
278
285
|
// 4 bytes
|
|
279
286
|
if (pendingSurrogate) {
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
287
|
+
byte1 = pendingSurrogate
|
|
288
|
+
pendingSurrogate = null
|
|
289
|
+
position += 3
|
|
290
|
+
return byte1
|
|
284
291
|
}
|
|
285
292
|
const byte2 = src[position++] & 0x3f
|
|
286
293
|
const byte3 = src[position++] & 0x3f
|
|
287
294
|
const byte4 = src[position++] & 0x3f
|
|
288
295
|
let unit = ((byte1 & 0x07) << 0x12) | (byte2 << 0x0c) | (byte3 << 0x06) | byte4
|
|
289
296
|
if (unit > 0xffff) {
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
position -= 4 // reset so we can return the next part of the surrogate pair
|
|
297
|
+
pendingSurrogate = 0xdc00 | (unit & 0x3ff)
|
|
298
|
+
unit = (((unit - 0x10000) >>> 10) & 0x3ff) | 0xd800
|
|
299
|
+
position -= 4 // reset so we can return the next part of the surrogate pair
|
|
294
300
|
}
|
|
295
301
|
return unit
|
|
296
302
|
} else {
|
|
@@ -298,7 +304,23 @@ function finishUtf8(byte1, src) {
|
|
|
298
304
|
}
|
|
299
305
|
}
|
|
300
306
|
|
|
301
|
-
const readString =
|
|
307
|
+
const readString =
|
|
308
|
+
typeof process !== 'undefined' && process.isBun ? // the eval in bun doesn't properly closure on position, so we
|
|
309
|
+
// have to manually update it
|
|
310
|
+
(function(reading) {
|
|
311
|
+
let { setPosition, getPosition, readString } = reading;
|
|
312
|
+
return (source) => {
|
|
313
|
+
setPosition(position);
|
|
314
|
+
let value = readString(source);
|
|
315
|
+
position = getPosition();
|
|
316
|
+
return value;
|
|
317
|
+
};
|
|
318
|
+
})((new Function('fromCharCode', 'let position; let readString = ' + makeStringBuilder() +
|
|
319
|
+
';return {' +
|
|
320
|
+
'setPosition(p) { position = p },' +
|
|
321
|
+
'getPosition() { return position },' +
|
|
322
|
+
'readString }'))(fromCharCode)) :
|
|
323
|
+
eval(makeStringBuilder())
|
|
302
324
|
|
|
303
325
|
export function compareKeys(a, b) {
|
|
304
326
|
// compare with type consistency that matches binary comparison
|
package/package.json
CHANGED
|
@@ -1,37 +1,37 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "ordered-binary",
|
|
3
|
-
"author": "Kris Zyp",
|
|
4
|
-
"version": "1.
|
|
5
|
-
"description": "Conversion of JavaScript primitives to and from Buffer with binary order matching natural primitive order",
|
|
6
|
-
"license": "MIT",
|
|
7
|
-
"repository": {
|
|
8
|
-
"type": "git",
|
|
9
|
-
"url": "http://github.com/kriszyp/ordered-binary"
|
|
10
|
-
},
|
|
11
|
-
"scripts": {
|
|
12
|
-
"build": "rollup -c",
|
|
13
|
-
"prepare": "rollup -c",
|
|
14
|
-
"test": "mocha tests -u tdd"
|
|
15
|
-
},
|
|
16
|
-
"type": "module",
|
|
17
|
-
"main": "dist/index.cjs",
|
|
18
|
-
"module": "index.js",
|
|
19
|
-
"exports": {
|
|
20
|
-
".": {
|
|
21
|
-
"require": "./dist/index.cjs",
|
|
22
|
-
"import": "./index.js"
|
|
23
|
-
},
|
|
24
|
-
"./index.js": {
|
|
25
|
-
"require": "./dist/index.cjs",
|
|
26
|
-
"import": "./index.js"
|
|
27
|
-
}
|
|
28
|
-
},
|
|
29
|
-
"typings": "./index.d.ts",
|
|
30
|
-
"optionalDependencies": {},
|
|
31
|
-
"devDependencies": {
|
|
32
|
-
"@types/node": "latest",
|
|
33
|
-
"chai": "^4",
|
|
34
|
-
"mocha": "^9.2.0",
|
|
35
|
-
"rollup": "^2.61.1"
|
|
36
|
-
}
|
|
37
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "ordered-binary",
|
|
3
|
+
"author": "Kris Zyp",
|
|
4
|
+
"version": "1.4.1",
|
|
5
|
+
"description": "Conversion of JavaScript primitives to and from Buffer with binary order matching natural primitive order",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "http://github.com/kriszyp/ordered-binary"
|
|
10
|
+
},
|
|
11
|
+
"scripts": {
|
|
12
|
+
"build": "rollup -c",
|
|
13
|
+
"prepare": "rollup -c",
|
|
14
|
+
"test": "mocha tests -u tdd"
|
|
15
|
+
},
|
|
16
|
+
"type": "module",
|
|
17
|
+
"main": "dist/index.cjs",
|
|
18
|
+
"module": "index.js",
|
|
19
|
+
"exports": {
|
|
20
|
+
".": {
|
|
21
|
+
"require": "./dist/index.cjs",
|
|
22
|
+
"import": "./index.js"
|
|
23
|
+
},
|
|
24
|
+
"./index.js": {
|
|
25
|
+
"require": "./dist/index.cjs",
|
|
26
|
+
"import": "./index.js"
|
|
27
|
+
}
|
|
28
|
+
},
|
|
29
|
+
"typings": "./index.d.ts",
|
|
30
|
+
"optionalDependencies": {},
|
|
31
|
+
"devDependencies": {
|
|
32
|
+
"@types/node": "latest",
|
|
33
|
+
"chai": "^4",
|
|
34
|
+
"mocha": "^9.2.0",
|
|
35
|
+
"rollup": "^2.61.1"
|
|
36
|
+
}
|
|
37
|
+
}
|
package/rollup.config.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
export default [
|
|
2
|
-
{
|
|
3
|
-
input: "index.js",
|
|
4
|
-
output: [
|
|
5
|
-
{
|
|
6
|
-
file: "dist/index.cjs",
|
|
7
|
-
format: "cjs"
|
|
8
|
-
}
|
|
9
|
-
]
|
|
10
|
-
}
|
|
11
|
-
];
|
|
1
|
+
export default [
|
|
2
|
+
{
|
|
3
|
+
input: "index.js",
|
|
4
|
+
output: [
|
|
5
|
+
{
|
|
6
|
+
file: "dist/index.cjs",
|
|
7
|
+
format: "cjs"
|
|
8
|
+
}
|
|
9
|
+
]
|
|
10
|
+
}
|
|
11
|
+
];
|
package/tests/test.js
CHANGED
|
@@ -44,6 +44,9 @@ suite('key buffers', () => {
|
|
|
44
44
|
assert.strictEqual(fromBufferKey(toBufferKey('4')), '4')
|
|
45
45
|
assert.strictEqual(fromBufferKey(toBufferKey('hello')), 'hello')
|
|
46
46
|
assert.strictEqual(fromBufferKey(toBufferKey('')), '')
|
|
47
|
+
assert.strictEqual(fromBufferKey(toBufferKey('\x00')), '\x00')
|
|
48
|
+
assert.strictEqual(fromBufferKey(toBufferKey('\x03test\x01\x00')), '\x03test\x01\x00')
|
|
49
|
+
assert.strictEqual(fromBufferKey(toBufferKey('prance 🧚🏻♀️🩷')), 'prance 🧚🏻♀️🩷')
|
|
47
50
|
})
|
|
48
51
|
test('string comparison', () => {
|
|
49
52
|
assertBufferComparison(toBufferKey('4'), toBufferKey('5'))
|
|
@@ -64,8 +67,8 @@ suite('key buffers', () => {
|
|
|
64
67
|
['hello', 5.25])
|
|
65
68
|
assert.deepEqual(fromBufferKey(toBufferKey([true, 1503579323825])),
|
|
66
69
|
[true, 1503579323825])
|
|
67
|
-
assert.deepEqual(fromBufferKey(toBufferKey([-0.2525, '
|
|
68
|
-
[-0.2525, '
|
|
70
|
+
assert.deepEqual(fromBufferKey(toBufferKey([-0.2525, 'sec\x00nd'])),
|
|
71
|
+
[-0.2525, 'sec\x00nd'])
|
|
69
72
|
assert.deepEqual(fromBufferKey(toBufferKey([-0.2525, '2nd', '3rd'])),
|
|
70
73
|
[-0.2525, '2nd', '3rd'])
|
|
71
74
|
})
|
package/uuid.js
DELETED
|
@@ -1,110 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
export function uuid3(uuid, target, position) {
|
|
4
|
-
let int32 = target.int32
|
|
5
|
-
if (!int32)
|
|
6
|
-
int32 = target.int32 = new Int32Array(target.buffer, target.byteOffset, target.byteLength >> 2)
|
|
7
|
-
let c
|
|
8
|
-
let intPosition = position >> 2
|
|
9
|
-
int32[intPosition] =
|
|
10
|
-
(((c = uuid.charCodeAt(0)) > 90 ? (c - 87) : (c - 48)) << 4) |
|
|
11
|
-
((c = uuid.charCodeAt(1)) > 90 ? (c - 87) : (c - 48)) |
|
|
12
|
-
(((c = uuid.charCodeAt(2)) > 90 ? (c - 87) : (c - 48)) << 12) |
|
|
13
|
-
(((c = uuid.charCodeAt(3)) > 90 ? (c - 87) : (c - 48)) << 8) |
|
|
14
|
-
(((c = uuid.charCodeAt(4)) > 90 ? (c - 87) : (c - 48)) << 20) |
|
|
15
|
-
(((c = uuid.charCodeAt(5)) > 90 ? (c - 87) : (c - 48)) << 16) |
|
|
16
|
-
(((c = uuid.charCodeAt(6)) > 90 ? (c - 87) : (c - 48)) << 28) |
|
|
17
|
-
(((c = uuid.charCodeAt(7)) > 90 ? (c - 87) : (c - 48)) << 24)
|
|
18
|
-
int32[intPosition + 1] =
|
|
19
|
-
(((c = uuid.charCodeAt(9)) > 90 ? (c - 87) : (c - 48)) << 4) |
|
|
20
|
-
((c = uuid.charCodeAt(10)) > 90 ? (c - 87) : (c - 48)) |
|
|
21
|
-
(((c = uuid.charCodeAt(11)) > 90 ? (c - 87) : (c - 48)) << 12) |
|
|
22
|
-
(((c = uuid.charCodeAt(12)) > 90 ? (c - 87) : (c - 48)) << 8) |
|
|
23
|
-
(((c = uuid.charCodeAt(14)) > 90 ? (c - 87) : (c - 48)) << 20) |
|
|
24
|
-
(((c = uuid.charCodeAt(15)) > 90 ? (c - 87) : (c - 48)) << 16) |
|
|
25
|
-
(((c = uuid.charCodeAt(16)) > 90 ? (c - 87) : (c - 48)) << 28) |
|
|
26
|
-
(((c = uuid.charCodeAt(17)) > 90 ? (c - 87) : (c - 48)) << 24)
|
|
27
|
-
int32[intPosition + 2] =
|
|
28
|
-
(((c = uuid.charCodeAt(19)) > 90 ? (c - 87) : (c - 48)) << 4) |
|
|
29
|
-
((c = uuid.charCodeAt(20)) > 90 ? (c - 87) : (c - 48)) |
|
|
30
|
-
(((c = uuid.charCodeAt(21)) > 90 ? (c - 87) : (c - 48)) << 12) |
|
|
31
|
-
(((c = uuid.charCodeAt(22)) > 90 ? (c - 87) : (c - 48)) << 8) |
|
|
32
|
-
(((c = uuid.charCodeAt(24)) > 90 ? (c - 87) : (c - 48)) << 20) |
|
|
33
|
-
(((c = uuid.charCodeAt(25)) > 90 ? (c - 87) : (c - 48)) << 16) |
|
|
34
|
-
(((c = uuid.charCodeAt(26)) > 90 ? (c - 87) : (c - 48)) << 28) |
|
|
35
|
-
(((c = uuid.charCodeAt(27)) > 90 ? (c - 87) : (c - 48)) << 24)
|
|
36
|
-
int32[intPosition + 3] =
|
|
37
|
-
(((c = uuid.charCodeAt(28)) > 90 ? (c - 87) : (c - 48)) << 4) |
|
|
38
|
-
((c = uuid.charCodeAt(29)) > 90 ? (c - 87) : (c - 48)) |
|
|
39
|
-
(((c = uuid.charCodeAt(30)) > 90 ? (c - 87) : (c - 48)) << 12) |
|
|
40
|
-
(((c = uuid.charCodeAt(31)) > 90 ? (c - 87) : (c - 48)) << 8) |
|
|
41
|
-
(((c = uuid.charCodeAt(32)) > 90 ? (c - 87) : (c - 48)) << 20) |
|
|
42
|
-
(((c = uuid.charCodeAt(33)) > 90 ? (c - 87) : (c - 48)) << 16) |
|
|
43
|
-
(((c = uuid.charCodeAt(34)) > 90 ? (c - 87) : (c - 48)) << 28) |
|
|
44
|
-
(((c = uuid.charCodeAt(35)) > 90 ? (c - 87) : (c - 48)) << 24)
|
|
45
|
-
|
|
46
|
-
return position + 16
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
export function uuid2(uuid, target, position) {
|
|
50
|
-
let c
|
|
51
|
-
target[0] =
|
|
52
|
-
(((c = uuid.charCodeAt(0)) > 90 ? (c - 87) : (c - 48)) << 4) |
|
|
53
|
-
((c = uuid.charCodeAt(1)) > 90 ? (c - 87) : (c - 48))
|
|
54
|
-
target[1] =
|
|
55
|
-
(((c = uuid.charCodeAt(2)) > 90 ? (c - 87) : (c - 48)) << 4) |
|
|
56
|
-
((c = uuid.charCodeAt(3)) > 90 ? (c - 87) : (c - 48))
|
|
57
|
-
target[2] =
|
|
58
|
-
(((c = uuid.charCodeAt(4)) > 90 ? (c - 87) : (c - 48)) << 4) |
|
|
59
|
-
((c = uuid.charCodeAt(5)) > 90 ? (c - 87) : (c - 48))
|
|
60
|
-
target[3] =
|
|
61
|
-
(((c = uuid.charCodeAt(6)) > 90 ? (c - 87) : (c - 48)) << 4) |
|
|
62
|
-
((c = uuid.charCodeAt(7)) > 90 ? (c - 87) : (c - 48))
|
|
63
|
-
target[4] =
|
|
64
|
-
(((c = uuid.charCodeAt(9)) > 90 ? (c - 87) : (c - 48)) << 4) |
|
|
65
|
-
((c = uuid.charCodeAt(10)) > 90 ? (c - 87) : (c - 48))
|
|
66
|
-
target[5] =
|
|
67
|
-
(((c = uuid.charCodeAt(11)) > 90 ? (c - 87) : (c - 48)) << 4) |
|
|
68
|
-
((c = uuid.charCodeAt(12)) > 90 ? (c - 87) : (c - 48))
|
|
69
|
-
target[6] =
|
|
70
|
-
(((c = uuid.charCodeAt(14)) > 90 ? (c - 87) : (c - 48)) << 4) |
|
|
71
|
-
((c = uuid.charCodeAt(15)) > 90 ? (c - 87) : (c - 48))
|
|
72
|
-
target[7] =
|
|
73
|
-
(((c = uuid.charCodeAt(16)) > 90 ? (c - 87) : (c - 48)) << 4) |
|
|
74
|
-
((c = uuid.charCodeAt(17)) > 90 ? (c - 87) : (c - 48))
|
|
75
|
-
target[8] =
|
|
76
|
-
(((c = uuid.charCodeAt(19)) > 90 ? (c - 87) : (c - 48)) << 4) |
|
|
77
|
-
((c = uuid.charCodeAt(20)) > 90 ? (c - 87) : (c - 48))
|
|
78
|
-
target[9] =
|
|
79
|
-
(((c = uuid.charCodeAt(21)) > 90 ? (c - 87) : (c - 48)) << 4) |
|
|
80
|
-
((c = uuid.charCodeAt(22)) > 90 ? (c - 87) : (c - 48))
|
|
81
|
-
target[10] =
|
|
82
|
-
(((c = uuid.charCodeAt(24)) > 90 ? (c - 87) : (c - 48)) << 4) |
|
|
83
|
-
((c = uuid.charCodeAt(25)) > 90 ? (c - 87) : (c - 48))
|
|
84
|
-
target[11] =
|
|
85
|
-
(((c = uuid.charCodeAt(26)) > 90 ? (c - 87) : (c - 48)) << 4) |
|
|
86
|
-
((c = uuid.charCodeAt(27)) > 90 ? (c - 87) : (c - 48))
|
|
87
|
-
target[12] =
|
|
88
|
-
(((c = uuid.charCodeAt(28)) > 90 ? (c - 87) : (c - 48)) << 4) |
|
|
89
|
-
((c = uuid.charCodeAt(29)) > 90 ? (c - 87) : (c - 48))
|
|
90
|
-
target[13] =
|
|
91
|
-
(((c = uuid.charCodeAt(30)) > 90 ? (c - 87) : (c - 48)) << 4) |
|
|
92
|
-
((c = uuid.charCodeAt(31)) > 90 ? (c - 87) : (c - 48))
|
|
93
|
-
target[14] =
|
|
94
|
-
(((c = uuid.charCodeAt(32)) > 90 ? (c - 87) : (c - 48)) << 4) |
|
|
95
|
-
((c = uuid.charCodeAt(33)) > 90 ? (c - 87) : (c - 48))
|
|
96
|
-
target[15] =
|
|
97
|
-
(((c = uuid.charCodeAt(34)) > 90 ? (c - 87) : (c - 48)) << 4) |
|
|
98
|
-
((c = uuid.charCodeAt(35)) > 90 ? (c - 87) : (c - 48))
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
return position + 16
|
|
102
|
-
}
|
|
103
|
-
export function uuidReadKey(buffer, start) {
|
|
104
|
-
let c
|
|
105
|
-
String.fromCharCode(
|
|
106
|
-
(c = buffer[0]) < 160 ? ((c >> 4) + 48) : ((c >> 4) + 87),
|
|
107
|
-
(c = c & 0xf) < 10 ? (c + 48) : (c + 87),
|
|
108
|
-
...
|
|
109
|
-
45)
|
|
110
|
-
}
|