-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
23 changed files
with
608 additions
and
684 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.