uint8arrays
Advanced tools
Comparing version 1.0.0 to 1.1.0
@@ -0,1 +1,11 @@ | ||
<a name="1.1.0"></a> | ||
# [1.1.0](https://github.com/achingbrain/uint8arrays/compare/v1.0.0...v1.1.0) (2020-08-07) | ||
### Features | ||
* support ascii encoding ([#2](https://github.com/achingbrain/uint8arrays/issues/2)) ([098724d](https://github.com/achingbrain/uint8arrays/commit/098724d)) | ||
<a name="1.0.0"></a> | ||
@@ -2,0 +12,0 @@ # [1.0.0](https://github.com/achingbrain/uint8arrays/compare/v0.0.2...v1.0.0) (2020-07-31) |
@@ -8,4 +8,25 @@ 'use strict' | ||
/** | ||
* Interperets each character in a string as a byte and | ||
* returns a Uint8Array of those bytes. | ||
* | ||
* @param {String} string The string to turn into an array | ||
* @returns {Uint8Array} | ||
*/ | ||
function asciiStringToUint8Array (string) { | ||
const array = new Uint8Array(string.length) | ||
for (let i = 0; i < string.length; i++) { | ||
array[i] = string.charCodeAt(i) | ||
} | ||
return array | ||
} | ||
/** | ||
* Create a `Uint8Array` from the passed string | ||
* | ||
* Supports `utf8`, `utf-8` and any encoding supported by the multibase module. | ||
* | ||
* Also `ascii` which is similar to node's 'binary' encoding. | ||
* | ||
* @param {String} string | ||
@@ -21,2 +42,6 @@ * @param {String} [encoding=utf8] utf8, base16, base64, base64urlpad, etc | ||
if (encoding === 'ascii') { | ||
return asciiStringToUint8Array(string) | ||
} | ||
const codec = names[encoding] | ||
@@ -23,0 +48,0 @@ |
{ | ||
"name": "uint8arrays", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "Utility functions to make dealing with Uint8Arrays easier", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -96,2 +96,3 @@ # Uint8Arrays <!-- omit in toc --> | ||
console.info(fromString('AAECA6q7zA', 'base64')) // Uint8Array[0, 1 ... | ||
console.info(fromString('01234', 'ascii')) // Uint8Array[48, 49 ... | ||
``` | ||
@@ -113,2 +114,3 @@ | ||
console.info(toString(Uint8Array.from([0, 1, 2...]), 'base64')) // 'AAECA6q7zA' | ||
console.info(toString(Uint8Array.from([48, 49, 50...]), 'ascii')) // '01234' | ||
``` |
@@ -8,7 +8,25 @@ 'use strict' | ||
/** | ||
* Turns a Uint8Array of bytes into a string with each | ||
* character being the char code of the corresponding byte | ||
* | ||
* @param {Uint8Array} array The array to turn into a string | ||
* @returns {String} | ||
*/ | ||
function uint8ArrayToAsciiString (array) { | ||
let string = '' | ||
for (let i = 0; i < array.length; i++) { | ||
string += String.fromCharCode(array[i]) | ||
} | ||
return string | ||
} | ||
/** | ||
* Turns a `Uint8Array` into a string. | ||
* | ||
* Supports `utf8` and any encoding supported by the multibase module | ||
* Supports `utf8`, `utf-8` and any encoding supported by the multibase module. | ||
* | ||
* @param {Uint8Array} buf The array to turn into a string | ||
* Also `ascii` which is similar to node's 'binary' encoding. | ||
* | ||
* @param {Uint8Array} array The array to turn into a string | ||
* @param {String} [encoding=utf8] The encoding to use | ||
@@ -18,7 +36,11 @@ * @returns {String} | ||
*/ | ||
function toString (buf, encoding = 'utf8') { | ||
function toString (array, encoding = 'utf8') { | ||
if (encoding === 'utf8' || encoding === 'utf-8') { | ||
return utf8Decoder.decode(buf) | ||
return utf8Decoder.decode(array) | ||
} | ||
if (encoding === 'ascii') { | ||
return uint8ArrayToAsciiString(array) | ||
} | ||
const codec = names[encoding] | ||
@@ -30,5 +52,5 @@ | ||
return codec.encode(buf) | ||
return codec.encode(array) | ||
} | ||
module.exports = toString |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
9257
166
115