1.0.2 • Published 3 years ago

@youngmayor/base64 v1.0.2

Weekly downloads
-
License
ISC
Repository
github
Last release
3 years ago

Base64 Manager

npm version install size npm downloads

A base64 management tool

Table of Contents


Features


  • Encode string to base64
  • Decode a base64 encoded string to any of the below listed formats
    • Blob
    • ObjectURL
  • Decode a base64 object to the appropriate format and download it directly in browser
  • A uniform API for managing base64 objects of Image, Text and PDF

Browser Support


ChromeFirefoxSafariOperaEdgeIE
Latest ✔Latest ✔Latest ✔Latest ✔Latest ✔11 ✔

Installing


  • Using npm:

    $ npm install @youngmayor/base64
  • Using bower:

    $ bower install @youngmayor/base64
  • Using yarn:

    $ yarn add @youngmayor/base64

Importing


  • In a node based application, you can import the package using commonJS as shown below

    const base64 = require('@youngmayor/base64');
    
    // or 
    
    import bas64 from '@youngmayor/base64';
  • It can also be linked to using any of the below CDNs

    <script src="https://cdn.jsdelivr.net/npm/@youngmayor/base64@latest" async></script>
    
    <!-- or -->
    
    <script src="https://unpkg.com/@youngmayor/base64" async></script>
<!-- the package can now be accessed via window.base64 -->
```

Usage


Encoding to base64

  • String

    base64 has a very simple API for encoding a string to base64 using the encodeString() method.

    Example

    const encoded_text = base64.encodeString('Hello World')
    console.log(encoded_text);
    
    // Result: SGVsbG8gV29ybGQ=

Managers

What are Managers?


Managers are Classes that expose you to a Unified API for handling the base64 object in it's right decode type...

base64 is inbuilted with the following managers:

Invoking Managers


Invoking a Manager is easy. Call the Manager name on the base64 object with the base64 encoded data

Example

const stringmanager = base64.managePDF(encoded_text);

Some managers require a second parameter: the type of the data. See the documentation the respective manager.

This creates a manager object for that file type. All managers extend the Base Manager class which exposes the below methods

  • toBlob(): Blob: Convert the Base64 object to a BLOB (Binary large object)

    ...
    blobData = stringmanager.toBlob();
    console.log(blobData);
    
    // Result: Blob Object
  • toObjectURL(): string: Convert the Base64 object to a URL.createObjectURL

    // ...
    url = stringmanager.toObjectURL();
    console.log(url)
    
    // Result: blob:null/8d20cde9-db64-49b2-8872-98ada802e85b
  • toDataURL(): string: Convert the encoded file to a DataURL

    // ...
    
    dataURL = stringmanager.toDataURL();
    console.log(dataURL)
    
    // Result: data:text/plain;base64,SGVsbG8gV29ybGQ=
  • download(filename): void: Download the encoded file

    PARAMETERS


    • filename: The name to save the downloaded file with
      // ...
      stringmanager.download('example-download.txt');
      // ...
  • open(): void: Open the decoded data on a new tab

    // ...
    stringmanager.open();
    // ...

Available Managers


The following Managers currently exist

Image Manager

  • Instantiation

    const base64 = require('@youngmayor/base64');
    
    const imagemanager = base64.manageImage(encoding, image_type)
    // image_type must be one of [ bmp, gif, vnd.microsofticon, jpeg, png, svg+xml, tiff, webp ]

PDF Manager

  • Instantiation

    const base64 = require('@youngmayor/base64');
    
    const pdfmanager = base64.managePDF(encoding)

String Manager

  • Instantiation

    const base64 = require('@youngmayor/base64');
    
    const stringmanager = base64.manageString(encoding, string_type)
    // string_type must be one of [ calendar, css, csv, html, javascript, plain, xml ]
  • Methods

    • decodeString(): Decode the string

          let decoded = stringmanager.decodeString(); 
          console.log(decoded)
      
          // Result: Hello World

Credits

Meyoron Aghogho (YoungMayor).