Skip to content

Commit 098724d

Browse files
authored
feat: support ascii encoding (#2)
For better or for worse, sometimes you want to covert between a Uint8Array and a string where each character of the string is created from the character code of the corresponding byte from the array. Adds an `ascii` encoding which is similar to `binary` as used by node buffers. It's not called `binary` as people confuse this with `base2`.
1 parent 5ccfa4d commit 098724d

File tree

5 files changed

+80
-5
lines changed

5 files changed

+80
-5
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ const fromString = require('uint8arrays/from-string')
9393
console.info(fromString('hello world')) // Uint8Array[104, 101 ...
9494
console.info(fromString('00010203aabbcc', 'base16')) // Uint8Array[0, 1 ...
9595
console.info(fromString('AAECA6q7zA', 'base64')) // Uint8Array[0, 1 ...
96+
console.info(fromString('01234', 'ascii')) // Uint8Array[48, 49 ...
9697
```
9798

9899
### toString(array, encoding = 'utf8')
@@ -109,4 +110,5 @@ const fromString = require('uint8arrays/from-string')
109110
console.info(toString(Uint8Array.from([104, 101...]))) // 'hello world'
110111
console.info(toString(Uint8Array.from([0, 1, 2...]), 'base16')) // '00010203aabbcc'
111112
console.info(toString(Uint8Array.from([0, 1, 2...]), 'base64')) // 'AAECA6q7zA'
113+
console.info(toString(Uint8Array.from([48, 49, 50...]), 'ascii')) // '01234'
112114
```

from-string.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,30 @@ const { names } = require('multibase/src/constants')
44
const { TextEncoder } = require('web-encoding')
55
const utf8Encoder = new TextEncoder()
66

7+
/**
8+
* Interperets each character in a string as a byte and
9+
* returns a Uint8Array of those bytes.
10+
*
11+
* @param {String} string The string to turn into an array
12+
* @returns {Uint8Array}
13+
*/
14+
function asciiStringToUint8Array (string) {
15+
const array = new Uint8Array(string.length)
16+
17+
for (let i = 0; i < string.length; i++) {
18+
array[i] = string.charCodeAt(i)
19+
}
20+
21+
return array
22+
}
23+
724
/**
825
* Create a `Uint8Array` from the passed string
926
*
27+
* Supports `utf8`, `utf-8` and any encoding supported by the multibase module.
28+
*
29+
* Also `ascii` which is similar to node's 'binary' encoding.
30+
*
1031
* @param {String} string
1132
* @param {String} [encoding=utf8] utf8, base16, base64, base64urlpad, etc
1233
* @returns {Uint8Array}
@@ -17,6 +38,10 @@ function fromString (string, encoding = 'utf8') {
1738
return utf8Encoder.encode(string)
1839
}
1940

41+
if (encoding === 'ascii') {
42+
return asciiStringToUint8Array(string)
43+
}
44+
2045
const codec = names[encoding]
2146

2247
if (!codec) {

test/from-string.spec.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,19 @@ describe('Uint8Array fromString', () => {
2727
expect(fromString(str, 'base64')).to.deep.equal(arr)
2828
})
2929

30+
it('creates a Uint8Array from an ascii string', () => {
31+
const str = [
32+
String.fromCharCode(0),
33+
String.fromCharCode(1),
34+
String.fromCharCode(2),
35+
String.fromCharCode(3),
36+
String.fromCharCode(4)
37+
].join('')
38+
const arr = Uint8Array.from([0, 1, 2, 3, 4])
39+
40+
expect(fromString(str, 'ascii')).to.deep.equal(arr)
41+
})
42+
3043
it('throws when an unknown base is passed', () => {
3144
const str = 'hello world'
3245

test/to-string.spec.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,19 @@ describe('Uint8Array toString', () => {
2727
expect(toString(arr, 'base64')).to.deep.equal(str)
2828
})
2929

30+
it('creates an ascii string from a Uint8Array', () => {
31+
const str = [
32+
String.fromCharCode(0),
33+
String.fromCharCode(1),
34+
String.fromCharCode(2),
35+
String.fromCharCode(3),
36+
String.fromCharCode(4)
37+
].join('')
38+
const arr = Uint8Array.from([0, 1, 2, 3, 4])
39+
40+
expect(toString(arr, 'ascii')).to.deep.equal(str)
41+
})
42+
3043
it('throws when an unknown base is passed', () => {
3144
const arr = Uint8Array.from([0, 1, 2, 3, 170, 187, 204])
3245

to-string.js

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,41 @@ const { names } = require('multibase/src/constants')
44
const { TextDecoder } = require('web-encoding')
55
const utf8Decoder = new TextDecoder('utf8')
66

7+
/**
8+
* Turns a Uint8Array of bytes into a string with each
9+
* character being the char code of the corresponding byte
10+
*
11+
* @param {Uint8Array} array The array to turn into a string
12+
* @returns {String}
13+
*/
14+
function uint8ArrayToAsciiString (array) {
15+
let string = ''
16+
17+
for (let i = 0; i < array.length; i++) {
18+
string += String.fromCharCode(array[i])
19+
}
20+
return string
21+
}
22+
723
/**
824
* Turns a `Uint8Array` into a string.
925
*
10-
* Supports `utf8` and any encoding supported by the multibase module
26+
* Supports `utf8`, `utf-8` and any encoding supported by the multibase module.
27+
*
28+
* Also `ascii` which is similar to node's 'binary' encoding.
1129
*
12-
* @param {Uint8Array} buf The array to turn into a string
30+
* @param {Uint8Array} array The array to turn into a string
1331
* @param {String} [encoding=utf8] The encoding to use
1432
* @returns {String}
1533
* @see {@link https://www.npmjs.com/package/multibase|multibase} for supported encodings other than `utf8`
1634
*/
17-
function toString (buf, encoding = 'utf8') {
35+
function toString (array, encoding = 'utf8') {
1836
if (encoding === 'utf8' || encoding === 'utf-8') {
19-
return utf8Decoder.decode(buf)
37+
return utf8Decoder.decode(array)
38+
}
39+
40+
if (encoding === 'ascii') {
41+
return uint8ArrayToAsciiString(array)
2042
}
2143

2244
const codec = names[encoding]
@@ -25,7 +47,7 @@ function toString (buf, encoding = 'utf8') {
2547
throw new Error('Unknown base')
2648
}
2749

28-
return codec.encode(buf)
50+
return codec.encode(array)
2951
}
3052

3153
module.exports = toString

0 commit comments

Comments
 (0)