base-endecoder
v1.0.0
Published
An ES module providing functions for converting (encoding and decoding) numbers and data to/from any base (input any charset). Works in Node.js, Deno and the browser. Includes functions for base32, base64 and base64url.
Downloads
5
Maintainers
Readme
base-endecoder
Description
An ES module providing functions for converting (encoding and decoding) numbers and data to/from any base (input any charset). Works in Node.js, Deno and the browser. Includes functions for base32, base64 and base64url.
But for base64 endecoding your target platform should have a native sollution e.g. the browser's atob
and btoa
functions or Node.js's Buffer.from(bytes).toString('base64')
and Buffer.from(base64, 'base64')
which will provide much higher performance than these pure JavaScript functions. Especially when working with huge amounts of data, but for shorter strings etc. it doesn't matter much.
Hence I named the pure JavaScript base64 functions encode_base64_slow
and decode_base64_slow
to make this pretty clear. But I've also implemented the faster versions which is using the native base64 functions for each platform.
Example
import * as baseEndecoder from 'base-endecoder'
const log = console.log
function textToData(text) {return new TextEncoder().encode(text)}
function dataToText(data) {return new TextDecoder().decode(data)}
const strangeBase = 'ÆØÅX'
const strangeNumber = baseEndecoder.xFromBase10(123456, strangeBase)
log(strangeNumber)
log(baseEndecoder.xToBase10(strangeNumber, strangeBase))
const strangeDataString = baseEndecoder.encode_baseX(textToData('Hello strange world!'), strangeBase)
log(strangeDataString)
log(dataToText(baseEndecoder.decode_baseX(strangeDataString, strangeBase)))
Console output from example:
ØXÅÆÅØÆÆÆ
123456
ØÆÅÆØÅØØØÅXÆØÅXÆØÅXXÆÅÆÆØXÆXØXØÆØXÆÅØÅÆØØÅXÅØÅØXØÅØØÆÅÆÆØXØXØÅXXØXÆÅØÅXÆØÅØÆÆÅÆØ
Hello strange world!
Supported platforms
- Node.js
- Deno
- A proper browser (Chromium based usually) or just Babel the shit out of it if you need legacy support.
How to use
Install using NPM
npm i base-endecoder
Import the ES module into Node.js
import * as baseEndecoder from 'base-endecoder'
Got problems using ES modules? Click here or read this.
Import the ES module into the browser or Deno
You use the same syntax as above, but for that to work you need to use import maps.
See the provided example for a demonstration on how to get it running on all platforms.
Funding
If you find this useful then please consider helping me out (I'm jobless and sick). For more information visit my GitHub sponsors page, my profile or my simple website.
Auto-generated API documentation (from JSDoc)
base-endecoder
An ES module providing functions for converting (encoding and decoding) numbers and data to/from any base (input any charset). Works in Node.js, Deno and the browser. Includes functions for base32, base64 and base64url.
- base-endecoder
- .xToBase10(number, charset) ⇒ number
- .xFromBase10(number, charset) ⇒ string
- .decode_baseX(string, charset, [paddingChar]) ⇒ Uint8Array
- .encode_baseX(data, charset, [paddingChar], [groupSize]) ⇒ string
- .encode_base32(data, [padding]) ⇒ string
- .encode_base64_slow(data, [padding]) ⇒ string
- .encode_base64url(data, [padding]) ⇒ string
- .decode_base32(base32) ⇒ Uint8Array
- .decode_base64_slow(base64) ⇒ Uint8Array
- .decode_base64url(base64url) ⇒ Uint8Array
- .encode_base64_fast(data) ⇒ string
- .decode_base64_fast(base64) ⇒ Uint8Array
baseEndecoder.xToBase10(number, charset) ⇒ number
Convert base X (as in any base) to base 10 (our number system).
Kind: static method of base-endecoder
Returns: number - The number as base 10.
| Param | Type | Description | | --- | --- | --- | | number | string | Base X as a string using the supplied charset. | | charset | string | The charset used by this base. E.g. "01" for base 2 (binary) or "0123456789ABCDEF" for base 16 (hex). |
baseEndecoder.xFromBase10(number, charset) ⇒ string
Convert a base 10 number (our number system) to base X (as in any base).
Kind: static method of base-endecoder
Returns: string - The number as the base defined in the charset.
| Param | Type | Description | | --- | --- | --- | | number | number | The number to convert. | | charset | string | The charset defining the base to use. E.g. "01" for base 2 (binary). |
baseEndecoder.decode_baseX(string, charset, [paddingChar]) ⇒ Uint8Array
Decodes a string of data in the base defined in the charset into binary data. This is only possible when the base is a power of two. An optional padding character can be supplied if used in the encoding of the string.
Kind: static method of base-endecoder
Returns: Uint8Array - A Uint8Array array containing the data.
| Param | Type | Description | | --- | --- | --- | | string | string | The string with data stored using this base's charset. | | charset | string | The charset which defines the base to use. | | [paddingChar] | string | An optional padding character to supply if the encoding scheme makes use of it. |
baseEndecoder.encode_baseX(data, charset, [paddingChar], [groupSize]) ⇒ string
Encodes any binary data into a string of the base defined in the charset. This is only possible when the base is a power of two. An optional padding character can be applied to the output if the string length is not aligned to the supplied groupSize
.
Kind: static method of base-endecoder
Returns: string - A string where the data is converted to this base.
| Param | Type | Description | | --- | --- | --- | | data | ArrayBuffer | ArrayBufferView | Buffer | Array.<number> | The data to convert, an ArrayBuffer, TypedArray, DataView, Node.js Buffer or an Array with unsigned bytes. | | charset | string | The charset which defines the base to use. | | [paddingChar] | string | An optional padding character. | | [groupSize] | number | Use the padding character if the string length is not aligned to this value. |
baseEndecoder.encode_base32(data, [padding]) ⇒ string
Encode data to base32 using the encode_baseX
function in this module.
Kind: static method of base-endecoder
Returns: string - base32.
| Param | Type | Default | Description | | --- | --- | --- | --- | | data | ArrayBuffer | ArrayBufferView | Buffer | Array.<number> | | The data to convert, an ArrayBuffer, TypedArray, DataView, Node.js Buffer or an Array with unsigned bytes. | | [padding] | boolean | true | Whether to use the standard padding scheme or not. |
baseEndecoder.encode_base64_slow(data, [padding]) ⇒ string
Encode data to base64 using the encode_baseX
function in this module. This is not recommended when it comes to performance since your platform have faster native functions for working with base64. Hence I also provide encode_base64_fast
which uses the faster native function on your platform. This function is mostly provided as an example on how to use encode_baseX
and also to be able to test it on base64 to see that it works as expected.
Kind: static method of base-endecoder
Returns: string - base64.
| Param | Type | Default | Description | | --- | --- | --- | --- | | data | ArrayBuffer | ArrayBufferView | Buffer | Array.<number> | | The data to convert, an ArrayBuffer, TypedArray, DataView, Node.js Buffer or an Array with unsigned bytes. | | [padding] | boolean | true | Whether to use padding or not, it's standard to use it. |
baseEndecoder.encode_base64url(data, [padding]) ⇒ string
Encode data to base64url using the encode_baseX
function in this module.
Kind: static method of base-endecoder
Returns: string - base64url.
| Param | Type | Default | Description | | --- | --- | --- | --- | | data | ArrayBuffer | ArrayBufferView | Buffer | Array.<number> | | The data to convert, an ArrayBuffer, TypedArray, DataView, Node.js Buffer or an Array with unsigned bytes. | | [padding] | boolean | true | Whether to use the standard padding scheme or not. |
baseEndecoder.decode_base32(base32) ⇒ Uint8Array
Decode base32 to binary data using the decode_baseX
function in this module.
Kind: static method of base-endecoder
Returns: Uint8Array - A Uint8Array array containing the data.
| Param | Type | Description | | --- | --- | --- | | base32 | string | The base32 string to decode. |
baseEndecoder.decode_base64_slow(base64) ⇒ Uint8Array
Decode base64 to binary data using the decode_baseX
function in this module. This is not recommended when it comes to performance since your platform have faster native functions for working with base64. Hence I also provide decode_base64_fast
which uses the faster native function on your platform. This function is mostly provided as an example on how to use decode_baseX
and also to be able to test it on base64 to see that it works as expected.
Kind: static method of base-endecoder
Returns: Uint8Array - A Uint8Array array containing the data.
| Param | Type | Description | | --- | --- | --- | | base64 | string | The base64 string to decode. |
baseEndecoder.decode_base64url(base64url) ⇒ Uint8Array
Decode base64url to binary data using the decode_baseX
function in this module.
Kind: static method of base-endecoder
Returns: Uint8Array - A Uint8Array array containing the data.
| Param | Type | Description | | --- | --- | --- | | base64url | string | The base64url string to decode. |
baseEndecoder.encode_base64_fast(data) ⇒ string
Encode data to base64 using the native function for that on your platform. In Deno and the browser this is btoa
, in Node.js this is Buffer.from(data).toString('base64')
.
Kind: static method of base-endecoder
Returns: string - base64.
| Param | Type | Description | | --- | --- | --- | | data | ArrayBuffer | ArrayBufferView | Buffer | Array.<number> | The data to convert, an ArrayBuffer, TypedArray, DataView, Node.js Buffer or an Array with unsigned bytes. |
baseEndecoder.decode_base64_fast(base64) ⇒ Uint8Array
Decode base64 to binary data using the native function for that on your platform. In Deno and the browser this is atob
, in Node.js this is Buffer.from(base64, 'base64')
.
Kind: static method of base-endecoder
Returns: Uint8Array - A Uint8Array array containing the data.
| Param | Type | Description | | --- | --- | --- | | base64 | string | The base64 string to decode. |
End of readme
To get back up click here (only works on GitHub?) or find your own way.