Skip to content

Commit

Permalink
docs: added and improved jsdocs (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
onmax authored Apr 9, 2024
1 parent 182c3e4 commit b3ea65b
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 4 deletions.
14 changes: 14 additions & 0 deletions src/encode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ import {
} from './qrcode'
import { QrCodeDataType, type QrCodeGenerateData, type QrCodeGenerateOptions, type QrCodeGenerateResult } from './types'

/**
* Encodes the given data into a QR code format according to the given options.
* @param {QrCodeGenerateData} data - The data to encode, either as a string or an array of bytes. See {@link QrCodeGenerateData}.
* @param {QrCodeGenerateOptions} [options] - QR Code generation configuration options. Optional. See {@link QrCodeGenerateOptions}.
* @returns {QrCodeGenerateResult} The result of the QR code generation, including the QR code matrix. See {@link QrCodeGenerateResult}.
*/
export function encode(data: QrCodeGenerateData, options?: QrCodeGenerateOptions): QrCodeGenerateResult {
const {
ecc = 'L',
Expand Down Expand Up @@ -85,6 +91,14 @@ function addBorder(input: QrCodeGenerateResult, border = 1): QrCodeGenerateResul
return input
}

/**
* Returns the data value at a given coordinate in the QR code matrix.
* @param {boolean[][]} data - The QR code data matrix.
* @param {number} x - The x coordinate (column index) within the matrix.
* @param {number} y - The y coordinate (row index) within the matrix.
* @param {boolean} [defaults=false] - The default value to return if the coordinates are out of bounds. Optional.
* @returns {boolean} The data value at the given coordinate or the default value if out of bounds.
*/
export function getDataAt(data: boolean[][], x: number, y: number, defaults = false) {
if (x < 0 || y < 0 || x >= data.length || y >= data.length)
return defaults
Expand Down
15 changes: 12 additions & 3 deletions src/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ import type { QrCodeGenerateData, QrCodeGenerateOptions, QrCodeGenerateUnicodeOp
import { encode, getDataAt } from './encode'

/**
* Render QR Code with unicode `█`, `░`
* Renders a QR code as a string using the specified Unicode characters for dark(`█`) and light(`░`) modules.
* @param {QrCodeGenerateData} data - The data to encode into the QR code. See {@link QrCodeGenerateData}.
* @param {QrCodeGenerateUnicodeOptions} [options={}] - Rendering options, including characters for white and black modules. optional. See {@link QrCodeGenerateUnicodeOptions}.
* Returns {string} A string representing the QR code, with each module replaced by the specified Unicode character.
*/
export function renderUnicode(
data: QrCodeGenerateData,
Expand All @@ -20,7 +23,10 @@ export function renderUnicode(
}

/**
* Render QR Code with ANSI color for terminal
* Renders a QR code as a string suitable for display on terminals using ANSI background colours.
* @param {QrCodeGenerateData} data - The data to encode into the QR code. See {@link QrCodeGenerateData}.
* @param {QrCodeGenerateOptions} [options={}] - Options to render the QR code. optional. See {@link QrCodeGenerateOptions}.
* @returns {string} A string representing the QR code using ANSI colours, formatted for terminal display.
*/
export function renderANSI(
data: QrCodeGenerateData,
Expand All @@ -34,7 +40,10 @@ export function renderANSI(
}

/**
* Render QR Code with two rows into one line with unicode `▀`, `▄`, `█`, ` `
* Renders a QR code as a compact string using a combination of top half block(`▀`), bottom half block(`▄`), full block(`█`) and spaces to represent two lines in a single line.
* @param {QrCodeGenerateData} data - The data to encode into the QR code. See {@link QrCodeGenerateData}.
* @param {QrCodeGenerateOptions} [options={}] - Options to render the QR code in a compact form. optional. See {@link QrCodeGenerateOptions}.
* @returns {string} A string representing the QR code in a compact format, using Unicode block characters to combine two lines per line.
*/
export function renderUnicodeCompact(
data: QrCodeGenerateData,
Expand Down
6 changes: 5 additions & 1 deletion src/svg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ import { encode } from './encode'
import type { QrCodeGenerateData, QrCodeGenerateSvgOptions } from './types'

/**
* Render QR Code as SVG string
* Renders a QR code as an SVG string.
* The function converts the input data into a QR code and then generates an SVG representation using the specified colours and pixel sizes.
* @param {QrCodeGenerateData} data - The data to encode into the QR code. See {@link QrCodeGenerateData}.
* @param {QrCodeGenerateSvgOptions} [options={}] - Options to render the QR code in SVG format, including pixel size and colours for modules. optional. See {@link QrCodeGenerateSvgOptions}.
* @returns {string} An SVG string representing the QR code.
*/
export function renderSVG(
data: QrCodeGenerateData,
Expand Down
9 changes: 9 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,20 @@ export interface QrCodeGenerateResult {
}

export interface QrCodeGenerateInvertableOptions extends QrCodeGenerateOptions {
/**
* Adds the option to invert the colour scheme of the QR code in addition to the standard options.
*/
invert?: boolean
}

export interface QrCodeGenerateUnicodeOptions extends QrCodeGenerateInvertableOptions {
/**
* Character used to represent white modules in the QR code.
*/
whiteChar?: string
/**
* Character used to represent black modules in the QR code.
*/
blackChar?: string
}

Expand Down

0 comments on commit b3ea65b

Please sign in to comment.