npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@bytarch/qrcode

v1.0.2

Published

A simple JavaScript package to generate QR codes that can output PNG files, Data URLs, and Buffers.

Downloads

20

Readme

QR Code Generator

A simple JavaScript package to generate QR codes that can output PNG files, Data URLs, and Buffers.

Installation

You can install the package using npm:

npm install @bytarch/qrcode

Code

const { QRCreation, QR64, QRBuffer } = require('@bytarch/qrcode'); // Use .js extension

// Generate a QR code and save it as a PNG file
QRCreation('HELLO WORLD', 'generated_code.png'); // Added .png extension to the filename

// Generate a QR code as a data URL
QR64('HELLO WORLD')
    .then(url => {
        console.log('QR Code Data URL:', url);
    })
    .catch(err => {
        console.error('Error generating QR Code Data URL:', err);
    }); // Added error handling

// Generate a QR code as a buffer
QRBuffer('HELLO WORLD')
    .then(buffer => {
        if (buffer) { // Check if buffer is defined
            // Save the buffer as a file (this part is removed)
            console.log('QR Code buffer generated but not saved.');
        } else {
            console.error('No buffer returned for QR Code');
        }
    })
    .catch(err => {
        console.error('Error generating QR Code Buffer:', err);
    }); // Added error handling

Usage

1. Importing the Package

To use the QR code generator, require it in your JavaScript file:

const { QRCreation, QR64, QRBuffer } = require('@bytarch/qrcode');

2. Generating a QR Code

A. Save QR Code as PNG

To generate and save a QR code as a PNG file, use the QRCreation function:

QRCreation(text, [filename='qrcode.png'], [options={}]);
  • Parameters:

    • text (string): The text to encode in the QR code.
    • filename (string, optional): The name of the output file (defaults to 'qrcode.png').
    • options (object, optional): Optional QR code generation settings.
  • Example:

const text = 'https://bytarch.netlify.app!';
QRCreation(text, 'bytarch-qr.png');

B. Generate QR Code as a Data URL

To generate a QR code as a Data URL (suitable for embedding in HTML or displaying as an image), use the QR64 function:

QR64(text, [options={}]);
  • Parameters:

    • text (string): The text to encode in the QR code.
    • options (object, optional): Optional QR code generation settings.
  • Returns: A Promise that resolves to a string containing the Data URL of the generated QR code.

  • Example:

const dataUrl = await QR64('https://bytarch.netlify.app!');
console.log(dataUrl); // Outputs the Data URL of the QR code

C. Generate QR Code as a Buffer

To generate a QR code as a Buffer (for file writing or other manipulation), use the QRBuffer function:

QRBuffer(text, [options={}]);
  • Parameters:

    • text (string): The text to encode in the QR code.
    • options (object, optional): Optional QR code generation settings.
  • Returns: A Promise that resolves to a Buffer containing the generated QR code image.

  • Example:

const buffer = await QRBuffer('https://bytarch.netlify.app!');
// You can now manipulate the buffer, save it to a file, etc.

Helper Functions

1. ensurePngExtension

Ensures that the provided filename has a .png extension. This function is used internally in the QRCreation function.

const ensurePngExtension = (filename) => filename.endsWith('.png') ? filename : `${filename}.png`;
  • Parameters:

    • filename (string): The filename to check.
  • Returns: The filename with a .png extension if necessary.

Error Handling

Each function includes basic error handling that logs any errors encountered during QR code generation to the console. You can further enhance error handling as needed in your application.

Example

Here’s an example of generating a QR code and saving it as a PNG file:

const { QRCreation } = require('@bytarch/qrcode');

const text = 'Visit bytarch!';
QRCreation(text, 'bytarch-qr.png');

This will create a QR code that encodes the text "Visit bytarch!" and saves it as bytarch-qr.png.

GitHub Repository

You can find the source code for this package on GitHub:

GitHub - QR Code Generator

Feel free to contribute or raise issues on the repository.