From abf817eb5647f7f03a62457b8a8a927d725bb4c0 Mon Sep 17 00:00:00 2001 From: Johan Lindell Date: Tue, 4 Jul 2017 18:20:15 +0200 Subject: [PATCH 1/4] First commit of improved barcode options --- src/JsBarcode.js | 21 ++++---- src/barcodes/Barcode.js | 4 ++ src/exceptions/ErrorHandler.js | 6 ++- src/help/applyOptions.js | 32 ++++++++++++ src/help/optionsFromStrings.js | 31 ----------- src/options/defaults.js | 95 +++++++++++++++++++++++++++------- 6 files changed, 128 insertions(+), 61 deletions(-) create mode 100644 src/help/applyOptions.js delete mode 100644 src/help/optionsFromStrings.js diff --git a/src/JsBarcode.js b/src/JsBarcode.js index 177d9dd4..d3c391e6 100644 --- a/src/JsBarcode.js +++ b/src/JsBarcode.js @@ -3,10 +3,10 @@ import barcodes from './barcodes/'; // Help functions import merge from './help/merge.js'; +import applyOptions from './help/applyOptions.js'; import linearizeEncodings from './help/linearizeEncodings.js'; import fixOptions from './help/fixOptions.js'; import getRenderProperties from './help/getRenderProperties.js'; -import optionsFromStrings from './help/optionsFromStrings.js'; // Exceptions import ErrorHandler from './exceptions/ErrorHandler.js'; @@ -31,18 +31,19 @@ let JsBarcode = function(element, text, options){ // Variables that will be pased through the API calls api._renderProperties = getRenderProperties(element); api._encodings = []; - api._options = defaults; + api._optionsBlueprint = defaults; api._errorHandler = new ErrorHandler(api); + api._options = options || {}; + api._options = applyOptions(api._optionsBlueprint, api._options); + // If text is set, use the simple syntax (render the barcode directly) if(typeof text !== "undefined"){ - options = options || {}; - - if(!options.format){ - options.format = autoSelectBarcode(); + if(!api._options.format || api._options.format === 'auto'){ + api._options.format = autoSelectBarcode(); } - api.options(options)[options.format](text, options).render(); + api[api._options.format](text, api._options).render(); } return api; @@ -69,9 +70,11 @@ function registerBarcode(barcodes, name){ // Ensure text is options.text options.text = typeof options.text === 'undefined' ? undefined : '' + options.text; - var newOptions = merge(api._options, options); - newOptions = optionsFromStrings(newOptions); var Encoder = barcodes[name]; + var optionsBlueprint = merge(api._optionsBlueprint, Encoder.options()); + var newOptions = applyOptions(optionsBlueprint, options); + newOptions = merge(api._options, newOptions); + var encoded = encode(text, Encoder, newOptions); api._encodings.push(encoded); diff --git a/src/barcodes/Barcode.js b/src/barcodes/Barcode.js index 89a55e0a..5a7df2ab 100644 --- a/src/barcodes/Barcode.js +++ b/src/barcodes/Barcode.js @@ -4,6 +4,10 @@ class Barcode{ this.text = options.text || data; this.options = options; } + + static options() { + return {}; + } } export default Barcode; diff --git a/src/exceptions/ErrorHandler.js b/src/exceptions/ErrorHandler.js index 1a684faf..311521d4 100644 --- a/src/exceptions/ErrorHandler.js +++ b/src/exceptions/ErrorHandler.js @@ -8,7 +8,7 @@ class ErrorHandler{ handleCatch(e){ // If babel supported extending of Error in a correct way instanceof would be used here if(e.name === "InvalidInputException"){ - if(this.api._options.valid !== this.api._defaults.valid){ + if(typeof this.api._options.valid !== 'undefined' && this.api._options.valid !== this.api._defaults.valid){ this.api._options.valid(false); } else{ @@ -25,7 +25,9 @@ class ErrorHandler{ wrapBarcodeCall(func){ try{ var result = func(...arguments); - this.api._options.valid(true); + if (typeof this.api._options.valid !== 'undefined') { + this.api._options.valid(true); + } return result; } catch(e){ diff --git a/src/help/applyOptions.js b/src/help/applyOptions.js new file mode 100644 index 00000000..35653c27 --- /dev/null +++ b/src/help/applyOptions.js @@ -0,0 +1,32 @@ +export default applyOptions; + +function applyOptions(optionsBlueprint, options) { + var returnOptions = {}; + for (var name in optionsBlueprint) { + if (optionsBlueprint.hasOwnProperty(name)) { + if (typeof options[name] !== 'undefined') { + returnOptions[name] = convertOption(options[name], optionsBlueprint[name].type); + } + else { + returnOptions[name] = optionsBlueprint[name].default; + } + } + } + return returnOptions; +} + +// Convert an option to a specified type +function convertOption(data, type) { + if (type === 'string') { + return '' + data; + } + else if (type === 'number') { + return parseInt(data, 10); + } + else if (type === 'boolean') { + return (typeof data === 'boolean' && data) || data === 'true' || data === 'True'; + } + else { + return data; + } +} diff --git a/src/help/optionsFromStrings.js b/src/help/optionsFromStrings.js deleted file mode 100644 index a9adfda3..00000000 --- a/src/help/optionsFromStrings.js +++ /dev/null @@ -1,31 +0,0 @@ -export default optionsFromStrings; - -// Convert string to integers/booleans where it should be -function optionsFromStrings(options){ - var intOptions = [ - "width", - "height", - "textMargin", - "fontSize", - "margin", - "marginTop", - "marginBottom", - "marginLeft", - "marginRight" - ]; - - for(var intOption in intOptions){ - if(intOptions.hasOwnProperty(intOption)){ - intOption = intOptions[intOption]; - if(typeof options[intOption] === "string"){ - options[intOption] = parseInt(options[intOption], 10); - } - } - } - - if(typeof options["displayValue"] === "string"){ - options["displayValue"] = (options["displayValue"] != "false"); - } - - return options; -} diff --git a/src/options/defaults.js b/src/options/defaults.js index f2362ffa..109ca2bc 100644 --- a/src/options/defaults.js +++ b/src/options/defaults.js @@ -1,23 +1,80 @@ var defaults = { - width: 2, - height: 100, - format: "auto", - displayValue: true, - fontOptions: "", - font: "monospace", - text: undefined, - textAlign: "center", - textPosition: "bottom", - textMargin: 2, - fontSize: 20, - background: "#ffffff", - lineColor: "#000000", - margin: 10, - marginTop: undefined, - marginBottom: undefined, - marginLeft: undefined, - marginRight: undefined, - valid: function(){} + width: { + type: "number", + default: 2, + }, + height: { + type: "number", + default: 100, + }, + format: { + type: "string", + default: "auto", + }, + displayValue: { + type: "boolean", + default: true, + }, + fontOptions: { + type: "string", + default: "", + }, + font: { + type: "string", + default: "monospace", + }, + text: { + type: "string", + default: undefined, + }, + textAlign: { + type: "string", + default: "center", + }, + textPosition: { + type: "string", + default: "bottom", + }, + textMargin: { + type: "number", + default: 2, + }, + fontSize: { + type: "number", + default: 20, + }, + background: { + type: "string", + default: "#ffffff", + }, + lineColor: { + type: "string", + default: "#000000", + }, + margin: { + type: "number", + default: 10, + }, + marginTop: { + type: "number", + default: undefined, + }, + marginBottom: { + type: "number", + default: undefined, + }, + marginLeft: { + type: "number", + default: undefined, + }, + marginRight: { + type: "number", + default: undefined, + }, + valid: { + type: "function", + default: function(){}, + }, }; export default defaults; From 300a46c299922ebdf20cac0da789b7228ca54d73 Mon Sep 17 00:00:00 2001 From: Johan Lindell Date: Tue, 4 Jul 2017 18:22:30 +0200 Subject: [PATCH 2/4] Implemented flat option in in EAN13 with new options --- src/barcodes/EAN_UPC/EAN13.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/barcodes/EAN_UPC/EAN13.js b/src/barcodes/EAN_UPC/EAN13.js index aa126a9f..f6a4c148 100644 --- a/src/barcodes/EAN_UPC/EAN13.js +++ b/src/barcodes/EAN_UPC/EAN13.js @@ -141,6 +141,15 @@ class EAN13 extends Barcode{ text: this.text }; } + + static options() { + return { + flat: { + type: "boolean", + default: false, + } + }; + } } // Calulate the checksum digit From c939f38b0563fa30d5c2c579497620d7bfd21e31 Mon Sep 17 00:00:00 2001 From: Johan Lindell Date: Tue, 11 Jul 2017 19:30:12 +0200 Subject: [PATCH 3/4] Fixing test for improved options --- src/exceptions/ErrorHandler.js | 2 +- src/options/defaults.js | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/exceptions/ErrorHandler.js b/src/exceptions/ErrorHandler.js index 311521d4..cbccb638 100644 --- a/src/exceptions/ErrorHandler.js +++ b/src/exceptions/ErrorHandler.js @@ -8,7 +8,7 @@ class ErrorHandler{ handleCatch(e){ // If babel supported extending of Error in a correct way instanceof would be used here if(e.name === "InvalidInputException"){ - if(typeof this.api._options.valid !== 'undefined' && this.api._options.valid !== this.api._defaults.valid){ + if(typeof this.api._options.valid !== 'undefined'){ this.api._options.valid(false); } else{ diff --git a/src/options/defaults.js b/src/options/defaults.js index 109ca2bc..1d433849 100644 --- a/src/options/defaults.js +++ b/src/options/defaults.js @@ -73,8 +73,12 @@ var defaults = { }, valid: { type: "function", - default: function(){}, + default: undefined, }, + xmlDocument: { + type: "function", + default: undefined, + } }; export default defaults; From 8fd488da111b6dcfc3c23a455e13dba750c64831 Mon Sep 17 00:00:00 2001 From: Johan Lindell Date: Tue, 11 Jul 2017 21:47:17 +0200 Subject: [PATCH 4/4] Added new options method to multiple barcodes --- src/barcodes/CODE128/CODE128.js | 20 ++++++++++---------- src/barcodes/CODE39/index.js | 9 +++++++++ src/barcodes/EAN_UPC/UPC.js | 9 +++++++++ src/barcodes/EAN_UPC/UPCE.js | 9 +++++++++ 4 files changed, 37 insertions(+), 10 deletions(-) diff --git a/src/barcodes/CODE128/CODE128.js b/src/barcodes/CODE128/CODE128.js index dfd929b1..a4f9cbf2 100644 --- a/src/barcodes/CODE128/CODE128.js +++ b/src/barcodes/CODE128/CODE128.js @@ -29,7 +29,7 @@ class CODE128 extends Barcode { throw new RangeError('The encoding does not start with a start character.'); } - if (this.shouldEncodeAsEan128() === true) { + if (this.options.ean128) { bytes.unshift(FNC1); } @@ -53,15 +53,6 @@ class CODE128 extends Barcode { }; } - // GS1-128/EAN-128 - shouldEncodeAsEan128() { - let isEAN128 = this.options.ean128 || false; - if (typeof isEAN128 === 'string') { - isEAN128 = isEAN128.toLowerCase() === 'true'; - } - return isEAN128; - } - // Get a bar symbol by index static getBar(index) { return BARS[index] ? BARS[index].toString() : ''; @@ -122,6 +113,15 @@ class CODE128 extends Barcode { checksum: weight + nextCode.checksum }; } + + static options() { + return { + ean128: { + type: "boolean", + default: false, + } + }; + } } export default CODE128; diff --git a/src/barcodes/CODE39/index.js b/src/barcodes/CODE39/index.js index e793a6ad..c6922ad4 100644 --- a/src/barcodes/CODE39/index.js +++ b/src/barcodes/CODE39/index.js @@ -36,6 +36,15 @@ class CODE39 extends Barcode { valid(){ return this.data.search(/^[0-9A-Z\-\.\ \$\/\+\%]+$/) !== -1; } + + static options() { + return { + mod43: { + type: "boolean", + default: false, + } + }; + } } diff --git a/src/barcodes/EAN_UPC/UPC.js b/src/barcodes/EAN_UPC/UPC.js index 13e1b984..413c1885 100644 --- a/src/barcodes/EAN_UPC/UPC.js +++ b/src/barcodes/EAN_UPC/UPC.js @@ -113,6 +113,15 @@ class UPC extends Barcode{ return result; } + + static options() { + return { + flat: { + type: "boolean", + default: false, + } + }; + } } // Calulate the checksum digit diff --git a/src/barcodes/EAN_UPC/UPCE.js b/src/barcodes/EAN_UPC/UPCE.js index 9c05e527..94fc9739 100644 --- a/src/barcodes/EAN_UPC/UPCE.js +++ b/src/barcodes/EAN_UPC/UPCE.js @@ -155,6 +155,15 @@ class UPCE extends Barcode{ const parity = PARITIES[parseInt(checkDigit)][parseInt(numberSystem)]; return encoder.encode(this.middleDigits, parity); } + + static options() { + return { + flat: { + type: "boolean", + default: false, + } + }; + } } function expandToUPCA(middleDigits, numberSystem) {