Skip to content

Commit bc4e529

Browse files
refactor: simplify options handling
1 parent 8851ab5 commit bc4e529

3 files changed

Lines changed: 14 additions & 25 deletions

File tree

dist/jsQR.js

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -362,15 +362,12 @@ var defaultOptions = {
362362
};
363363
function jsQR(data, width, height, providedOptions) {
364364
if (providedOptions === void 0) { providedOptions = {}; }
365-
var options = defaultOptions;
366-
Object.keys(options || {}).forEach(function (opt) {
367-
options[opt] = providedOptions[opt] || options[opt];
368-
});
369-
var shouldInvert = options.inversionAttempts === "attemptBoth" || options.inversionAttempts === "invertFirst";
370-
var tryInvertedFirst = options.inversionAttempts === "onlyInvert" || options.inversionAttempts === "invertFirst";
371-
var _a = binarizer_1.binarize(data, width, height, shouldInvert), binarized = _a.binarized, inverted = _a.inverted;
365+
var _a = providedOptions.inversionAttempts, inversionAttempts = _a === void 0 ? defaultOptions.inversionAttempts : _a;
366+
var shouldInvert = inversionAttempts === "attemptBoth" || inversionAttempts === "invertFirst";
367+
var tryInvertedFirst = inversionAttempts === "onlyInvert" || inversionAttempts === "invertFirst";
368+
var _b = binarizer_1.binarize(data, width, height, shouldInvert), binarized = _b.binarized, inverted = _b.inverted;
372369
var result = scan(tryInvertedFirst ? inverted : binarized);
373-
if (!result && (options.inversionAttempts === "attemptBoth" || options.inversionAttempts === "invertFirst")) {
370+
if (!result && (inversionAttempts === "attemptBoth" || inversionAttempts === "invertFirst")) {
374371
result = scan(tryInvertedFirst ? binarized : inverted);
375372
}
376373
return result;

docs/jsQR.js

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -362,15 +362,12 @@ var defaultOptions = {
362362
};
363363
function jsQR(data, width, height, providedOptions) {
364364
if (providedOptions === void 0) { providedOptions = {}; }
365-
var options = defaultOptions;
366-
Object.keys(options || {}).forEach(function (opt) {
367-
options[opt] = providedOptions[opt] || options[opt];
368-
});
369-
var shouldInvert = options.inversionAttempts === "attemptBoth" || options.inversionAttempts === "invertFirst";
370-
var tryInvertedFirst = options.inversionAttempts === "onlyInvert" || options.inversionAttempts === "invertFirst";
371-
var _a = binarizer_1.binarize(data, width, height, shouldInvert), binarized = _a.binarized, inverted = _a.inverted;
365+
var _a = providedOptions.inversionAttempts, inversionAttempts = _a === void 0 ? defaultOptions.inversionAttempts : _a;
366+
var shouldInvert = inversionAttempts === "attemptBoth" || inversionAttempts === "invertFirst";
367+
var tryInvertedFirst = inversionAttempts === "onlyInvert" || inversionAttempts === "invertFirst";
368+
var _b = binarizer_1.binarize(data, width, height, shouldInvert), binarized = _b.binarized, inverted = _b.inverted;
372369
var result = scan(tryInvertedFirst ? inverted : binarized);
373-
if (!result && (options.inversionAttempts === "attemptBoth" || options.inversionAttempts === "invertFirst")) {
370+
if (!result && (inversionAttempts === "attemptBoth" || inversionAttempts === "invertFirst")) {
374371
result = scan(tryInvertedFirst ? binarized : inverted);
375372
}
376373
return result;

src/index.ts

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -64,17 +64,12 @@ const defaultOptions: Options = {
6464
};
6565

6666
function jsQR(data: Uint8ClampedArray, width: number, height: number, providedOptions: Options = {}): QRCode | null {
67-
68-
const options = defaultOptions;
69-
Object.keys(options || {}).forEach(opt => { // Sad implementation of Object.assign since we target es5 not es6
70-
(options as any)[opt] = (providedOptions as any)[opt] || (options as any)[opt];
71-
});
72-
73-
const shouldInvert = options.inversionAttempts === "attemptBoth" || options.inversionAttempts === "invertFirst";
74-
const tryInvertedFirst = options.inversionAttempts === "onlyInvert" || options.inversionAttempts === "invertFirst";
67+
const { inversionAttempts = defaultOptions.inversionAttempts } = providedOptions
68+
const shouldInvert = inversionAttempts === "attemptBoth" || inversionAttempts === "invertFirst";
69+
const tryInvertedFirst = inversionAttempts === "onlyInvert" || inversionAttempts === "invertFirst";
7570
const {binarized, inverted} = binarize(data, width, height, shouldInvert);
7671
let result = scan(tryInvertedFirst ? inverted : binarized);
77-
if (!result && (options.inversionAttempts === "attemptBoth" || options.inversionAttempts === "invertFirst")) {
72+
if (!result && (inversionAttempts === "attemptBoth" || inversionAttempts === "invertFirst")) {
7873
result = scan(tryInvertedFirst ? binarized : inverted);
7974
}
8075
return result;

0 commit comments

Comments
 (0)