Skip to content

Commit 3313702

Browse files
authored
feat: add xor compare (#89)
To allow sorting arrays of xor values, add an xor compare function that compares the relative distance of two xor arrays.
1 parent b7f9f21 commit 3313702

File tree

6 files changed

+75
-1
lines changed

6 files changed

+75
-1
lines changed

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,26 @@ import { xor } from 'uint8arrays/xor'
161161
console.info(xor(Uint8Array.from([1, 0]), Uint8Array.from([0, 1]))) // Uint8Array[1, 1]
162162
```
163163

164+
## xorCompare(a, b)
165+
166+
Compares the distances between two xor `Uint8Array`s.
167+
168+
### Example
169+
170+
```ts
171+
import { xor } from 'uint8arrays/xor'
172+
import { xorCompare } from 'uint8arrays/xor-compare'
173+
174+
const target = Uint8Array.from([1, 1])
175+
const val1 = Uint8Array.from([1, 0])
176+
const xor1 = xor(target, val1)
177+
178+
const val2 = Uint8Array.from([0, 1])
179+
const xor2 = xor(target, val2)
180+
181+
console.info(xorCompare(xor1, xor2)) // -1 or 0 or 1
182+
```
183+
164184
# Install
165185

166186
```console

package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,10 @@
7777
"./xor": {
7878
"types": "./dist/src/xor.d.ts",
7979
"import": "./dist/src/xor.js"
80+
},
81+
"./xor-compare": {
82+
"types": "./dist/src/xor-compare.d.ts",
83+
"import": "./dist/src/xor-compare.js"
8084
}
8185
},
8286
"imports": {

src/index.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,26 @@
141141
*
142142
* console.info(xor(Uint8Array.from([1, 0]), Uint8Array.from([0, 1]))) // Uint8Array[1, 1]
143143
* ```
144+
*
145+
* ## xorCompare(a, b)
146+
*
147+
* Compares the distances between two xor `Uint8Array`s.
148+
*
149+
* ### Example
150+
*
151+
* ```ts
152+
* import { xor } from 'uint8arrays/xor'
153+
* import { xorCompare } from 'uint8arrays/xor-compare'
154+
*
155+
* const target = Uint8Array.from([1, 1])
156+
* const val1 = Uint8Array.from([1, 0])
157+
* const xor1 = xor(target, val1)
158+
*
159+
* const val2 = Uint8Array.from([0, 1])
160+
* const xor2 = xor(target, val2)
161+
*
162+
* console.info(xorCompare(xor1, xor2)) // -1 or 0 or 1
163+
* ```
144164
*/
145165

146166
import { equals } from './equals.js'

src/xor-compare.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* Compares two Uint8Arrays representing two xor distances. Returns `-1` if `a`
3+
* is a lower distance, `1` if `b` is a lower distance or `0` if the distances
4+
* are equal.
5+
*/
6+
export function xorCompare (a: Uint8Array, b: Uint8Array): -1 | 0 | 1 {
7+
if (a.byteLength !== b.byteLength) {
8+
throw new Error('Inputs should have the same length')
9+
}
10+
11+
for (let i = 0; i < a.byteLength; i++) {
12+
if (a[i] === b[i]) {
13+
continue
14+
}
15+
16+
return a[i] < b[i] ? -1 : 1
17+
}
18+
19+
return 0
20+
}

src/xor.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { allocUnsafe } from '#alloc'
22
import { asUint8Array } from '#util/as-uint8array'
33

44
/**
5-
* Returns the xor distance between two arrays
5+
* Returns the xor distance between two Uint8Arrays
66
*/
77
export function xor (a: Uint8Array, b: Uint8Array): Uint8Array {
88
if (a.length !== b.length) {

test/xor-compare.spec.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { expect } from 'aegir/chai'
2+
import { xorCompare } from '../src/xor-compare.js'
3+
4+
describe('xor-compare', () => {
5+
it('compare', () => {
6+
expect(xorCompare(Uint8Array.from([0, 0]), Uint8Array.from([0, 1]))).to.equal(-1)
7+
expect(xorCompare(Uint8Array.from([0, 1]), Uint8Array.from([0, 1]))).to.equal(0)
8+
expect(xorCompare(Uint8Array.from([1, 1]), Uint8Array.from([0, 1]))).to.equal(1)
9+
})
10+
})

0 commit comments

Comments
 (0)