diff --git a/README.md b/README.md index 300fbbb..e129434 100644 --- a/README.md +++ b/README.md @@ -83,7 +83,7 @@ To use as a [ES-module](https://developer.mozilla.org/en-US/docs/Web/JavaScript/ ```html diff --git a/docs/base64.js.html b/docs/base64.js.html index 22d4da7..b30a3e4 100644 --- a/docs/base64.js.html +++ b/docs/base64.js.html @@ -271,6 +271,33 @@

return base64.fromArrayBuffer(new TextEncoder().encode(str), urlMode); }; +/** + * Function to validate base64 + * @public + * @param {string} encoded - Base64 or Base64url encoded data + * @param {boolean} [urlMode] - If set to true, base64url will be expected + * @returns {boolean} - Valid base64/base64url? + */ +base64.validate = (encoded, urlMode) => { + + // Bail out if not string + if (!(typeof encoded === "string" || encoded instanceof String)) { + return false; + } + + // Go on validate + try { + if (urlMode) { + return /^[-A-Za-z0-9\-_]*$/.test(encoded); + } else { + return /^[-A-Za-z0-9+/]*={0,3}$/.test(encoded); + } + } catch (_e) { + return false; + } +}; + +base64.base64 = base64; export default base64; export { base64 }; diff --git a/docs/index.html b/docs/index.html index 1bb05ee..4e5191f 100644 --- a/docs/index.html +++ b/docs/index.html @@ -128,13 +128,14 @@


Probably the only JavaScript base64 library you'll ever need.

@hexagon/base64

-

Base64 and base64url to string or arraybuffer, and back. Works in Node, Deno or browser.

-

Node.js CI +

Encode, decode and validate base64 and base64url to string or arraybuffer, and back. Works in Node, Deno or browser.

+

Node.js CI Deno CI npm version NPM Downloads jsdelivr Codacy Badge MIT License