ordered-binary 1.2.5 → 1.4.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/.idea/modules.xml +8 -0
- package/.idea/ordered-binary.iml +14 -0
- package/LICENSE +21 -21
- package/README.md +39 -39
- package/dist/index.cjs +359 -330
- package/index.d.ts +22 -22
- package/index.js +359 -330
- package/package.json +37 -37
- package/rollup.config.js +11 -11
- package/tests/test.js +130 -128
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.0",
|
|
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
|
@@ -1,128 +1,130 @@
|
|
|
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
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
assertBufferComparison(toBufferKey('
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
assert.deepEqual(fromBufferKey(toBufferKey([
|
|
64
|
-
[
|
|
65
|
-
assert.deepEqual(fromBufferKey(toBufferKey([
|
|
66
|
-
[
|
|
67
|
-
assert.deepEqual(fromBufferKey(toBufferKey([
|
|
68
|
-
[
|
|
69
|
-
assert.deepEqual(fromBufferKey(toBufferKey([-0.2525, '
|
|
70
|
-
[-0.2525, '
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
Buffer.concat([toBufferKey(
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
Buffer.concat([toBufferKey(
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
Buffer.concat([toBufferKey(
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
let
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
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
|
+
assert.strictEqual(fromBufferKey(toBufferKey('\x00')), '\x00')
|
|
48
|
+
assert.strictEqual(fromBufferKey(toBufferKey('\x03test\x01\x00')), '\x03test\x01\x00')
|
|
49
|
+
})
|
|
50
|
+
test('string comparison', () => {
|
|
51
|
+
assertBufferComparison(toBufferKey('4'), toBufferKey('5'))
|
|
52
|
+
assertBufferComparison(toBufferKey('and'), toBufferKey('bad'))
|
|
53
|
+
assertBufferComparison(toBufferKey('hello'), toBufferKey('hello2'))
|
|
54
|
+
let buffer = Buffer.alloc(1024)
|
|
55
|
+
let end = writeKey(['this is a test', 5.25], buffer, 0)
|
|
56
|
+
})
|
|
57
|
+
test('boolean equivalence', () => {
|
|
58
|
+
assert.strictEqual(fromBufferKey(toBufferKey(true)), true)
|
|
59
|
+
assert.strictEqual(fromBufferKey(toBufferKey(false)), false)
|
|
60
|
+
})
|
|
61
|
+
|
|
62
|
+
test('multipart equivalence', () => {
|
|
63
|
+
assert.deepEqual(fromBufferKey(toBufferKey([4, 5])),
|
|
64
|
+
[4, 5])
|
|
65
|
+
assert.deepEqual(fromBufferKey(toBufferKey(['hello', 5.25])),
|
|
66
|
+
['hello', 5.25])
|
|
67
|
+
assert.deepEqual(fromBufferKey(toBufferKey([true, 1503579323825])),
|
|
68
|
+
[true, 1503579323825])
|
|
69
|
+
assert.deepEqual(fromBufferKey(toBufferKey([-0.2525, 'sec\x00nd'])),
|
|
70
|
+
[-0.2525, 'sec\x00nd'])
|
|
71
|
+
assert.deepEqual(fromBufferKey(toBufferKey([-0.2525, '2nd', '3rd'])),
|
|
72
|
+
[-0.2525, '2nd', '3rd'])
|
|
73
|
+
})
|
|
74
|
+
|
|
75
|
+
test('multipart comparison', () => {
|
|
76
|
+
assertBufferComparison(
|
|
77
|
+
Buffer.concat([toBufferKey(4), Buffer.from([30]), toBufferKey(5)]),
|
|
78
|
+
Buffer.concat([toBufferKey(5), Buffer.from([30]), toBufferKey(5)]))
|
|
79
|
+
assertBufferComparison(
|
|
80
|
+
Buffer.concat([toBufferKey(4), Buffer.from([30]), toBufferKey(5)]),
|
|
81
|
+
Buffer.concat([toBufferKey(4), Buffer.from([30]), toBufferKey(6)]))
|
|
82
|
+
assertBufferComparison(
|
|
83
|
+
Buffer.concat([toBufferKey('and'), Buffer.from([30]), toBufferKey(5)]),
|
|
84
|
+
Buffer.concat([toBufferKey('and2'), Buffer.from([30]), toBufferKey(5)]))
|
|
85
|
+
assertBufferComparison(
|
|
86
|
+
Buffer.concat([toBufferKey(4), Buffer.from([30]), toBufferKey('and')]),
|
|
87
|
+
Buffer.concat([toBufferKey(4), Buffer.from([30]), toBufferKey('cat')]))
|
|
88
|
+
})
|
|
89
|
+
test('performance', () => {
|
|
90
|
+
let buffer = Buffer.alloc(1024)
|
|
91
|
+
let start = process.hrtime.bigint()
|
|
92
|
+
let end, value
|
|
93
|
+
for (let i = 0; i < 1000000; i++) {
|
|
94
|
+
end = writeKey('this is a test of a longer string to read and write', buffer, 0)
|
|
95
|
+
}
|
|
96
|
+
console.log('writeKey string time', nextTime(), end)
|
|
97
|
+
for (let i = 0; i < 1000000; i++) {
|
|
98
|
+
value = readKey(buffer, 0, end)
|
|
99
|
+
}
|
|
100
|
+
console.log('readKey string time', nextTime(), value)
|
|
101
|
+
|
|
102
|
+
for (let i = 0; i < 1000000; i++) {
|
|
103
|
+
end = writeKey(33456, buffer, 0)
|
|
104
|
+
}
|
|
105
|
+
console.log('writeKey number time', nextTime(), end)
|
|
106
|
+
|
|
107
|
+
for (let i = 0; i < 1000000; i++) {
|
|
108
|
+
value = readKey(buffer, 2, end)
|
|
109
|
+
}
|
|
110
|
+
console.log('readKey number time', nextTime(), value)
|
|
111
|
+
|
|
112
|
+
for (let i = 0; i < 1000000; i++) {
|
|
113
|
+
end = writeKey(['hello', 33456], buffer, 0)
|
|
114
|
+
}
|
|
115
|
+
console.log('writeKey array time', nextTime(), end, buffer.slice(0, end))
|
|
116
|
+
|
|
117
|
+
for (let i = 0; i < 1000000; i++) {
|
|
118
|
+
value = readKey(buffer, 0, end)
|
|
119
|
+
}
|
|
120
|
+
console.log('readKey array time', nextTime(), value)
|
|
121
|
+
|
|
122
|
+
function nextTime() {
|
|
123
|
+
let ns = process.hrtime.bigint()
|
|
124
|
+
let elapsed = ns - start
|
|
125
|
+
start = ns
|
|
126
|
+
return Number(elapsed) / 1000000 + 'ns'
|
|
127
|
+
}
|
|
128
|
+
})
|
|
129
|
+
|
|
130
|
+
})
|