From 8db3cfef81994ebd549a68e7f97712470e61fbdd Mon Sep 17 00:00:00 2001 From: Project Nayuki Date: Fri, 31 Aug 2018 17:41:20 +0000 Subject: [PATCH] Simplified TypeScript code by using constructor parameter properties to replace most fields. --- typescript/qrcodegen.ts | 95 +++++++++++++++++------------------------ 1 file changed, 38 insertions(+), 57 deletions(-) diff --git a/typescript/qrcodegen.ts b/typescript/qrcodegen.ts index 797b493..52d2906 100644 --- a/typescript/qrcodegen.ts +++ b/typescript/qrcodegen.ts @@ -131,21 +131,10 @@ namespace qrcodegen { /*-- Fields --*/ - // This QR Code symbol's version number, which is always between 1 and 40 (inclusive). - public readonly version: int; - // The width and height of this QR Code symbol, measured in modules. // Always equal to version * 4 + 17, in the range 21 to 177. public readonly size: int; - // The error correction level used in this QR Code symbol. - public readonly errorCorrectionLevel: QrCode.Ecc; - - // The mask pattern used in this QR Code symbol, in the range 0 to 7 (i.e. unsigned 3-bit integer). - // Note that even if the constructor was called with automatic masking requested - // (mask = -1), the resulting object will still have a mask value between 0 and 7. - public readonly mask: int; - // The modules of this QR Code symbol (false = white, true = black). private readonly modules: Array> = []; @@ -153,15 +142,28 @@ namespace qrcodegen { private readonly isFunction: Array> = []; - public constructor(datacodewords: Array, mask: int, version: int, errCorLvl: QrCode.Ecc) { + /*-- Constructor and fields --*/ + + public constructor( + datacodewords: Array, + + // The mask pattern used in this QR Code symbol, in the range 0 to 7 (i.e. unsigned 3-bit integer). + // Note that even if the constructor was called with automatic masking requested + // (mask = -1), the resulting object will still have a mask value between 0 and 7. + public readonly mask: int, + + // This QR Code symbol's version number, which is always between 1 and 40 (inclusive). + public readonly version: int, + + // The error correction level used in this QR Code symbol. + public readonly errorCorrectionLevel: QrCode.Ecc) { + // Check arguments and handle simple scalar fields if (mask < -1 || mask > 7) throw "Mask value out of range"; if (version < QrCode.MIN_VERSION || version > QrCode.MAX_VERSION) throw "Version value out of range"; - this.version = version; this.size = version * 4 + 17; - this.errorCorrectionLevel = errCorLvl; // Initialize both grids to be size*size arrays of Boolean false let row: Array = []; @@ -733,25 +735,20 @@ namespace qrcodegen { } - /*-- Fields --*/ - - // The mode indicator for this segment. - public readonly mode: QrSegment.Mode; - - // The length of this segment's unencoded data, measured in characters. Always zero or positive. - public readonly numChars: int; - - private readonly bitData: Array; - - - /*-- Constructor --*/ + /*-- Constructor and fields --*/ // Creates a new QR Code segment with the given parameters and data. - public constructor(mode: QrSegment.Mode, numChars: int, bitData: Array) { + public constructor( + // The mode indicator for this segment. + public readonly mode: QrSegment.Mode, + + // The length of this segment's unencoded data, measured in characters. Always zero or positive. + public readonly numChars: int, + + private readonly bitData: Array) { + if (numChars < 0) throw "Invalid argument"; - this.mode = mode; - this.numChars = numChars; this.bitData = bitData.slice(); // Make defensive copy } @@ -939,21 +936,13 @@ namespace qrcodegen.QrCode { public static readonly HIGH = new Ecc(3, 2); - /*-- Fields --*/ + /*-- Constructor and fields --*/ - // In the range 0 to 3 (unsigned 2-bit integer). - public readonly ordinal: int; - - // (Package-private) In the range 0 to 3 (unsigned 2-bit integer). - public readonly formatBits: int; - - - /*-- Constructor --*/ - - private constructor(ord: int, fb: int) { - this.ordinal = ord; - this.formatBits = fb; - } + private constructor( + // In the range 0 to 3 (unsigned 2-bit integer). + public readonly ordinal: int, + // (Package-private) In the range 0 to 3 (unsigned 2-bit integer). + public readonly formatBits: int) {} } } @@ -981,21 +970,13 @@ namespace qrcodegen.QrSegment { public static readonly ECI = new Mode(0x7, [ 0, 0, 0]); - /*-- Fields --*/ + /*-- Constructor and fields --*/ - // The mode indicator bits, which is a uint4 value (range 0 to 15). - public readonly modeBits: int; - - // Three values for different version ranges. - private readonly numBitsCharCount: [int,int,int]; - - - /*-- Constructor --*/ - - private constructor(mode: int, ccbits: [int,int,int]) { - this.modeBits = mode; - this.numBitsCharCount = ccbits; - } + private constructor( + // The mode indicator bits, which is a uint4 value (range 0 to 15). + public readonly modeBits: int, + // Three values for different version ranges. + private readonly numBitsCharCount: [int,int,int]) {} /*-- Method --*/