Skip to content

Commit 82e4129

Browse files
committed
feat: add xor function
1 parent 687f32f commit 82e4129

11 files changed

+1219
-1144
lines changed

README.md

+14
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ Some utility functions to make dealing with `Uint8Array`s more pleasant.
1313
- [Example](#example-3)
1414
- [toString(array, encoding = 'utf8')](#tostringarray-encoding--utf8)
1515
- [Example](#example-4)
16+
- [xor(a, b)](#xora-b)
17+
- [Example](#example-5)
1618

1719
## API
1820

@@ -112,3 +114,15 @@ console.info(toString(Uint8Array.from([0, 1, 2...]), 'base16')) // '00010203aabb
112114
console.info(toString(Uint8Array.from([0, 1, 2...]), 'base64')) // 'AAECA6q7zA'
113115
console.info(toString(Uint8Array.from([48, 49, 50...]), 'ascii')) // '01234'
114116
```
117+
118+
### xor(a, b)
119+
120+
Returns a `Uint8Array` containing `a` and `b` xored together.
121+
122+
#### Example
123+
124+
```js
125+
const xor = require('uint8arrays/xor')
126+
127+
console.info(xor(Uint8Array.from([1, 0]), Uint8Array.from([0, 1]))) // Uint8Array[1, 1]
128+
```

package-lock.json

+1,145-1,135
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"from-string.js",
2323
"index.js",
2424
"to-string.js",
25+
"xor.js",
2526
"dist/*.ts",
2627
"dist/*.map",
2728
"dist/*.js"
@@ -46,7 +47,7 @@
4647
"web-encoding": "^1.0.5"
4748
},
4849
"devDependencies": {
49-
"aegir": "^30.1.0"
50+
"aegir": "^30.3.0"
5051
},
5152
"contributors": [
5253
"achingbrain <[email protected]>",

test/compare.spec.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
/* eslint-env mocha */
12
'use strict'
23

3-
/* eslint-env mocha */
44
const { expect } = require('aegir/utils/chai')
55
const compare = require('../compare')
66

test/concat.spec.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
/* eslint-env mocha */
12
'use strict'
23

3-
/* eslint-env mocha */
44
const { expect } = require('aegir/utils/chai')
55
const concat = require('../concat')
66

test/equals.spec.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
/* eslint-env mocha */
12
'use strict'
23

3-
/* eslint-env mocha */
44
const { expect } = require('aegir/utils/chai')
55
const equals = require('../equals')
66

test/from-string.spec.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
/* eslint-env mocha */
12
'use strict'
23

3-
/* eslint-env mocha */
44
const { expect } = require('aegir/utils/chai')
55
const fromString = require('../from-string')
66
const { TextEncoder } = require('web-encoding')

test/to-string.spec.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
'use strict'
2-
31
/* eslint-env mocha */
2+
'use strict'
43

5-
// @ts-ignore
64
const { expect } = require('aegir/utils/chai')
75
const toString = require('../to-string')
86
const { TextEncoder } = require('web-encoding')

test/xor.spec.js

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/* eslint-env mocha */
2+
'use strict'
3+
4+
const { expect } = require('aegir/utils/chai')
5+
const xor = require('../xor')
6+
7+
describe('Uint8Array xor', () => {
8+
it('xors 1,0 and 0,1', () => {
9+
const a = Uint8Array.from([1, 0])
10+
const b = Uint8Array.from([0, 1])
11+
12+
expect(xor(a, b)).to.deep.equal(Uint8Array.from([1, 1]))
13+
})
14+
15+
it('xors 1,1 and 0,1', () => {
16+
const a = Uint8Array.from([1, 1])
17+
const b = Uint8Array.from([0, 1])
18+
19+
expect(xor(a, b)).to.deep.equal(Uint8Array.from([1, 0]))
20+
})
21+
22+
it('xors 1,1 and 1,1', () => {
23+
const a = Uint8Array.from([1, 1])
24+
const b = Uint8Array.from([1, 1])
25+
26+
expect(xor(a, b)).to.deep.equal(Uint8Array.from([0, 0]))
27+
})
28+
})

tsconfig.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"outDir": "dist"
55
},
66
"include": [
7-
"test", // remove this line if you don't want to type-check tests
7+
"test",
88
"*.js"
99
]
1010
}

xor.js

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
'use strict'
2+
3+
/**
4+
* Returns the xor distance between two arrays
5+
*
6+
* @param {Uint8Array} a
7+
* @param {Uint8Array} b
8+
* @returns {Uint8Array}
9+
*/
10+
function xor (a, b) {
11+
if (a.length !== b.length) {
12+
throw new Error('Inputs should have the same length')
13+
}
14+
15+
const result = new Uint8Array(a.length)
16+
17+
for (let i = 0; i < a.length; i++) {
18+
result[i] = a[i] ^ b[i]
19+
}
20+
21+
return result
22+
}
23+
24+
module.exports = xor

0 commit comments

Comments
 (0)