11'use strict'
22
3- const { names } = require ( 'multibase/src/constants ' )
3+ const { encoding : getCodec } = require ( 'multibase' )
44const { TextEncoder } = require ( 'web-encoding' )
55const utf8Encoder = new TextEncoder ( )
66
77/**
8- * Interperets each character in a string as a byte and
8+ * @typedef {import('multibase/src/types').BaseName } BaseName
9+ */
10+
11+ /**
12+ * Interprets each character in a string as a byte and
913 * returns a Uint8Array of those bytes.
1014 *
11- * @param {String } string The string to turn into an array
15+ * @param {string } string - The string to turn into an array
1216 * @returns {Uint8Array }
1317 */
1418function asciiStringToUint8Array ( string ) {
@@ -28,10 +32,9 @@ function asciiStringToUint8Array (string) {
2832 *
2933 * Also `ascii` which is similar to node's 'binary' encoding.
3034 *
31- * @param {String } string
32- * @param {String } [encoding=utf8] utf8, base16, base64, base64urlpad, etc
35+ * @param {string } string
36+ * @param {BaseName | 'utf8' | 'utf-8' | 'ascii' } [encoding=utf8] - utf8, base16, base64, base64urlpad, etc
3337 * @returns {Uint8Array }
34- * @see {@link https://www.npmjs.com/package/multibase|multibase } for supported encodings other than `utf8`
3538 */
3639function fromString ( string , encoding = 'utf8' ) {
3740 if ( encoding === 'utf8' || encoding === 'utf-8' ) {
@@ -42,13 +45,7 @@ function fromString (string, encoding = 'utf8') {
4245 return asciiStringToUint8Array ( string )
4346 }
4447
45- const codec = names [ encoding ]
46-
47- if ( ! codec ) {
48- throw new Error ( 'Unknown base' )
49- }
50-
51- return codec . decode ( string )
48+ return getCodec ( encoding ) . decode ( string )
5249}
5350
5451module . exports = fromString
0 commit comments