Skip to content

Commit

Permalink
Dependency update + typo
Browse files Browse the repository at this point in the history
  • Loading branch information
Hexagon committed Jul 1, 2022
1 parent a7a8ec5 commit 71de63b
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 75 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -83,7 +83,7 @@ To use as a [ES-module](https://developer.mozilla.org/en-US/docs/Web/JavaScript/

```html
<script type="module">
import base64 from "https://cdn.jsdelivr.net/npm/@hexagon/base64@/dist/base64.min.mjs";
import base64 from "https://cdn.jsdelivr.net/npm/hexagon/base64@1/dist/base64.min.mjs";
// ... see usage section ...
</script>
Expand Down
27 changes: 27 additions & 0 deletions docs/base64.js.html
Expand Up @@ -271,6 +271,33 @@ <h1 class="page-title">
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 };</code></pre>
</article>
Expand Down
38 changes: 23 additions & 15 deletions docs/index.html
Expand Up @@ -128,13 +128,14 @@ <h1>
<br>Probably the only JavaScript base64 library you'll ever need.<br>
</p>
<h1>@hexagon/base64</h1>
<p>Base64 and base64url to string or arraybuffer, and back. Works in Node, Deno or browser.</p>
<p><a href="https://github.com/Hexagon/base64/actions/workflows/node.js.yml"><img src="https://github.com/Hexagon/base64/actions/workflows/node.js.yml/badge.svg" alt="Node.js CI"></a>
<p>Encode, decode and validate base64 and base64url to string or arraybuffer, and back. Works in Node, Deno or browser.</p>
<p><a href="https://github.com/Hexagon/base64/actions/workflows/node.js.yml"><img src="https://github.com/Hexagon/base64/actions/workflows/node.js.yml/badge.svg" alt="Node.js CI"></a> <a href="https://github.com/Hexagon/base64/actions/workflows/deno.yml"><img src="https://github.com/Hexagon/base64/actions/workflows/deno.yml/badge.svg" alt="Deno CI"></a>
<a href="https://badge.fury.io/js/@hexagon%2Fbase64"><img src="https://badge.fury.io/js/@hexagon%2Fbase64.svg" alt="npm version"></a> <a href="https://www.npmjs.org/package/@hexagon/base64"><img src="https://img.shields.io/npm/dm/@hexagon/base64.svg" alt="NPM Downloads"></a> <a href="https://www.jsdelivr.com/package/npm/@hexagon/base64"><img src="https://data.jsdelivr.com/v1/package/npm/@hexagon/base64/badge?style=rounded" alt="jsdelivr"></a> <a href="https://www.codacy.com/gh/Hexagon/base64/dashboard?utm_source=github.com&amp;utm_medium=referral&amp;utm_content=Hexagon/base64&amp;utm_campaign=Badge_Grade"><img src="https://app.codacy.com/project/badge/Grade/4978bdbf495941c087ecb32b120f28ff" alt="Codacy Badge"></a>
<a href="https://github.com/Hexagon/base64/blob/master/LICENSE"><img src="https://img.shields.io/badge/license-MIT-blue.svg" alt="MIT License"></a></p>
<ul>
<li>Supports regular base64, as well as base64url</li>
<li>Convert to/from string or arraybuffer</li>
<li>Validate / identify base64 and base64url</li>
<li>Works in Node.js &gt;=4.0 (both require and import).</li>
<li>Works in Deno &gt;=1.16.</li>
<li>Works in browsers as standalone, UMD or ES-module.</li>
Expand Down Expand Up @@ -180,7 +181,7 @@ <h4>CDN</h4>
</code></pre>
<p>To use as a <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules">ES-module</a></p>
<pre class="prettyprint source lang-html"><code>&lt;script type=&quot;module&quot;>
import base64 from &quot;https://cdn.jsdelivr.net/npm/@hexagon/base64@/dist/base64.min.mjs&quot;;
import base64 from &quot;https://cdn.jsdelivr.net/npm/hexagon/base64@1/dist/base64.min.mjs&quot;;

// ... see usage section ...
&lt;/script>
Expand Down Expand Up @@ -208,21 +209,31 @@ <h3>Examples</h3>
const example2dec = base64.toString(&quot;SGVsbMO2IFfDtnJsZCwgaG93IGFyZSB5b3UgZG9pbmcgdG9kYXk_IQ&quot;, true);
console.log(example2dec);
// > Hellö Wörld, how are you doing today?!

// Check if string is base64url (setting the second parameter to true validates base64url)
const example3valid = base64.validate(&quot;SGVsbMO2IFfDtnJsZCwgaG93IGFyZSB5b3UgZG9pbmcgdG9kYXk_IQ&quot;, true);
console.log(example3valid);
// > true

// Check if string is base64
const example4valid = base64.validate(&quot;SGVsbMO2IFfDtnJsZCwgaG93IGFyZSB5b3UgZG9pbmcgdG9kYXk_IQ&quot;);
console.log(example4valid);
// > false

// Check if string is base64
const example5valid = base64.validate(&quot;SGVsbMO2IFfDtnJsZCwgaG93IGFyZSB5b3UgZG9pbmcgdG9kYXk/IQ==&quot;);
console.log(example5valid);
// > true

</code></pre>
<h3>Full API</h3>
<p>The library encodes and decodes base64/base64url to and from ArrayBuffers</p>
<ul>
<li>
<p><strong>fromArrayBuffer(buffer)</strong> - Encodes <code>ArrayBuffer</code> into base64 string</p>
</li>
<li>
<p><strong>toArrayBuffer(str)</strong> - Decodes base64 string to <code>ArrayBuffer</code></p>
</li>
<li>
<p><strong>fromArrayBuffer(buffer, true)</strong> - Encodes <code>ArrayBuffer</code> into base64url string</p>
<p><strong>fromArrayBuffer(buffer, urlMode)</strong> - Encodes <code>ArrayBuffer</code> into base64 or base64url if urlMode (optional) is true</p>
</li>
<li>
<p><strong>toArrayBuffer(str, true)</strong> - Decodes base64url string to <code>ArrayBuffer</code></p>
<p><strong>toArrayBuffer(str, urlMode)</strong> - Decodes base64url string (or base64url string if urlMode is true) to <code>ArrayBuffer</code></p>
</li>
<li>
<p><strong>fromString(str)</strong> - Encodes <code>String</code> into base64 string</p>
Expand All @@ -231,10 +242,7 @@ <h3>Full API</h3>
<p><strong>toString(str)</strong> - Decodes base64 string to <code>String</code></p>
</li>
<li>
<p><strong>fromString(buffer, true)</strong> - Encodes <code>String</code> into base64url string</p>
</li>
<li>
<p><strong>toString(str, true)</strong> - Decodes base64url string to <code>String</code></p>
<p><strong>validate(str, urlMode)</strong> - Returns true if <code>String</code> str is valid base64/base64 dependending on urlMode</p>
</li>
</ul>
<h2>Contributing</h2>
Expand Down
116 changes: 58 additions & 58 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "@hexagon/base64",
"version": "1.1.21",
"version": "1.1.22",
"description": "Base64 and base64url to string or arraybuffer, and back. Node, Deno or browser.",
"author": "Hexagon <github.com/hexagon>",
"contributors": [{
Expand Down

0 comments on commit 71de63b

Please sign in to comment.