Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: simplify options handling #174

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 5 additions & 8 deletions dist/jsQR.js
Original file line number Diff line number Diff line change
Expand Up @@ -362,15 +362,12 @@ var defaultOptions = {
};
function jsQR(data, width, height, providedOptions) {
if (providedOptions === void 0) { providedOptions = {}; }
var options = defaultOptions;
Object.keys(options || {}).forEach(function (opt) {
options[opt] = providedOptions[opt] || options[opt];
});
var shouldInvert = options.inversionAttempts === "attemptBoth" || options.inversionAttempts === "invertFirst";
var tryInvertedFirst = options.inversionAttempts === "onlyInvert" || options.inversionAttempts === "invertFirst";
var _a = binarizer_1.binarize(data, width, height, shouldInvert), binarized = _a.binarized, inverted = _a.inverted;
var _a = providedOptions.inversionAttempts, inversionAttempts = _a === void 0 ? defaultOptions.inversionAttempts : _a;
var shouldInvert = inversionAttempts === "attemptBoth" || inversionAttempts === "invertFirst";
var tryInvertedFirst = inversionAttempts === "onlyInvert" || inversionAttempts === "invertFirst";
var _b = binarizer_1.binarize(data, width, height, shouldInvert), binarized = _b.binarized, inverted = _b.inverted;
var result = scan(tryInvertedFirst ? inverted : binarized);
if (!result && (options.inversionAttempts === "attemptBoth" || options.inversionAttempts === "invertFirst")) {
if (!result && (inversionAttempts === "attemptBoth" || inversionAttempts === "invertFirst")) {
result = scan(tryInvertedFirst ? binarized : inverted);
}
return result;
Expand Down
13 changes: 5 additions & 8 deletions docs/jsQR.js
Original file line number Diff line number Diff line change
Expand Up @@ -362,15 +362,12 @@ var defaultOptions = {
};
function jsQR(data, width, height, providedOptions) {
if (providedOptions === void 0) { providedOptions = {}; }
var options = defaultOptions;
Object.keys(options || {}).forEach(function (opt) {
options[opt] = providedOptions[opt] || options[opt];
});
var shouldInvert = options.inversionAttempts === "attemptBoth" || options.inversionAttempts === "invertFirst";
var tryInvertedFirst = options.inversionAttempts === "onlyInvert" || options.inversionAttempts === "invertFirst";
var _a = binarizer_1.binarize(data, width, height, shouldInvert), binarized = _a.binarized, inverted = _a.inverted;
var _a = providedOptions.inversionAttempts, inversionAttempts = _a === void 0 ? defaultOptions.inversionAttempts : _a;
var shouldInvert = inversionAttempts === "attemptBoth" || inversionAttempts === "invertFirst";
var tryInvertedFirst = inversionAttempts === "onlyInvert" || inversionAttempts === "invertFirst";
var _b = binarizer_1.binarize(data, width, height, shouldInvert), binarized = _b.binarized, inverted = _b.inverted;
var result = scan(tryInvertedFirst ? inverted : binarized);
if (!result && (options.inversionAttempts === "attemptBoth" || options.inversionAttempts === "invertFirst")) {
if (!result && (inversionAttempts === "attemptBoth" || inversionAttempts === "invertFirst")) {
result = scan(tryInvertedFirst ? binarized : inverted);
}
return result;
Expand Down
13 changes: 4 additions & 9 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,17 +64,12 @@ const defaultOptions: Options = {
};

function jsQR(data: Uint8ClampedArray, width: number, height: number, providedOptions: Options = {}): QRCode | null {

const options = defaultOptions;
Object.keys(options || {}).forEach(opt => { // Sad implementation of Object.assign since we target es5 not es6
(options as any)[opt] = (providedOptions as any)[opt] || (options as any)[opt];
});

const shouldInvert = options.inversionAttempts === "attemptBoth" || options.inversionAttempts === "invertFirst";
const tryInvertedFirst = options.inversionAttempts === "onlyInvert" || options.inversionAttempts === "invertFirst";
const { inversionAttempts = defaultOptions.inversionAttempts } = providedOptions;
const shouldInvert = inversionAttempts === "attemptBoth" || inversionAttempts === "invertFirst";
const tryInvertedFirst = inversionAttempts === "onlyInvert" || inversionAttempts === "invertFirst";
const {binarized, inverted} = binarize(data, width, height, shouldInvert);
let result = scan(tryInvertedFirst ? inverted : binarized);
if (!result && (options.inversionAttempts === "attemptBoth" || options.inversionAttempts === "invertFirst")) {
if (!result && (inversionAttempts === "attemptBoth" || inversionAttempts === "invertFirst")) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this can be simplified to:

-if (!result && (inversionAttempts === "attemptBoth" || inversionAttempts === "invertFirst")) {
+if (!result && shouldInvert) {

result = scan(tryInvertedFirst ? binarized : inverted);
}
return result;
Expand Down