From 7fd939e3756dcf08398fdc4d56f25aae55fc9d92 Mon Sep 17 00:00:00 2001 From: flashcloud Date: Tue, 20 Jun 2017 16:59:33 +0800 Subject: [PATCH 1/7] change to module for nodejs --- .gitignore | 18 +++++++++ scripts/BarcodeParser.js | 84 +++++++++++++++++++++------------------- scripts/gs1Barcode.js | 42 ++++++++++++++++++++ scripts/package.json | 19 +++++++++ 4 files changed, 123 insertions(+), 40 deletions(-) create mode 100644 .gitignore create mode 100644 scripts/gs1Barcode.js create mode 100644 scripts/package.json diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..950e5d7 --- /dev/null +++ b/.gitignore @@ -0,0 +1,18 @@ + +*.iml + +.idea/.name + +.idea/compiler.xml + +.idea/copyright/profiles_settings.xml + +.idea/workspace.xml + +.idea/vcs.xml + +.idea/modules.xml + +.idea/misc.xml + +.idea/encodings.xml diff --git a/scripts/BarcodeParser.js b/scripts/BarcodeParser.js index 43bdc88..b19871b 100644 --- a/scripts/BarcodeParser.js +++ b/scripts/BarcodeParser.js @@ -12,7 +12,6 @@ * * encapsulating the barcode parsing function in an anonymous, self-executing function */ -var parseBarcode = (function () { 'use strict'; /** * This is the main routine provided by the parseBarcode library. It takes a string, @@ -1263,79 +1262,79 @@ var parseBarcode = (function () { } catch (e) { switch (e) { case "01": - throw "invalid AI after '0'"; + throw "在'0'后面的AI标识无效"; case "02": - throw "invalid AI after '1'"; + throw "在'1'后面的AI标识无效"; case "03": - throw "invalid AI after '24'"; + throw "在'24'后面的AI标识无效"; case "04": - throw "invalid AI after '25'"; + throw "在'25'后面的AI标识无效"; case "05": - throw "invalid AI after '2'"; + throw "在'2'后面的AI标识无效"; case "06": - throw "invalid AI after '31'"; + throw "在'31'后面的AI标识无效"; case "07": - throw "invalid AI after '32'"; + throw "在'32'后面的AI标识无效"; case "08": - throw "invalid AI after '33'"; + throw "在'33'后面的AI标识无效"; case "09": - throw "invalid AI after '34'"; + throw "在'34'后面的AI标识无效"; case "10": - throw "invalid AI after '35'"; + throw "在'35'后面的AI标识无效"; case "11": - throw "invalid AI after '36'"; + throw "在'36'后面的AI标识无效"; case "12": - throw "invalid AI after '39'"; + throw "在'39'后面的AI标识无效"; case "13": - throw "invalid AI after '3'"; + throw "在'3'后面的AI标识无效"; case "14": - throw "invalid AI after '40'"; + throw "在'40'后面的AI标识无效"; case "15": - throw "invalid AI after '41'"; + throw "在'41'后面的AI标识无效"; case "16": - throw "invalid AI after '42'"; + throw "在'42'后面的AI标识无效"; case "17": - throw "invalid AI after '4'"; + throw "在'4'后面的AI标识无效"; case "18": - throw "invalid AI after '700'"; + throw "在'700'后面的AI标识无效"; case "19": - throw "invalid AI after '70'"; + throw "在'70'后面的AI标识无效"; case "20": - throw "invalid AI after '71'"; + throw "在'71'后面的AI标识无效"; case "21": - throw "invalid AI after '7'"; + throw "在'7'后面的AI标识无效"; case "22": - throw "invalid AI after '800'"; + throw "在'800'后面的AI标识无效"; case "23": - throw "invalid AI after '801'"; + throw "在'801'后面的AI标识无效"; case "24": - throw "invalid AI after '802'"; + throw "在'802'后面的AI标识无效"; case "25": - throw "invalid AI after '80'"; + throw "在'80'后面的AI标识无效"; case "26": - throw "invalid AI after '810'"; + throw "在'810'后面的AI标识无效"; case "27": - throw "invalid AI after '811'"; + throw "在'811'后面的AI标识无效"; case "28": - throw "invalid AI after '81'"; + throw "在'81'后面的AI标识无效"; case "29": - throw "invalid AI after '82'"; + throw "在'82'后面的AI标识无效"; case "30": - throw "invalid AI after '8'"; + throw "在'8'后面的AI标识无效"; case "31": - throw "invalid AI after '9'"; + throw "在'9'后面的AI标识无效"; case "32": - throw "no valid AI"; + throw "AI标识无效"; case "33": - throw "invalid year in date"; + throw "日期中的年份有误"; case "34": - throw "invalid month in date"; + throw "日期中的月份有误"; case "35": - throw "invalid day in date"; + throw "日期中日有误"; case "36": - throw "invalid number"; + throw "无效的数字"; default: - throw "unknown error"; + throw "未知的错误"; } } } @@ -1345,5 +1344,10 @@ var parseBarcode = (function () { */ return answer; } - return parseBarcode; -}()); \ No newline at end of file + +/** + * Created by flashcloud on 17/6/15. + */ +module.exports = { + decode: parseBarcode +}; \ No newline at end of file diff --git a/scripts/gs1Barcode.js b/scripts/gs1Barcode.js new file mode 100644 index 0000000..0a54c5d --- /dev/null +++ b/scripts/gs1Barcode.js @@ -0,0 +1,42 @@ +/** + * GS1 Barcode Object + */ + +var parser = require('./BarcodeParser'); + +var AIMap = { + ai00: 'SSCC', + ai01: 'GTIN', + ai02: 'product', + ai10: 'lot', + ai11: 'prodDate', //生产日期 + ai13: 'packDate', //包装日期 + ai15: 'safeDate', //保质期, Best Before Date + ai17: 'exp', + ai21: 'serial' //序列号 +} + +function buildGS1BarcodeObj(barcode) { + var parseData = parser.decode(barcode); + var parsedCodeItems = parseData.parsedCodeItems; + var itemsCount = parsedCodeItems.length; + var i = 0; + var buildedObj = {}; + var ai; + var item; + var prop; + + for (i = 0; i < itemsCount; i++) { + item = parsedCodeItems[i]; + ai = item.ai; + prop = AIMap["ai" + ai]; + if (!(prop === null || prop === undefined)) + buildedObj[prop] = item.data; + } + + return buildedObj +} + +module.exports = { + decode: buildGS1BarcodeObj +}; diff --git a/scripts/package.json b/scripts/package.json new file mode 100644 index 0000000..328bc56 --- /dev/null +++ b/scripts/package.json @@ -0,0 +1,19 @@ +{ + "name": "gs1-parser", + "version": "1.0.2", + "description": "a hibc-barcode parser. fork it from https://github.com/PeterBrockfeld/BarcodeParser", + "main": "gs1Barcode.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "keywords": [ + "gs1", + "barcode" + ], + "author": "sungole", + "license": "MIT", + "repository": { + "type": "git", + "url": "git+https://github.com/flashcloud/BarcodeParser.git" + } +} From befe5057f3b2b5f691a5ff2ebf002d9f279fed4b Mon Sep 17 00:00:00 2001 From: flashcloud Date: Tue, 20 Jun 2017 17:04:41 +0800 Subject: [PATCH 2/7] delete console.log --- scripts/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/package.json b/scripts/package.json index 328bc56..02a5956 100644 --- a/scripts/package.json +++ b/scripts/package.json @@ -1,6 +1,6 @@ { "name": "gs1-parser", - "version": "1.0.2", + "version": "1.0.3", "description": "a hibc-barcode parser. fork it from https://github.com/PeterBrockfeld/BarcodeParser", "main": "gs1Barcode.js", "scripts": { From 0c1b950951d62118ed77d74e23716f7ea5f6aaf0 Mon Sep 17 00:00:00 2001 From: flashcloud Date: Wed, 21 Jun 2017 14:25:17 +0800 Subject: [PATCH 3/7] friendly error --- scripts/BarcodeParser.js | 88 +++++++++++++++++++++++----------------- scripts/package.json | 2 +- 2 files changed, 52 insertions(+), 38 deletions(-) diff --git a/scripts/BarcodeParser.js b/scripts/BarcodeParser.js index b19871b..8b53248 100644 --- a/scripts/BarcodeParser.js +++ b/scripts/BarcodeParser.js @@ -1262,79 +1262,79 @@ } catch (e) { switch (e) { case "01": - throw "在'0'后面的AI标识无效"; + throw Error.InvalidNumAI(1, "在'0'后面的AI标识无效"); case "02": - throw "在'1'后面的AI标识无效"; + throw Error.InvalidNumAI(2, "在'1'后面的AI标识无效"); case "03": - throw "在'24'后面的AI标识无效"; + throw Error.InvalidNumAI(3, "在'24'后面的AI标识无效"); case "04": - throw "在'25'后面的AI标识无效"; + throw Error.InvalidNumAI(4, "在'25'后面的AI标识无效"); case "05": - throw "在'2'后面的AI标识无效"; + throw Error.InvalidNumAI(5, "在'2'后面的AI标识无效"); case "06": - throw "在'31'后面的AI标识无效"; + throw Error.InvalidNumAI(6, "在'31'后面的AI标识无效"); case "07": - throw "在'32'后面的AI标识无效"; + throw Error.InvalidNumAI(7, "在'32'后面的AI标识无效"); case "08": - throw "在'33'后面的AI标识无效"; + throw Error.InvalidNumAI(8, "在'33'后面的AI标识无效"); case "09": - throw "在'34'后面的AI标识无效"; + throw Error.InvalidNumAI(9, "在'34'后面的AI标识无效"); case "10": - throw "在'35'后面的AI标识无效"; + throw Error.InvalidNumAI(10, "在'35'后面的AI标识无效"); case "11": - throw "在'36'后面的AI标识无效"; + throw Error.InvalidNumAI(11, "在'36'后面的AI标识无效"); case "12": - throw "在'39'后面的AI标识无效"; + throw Error.InvalidNumAI(12, "在'39'后面的AI标识无效"); case "13": - throw "在'3'后面的AI标识无效"; + throw Error.InvalidNumAI(13, "在'3'后面的AI标识无效"); case "14": - throw "在'40'后面的AI标识无效"; + throw Error.InvalidNumAI(14, "在'40'后面的AI标识无效"); case "15": - throw "在'41'后面的AI标识无效"; + throw Error.InvalidNumAI(15, "在'41'后面的AI标识无效"); case "16": - throw "在'42'后面的AI标识无效"; + throw Error.InvalidNumAI(16, "在'42'后面的AI标识无效"); case "17": - throw "在'4'后面的AI标识无效"; + throw Error.InvalidNumAI(17, "在'4'后面的AI标识无效"); case "18": - throw "在'700'后面的AI标识无效"; + throw Error.InvalidNumAI(18, "在'700'后面的AI标识无效"); case "19": - throw "在'70'后面的AI标识无效"; + throw Error.InvalidNumAI(19, "在'70'后面的AI标识无效"); case "20": - throw "在'71'后面的AI标识无效"; + throw Error.InvalidNumAI(20, "在'71'后面的AI标识无效"); case "21": - throw "在'7'后面的AI标识无效"; + throw Error.InvalidNumAI(21, "在'7'后面的AI标识无效"); case "22": - throw "在'800'后面的AI标识无效"; + throw Error.InvalidNumAI(22, "在'800'后面的AI标识无效"); case "23": - throw "在'801'后面的AI标识无效"; + throw Error.InvalidNumAI(23, "在'801'后面的AI标识无效"); case "24": - throw "在'802'后面的AI标识无效"; + throw Error.InvalidNumAI(24, "在'802'后面的AI标识无效"); case "25": - throw "在'80'后面的AI标识无效"; + throw Error.InvalidNumAI(25, "在'80'后面的AI标识无效"); case "26": - throw "在'810'后面的AI标识无效"; + throw Error.InvalidNumAI(26, "在'810'后面的AI标识无效"); case "27": - throw "在'811'后面的AI标识无效"; + throw Error.InvalidNumAI(27, "在'811'后面的AI标识无效"); case "28": - throw "在'81'后面的AI标识无效"; + throw Error.InvalidNumAI(28, "在'81'后面的AI标识无效"); case "29": - throw "在'82'后面的AI标识无效"; + throw Error.InvalidNumAI(29, "在'82'后面的AI标识无效"); case "30": - throw "在'8'后面的AI标识无效"; + throw Error.InvalidNumAI(30, "在'8'后面的AI标识无效"); case "31": - throw "在'9'后面的AI标识无效"; + throw Error.InvalidNumAI(31, "在'9'后面的AI标识无效"); case "32": - throw "AI标识无效"; + throw Error.InvalidNormalAI; case "33": - throw "日期中的年份有误"; + throw Error.InvalidDate(33, "日期中的年份有误"); case "34": - throw "日期中的月份有误"; + throw Error.InvalidDate(34, "日期中的月份有误"); case "35": - throw "日期中日有误"; + throw Error.InvalidDate(35, "日期中日有误"); case "36": - throw "无效的数字"; + throw Error.InvalidNum; default: - throw "未知的错误"; + throw Error.UnkownErr; } } } @@ -1345,6 +1345,20 @@ return answer; } +Error.BarcodeNotANum = {num:1, code: '', des:''}; +Error.EmptyBarcode = {num:2, code: '', des:''}; +Error.BarcodeNotGS1 = {num:3, code: '', des:''}; +Error.InvalidBarcode = {num:4, code: '', des:''}; +Error.InvalidNumAI = function(code, des) { + return { num: 5, code: code, des: des }; +}; +Error.InvalidNormalAI = {num: 6, code: '32', des: "AI标识无效"}; +Error.InvalidDate = function(code, des) { + return { num: 7, code: code, des: des }; +}; +Error.InvalidNum = {num:8, code: '36', des:'无效的数字'}; +Error.UnkownErr = {num:9, code: '', des:'未知的错误'}; + /** * Created by flashcloud on 17/6/15. */ diff --git a/scripts/package.json b/scripts/package.json index 02a5956..704030e 100644 --- a/scripts/package.json +++ b/scripts/package.json @@ -1,6 +1,6 @@ { "name": "gs1-parser", - "version": "1.0.3", + "version": "1.0.4", "description": "a hibc-barcode parser. fork it from https://github.com/PeterBrockfeld/BarcodeParser", "main": "gs1Barcode.js", "scripts": { From e05bf78e6d2fc4d8fb874ceeb2b0d443f843f682 Mon Sep 17 00:00:00 2001 From: flashcloud Date: Fri, 1 Sep 2017 14:40:50 +0800 Subject: [PATCH 4/7] Replacing parentheses in the barcode --- scripts/gs1Barcode.js | 1 + 1 file changed, 1 insertion(+) diff --git a/scripts/gs1Barcode.js b/scripts/gs1Barcode.js index 0a54c5d..3c9f275 100644 --- a/scripts/gs1Barcode.js +++ b/scripts/gs1Barcode.js @@ -17,6 +17,7 @@ var AIMap = { } function buildGS1BarcodeObj(barcode) { + barcode = barcode.replace(/\(/g, "").replace(/\)/g, ""); var parseData = parser.decode(barcode); var parsedCodeItems = parseData.parsedCodeItems; var itemsCount = parsedCodeItems.length; From 7dbe29b23ce4e22cf9c258f58e1df5a06b66f85b Mon Sep 17 00:00:00 2001 From: flashcloud Date: Fri, 1 Sep 2017 15:36:59 +0800 Subject: [PATCH 5/7] chage version --- scripts/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/package.json b/scripts/package.json index 704030e..e9b0dd7 100644 --- a/scripts/package.json +++ b/scripts/package.json @@ -1,6 +1,6 @@ { "name": "gs1-parser", - "version": "1.0.4", + "version": "1.0.5", "description": "a hibc-barcode parser. fork it from https://github.com/PeterBrockfeld/BarcodeParser", "main": "gs1Barcode.js", "scripts": { From ea8d93fc1b5c94569882b6c43a0b435751fa6486 Mon Sep 17 00:00:00 2001 From: flashcloud Date: Fri, 1 Sep 2017 16:11:11 +0800 Subject: [PATCH 6/7] Enhance the parenthesized barcode --- scripts/gs1Barcode.js | 5 +++-- scripts/package.json | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/scripts/gs1Barcode.js b/scripts/gs1Barcode.js index 3c9f275..6d79f18 100644 --- a/scripts/gs1Barcode.js +++ b/scripts/gs1Barcode.js @@ -13,11 +13,12 @@ var AIMap = { ai13: 'packDate', //包装日期 ai15: 'safeDate', //保质期, Best Before Date ai17: 'exp', - ai21: 'serial' //序列号 + ai21: 'serial', //序列号 + ai37: 'count' //物流单位内含商品品项数 } function buildGS1BarcodeObj(barcode) { - barcode = barcode.replace(/\(/g, "").replace(/\)/g, ""); + barcode = barcode.replace(/\(/g, String.fromCharCode(29)).replace(/\)/g, ""); var parseData = parser.decode(barcode); var parsedCodeItems = parseData.parsedCodeItems; var itemsCount = parsedCodeItems.length; diff --git a/scripts/package.json b/scripts/package.json index e9b0dd7..3bd805f 100644 --- a/scripts/package.json +++ b/scripts/package.json @@ -1,6 +1,6 @@ { "name": "gs1-parser", - "version": "1.0.5", + "version": "1.0.6", "description": "a hibc-barcode parser. fork it from https://github.com/PeterBrockfeld/BarcodeParser", "main": "gs1Barcode.js", "scripts": { From b3658caef0d43b724c2d6d854a8c7d045b49cd52 Mon Sep 17 00:00:00 2001 From: flashcloud Date: Thu, 21 Apr 2022 12:14:37 +0800 Subject: [PATCH 7/7] Only the letters and numbers in the barcode are extracted Only the letters and numbers in the barcode are extracted, and special characters in the image coding are prohibited --- .gitignore | 1 + scripts/gs1Barcode.js | 1 + scripts/package.json | 2 +- 3 files changed, 3 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 950e5d7..2be3fdb 100644 --- a/.gitignore +++ b/.gitignore @@ -16,3 +16,4 @@ .idea/misc.xml .idea/encodings.xml +scripts/.DS_Store diff --git a/scripts/gs1Barcode.js b/scripts/gs1Barcode.js index 6d79f18..5513e1c 100644 --- a/scripts/gs1Barcode.js +++ b/scripts/gs1Barcode.js @@ -19,6 +19,7 @@ var AIMap = { function buildGS1BarcodeObj(barcode) { barcode = barcode.replace(/\(/g, String.fromCharCode(29)).replace(/\)/g, ""); + barcode = barcode.replace(/[^A-Za-z0-9]+/g, ""); //只提取里面的字母和数字,禁止图像编码中的特殊字符扫入 var parseData = parser.decode(barcode); var parsedCodeItems = parseData.parsedCodeItems; var itemsCount = parsedCodeItems.length; diff --git a/scripts/package.json b/scripts/package.json index 3bd805f..d2bebad 100644 --- a/scripts/package.json +++ b/scripts/package.json @@ -1,6 +1,6 @@ { "name": "gs1-parser", - "version": "1.0.6", + "version": "1.0.7", "description": "a hibc-barcode parser. fork it from https://github.com/PeterBrockfeld/BarcodeParser", "main": "gs1Barcode.js", "scripts": {