Skip to content

Commit

Permalink
📦 Release v3.7.0
Browse files Browse the repository at this point in the history
  • Loading branch information
lindell committed May 17, 2017
1 parent f0e03c3 commit 58f6d86
Show file tree
Hide file tree
Showing 23 changed files with 608 additions and 684 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,14 +132,14 @@ Download or get the CDN link to the script:

| Name | Supported barcodes | Size (gzip) | CDN / Download |
|------|--------------------|:-----------:|---------------:|
| *All* | *All the barcodes!* | *8.5 kB* | *[JsBarcode.all.min.js][1]* |
| CODE128 | CODE128 (auto and force mode) | 5.7 kB | [JsBarcode.code128.min.js][2] |
| *All* | *All the barcodes!* | *8.8 kB* | *[JsBarcode.all.min.js][1]* |
| CODE128 | CODE128 (auto and force mode) | 5.9 kB | [JsBarcode.code128.min.js][2] |
| CODE39 | CODE39 | 4.8 kB | [JsBarcode.code39.min.js][3] |
| EAN / UPC | EAN-13, EAN-8, EAN-5, EAN-2, UPC (A) | 5.5 kB | [JsBarcode.ean-upc.min.js][4] |
| EAN / UPC | EAN-13, EAN-8, EAN-5, EAN-2, UPC (A) | 5.6 kB | [JsBarcode.ean-upc.min.js][4] |
| ITF-14 | ITF-14 | 4.6 kB | [JsBarcode.itf-14.min.js][5] |
| ITF | ITF | 4.5 kB | [JsBarcode.itf.min.js][6] |
| ITF | ITF | 4.6 kB | [JsBarcode.itf.min.js][6] |
| MSI | MSI, MSI10, MSI11, MSI1010, MSI1110 | 4.8 kB | [JsBarcode.msi.min.js][7] |
| Pharmacode | Pharmacode | 4.4 kB | [JsBarcode.pharmacode.min.js][8] |
| Pharmacode | Pharmacode | 4.5 kB | [JsBarcode.pharmacode.min.js][8] |
| Codabar | Codabar | 4.6 kB | [JsBarcode.codabar.min.js][9] |

### Step 2:
Expand Down
239 changes: 66 additions & 173 deletions bin/barcodes/CODE128/CODE128.js
Original file line number Diff line number Diff line change
@@ -1,249 +1,142 @@
"use strict";
'use strict';

Object.defineProperty(exports, "__esModule", {
value: true
});

var _Barcode2 = require("../Barcode.js");
var _Barcode2 = require('../Barcode.js');

var _Barcode3 = _interopRequireDefault(_Barcode2);

var _constants = require('./constants');

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } // This is the master class, it does require the start code to be
// included in the string
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }

// This is the master class,
// it does require the start code to be included in the string
var CODE128 = function (_Barcode) {
_inherits(CODE128, _Barcode);

function CODE128(data, options) {
_classCallCheck(this, CODE128);

// Fill the bytes variable with the ascii codes of string
// Get array of ascii codes from data
var _this = _possibleConstructorReturn(this, _Barcode.call(this, data.substring(1), options));

_this.bytes = [];
for (var i = 0; i < data.length; ++i) {
_this.bytes.push(data.charCodeAt(i));
}

// Data for each character, the last characters will not be encoded but are used for error correction
// Numbers encode to (n + 1000) -> binary; 740 -> (740 + 1000).toString(2) -> "11011001100"
_this.encodings = [// + 1000
740, 644, 638, 176, 164, 100, 224, 220, 124, 608, 604, 572, 436, 244, 230, 484, 260, 254, 650, 628, 614, 764, 652, 902, 868, 836, 830, 892, 844, 842, 752, 734, 590, 304, 112, 94, 416, 128, 122, 672, 576, 570, 464, 422, 134, 496, 478, 142, 910, 678, 582, 768, 762, 774, 880, 862, 814, 896, 890, 818, 914, 602, 930, 328, 292, 200, 158, 68, 62, 424, 412, 232, 218, 76, 74, 554, 616, 978, 556, 146, 340, 212, 182, 508, 268, 266, 956, 940, 938, 758, 782, 974, 400, 310, 118, 512, 506, 960, 954, 502, 518, 886, 966, /* Start codes */668, 680, 692, 5379];
_this.bytes = data.split('').map(function (char) {
return char.charCodeAt(0);
});
return _this;
}

CODE128.prototype.valid = function valid() {
// ASCII value ranges 0-127, 200-211
return (/^[\x00-\x7F\xC8-\xD3]+$/.test(this.data)
);
};

// The public encoding function


CODE128.prototype.encode = function encode() {
var encodingResult;
var bytes = this.bytes;
// Remove the startcode from the bytes and set its index
// Remove the start code from the bytes and set its index
var startIndex = bytes.shift() - 105;
// Get start set by index
var startSet = _constants.SET_BY_CODE[startIndex];

// Start encode with the right type
if (startIndex === 103) {
encodingResult = this.nextA(bytes, 1);
} else if (startIndex === 104) {
encodingResult = this.nextB(bytes, 1);
} else if (startIndex === 105) {
encodingResult = this.nextC(bytes, 1);
} else {
throw new InvalidStartCharacterException();
if (startSet === undefined) {
throw new RangeError('The encoding does not start with a start character.');
}

// Start encode with the right type
var encodingResult = CODE128.next(bytes, 1, startSet);

return {
text: this.text == this.data ? this.text.replace(/[^\x20-\x7E]/g, "") : this.text,
text: this.text === this.data ? this.text.replace(/[^\x20-\x7E]/g, '') : this.text,
data:
// Add the start bits
this.getEncoding(startIndex) +
CODE128.getBar(startIndex) +
// Add the encoded bits
encodingResult.result +
// Add the checksum
this.getEncoding((encodingResult.checksum + startIndex) % 103) +
CODE128.getBar((encodingResult.checksum + startIndex) % _constants.MODULO) +
// Add the end bits
this.getEncoding(106)
CODE128.getBar(_constants.STOP)
};
};

CODE128.prototype.getEncoding = function getEncoding(n) {
return this.encodings[n] ? (this.encodings[n] + 1000).toString(2) : '';
};

// Use the regexp variable for validation
// Get a bar symbol by index


CODE128.prototype.valid = function valid() {
// ASCII value ranges 0-127, 200-211
return this.data.search(/^[\x00-\x7F\xC8-\xD3]+$/) !== -1;
CODE128.getBar = function getBar(index) {
return _constants.BARS[index] ? _constants.BARS[index].toString() : '';
};

CODE128.prototype.nextA = function nextA(bytes, depth) {
if (bytes.length <= 0) {
return { "result": "", "checksum": 0 };
}
// Correct an index by a set and shift it from the bytes array

var next, index;

// Special characters
if (bytes[0] >= 200) {
index = bytes[0] - 105;

// Remove first element
bytes.shift();

// Swap to CODE128C
if (index === 99) {
next = this.nextC(bytes, depth + 1);
}
// Swap to CODE128B
else if (index === 100) {
next = this.nextB(bytes, depth + 1);
}
// Shift
else if (index === 98) {
// Convert the next character so that is encoded correctly
bytes[0] = bytes[0] > 95 ? bytes[0] - 96 : bytes[0];
next = this.nextA(bytes, depth + 1);
}
// Continue on CODE128A but encode a special character
else {
next = this.nextA(bytes, depth + 1);
}
CODE128.correctIndex = function correctIndex(bytes, set) {
if (set === _constants.SET_A) {
var charCode = bytes.shift();
return charCode < 32 ? charCode + 64 : charCode - 32;
} else if (set === _constants.SET_B) {
return bytes.shift() - 32;
} else {
return (bytes.shift() - 48) * 10 + bytes.shift() - 48;
}
// Continue encoding of CODE128A
else {
var charCode = bytes[0];
index = charCode < 32 ? charCode + 64 : charCode - 32;

// Remove first element
bytes.shift();

next = this.nextA(bytes, depth + 1);
}

// Get the correct binary encoding and calculate the weight
var enc = this.getEncoding(index);
var weight = index * depth;

return {
"result": enc + next.result,
"checksum": weight + next.checksum
};
};

CODE128.prototype.nextB = function nextB(bytes, depth) {
if (bytes.length <= 0) {
return { "result": "", "checksum": 0 };
CODE128.next = function next(bytes, pos, set) {
if (!bytes.length) {
return { result: '', checksum: 0 };
}

var next, index;
var nextCode = void 0,
index = void 0;

// Special characters
if (bytes[0] >= 200) {
index = bytes[0] - 105;

// Remove first element
bytes.shift();
index = bytes.shift() - 105;
var nextSet = _constants.SWAP[index];

// Swap to CODE128C
if (index === 99) {
next = this.nextC(bytes, depth + 1);
// Swap to other set
if (nextSet !== undefined) {
nextCode = CODE128.next(bytes, pos + 1, nextSet);
}
// Swap to CODE128A
else if (index === 101) {
next = this.nextA(bytes, depth + 1);
}
// Shift
else if (index === 98) {
// Continue on current set but encode a special character
else {
// Shift
if ((set === _constants.SET_A || set === _constants.SET_B) && index === _constants.SHIFT) {
// Convert the next character so that is encoded correctly
bytes[0] = bytes[0] < 32 ? bytes[0] + 96 : bytes[0];
next = this.nextB(bytes, depth + 1);
bytes[0] = set === _constants.SET_A ? bytes[0] > 95 ? bytes[0] - 96 : bytes[0] : bytes[0] < 32 ? bytes[0] + 96 : bytes[0];
}
// Continue on CODE128B but encode a special character
else {
next = this.nextB(bytes, depth + 1);
}
}
// Continue encoding of CODE128B
else {
index = bytes[0] - 32;
bytes.shift();
next = this.nextB(bytes, depth + 1);
}

// Get the correct binary encoding and calculate the weight
var enc = this.getEncoding(index);
var weight = index * depth;

return { "result": enc + next.result, "checksum": weight + next.checksum };
};

CODE128.prototype.nextC = function nextC(bytes, depth) {
if (bytes.length <= 0) {
return { "result": "", "checksum": 0 };
}

var next, index;

// Special characters
if (bytes[0] >= 200) {
index = bytes[0] - 105;

// Remove first element
bytes.shift();

// Swap to CODE128B
if (index === 100) {
next = this.nextB(bytes, depth + 1);
}
// Swap to CODE128A
else if (index === 101) {
next = this.nextA(bytes, depth + 1);
nextCode = CODE128.next(bytes, pos + 1, set);
}
// Continue on CODE128C but encode a special character
else {
next = this.nextC(bytes, depth + 1);
}
}
// Continue encoding of CODE128C
// Continue encoding
else {
index = (bytes[0] - 48) * 10 + bytes[1] - 48;
bytes.shift();
bytes.shift();
next = this.nextC(bytes, depth + 1);
index = CODE128.correctIndex(bytes, set);
nextCode = CODE128.next(bytes, pos + 1, set);
}

// Get the correct binary encoding and calculate the weight
var enc = this.getEncoding(index);
var weight = index * depth;
var enc = CODE128.getBar(index);
var weight = index * pos;

return { "result": enc + next.result, "checksum": weight + next.checksum };
return {
result: enc + nextCode.result,
checksum: weight + nextCode.checksum
};
};

return CODE128;
}(_Barcode3.default);

var InvalidStartCharacterException = function (_Error) {
_inherits(InvalidStartCharacterException, _Error);

function InvalidStartCharacterException() {
_classCallCheck(this, InvalidStartCharacterException);

var _this2 = _possibleConstructorReturn(this, _Error.call(this));

_this2.name = "InvalidStartCharacterException";
_this2.message = "The encoding does not start with a start character.";
return _this2;
}

return InvalidStartCharacterException;
}(Error);

exports.default = CODE128;
6 changes: 4 additions & 2 deletions bin/barcodes/CODE128/CODE128A.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ var _CODE2 = require('./CODE128.js');

var _CODE3 = _interopRequireDefault(_CODE2);

var _constants = require('./constants');

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
Expand All @@ -22,11 +24,11 @@ var CODE128A = function (_CODE) {
function CODE128A(string, options) {
_classCallCheck(this, CODE128A);

return _possibleConstructorReturn(this, _CODE.call(this, String.fromCharCode(208) + string, options));
return _possibleConstructorReturn(this, _CODE.call(this, _constants.A_START_CHAR + string, options));
}

CODE128A.prototype.valid = function valid() {
return this.data.search(/^[\x00-\x5F\xC8-\xCF]+$/) !== -1;
return new RegExp('^' + _constants.A_CHARS + '+$').test(this.data);
};

return CODE128A;
Expand Down
6 changes: 4 additions & 2 deletions bin/barcodes/CODE128/CODE128B.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ var _CODE2 = require('./CODE128.js');

var _CODE3 = _interopRequireDefault(_CODE2);

var _constants = require('./constants');

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
Expand All @@ -22,11 +24,11 @@ var CODE128B = function (_CODE) {
function CODE128B(string, options) {
_classCallCheck(this, CODE128B);

return _possibleConstructorReturn(this, _CODE.call(this, String.fromCharCode(209) + string, options));
return _possibleConstructorReturn(this, _CODE.call(this, _constants.B_START_CHAR + string, options));
}

CODE128B.prototype.valid = function valid() {
return this.data.search(/^[\x20-\x7F\xC8-\xCF]+$/) !== -1;
return new RegExp('^' + _constants.B_CHARS + '+$').test(this.data);
};

return CODE128B;
Expand Down
Loading

0 comments on commit 58f6d86

Please sign in to comment.