Skip to content
Merged
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
1 change: 1 addition & 0 deletions apps/cards/ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
0.02: Hiding widgets while showing the code
0.03: Added option to use max brightness when showing code
0.04: Minor code improvements
0.05: Add EAN & UPC codes
73 changes: 73 additions & 0 deletions apps/cards/EAN.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* JS source adapted from https://github.com/lindell/JsBarcode
*
* The MIT License (MIT)
*
* Copyright (c) 2016 Johan Lindell (johan@lindell.me)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/

const encode = require("cards.encode.js");
const Barcode = require("cards.Barcode.js");

// Standard start end and middle bits
const SIDE_BIN = '101';
const MIDDLE_BIN = '01010';

// Base class for EAN8 & EAN13
class EAN extends Barcode {
constructor(data, options) {
super(data, options);
}

leftText(from, to) {
return this.text.substr(from, to);
}

leftEncode(data, structure) {
return encode(data, structure);
}

rightText(from, to) {
return this.text.substr(from, to);
}

rightEncode(data, structure) {
return encode(data, structure);
}

encode() {
const data = [
SIDE_BIN,
this.leftEncode(),
MIDDLE_BIN,
this.rightEncode(),
SIDE_BIN
];

return {
data: data.join(''),
text: this.text
};
}

}

module.exports = EAN;
92 changes: 92 additions & 0 deletions apps/cards/EAN13.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// Encoding documentation:
// https://en.wikipedia.org/wiki/International_Article_Number_(EAN)#Binary_encoding_of_data_digits_into_EAN-13_barcode
/*
* JS source adapted from https://github.com/lindell/JsBarcode
*
* The MIT License (MIT)
*
* Copyright (c) 2016 Johan Lindell (johan@lindell.me)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/

const EAN = require("cards.EAN.js");

const EAN13_STRUCTURE = [
'LLLLLL', 'LLGLGG', 'LLGGLG', 'LLGGGL', 'LGLLGG',
'LGGLLG', 'LGGGLL', 'LGLGLG', 'LGLGGL', 'LGGLGL'
];

// Calculate the checksum digit
// https://en.wikipedia.org/wiki/International_Article_Number_(EAN)#Calculation_of_checksum_digit
const checksum = (number) => {
const res = number
.substr(0, 12)
.split('')
.map((n) => +n)
.reduce((sum, a, idx) => (
idx % 2 ? sum + a * 3 : sum + a
), 0);

return (10 - (res % 10)) % 10;
};

class EAN13 extends EAN {

constructor(data, options) {
// Add checksum if it does not exist
if (/^[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]$/.test(data)) {
data += checksum(data);
}

super(data, options);

// Adds a last character to the end of the barcode
this.lastChar = options.lastChar;
}

valid() {
return (
/^[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]$/.test(this.data) &&
+this.data[12] === checksum(this.data)
);
}

leftText() {
return super.leftText(1, 6);
}

leftEncode() {
const data = this.data.substr(1, 6);
const structure = EAN13_STRUCTURE[this.data[0]];
return super.leftEncode(data, structure);
}

rightText() {
return super.rightText(7, 6);
}

rightEncode() {
const data = this.data.substr(7, 6);
return super.rightEncode(data, 'RRRRRR');
}

}

module.exports = EAN13;
82 changes: 82 additions & 0 deletions apps/cards/EAN8.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// Encoding documentation:
// http://www.barcodeisland.com/ean8.phtml
/*
* JS source adapted from https://github.com/lindell/JsBarcode
*
* The MIT License (MIT)
*
* Copyright (c) 2016 Johan Lindell (johan@lindell.me)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/

const EAN = require("cards.EAN.js");

// Calculate the checksum digit
const checksum = (number) => {
const res = number
.substr(0, 7)
.split('')
.map((n) => +n)
.reduce((sum, a, idx) => (
idx % 2 ? sum + a : sum + a * 3
), 0);

return (10 - (res % 10)) % 10;
};

class EAN8 extends EAN {

constructor(data, options) {
// Add checksum if it does not exist
if (/^[0-9][0-9][0-9][0-9][0-9][0-9][0-9]$/.test(data)) {
data += checksum(data);
}

super(data, options);
}

valid() {
return (
/^[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]$/.test(this.data) &&
+this.data[7] === checksum(this.data)
);
}

leftText() {
return super.leftText(0, 4);
}

leftEncode() {
const data = this.data.substr(0, 4);
return super.leftEncode(data, 'LLLL');
}

rightText() {
return super.rightText(4, 4);
}

rightEncode() {
const data = this.data.substr(4, 4);
return super.rightEncode(data, 'RRRR');
}

}

module.exports = EAN8;
79 changes: 79 additions & 0 deletions apps/cards/UPC.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// Encoding documentation:
// https://en.wikipedia.org/wiki/Universal_Product_Code#Encoding
/*
* JS source adapted from https://github.com/lindell/JsBarcode
*
* The MIT License (MIT)
*
* Copyright (c) 2016 Johan Lindell (johan@lindell.me)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/

const encode = require("cards.encode.js");
const Barcode = require("cards.Barcode.js");

class UPC extends Barcode{
constructor(data, options){
// Add checksum if it does not exist
if(/^[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]$/.test(data)){
data += checksum(data);
}

super(data, options);
}

valid(){
return /^[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]$/.test(this.data) &&
this.data[11] == checksum(this.data);
}

encode(){
var result = "";

result += "101";
result += encode(this.data.substr(0, 6), "LLLLLL");
result += "01010";
result += encode(this.data.substr(6, 6), "RRRRRR");
result += "101";

return {
data: result,
text: this.text
};
}
}

// Calulate the checksum digit
// https://en.wikipedia.org/wiki/International_Article_Number_(EAN)#Calculation_of_checksum_digit
function checksum(number){
var result = 0;

var i;
for(i = 1; i < 11; i += 2){
result += parseInt(number[i]);
}
for(i = 0; i < 11; i += 2){
result += parseInt(number[i]) * 3;
}

return (10 - (result % 10)) % 10;
}

module.exports = { UPC, checksum };
Loading