Skip to content

Commit

Permalink
feat: add xor function
Browse files Browse the repository at this point in the history
  • Loading branch information
achingbrain committed Feb 4, 2021
1 parent 687f32f commit 82e4129
Show file tree
Hide file tree
Showing 11 changed files with 1,219 additions and 1,144 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ Some utility functions to make dealing with `Uint8Array`s more pleasant.
- [Example](#example-3)
- [toString(array, encoding = 'utf8')](#tostringarray-encoding--utf8)
- [Example](#example-4)
- [xor(a, b)](#xora-b)
- [Example](#example-5)

## API

Expand Down Expand Up @@ -112,3 +114,15 @@ console.info(toString(Uint8Array.from([0, 1, 2...]), 'base16')) // '00010203aabb
console.info(toString(Uint8Array.from([0, 1, 2...]), 'base64')) // 'AAECA6q7zA'
console.info(toString(Uint8Array.from([48, 49, 50...]), 'ascii')) // '01234'
```

### xor(a, b)

Returns a `Uint8Array` containing `a` and `b` xored together.

#### Example

```js
const xor = require('uint8arrays/xor')

console.info(xor(Uint8Array.from([1, 0]), Uint8Array.from([0, 1]))) // Uint8Array[1, 1]
```
2,280 changes: 1,145 additions & 1,135 deletions package-lock.json

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"from-string.js",
"index.js",
"to-string.js",
"xor.js",
"dist/*.ts",
"dist/*.map",
"dist/*.js"
Expand All @@ -46,7 +47,7 @@
"web-encoding": "^1.0.5"
},
"devDependencies": {
"aegir": "^30.1.0"
"aegir": "^30.3.0"
},
"contributors": [
"achingbrain <alex@achingbrain.net>",
Expand Down
2 changes: 1 addition & 1 deletion test/compare.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-env mocha */
'use strict'

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

Expand Down
2 changes: 1 addition & 1 deletion test/concat.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-env mocha */
'use strict'

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

Expand Down
2 changes: 1 addition & 1 deletion test/equals.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-env mocha */
'use strict'

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

Expand Down
2 changes: 1 addition & 1 deletion test/from-string.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-env mocha */
'use strict'

/* eslint-env mocha */
const { expect } = require('aegir/utils/chai')
const fromString = require('../from-string')
const { TextEncoder } = require('web-encoding')
Expand Down
4 changes: 1 addition & 3 deletions test/to-string.spec.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
'use strict'

/* eslint-env mocha */
'use strict'

// @ts-ignore
const { expect } = require('aegir/utils/chai')
const toString = require('../to-string')
const { TextEncoder } = require('web-encoding')
Expand Down
28 changes: 28 additions & 0 deletions test/xor.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/* eslint-env mocha */
'use strict'

const { expect } = require('aegir/utils/chai')
const xor = require('../xor')

describe('Uint8Array xor', () => {
it('xors 1,0 and 0,1', () => {
const a = Uint8Array.from([1, 0])
const b = Uint8Array.from([0, 1])

expect(xor(a, b)).to.deep.equal(Uint8Array.from([1, 1]))
})

it('xors 1,1 and 0,1', () => {
const a = Uint8Array.from([1, 1])
const b = Uint8Array.from([0, 1])

expect(xor(a, b)).to.deep.equal(Uint8Array.from([1, 0]))
})

it('xors 1,1 and 1,1', () => {
const a = Uint8Array.from([1, 1])
const b = Uint8Array.from([1, 1])

expect(xor(a, b)).to.deep.equal(Uint8Array.from([0, 0]))
})
})
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"outDir": "dist"
},
"include": [
"test", // remove this line if you don't want to type-check tests
"test",
"*.js"
]
}
24 changes: 24 additions & 0 deletions xor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
'use strict'

/**
* Returns the xor distance between two arrays
*
* @param {Uint8Array} a
* @param {Uint8Array} b
* @returns {Uint8Array}
*/
function xor (a, b) {
if (a.length !== b.length) {
throw new Error('Inputs should have the same length')
}

const result = new Uint8Array(a.length)

for (let i = 0; i < a.length; i++) {
result[i] = a[i] ^ b[i]
}

return result
}

module.exports = xor

0 comments on commit 82e4129

Please sign in to comment.