From f4d1db5ee845564fc66acb8fc5a6aff2aa9a46e6 Mon Sep 17 00:00:00 2001 From: "Ferreira, Eric (ef183v)" Date: Wed, 3 Apr 2019 00:34:52 -0400 Subject: [PATCH] [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. --- types/qrcode-svg/index.d.ts | 68 ++++++++++++++++++++++++++++ types/qrcode-svg/qrcode-svg-tests.ts | 24 ++++++++++ types/qrcode-svg/tsconfig.json | 24 ++++++++++ types/qrcode-svg/tslint.json | 6 +++ 4 files changed, 122 insertions(+) create mode 100644 types/qrcode-svg/index.d.ts create mode 100644 types/qrcode-svg/qrcode-svg-tests.ts create mode 100644 types/qrcode-svg/tsconfig.json create mode 100644 types/qrcode-svg/tslint.json diff --git a/types/qrcode-svg/index.d.ts b/types/qrcode-svg/index.d.ts new file mode 100644 index 0000000000..42f5397e6f --- /dev/null +++ b/types/qrcode-svg/index.d.ts @@ -0,0 +1,68 @@ +// Type definitions for qrcode-svg 1.0 +// Project: https://github.com/papnkukn/qrcode-svg +// Definitions by: Eric Ferreira +// 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; diff --git a/types/qrcode-svg/qrcode-svg-tests.ts b/types/qrcode-svg/qrcode-svg-tests.ts new file mode 100644 index 0000000000..b26f829244 --- /dev/null +++ b/types/qrcode-svg/qrcode-svg-tests.ts @@ -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"); diff --git a/types/qrcode-svg/tsconfig.json b/types/qrcode-svg/tsconfig.json new file mode 100644 index 0000000000..94472b7ede --- /dev/null +++ b/types/qrcode-svg/tsconfig.json @@ -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" + ] +} diff --git a/types/qrcode-svg/tslint.json b/types/qrcode-svg/tslint.json new file mode 100644 index 0000000000..08b1465cd6 --- /dev/null +++ b/types/qrcode-svg/tslint.json @@ -0,0 +1,6 @@ +{ + "extends": "dtslint/dt.json", + "rules": { + "unified-signatures": false + } +}