Skip to content

Commit 2c849dc

Browse files
committed
feat: add implementation
0 parents  commit 2c849dc

16 files changed

+18878
-0
lines changed

.aegir.js

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
'use strict'
2+
3+
module.exports = {
4+
webpack: {
5+
entry: './index.js'
6+
}
7+
}

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
dist

README.md

+112
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
# Uint8Arrays <!-- omit in toc -->
2+
3+
Some utility functions to make dealing with `Uint8Array`s more pleasant.
4+
5+
- [API](#api)
6+
- [compare(a, b)](#comparea-b)
7+
- [Example](#example)
8+
- [concat(arrays, [length])](#concatarrays-length)
9+
- [Example](#example-1)
10+
- [equals(a, b)](#equalsa-b)
11+
- [Example](#example-2)
12+
- [fromString(string, encoding = 'utf8')](#fromstringstring-encoding--utf8)
13+
- [Example](#example-3)
14+
- [toString(array, encoding = 'utf8')](#tostringarray-encoding--utf8)
15+
- [Example](#example-4)
16+
17+
## API
18+
19+
### compare(a, b)
20+
21+
Compare two `Uint8Arrays`
22+
23+
#### Example
24+
25+
```js
26+
const compare = require('uint8arrays/compare')
27+
28+
const arrays = [
29+
Uint8Array.from([3, 4, 5]),
30+
Uint8Array.from([0, 1, 2])
31+
]
32+
33+
const sorted = arrays.sort(compare)
34+
35+
console.info(sorted)
36+
// [
37+
// Uint8Array[0, 1, 2]
38+
// Uint8Array[3, 4, 5]
39+
// ]
40+
```
41+
42+
### concat(arrays, [length])
43+
44+
Concatenate one or more array-likes and return a `Uint8Array` with their contents.
45+
46+
If you know the length of the arrays, pass it as a second parameter, otherwise it will be calculated by traversing the list of arrays.
47+
48+
#### Example
49+
50+
```js
51+
const concat = require('uint8arrays/concat')
52+
53+
const arrays = [
54+
Uint8Array.from([0, 1, 2]),
55+
Uint8Array.from([3, 4, 5])
56+
]
57+
58+
const all = concat(arrays, 6)
59+
60+
console.info(all)
61+
// Uint8Array[0, 1, 2, 3, 4, 5]
62+
```
63+
64+
### equals(a, b)
65+
66+
Returns true if the two arrays are the same array or if they have the same length and contents.
67+
68+
#### Example
69+
70+
```js
71+
const equals = require('uint8arrays/equals')
72+
73+
const a = Uint8Array.from([0, 1, 2])
74+
const b = Uint8Array.from([3, 4, 5])
75+
const c = Uint8Array.from([0, 1, 2])
76+
77+
console.info(equals(a, b)) // false
78+
console.info(equals(a, c)) // true
79+
console.info(equals(a, a)) // true
80+
```
81+
82+
### fromString(string, encoding = 'utf8')
83+
84+
Returns a new `Uint8Array` created from the passed string and interpreted as the passed encoding.
85+
86+
Supports `utf8` and any of the [multiformats encodings](https://www.npmjs.com/package/multibase#supported-encodings-see-srcconstantsjs).
87+
88+
#### Example
89+
90+
```js
91+
const fromString = require('uint8arrays/from-string')
92+
93+
console.info(fromString('hello world')) // Uint8Array[104, 101 ...
94+
console.info(fromString('00010203aabbcc', 'base16')) // Uint8Array[0, 1 ...
95+
console.info(fromString('AAECA6q7zA', 'base64')) // Uint8Array[0, 1 ...
96+
```
97+
98+
### toString(array, encoding = 'utf8')
99+
100+
Returns a string created from the passed `Uint8Array` in the passed encoding.
101+
102+
Supports `utf8` and any of the [multiformats encodings](https://www.npmjs.com/package/multibase#supported-encodings-see-srcconstantsjs).
103+
104+
#### Example
105+
106+
```js
107+
const fromString = require('uint8arrays/from-string')
108+
109+
console.info(toString(Uint8Array.from([104, 101...]))) // 'hello world'
110+
console.info(toString(Uint8Array.from([0, 1, 2...]), 'base16')) // '00010203aabbcc'
111+
console.info(toString(Uint8Array.from([0, 1, 2...]), 'base64')) // 'AAECA6q7zA'
112+
```

compare.js

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
'use strict'
2+
3+
/**
4+
* Can be used with Array.sort to sort and array with Uint8Array entries
5+
*
6+
* @param {Uint8Array} a
7+
* @param {Uint8Array} b
8+
* @returns {Number}
9+
*/
10+
function compare (a, b) {
11+
for (let i = 0; i < a.byteLength; i++) {
12+
if (a[i] < b[i]) {
13+
return -1
14+
}
15+
16+
if (a[i] > b[i]) {
17+
return 1
18+
}
19+
}
20+
21+
if (a.byteLength > b.byteLength) {
22+
return 1
23+
}
24+
25+
if (a.byteLength < b.byteLength) {
26+
return -1
27+
}
28+
29+
return 0
30+
}
31+
32+
module.exports = compare

concat.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
'use strict'
2+
3+
/**
4+
* Returns a new Uint8Array created by concatenating the passed ArrayLikes
5+
*
6+
* @param {Array<ArrayLike<number>>} arrays
7+
* @param {Number} length
8+
* @returns {Uint8Array}
9+
*/
10+
function concat (arrays, length) {
11+
if (!length) {
12+
length = arrays.reduce((acc, curr) => acc + curr.length, 0)
13+
}
14+
15+
const output = new Uint8Array(length)
16+
let offset = 0
17+
18+
for (const arr of arrays) {
19+
output.set(arr, offset)
20+
offset += arr.length
21+
}
22+
23+
return output
24+
}
25+
26+
module.exports = concat

equals.js

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
'use strict'
2+
3+
/**
4+
* Returns true if the two passed Uint8Arrays have the same content
5+
*
6+
* @param {Uint8Array} a
7+
* @param {Uint8Array} b
8+
* @returns {boolean}
9+
*/
10+
function equals (a, b) {
11+
if (a === b) {
12+
return true
13+
}
14+
15+
if (a.byteLength !== b.byteLength) {
16+
return false
17+
}
18+
19+
for (let i = 0; i < a.byteLength; i++) {
20+
if (a[i] !== b[i]) {
21+
return false
22+
}
23+
}
24+
25+
return true
26+
}
27+
28+
module.exports = equals

from-string.js

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
'use strict'
2+
3+
const { names } = require('multibase/src/constants')
4+
const { TextEncoder } = require('web-encoding')
5+
const utf8Encoder = new TextEncoder()
6+
7+
/**
8+
* Create a `Uint8Array` from the passed string
9+
*
10+
* @param {String} string
11+
* @param {String} [encoding=utf8] utf8, base16, base64, base64urlpad, etc
12+
* @returns {Uint8Array}
13+
* @see {@link https://www.npmjs.com/package/multibase|multibase} for supported encodings other than `utf8`
14+
*/
15+
function fromString (string, encoding = 'utf8') {
16+
if (encoding === 'utf8' || encoding === 'utf-8') {
17+
return utf8Encoder.encode(string)
18+
}
19+
20+
const codec = names[encoding]
21+
22+
if (!codec) {
23+
throw new Error('Unknown base')
24+
}
25+
26+
return codec.decode(string)
27+
}
28+
29+
module.exports = fromString

index.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
'use strict'
2+
3+
module.exports = {
4+
compare: require('./compare'),
5+
concat: require('./concat'),
6+
equals: require('./equals'),
7+
fromString: require('./from-string'),
8+
toString: require('./to-string')
9+
}

0 commit comments

Comments
 (0)