-
Notifications
You must be signed in to change notification settings - Fork 31
feat: add utility functions for dealing with Uint8Arrays #55
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
cf9c5c6
feat: add utility methods for dealing with Uint8Arrays
achingbrain 837bbb1
chore: add from hex string and jsdoc comments
achingbrain 659b76c
chore: rename for consistency
achingbrain 079f80f
chore: unsaved files
achingbrain 371b6f4
chore: add to hex string method
achingbrain fb83a28
chore: add encoding to to/from string
achingbrain bb5033e
chore: rename sort to compare
achingbrain 4030927
chore: address PR comments
achingbrain File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
'use strict' | ||
|
||
/** | ||
* Can be used with Array.sort to sort and array with Uint8Array entries | ||
* | ||
* @param {Uint8Array} a | ||
* @param {Uint8Array} b | ||
* @returns {Number} | ||
*/ | ||
function compare (a, b) { | ||
for (let i = 0; i < a.byteLength; i++) { | ||
if (a[i] < b[i]) { | ||
return -1 | ||
} | ||
|
||
if (a[i] > b[i]) { | ||
return 1 | ||
} | ||
} | ||
|
||
if (a.byteLength > b.byteLength) { | ||
return 1 | ||
} | ||
|
||
if (a.byteLength < b.byteLength) { | ||
return -1 | ||
} | ||
|
||
return 0 | ||
} | ||
|
||
module.exports = compare |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
'use strict' | ||
|
||
function byteLength (arrs) { | ||
return arrs.reduce((acc, curr) => { | ||
if (ArrayBuffer.isView(curr)) { | ||
return acc + curr.byteLength | ||
} else if (Array.isArray(curr)) { | ||
return acc + curr.length | ||
} | ||
|
||
throw new Error('Invalid input passed to concat, should be an Array or ArrayBuffer view') | ||
}, 0) | ||
} | ||
|
||
/** | ||
* Returns a new Uint8Array created by concatenating the passed Arrays | ||
* | ||
* @param {Array<Array|TypedArray>} arrs | ||
* @param {Number} [length] | ||
* @returns {Uint8Array} | ||
*/ | ||
function concat (arrs, length) { | ||
if (!length) { | ||
length = byteLength(arrs) | ||
} | ||
|
||
const output = new Uint8Array(length) | ||
let offset = 0 | ||
|
||
for (const arr of arrs) { | ||
output.set(arr, offset) | ||
|
||
if (ArrayBuffer.isView(arr)) { | ||
offset += arr.byteLength | ||
} else if (Array.isArray(arr)) { | ||
offset += arr.length | ||
} else { | ||
throw new Error('Invalid input passed to concat, should be an Array or ArrayBuffer view') | ||
} | ||
} | ||
|
||
return output | ||
} | ||
|
||
module.exports = concat |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
'use strict' | ||
|
||
/** | ||
* Returns true if the two passed Uint8Arrays have the same content | ||
* | ||
* @param {Uint8Array} a | ||
* @param {Uint8Array} b | ||
* @returns {boolean} | ||
*/ | ||
function equals (a, b) { | ||
if (a === b) { | ||
return true | ||
} | ||
|
||
if (a.byteLength !== b.byteLength) { | ||
return false | ||
} | ||
|
||
for (let i = 0; i < a.byteLength; i++) { | ||
if (a[i] !== b[i]) { | ||
return false | ||
} | ||
} | ||
|
||
return true | ||
} | ||
|
||
module.exports = equals |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
'use strict' | ||
|
||
const multibase = require('multibase') | ||
const { names } = require('multibase/src/constants') | ||
const TextEncoder = require('../text-encoder') | ||
const utf8Encoder = new TextEncoder() | ||
|
||
/** | ||
* Create a `Uint8Array` from the passed string | ||
* | ||
* @param {String} string | ||
* @param {String} [encoding=utf8] utf8, base16, base64, base64urlpad, etc | ||
* @returns {Uint8Array} | ||
* @see {@link https://www.npmjs.com/package/multibase|multibase} for supported encodings other than `utf8` | ||
*/ | ||
function fromString (string, encoding = 'utf8') { | ||
if (encoding === 'utf8' || encoding === 'utf-8') { | ||
return utf8Encoder.encode(string) | ||
} | ||
|
||
const base = names[encoding] | ||
|
||
if (!base) { | ||
throw new Error('Unknown base') | ||
} | ||
|
||
string = `${base.code}${string}` | ||
|
||
return Uint8Array.from(multibase.decode(string)) | ||
} | ||
|
||
module.exports = fromString |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
'use strict' | ||
|
||
const multibase = require('multibase') | ||
const TextDecoder = require('../text-decoder') | ||
const utf8Decoder = new TextDecoder('utf8') | ||
|
||
/** | ||
* Turns a `Uint8Array` into a string. | ||
* | ||
* Supports `utf8` and any encoding supported by the multibase module | ||
* | ||
* @param {Uint8Array} buf The array to turn into a string | ||
* @param {String} [encoding=utf8] The encoding to use | ||
* @returns {String} | ||
* @see {@link https://www.npmjs.com/package/multibase|multibase} for supported encodings other than `utf8` | ||
*/ | ||
function toString (buf, encoding = 'utf8') { | ||
if (encoding !== 'utf8' && encoding !== 'utf-8') { | ||
buf = multibase.encode(encoding, buf).subarray(1) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same sentiment as above. |
||
} | ||
|
||
return utf8Decoder.decode(buf) | ||
} | ||
|
||
module.exports = toString |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
'use strict' | ||
|
||
/* eslint-env mocha */ | ||
const { expect } = require('aegir/utils/chai') | ||
const compare = require('../../src/uint8arrays/compare') | ||
|
||
describe('Uint8Array compare', () => { | ||
it('is stable', () => { | ||
const a = Uint8Array.from([0, 1, 2, 3]) | ||
const b = Uint8Array.from([0, 1, 2, 3]) | ||
|
||
expect([a, b].sort(compare)).to.deep.equal([ | ||
a, | ||
b | ||
]) | ||
expect([b, a].sort(compare)).to.deep.equal([ | ||
b, | ||
a | ||
]) | ||
}) | ||
|
||
it('compares two Uint8Arrays', () => { | ||
const a = Uint8Array.from([0, 1, 2, 4]) | ||
const b = Uint8Array.from([0, 1, 2, 3]) | ||
|
||
expect([a, b].sort(compare)).to.deep.equal([ | ||
b, | ||
a | ||
]) | ||
expect([b, a].sort(compare)).to.deep.equal([ | ||
b, | ||
a | ||
]) | ||
}) | ||
|
||
it('compares two Uint8Arrays with different lengths', () => { | ||
const a = Uint8Array.from([0, 1, 2, 3, 4]) | ||
const b = Uint8Array.from([0, 1, 2, 3]) | ||
|
||
expect([a, b].sort(compare)).to.deep.equal([ | ||
b, | ||
a | ||
]) | ||
expect([b, a].sort(compare)).to.deep.equal([ | ||
b, | ||
a | ||
]) | ||
}) | ||
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
'use strict' | ||
|
||
/* eslint-env mocha */ | ||
const { expect } = require('aegir/utils/chai') | ||
const concat = require('../../src/uint8arrays/concat') | ||
|
||
describe('Uint8Array concat', () => { | ||
it('concats two Uint8Arrays', () => { | ||
const a = Uint8Array.from([0, 1, 2, 3]) | ||
const b = Uint8Array.from([4, 5, 6, 7]) | ||
const c = Uint8Array.from([0, 1, 2, 3, 4, 5, 6, 7]) | ||
|
||
expect(concat([a, b])).to.deep.equal(c) | ||
}) | ||
|
||
it('concats two Uint8Arrays with a length', () => { | ||
const a = Uint8Array.from([0, 1, 2, 3]) | ||
const b = Uint8Array.from([4, 5, 6, 7]) | ||
const c = Uint8Array.from([0, 1, 2, 3, 4, 5, 6, 7]) | ||
|
||
expect(concat([a, b], 8)).to.deep.equal(c) | ||
}) | ||
|
||
it('concats mixed Uint8Arrays and Arrays', () => { | ||
const a = Uint8Array.from([0, 1, 2, 3]) | ||
const b = [4, 5, 6, 7] | ||
const c = Uint8Array.from([0, 1, 2, 3, 4, 5, 6, 7]) | ||
|
||
expect(concat([a, b])).to.deep.equal(c) | ||
}) | ||
|
||
it('concats mixed Uint8Arrays and Arrays with a length', () => { | ||
const a = Uint8Array.from([0, 1, 2, 3]) | ||
const b = [4, 5, 6, 7] | ||
const c = Uint8Array.from([0, 1, 2, 3, 4, 5, 6, 7]) | ||
|
||
expect(concat([a, b], 8)).to.deep.equal(c) | ||
}) | ||
|
||
it('throws when passed non ArrayBuffer views/Arrays', () => { | ||
const a = Uint8Array.from([0, 1, 2, 3]) | ||
const b = 'Hello world' | ||
|
||
expect(() => concat([a, b])).to.throw(/Invalid input/) | ||
}) | ||
|
||
it('throws when passed non ArrayBuffer views/Arrays with a length', () => { | ||
const a = Uint8Array.from([0, 1, 2, 3]) | ||
const b = 'Hello world' | ||
|
||
expect(() => concat([a, b], 100)).to.throw(/Invalid input/) | ||
}) | ||
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
'use strict' | ||
|
||
/* eslint-env mocha */ | ||
const { expect } = require('aegir/utils/chai') | ||
const equals = require('../../src/uint8arrays/equals') | ||
|
||
describe('Uint8Array equals', () => { | ||
it('finds two Uint8Arrays equal', () => { | ||
const a = Uint8Array.from([0, 1, 2, 3]) | ||
const b = Uint8Array.from([0, 1, 2, 3]) | ||
|
||
expect(equals(a, b)).to.be.true() | ||
}) | ||
|
||
it('finds two Uint8Arrays not equal', () => { | ||
const a = Uint8Array.from([0, 1, 2, 3]) | ||
const b = Uint8Array.from([0, 1, 2, 4]) | ||
|
||
expect(equals(a, b)).to.be.false() | ||
}) | ||
|
||
it('finds two Uint8Arrays with different lengths not equal', () => { | ||
const a = Uint8Array.from([0, 1, 2, 3]) | ||
const b = Uint8Array.from([0, 1, 2, 3, 4]) | ||
|
||
expect(equals(a, b)).to.be.false() | ||
}) | ||
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
'use strict' | ||
|
||
/* eslint-env mocha */ | ||
const { expect } = require('aegir/utils/chai') | ||
const fromString = require('../../src/uint8arrays/from-string') | ||
const TextEncoder = require('../../src/text-encoder') | ||
|
||
describe('Uint8Array fromString', () => { | ||
it('creates a Uint8Array from a string', () => { | ||
const str = 'hello world' | ||
const arr = new TextEncoder('utf8').encode(str) | ||
|
||
expect(fromString(str)).to.deep.equal(arr) | ||
}) | ||
|
||
it('creates a Uint8Array from a base16 string', () => { | ||
const str = '00010203aabbcc' | ||
const arr = Uint8Array.from([0, 1, 2, 3, 170, 187, 204]) | ||
|
||
expect(fromString(str, 'base16')).to.deep.equal(arr) | ||
}) | ||
|
||
it('creates a Uint8Array from a base64 string', () => { | ||
const str = 'AAECA6q7zA' | ||
const arr = Uint8Array.from([0, 1, 2, 3, 170, 187, 204]) | ||
|
||
expect(fromString(str, 'base64')).to.deep.equal(arr) | ||
}) | ||
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
'use strict' | ||
|
||
/* eslint-env mocha */ | ||
const { expect } = require('aegir/utils/chai') | ||
const toString = require('../../src/uint8arrays/to-string') | ||
const TextEncoder = require('../../src/text-encoder') | ||
|
||
describe('Uint8Array toString', () => { | ||
it('creates a String from a Uint8Array', () => { | ||
const str = 'hello world' | ||
const arr = new TextEncoder('utf8').encode(str) | ||
|
||
expect(toString(arr)).to.deep.equal(str) | ||
}) | ||
|
||
it('creates a hex string from a Uint8Array', () => { | ||
const str = '00010203aabbcc' | ||
const arr = Uint8Array.from([0, 1, 2, 3, 170, 187, 204]) | ||
|
||
expect(toString(arr, 'base16')).to.deep.equal(str) | ||
}) | ||
|
||
it('creates a base64 string from a Uint8Array', () => { | ||
const str = 'AAECA6q7zA' | ||
const arr = Uint8Array.from([0, 1, 2, 3, 170, 187, 204]) | ||
|
||
expect(toString(arr, 'base64')).to.deep.equal(str) | ||
}) | ||
}) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you please add some comment what this supposed to do and a note what encoding this supposed to support.