ordered-binary 1.2.3 → 1.3.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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "ordered-binary",
3
3
  "author": "Kris Zyp",
4
- "version": "1.2.3",
4
+ "version": "1.3.0",
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": {
@@ -31,7 +31,7 @@
31
31
  "devDependencies": {
32
32
  "@types/node": "latest",
33
33
  "chai": "^4",
34
- "mocha": "^9.1.3",
34
+ "mocha": "^9.2.0",
35
35
  "rollup": "^2.61.1"
36
36
  }
37
37
  }
package/tests/test.js CHANGED
@@ -1,128 +1,128 @@
1
- import { assert } from 'chai'
2
-
3
- import { toBufferKey, fromBufferKey, readKey, writeKey } from '../index.js'
4
-
5
- function assertBufferComparison(lesser, greater) {
6
- for (let i = 0; i < lesser.length; i++) {
7
- if (lesser[i] < greater[i]) {
8
- return
9
- }
10
- if (lesser[i] > (greater[i] || 0)) {
11
- assert.fail('Byte ' + i + 'should not be ' + lesser[i] + '>' + greater[i])
12
- }
13
- }
14
- }
15
- //var inspector = require('inspector'); inspector.open(9330, null, true); debugger
16
-
17
- suite('key buffers', () => {
18
-
19
- test('numbers equivalence', () => {
20
- assert.strictEqual(fromBufferKey(toBufferKey(4)), 4)
21
- assert.strictEqual(fromBufferKey(toBufferKey(-4)), -4)
22
- assert.strictEqual(fromBufferKey(toBufferKey(3.4)), 3.4)
23
- assert.strictEqual(fromBufferKey(toBufferKey(Math.PI)), Math.PI)
24
- assert.strictEqual(fromBufferKey(toBufferKey(9377288)), 9377288)
25
- assert.strictEqual(fromBufferKey(toBufferKey(1503579323825)), 1503579323825)
26
- assert.strictEqual(fromBufferKey(toBufferKey(1503579323825.3523532)), 1503579323825.3523532)
27
- assert.strictEqual(fromBufferKey(toBufferKey(-1503579323825)), -1503579323825)
28
- assert.strictEqual(fromBufferKey(toBufferKey(0.00005032)), 0.00005032)
29
- assert.strictEqual(fromBufferKey(toBufferKey(-0.00005032)), -0.00005032)
30
- assert.strictEqual(fromBufferKey(toBufferKey(0.00000000000000000000000005431)), 0.00000000000000000000000005431)
31
- })
32
- test('number comparison', () => {
33
- assertBufferComparison(toBufferKey(4), toBufferKey(5))
34
- assertBufferComparison(toBufferKey(1503579323824), toBufferKey(1503579323825))
35
- assertBufferComparison(toBufferKey(1.4), toBufferKey(2))
36
- assertBufferComparison(toBufferKey(0.000000001), toBufferKey(0.00000001))
37
- assertBufferComparison(toBufferKey(-4), toBufferKey(3))
38
- assertBufferComparison(toBufferKey(0), toBufferKey(1))
39
- assertBufferComparison(toBufferKey(-0.001), toBufferKey(0))
40
- assertBufferComparison(toBufferKey(-0.001), toBufferKey(-0.000001))
41
- assertBufferComparison(toBufferKey(-5236532532532), toBufferKey(-5236532532531))
42
- })
43
- test('string equivalence', () => {
44
- assert.strictEqual(fromBufferKey(toBufferKey('4')), '4')
45
- assert.strictEqual(fromBufferKey(toBufferKey('hello')), 'hello')
46
- assert.strictEqual(fromBufferKey(toBufferKey('')), '')
47
- })
48
- test('string comparison', () => {
49
- assertBufferComparison(toBufferKey('4'), toBufferKey('5'))
50
- assertBufferComparison(toBufferKey('and'), toBufferKey('bad'))
51
- assertBufferComparison(toBufferKey('hello'), toBufferKey('hello2'))
52
- let buffer = Buffer.alloc(1024)
53
- let end = writeKey(['this is a test', 5.25], buffer, 0)
54
- })
55
- test('boolean equivalence', () => {
56
- assert.strictEqual(fromBufferKey(toBufferKey(true)), true)
57
- assert.strictEqual(fromBufferKey(toBufferKey(false)), false)
58
- })
59
-
60
- test('multipart equivalence', () => {
61
- assert.deepEqual(fromBufferKey(toBufferKey([4, 5])),
62
- [4, 5])
63
- assert.deepEqual(fromBufferKey(toBufferKey(['hello', 5.25])),
64
- ['hello', 5.25])
65
- assert.deepEqual(fromBufferKey(toBufferKey([true, 1503579323825])),
66
- [true, 1503579323825])
67
- assert.deepEqual(fromBufferKey(toBufferKey([-0.2525, 'second'])),
68
- [-0.2525, 'second'])
69
- assert.deepEqual(fromBufferKey(toBufferKey([-0.2525, '2nd', '3rd'])),
70
- [-0.2525, '2nd', '3rd'])
71
- })
72
-
73
- test('multipart comparison', () => {
74
- assertBufferComparison(
75
- Buffer.concat([toBufferKey(4), Buffer.from([30]), toBufferKey(5)]),
76
- Buffer.concat([toBufferKey(5), Buffer.from([30]), toBufferKey(5)]))
77
- assertBufferComparison(
78
- Buffer.concat([toBufferKey(4), Buffer.from([30]), toBufferKey(5)]),
79
- Buffer.concat([toBufferKey(4), Buffer.from([30]), toBufferKey(6)]))
80
- assertBufferComparison(
81
- Buffer.concat([toBufferKey('and'), Buffer.from([30]), toBufferKey(5)]),
82
- Buffer.concat([toBufferKey('and2'), Buffer.from([30]), toBufferKey(5)]))
83
- assertBufferComparison(
84
- Buffer.concat([toBufferKey(4), Buffer.from([30]), toBufferKey('and')]),
85
- Buffer.concat([toBufferKey(4), Buffer.from([30]), toBufferKey('cat')]))
86
- })
87
- test('performance', () => {
88
- let buffer = Buffer.alloc(1024)
89
- let start = process.hrtime.bigint()
90
- let end, value
91
- for (let i = 0; i < 1000000; i++) {
92
- end = writeKey('this is a test of a longer string to read and write', buffer, 0)
93
- }
94
- console.log('writeKey string time', nextTime(), end)
95
- for (let i = 0; i < 1000000; i++) {
96
- value = readKey(buffer, 0, end)
97
- }
98
- console.log('readKey string time', nextTime(), value)
99
-
100
- for (let i = 0; i < 1000000; i++) {
101
- end = writeKey(33456, buffer, 0)
102
- }
103
- console.log('writeKey number time', nextTime(), end)
104
-
105
- for (let i = 0; i < 1000000; i++) {
106
- value = readKey(buffer, 2, end)
107
- }
108
- console.log('readKey number time', nextTime(), value)
109
-
110
- for (let i = 0; i < 1000000; i++) {
111
- end = writeKey(['hello', 33456], buffer, 0)
112
- }
113
- console.log('writeKey array time', nextTime(), end, buffer.slice(0, end))
114
-
115
- for (let i = 0; i < 1000000; i++) {
116
- value = readKey(buffer, 0, end)
117
- }
118
- console.log('readKey array time', nextTime(), value)
119
-
120
- function nextTime() {
121
- let ns = process.hrtime.bigint()
122
- let elapsed = ns - start
123
- start = ns
124
- return Number(elapsed) / 1000000 + 'ns'
125
- }
126
- })
127
-
128
- })
1
+ import { assert } from 'chai'
2
+
3
+ import { toBufferKey, fromBufferKey, readKey, writeKey } from '../index.js'
4
+
5
+ function assertBufferComparison(lesser, greater) {
6
+ for (let i = 0; i < lesser.length; i++) {
7
+ if (lesser[i] < greater[i]) {
8
+ return
9
+ }
10
+ if (lesser[i] > (greater[i] || 0)) {
11
+ assert.fail('Byte ' + i + 'should not be ' + lesser[i] + '>' + greater[i])
12
+ }
13
+ }
14
+ }
15
+ //var inspector = require('inspector'); inspector.open(9330, null, true); debugger
16
+
17
+ suite('key buffers', () => {
18
+
19
+ test('numbers equivalence', () => {
20
+ assert.strictEqual(fromBufferKey(toBufferKey(4)), 4)
21
+ assert.strictEqual(fromBufferKey(toBufferKey(-4)), -4)
22
+ assert.strictEqual(fromBufferKey(toBufferKey(3.4)), 3.4)
23
+ assert.strictEqual(fromBufferKey(toBufferKey(Math.PI)), Math.PI)
24
+ assert.strictEqual(fromBufferKey(toBufferKey(9377288)), 9377288)
25
+ assert.strictEqual(fromBufferKey(toBufferKey(1503579323825)), 1503579323825)
26
+ assert.strictEqual(fromBufferKey(toBufferKey(1503579323825.3523532)), 1503579323825.3523532)
27
+ assert.strictEqual(fromBufferKey(toBufferKey(-1503579323825)), -1503579323825)
28
+ assert.strictEqual(fromBufferKey(toBufferKey(0.00005032)), 0.00005032)
29
+ assert.strictEqual(fromBufferKey(toBufferKey(-0.00005032)), -0.00005032)
30
+ assert.strictEqual(fromBufferKey(toBufferKey(0.00000000000000000000000005431)), 0.00000000000000000000000005431)
31
+ })
32
+ test('number comparison', () => {
33
+ assertBufferComparison(toBufferKey(4), toBufferKey(5))
34
+ assertBufferComparison(toBufferKey(1503579323824), toBufferKey(1503579323825))
35
+ assertBufferComparison(toBufferKey(1.4), toBufferKey(2))
36
+ assertBufferComparison(toBufferKey(0.000000001), toBufferKey(0.00000001))
37
+ assertBufferComparison(toBufferKey(-4), toBufferKey(3))
38
+ assertBufferComparison(toBufferKey(0), toBufferKey(1))
39
+ assertBufferComparison(toBufferKey(-0.001), toBufferKey(0))
40
+ assertBufferComparison(toBufferKey(-0.001), toBufferKey(-0.000001))
41
+ assertBufferComparison(toBufferKey(-5236532532532), toBufferKey(-5236532532531))
42
+ })
43
+ test('string equivalence', () => {
44
+ assert.strictEqual(fromBufferKey(toBufferKey('4')), '4')
45
+ assert.strictEqual(fromBufferKey(toBufferKey('hello')), 'hello')
46
+ assert.strictEqual(fromBufferKey(toBufferKey('')), '')
47
+ })
48
+ test('string comparison', () => {
49
+ assertBufferComparison(toBufferKey('4'), toBufferKey('5'))
50
+ assertBufferComparison(toBufferKey('and'), toBufferKey('bad'))
51
+ assertBufferComparison(toBufferKey('hello'), toBufferKey('hello2'))
52
+ let buffer = Buffer.alloc(1024)
53
+ let end = writeKey(['this is a test', 5.25], buffer, 0)
54
+ })
55
+ test('boolean equivalence', () => {
56
+ assert.strictEqual(fromBufferKey(toBufferKey(true)), true)
57
+ assert.strictEqual(fromBufferKey(toBufferKey(false)), false)
58
+ })
59
+
60
+ test('multipart equivalence', () => {
61
+ assert.deepEqual(fromBufferKey(toBufferKey([4, 5])),
62
+ [4, 5])
63
+ assert.deepEqual(fromBufferKey(toBufferKey(['hello', 5.25])),
64
+ ['hello', 5.25])
65
+ assert.deepEqual(fromBufferKey(toBufferKey([true, 1503579323825])),
66
+ [true, 1503579323825])
67
+ assert.deepEqual(fromBufferKey(toBufferKey([-0.2525, 'second'])),
68
+ [-0.2525, 'second'])
69
+ assert.deepEqual(fromBufferKey(toBufferKey([-0.2525, '2nd', '3rd'])),
70
+ [-0.2525, '2nd', '3rd'])
71
+ })
72
+
73
+ test('multipart comparison', () => {
74
+ assertBufferComparison(
75
+ Buffer.concat([toBufferKey(4), Buffer.from([30]), toBufferKey(5)]),
76
+ Buffer.concat([toBufferKey(5), Buffer.from([30]), toBufferKey(5)]))
77
+ assertBufferComparison(
78
+ Buffer.concat([toBufferKey(4), Buffer.from([30]), toBufferKey(5)]),
79
+ Buffer.concat([toBufferKey(4), Buffer.from([30]), toBufferKey(6)]))
80
+ assertBufferComparison(
81
+ Buffer.concat([toBufferKey('and'), Buffer.from([30]), toBufferKey(5)]),
82
+ Buffer.concat([toBufferKey('and2'), Buffer.from([30]), toBufferKey(5)]))
83
+ assertBufferComparison(
84
+ Buffer.concat([toBufferKey(4), Buffer.from([30]), toBufferKey('and')]),
85
+ Buffer.concat([toBufferKey(4), Buffer.from([30]), toBufferKey('cat')]))
86
+ })
87
+ test('performance', () => {
88
+ let buffer = Buffer.alloc(1024)
89
+ let start = process.hrtime.bigint()
90
+ let end, value
91
+ for (let i = 0; i < 1000000; i++) {
92
+ end = writeKey('this is a test of a longer string to read and write', buffer, 0)
93
+ }
94
+ console.log('writeKey string time', nextTime(), end)
95
+ for (let i = 0; i < 1000000; i++) {
96
+ value = readKey(buffer, 0, end)
97
+ }
98
+ console.log('readKey string time', nextTime(), value)
99
+
100
+ for (let i = 0; i < 1000000; i++) {
101
+ end = writeKey(33456, buffer, 0)
102
+ }
103
+ console.log('writeKey number time', nextTime(), end)
104
+
105
+ for (let i = 0; i < 1000000; i++) {
106
+ value = readKey(buffer, 2, end)
107
+ }
108
+ console.log('readKey number time', nextTime(), value)
109
+
110
+ for (let i = 0; i < 1000000; i++) {
111
+ end = writeKey(['hello', 33456], buffer, 0)
112
+ }
113
+ console.log('writeKey array time', nextTime(), end, buffer.slice(0, end))
114
+
115
+ for (let i = 0; i < 1000000; i++) {
116
+ value = readKey(buffer, 0, end)
117
+ }
118
+ console.log('readKey array time', nextTime(), value)
119
+
120
+ function nextTime() {
121
+ let ns = process.hrtime.bigint()
122
+ let elapsed = ns - start
123
+ start = ns
124
+ return Number(elapsed) / 1000000 + 'ns'
125
+ }
126
+ })
127
+
128
+ })
package/uuid.js ADDED
@@ -0,0 +1,110 @@
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
+ }