[qrcode-svg] Add types for qrcode-svg module

This commit adds the QRCode class, along with the associated types in a namespace of the same name. It also adds a couple tests to validate the main parts are as expected.
This commit is contained in:
Ferreira, Eric (ef183v)
2019-04-03 09:40:09 -04:00
parent 98ed28785e
commit f4d1db5ee8
4 changed files with 122 additions and 0 deletions
+68
View File
@@ -0,0 +1,68 @@
// Type definitions for qrcode-svg 1.0
// Project: https://github.com/papnkukn/qrcode-svg
// Definitions by: Eric Ferreira <https://github.com/ericbf>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
declare class QRCode {
constructor(content: string);
constructor(options: QRCode.Options);
qrcode: QRCode.Model;
options: QRCode.Options;
/**
* Generates QR Code as SVG image
* @param opt Set the container. Defaults to `{ container: "svg" }`.
* @return The svg string.
*/
svg(opt?: {
container: "svg" | "g" | "none"
}): string;
/**
* Writes QR Code image to a file. Requires `fs`.
* @param file The filename to write to
* @param callback The callback that will be called, possibly with an error, when done.
*/
save(file: string, callback: (error?: Error) => void): void;
}
declare namespace QRCode {
interface Options {
/** Default is `"#ffffff"`. */
background?: string;
/** Default is `"#000000"`. */
color?: string;
/** The actual content of the QR code */
content: string;
/** Default is `"M"`. */
ecl?: "L" | "M" | "H" | "Q";
/** Default is `256`. */
height?: number;
/** Default is `4`. */
padding?: number;
/** Default is `256`. */
width?: number;
/** Default is `4`. */
typeNumber?: number;
}
interface Model {
modules: boolean[][];
typeNumber: number;
errorCorrectLevel: number;
moduleCount: number;
dataCache: number[] | null;
dataList: Array<{
data: string;
mode: number;
parsedData: number[];
}>;
addData(data: string): void;
isDark(row: number, col: number): boolean;
getModuleCount(): number;
make(): void;
}
}
export = QRCode;
+24
View File
@@ -0,0 +1,24 @@
import * as QRCode from "qrcode-svg";
const qrCode = new QRCode(`sample-data`);
const assert = (result: boolean, messagePrefix: string, successMessage: string, errorMessage: string) => {
if (result) {
console.log(`${messagePrefix} ${successMessage}`);
} else {
console.error(`${messagePrefix} ${errorMessage}`);
}
};
assert(qrCode.options.padding === 4, "Default value for padding", "matches expected.", "does not match expected.");
assert(qrCode.options.width === 256, "Default value for width", "matches expected.", "does not match expected.");
assert(qrCode.options.height === 256, "Default value for height", "matches expected.", "does not match expected.");
assert(qrCode.options.typeNumber === 4, "Default value for typeNumber", "matches expected.", "does not match expected.");
assert(qrCode.options.background === "#ffffff", "Default value for background", "matches expected.", "does not match expected.");
assert(qrCode.options.color === "#000000", "Default value for color", "matches expected.", "does not match expected.");
assert(Array.isArray(qrCode.qrcode.modules) &&
qrCode.qrcode.modules.length === 0 ||
Array.isArray(qrCode.qrcode.modules[0]), "Modules in qrcode is", "a two-dimensional array", "not a two-dimensional array");
assert(typeof qrCode.svg() === "string", "QRCode", "generated an svg string", "did not generate an svg string");
+24
View File
@@ -0,0 +1,24 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": [
"es6",
"dom"
],
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"baseUrl": "../",
"typeRoots": [
"../"
],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.d.ts",
"qrcode-svg-tests.ts"
]
}
+6
View File
@@ -0,0 +1,6 @@
{
"extends": "dtslint/dt.json",
"rules": {
"unified-signatures": false
}
}