@loxjs/node-base64
is a Node.js module that provides URL-safe Base64 encoding and decoding. It follows the RFC 4648 Spec for Base64 encoding, where '+' is encoded as '-' and '/' is encoded as '_'. Additionally, it can remove padding characters '=' for a more compact representation.
npm install @loxjs/node-base64
Or with yarn:
yarn add @loxjs/node-base64
This module exports three main functions: encode
, decode
, and validate
. You can use these functions to handle URL-safe Base64 strings within your Node.js applications.
const base64 = require('@loxjs/node-base64');
// Encode a string to Base64
const encodedString = base64.encode('Hello World!');
console.log(encodedString); // Output: SGVsbG8gV29ybGQh (without padding '=' characters)
const base64 = require('@loxjs/node-base64');
// Encode a string to Base64 and keep padding '=' characters
const encodedStringWithEq = base64.encode('Hello World!', true);
console.log(encodedStringWithEq); // Output: SGVsbG8gV29ybGQh== (with padding '=' characters)
const base64 = require('@loxjs/node-base64');
// Decode a URL Safe Base64 string
const decodedString = base64.decode('SGVsbG8gV29ybGQh');
console.log(decodedString); // Output: Hello World!
const base64 = require('@loxjs/node-base64');
// Validate a URL Safe Base64 string
const isValid = base64.validate('SGVsbG8gV29ybGQh');
console.log(isValid); // Output: true
Encodes a buffer or string to URL Safe Base64.
-
str
(String|Buffer): The string or buffer to encode. -
witheq
(Boolean): Optional. Iftrue
, retains padding '=' characters. Defaults tofalse
.
Decodes a URL Safe Base64 string to its original representation.
-
str
(String): The URL Safe Base64 string to decode.
Validates whether a string is a URL Safe Base64 encoded string.
-
str
(String): The string to validate.
Contributions to @loxjs/node-base64
are welcome! Please ensure that your contributions adhere to the following guidelines:
- Write clear, readable, and maintainable code.
- Follow existing coding styles and practices.
- Write meaningful commit messages.
- Update the documentation accordingly.
For more detailed information, please read the contributing guide.
Enjoy using @loxjs/node-base64
!