Refactored TypeScript code to use for-of loop instead of Array.forEach(), for simplicity and clarity.

pull/39/merge
Project Nayuki 6 years ago
parent 0a96eec52e
commit 7217de3eee

@ -95,19 +95,19 @@ namespace qrcodegen {
} }
// Increase the error correction level while the data still fits in the current version number // Increase the error correction level while the data still fits in the current version number
[QrCode.Ecc.MEDIUM, QrCode.Ecc.QUARTILE, QrCode.Ecc.HIGH].forEach((newEcl: QrCode.Ecc) => { // From low to high for (let newEcl of [QrCode.Ecc.MEDIUM, QrCode.Ecc.QUARTILE, QrCode.Ecc.HIGH]) { // From low to high
if (boostEcl && dataUsedBits <= QrCode.getNumDataCodewords(version, newEcl) * 8) if (boostEcl && dataUsedBits <= QrCode.getNumDataCodewords(version, newEcl) * 8)
ecl = newEcl; ecl = newEcl;
}); }
// Concatenate all segments to create the data bit string // Concatenate all segments to create the data bit string
let bb = new BitBuffer(); let bb = new BitBuffer();
segs.forEach((seg: QrSegment) => { for (let seg of segs) {
bb.appendBits(seg.mode.modeBits, 4); bb.appendBits(seg.mode.modeBits, 4);
bb.appendBits(seg.numChars, seg.mode.numCharCountBits(version)); bb.appendBits(seg.numChars, seg.mode.numCharCountBits(version));
seg.getBits().forEach( for (let b of seg.getBits())
(b: bit) => bb.push(b)); bb.push(b);
}); }
if (bb.length != dataUsedBits) if (bb.length != dataUsedBits)
throw "Assertion error"; throw "Assertion error";
@ -541,12 +541,12 @@ namespace qrcodegen {
// Balance of black and white modules // Balance of black and white modules
let black: int = 0; let black: int = 0;
this.modules.forEach((row: Array<boolean>) => { for (let row of this.modules) {
row.forEach((color: boolean) => { for (let color of row) {
if (color) if (color)
black++; black++;
}); }
}); }
let total: int = this.size * this.size; // Note that size is odd, so black/total != 1/2 let total: int = this.size * this.size; // Note that size is odd, so black/total != 1/2
// Compute the smallest integer k >= 0 such that (45-5k)% <= black/total <= (55+5k)% // Compute the smallest integer k >= 0 such that (45-5k)% <= black/total <= (55+5k)%
let k: int = Math.ceil(Math.abs(black * 20 - total * 10) / total) - 1; let k: int = Math.ceil(Math.abs(black * 20 - total * 10) / total) - 1;
@ -660,8 +660,8 @@ namespace qrcodegen {
// Returns a segment representing the given binary data encoded in byte mode. // Returns a segment representing the given binary data encoded in byte mode.
public static makeBytes(data: Array<byte>): QrSegment { public static makeBytes(data: Array<byte>): QrSegment {
let bb = new BitBuffer(); let bb = new BitBuffer();
data.forEach( for (let b of data)
(b: byte) => bb.appendBits(b, 8)); bb.appendBits(b, 8);
return new QrSegment(QrSegment.Mode.BYTE, data.length, bb); return new QrSegment(QrSegment.Mode.BYTE, data.length, bb);
} }
@ -857,12 +857,12 @@ namespace qrcodegen {
public getRemainder(data: Array<byte>): Array<byte> { public getRemainder(data: Array<byte>): Array<byte> {
// Compute the remainder by performing polynomial division // Compute the remainder by performing polynomial division
let result: Array<byte> = this.coefficients.map(_ => 0); let result: Array<byte> = this.coefficients.map(_ => 0);
data.forEach((b: byte) => { for (let b of data) {
let factor: byte = b ^ (result.shift() as int); let factor: byte = b ^ (result.shift() as int);
result.push(0); result.push(0);
for (let i = 0; i < result.length; i++) for (let i = 0; i < result.length; i++)
result[i] ^= ReedSolomonGenerator.multiply(this.coefficients[i], factor); result[i] ^= ReedSolomonGenerator.multiply(this.coefficients[i], factor);
}); }
return result; return result;
} }

Loading…
Cancel
Save