uint8arrays
Advanced tools
Comparing version
@@ -0,1 +1,5 @@ | ||
## [2.1.6](https://github.com/achingbrain/uint8arrays/compare/v2.1.5...v2.1.6) (2021-07-15) | ||
## [2.1.5](https://github.com/achingbrain/uint8arrays/compare/v2.1.4...v2.1.5) (2021-04-15) | ||
@@ -2,0 +6,0 @@ |
@@ -8,3 +8,3 @@ export = compare; | ||
*/ | ||
declare function compare(a: Uint8Array, b: Uint8Array): 0 | 1 | -1; | ||
declare function compare(a: Uint8Array, b: Uint8Array): 1 | 0 | -1; | ||
//# sourceMappingURL=compare.d.ts.map |
export = fromString; | ||
/** | ||
* @typedef {import('./util/bases').SupportedEncodings} SupportedEncodings | ||
*/ | ||
/** | ||
* Create a `Uint8Array` from the passed string | ||
* | ||
* Supports `utf8`, `utf-8` and any encoding supported by the multibase module. | ||
* Supports `utf8`, `utf-8`, `hex`, and any encoding supported by the multiformats module. | ||
* | ||
@@ -13,7 +16,8 @@ * Also `ascii` which is similar to node's 'binary' encoding. | ||
*/ | ||
declare function fromString(string: string, encoding?: SupportedEncodings): Uint8Array; | ||
declare function fromString(string: string, encoding?: bases.SupportedEncodings | undefined): Uint8Array; | ||
declare namespace fromString { | ||
export { SupportedEncodings }; | ||
} | ||
type SupportedEncodings = import('multibase/src/types').BaseName | 'utf8' | 'utf-8' | 'ascii' | undefined; | ||
import bases = require("./util/bases"); | ||
type SupportedEncodings = import('./util/bases').SupportedEncodings; | ||
//# sourceMappingURL=from-string.d.ts.map |
export = toString; | ||
/** | ||
* @typedef {import('./util/bases').SupportedEncodings} SupportedEncodings | ||
*/ | ||
/** | ||
* Turns a `Uint8Array` into a string. | ||
@@ -13,7 +16,8 @@ * | ||
*/ | ||
declare function toString(array: Uint8Array, encoding?: SupportedEncodings): string; | ||
declare function toString(array: Uint8Array, encoding?: bases.SupportedEncodings | undefined): string; | ||
declare namespace toString { | ||
export { SupportedEncodings }; | ||
} | ||
type SupportedEncodings = import('multibase/src/types').BaseName | 'utf8' | 'utf-8' | 'ascii' | undefined; | ||
import bases = require("./util/bases"); | ||
type SupportedEncodings = import('./util/bases').SupportedEncodings; | ||
//# sourceMappingURL=to-string.d.ts.map |
'use strict' | ||
const { encoding: getCodec } = require('multibase') | ||
const utf8Encoder = new TextEncoder() | ||
const bases = require('./util/bases') | ||
/** | ||
* @typedef {import('multibase/src/types').BaseName | 'utf8' | 'utf-8' | 'ascii' | undefined} SupportedEncodings | ||
* @typedef {import('./util/bases').SupportedEncodings} SupportedEncodings | ||
*/ | ||
/** | ||
* Interprets 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 | ||
*/ | ||
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. | ||
* Supports `utf8`, `utf-8`, `hex`, and any encoding supported by the multiformats module. | ||
* | ||
@@ -38,13 +21,12 @@ * Also `ascii` which is similar to node's 'binary' encoding. | ||
function fromString (string, encoding = 'utf8') { | ||
if (encoding === 'utf8' || encoding === 'utf-8') { | ||
return utf8Encoder.encode(string) | ||
} | ||
const base = bases[encoding] | ||
if (encoding === 'ascii') { | ||
return asciiStringToUint8Array(string) | ||
if (!base) { | ||
throw new Error(`Unsupported encoding "${encoding}"`) | ||
} | ||
return getCodec(encoding).decode(string) | ||
// add multibase prefix | ||
return base.decoder.decode(`${base.prefix}${string}`) | ||
} | ||
module.exports = fromString |
{ | ||
"name": "uint8arrays", | ||
"version": "2.1.5", | ||
"version": "2.1.6", | ||
"description": "Utility functions to make dealing with Uint8Arrays easier", | ||
@@ -30,3 +30,4 @@ "main": "index.js", | ||
"dist/*.map", | ||
"dist/*.js" | ||
"dist/*.js", | ||
"util/*.js" | ||
], | ||
@@ -40,3 +41,3 @@ "repository": { | ||
"test": "aegir test", | ||
"lint": "aegir lint", | ||
"lint": "aegir ts -p check && aegir lint", | ||
"release": "aegir release", | ||
@@ -49,6 +50,7 @@ "release-minor": "aegir release --type minor", | ||
"dependencies": { | ||
"multibase": "^4.0.1" | ||
"multiformats": "^9.4.1" | ||
}, | ||
"devDependencies": { | ||
"aegir": "^33.1.0" | ||
"aegir": "^34.0.2", | ||
"util": "^0.12.4" | ||
}, | ||
@@ -55,0 +57,0 @@ "eslintConfig": { |
@@ -88,3 +88,3 @@ # Uint8Arrays <!-- omit in toc --> | ||
Supports `utf8` and any of the [multiformats encodings](https://www.npmjs.com/package/multibase#supported-encodings-see-srcconstantsjs). | ||
Supports `utf8` and any of the [multibase encodings](https://github.com/multiformats/multibase/blob/master/multibase.csv) as implemented by the [multiformats module](https://www.npmjs.com/package/multiformats). | ||
@@ -106,3 +106,3 @@ #### Example | ||
Supports `utf8` and any of the [multiformats encodings](https://www.npmjs.com/package/multibase#supported-encodings-see-srcconstantsjs). | ||
Supports `utf8` and any of the [multibase encodings](https://github.com/multiformats/multibase/blob/master/multibase.csv) as implemented by the [multiformats module](https://www.npmjs.com/package/multiformats). | ||
@@ -109,0 +109,0 @@ #### Example |
'use strict' | ||
const { encoding: getCodec } = require('multibase') | ||
const utf8Decoder = new TextDecoder('utf8') | ||
const bases = require('./util/bases') | ||
/** | ||
* @typedef {import('multibase/src/types').BaseName | 'utf8' | 'utf-8' | 'ascii' | undefined} SupportedEncodings | ||
* @typedef {import('./util/bases').SupportedEncodings} SupportedEncodings | ||
*/ | ||
/** | ||
* 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 | ||
*/ | ||
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. | ||
@@ -37,13 +21,12 @@ * | ||
function toString (array, encoding = 'utf8') { | ||
if (encoding === 'utf8' || encoding === 'utf-8') { | ||
return utf8Decoder.decode(array) | ||
} | ||
const base = bases[encoding] | ||
if (encoding === 'ascii') { | ||
return uint8ArrayToAsciiString(array) | ||
if (!base) { | ||
throw new Error(`Unsupported encoding "${encoding}"`) | ||
} | ||
return getCodec(encoding).encode(array) | ||
// strip multibase prefix | ||
return base.encoder.encode(array).substring(1) | ||
} | ||
module.exports = toString |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
17782
5.62%25
4.17%294
16.21%2
100%+ Added
+ Added
- Removed
- Removed
- Removed