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

[WIP] Improved barcode options #171

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
21 changes: 12 additions & 9 deletions src/JsBarcode.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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;
Expand All @@ -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);

Expand Down
4 changes: 4 additions & 0 deletions src/barcodes/Barcode.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ class Barcode{
this.text = options.text || data;
this.options = options;
}

static options() {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Did you think to make it as a property, but not as a function?

Copy link
Owner Author

Choose a reason for hiding this comment

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

I thought about it. The main reason why I wanted to make this a function is that it will make inheritance of options easier. If for all CODE128 codes have one option but we want to add additional options to that sub-symbology it can easily be done like follow.

static options() {
  return Object.assign({}, super.options(), {
    somenewoptions: {}
  });
}

return {};
}
}

export default Barcode;
20 changes: 10 additions & 10 deletions src/barcodes/CODE128/CODE128.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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() : '';
Expand Down Expand Up @@ -122,6 +113,15 @@ class CODE128 extends Barcode {
checksum: weight + nextCode.checksum
};
}

static options() {
return {
ean128: {
type: "boolean",
default: false,
}
};
}
}

export default CODE128;
9 changes: 9 additions & 0 deletions src/barcodes/CODE39/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
};
}
}


Expand Down
9 changes: 9 additions & 0 deletions src/barcodes/EAN_UPC/EAN13.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,15 @@ class EAN13 extends Barcode{
text: this.text
};
}

static options() {
return {
flat: {
type: "boolean",
default: false,
}
};
}
}

// Calulate the checksum digit
Expand Down
9 changes: 9 additions & 0 deletions src/barcodes/EAN_UPC/UPC.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,15 @@ class UPC extends Barcode{

return result;
}

static options() {
return {
flat: {
type: "boolean",
default: false,
}
};
}
}

// Calulate the checksum digit
Expand Down
9 changes: 9 additions & 0 deletions src/barcodes/EAN_UPC/UPCE.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
6 changes: 4 additions & 2 deletions src/exceptions/ErrorHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(false);
}
else{
Expand All @@ -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') {
Copy link
Collaborator

Choose a reason for hiding this comment

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

this.api._options.valid !== undefined should be enough, I think.

Copy link
Owner Author

Choose a reason for hiding this comment

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

Copy link
Collaborator

Choose a reason for hiding this comment

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

Oh, you can do it in old specs as I see (http://www.ecma-international.org/ecma-262/5.1/#sec-15.1.1.3). But for a public library your way is better :)

this.api._options.valid(true);
}
return result;
}
catch(e){
Expand Down
32 changes: 32 additions & 0 deletions src/help/applyOptions.js
Original file line number Diff line number Diff line change
@@ -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') {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Maybe check type before:

if (typeof data !== type) {
  // do conversions
}
return data;

Copy link
Owner Author

Choose a reason for hiding this comment

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

type is the type it should be converted TO, not the type it already is. Or I might have misunderstood this.

Copy link
Collaborator

Choose a reason for hiding this comment

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

For example convertOption(10, 'number') causes return parseInt(data, 10); anyway. I suggest check type of data before and avoid any unnecessary conversion. Or I missed something :)

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;
}
}
31 changes: 0 additions & 31 deletions src/help/optionsFromStrings.js

This file was deleted.

99 changes: 80 additions & 19 deletions src/options/defaults.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,84 @@
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: undefined,
},
xmlDocument: {
type: "function",
default: undefined,
}
};

export default defaults;