diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2be3fdb --- /dev/null +++ b/.gitignore @@ -0,0 +1,19 @@ + +*.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 +scripts/.DS_Store diff --git a/scripts/BarcodeParser.js b/scripts/BarcodeParser.js index 43bdc88..8b53248 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 Error.InvalidNumAI(1, "在'0'后面的AI标识无效"); case "02": - throw "invalid AI after '1'"; + throw Error.InvalidNumAI(2, "在'1'后面的AI标识无效"); case "03": - throw "invalid AI after '24'"; + throw Error.InvalidNumAI(3, "在'24'后面的AI标识无效"); case "04": - throw "invalid AI after '25'"; + throw Error.InvalidNumAI(4, "在'25'后面的AI标识无效"); case "05": - throw "invalid AI after '2'"; + throw Error.InvalidNumAI(5, "在'2'后面的AI标识无效"); case "06": - throw "invalid AI after '31'"; + throw Error.InvalidNumAI(6, "在'31'后面的AI标识无效"); case "07": - throw "invalid AI after '32'"; + throw Error.InvalidNumAI(7, "在'32'后面的AI标识无效"); case "08": - throw "invalid AI after '33'"; + throw Error.InvalidNumAI(8, "在'33'后面的AI标识无效"); case "09": - throw "invalid AI after '34'"; + throw Error.InvalidNumAI(9, "在'34'后面的AI标识无效"); case "10": - throw "invalid AI after '35'"; + throw Error.InvalidNumAI(10, "在'35'后面的AI标识无效"); case "11": - throw "invalid AI after '36'"; + throw Error.InvalidNumAI(11, "在'36'后面的AI标识无效"); case "12": - throw "invalid AI after '39'"; + throw Error.InvalidNumAI(12, "在'39'后面的AI标识无效"); case "13": - throw "invalid AI after '3'"; + throw Error.InvalidNumAI(13, "在'3'后面的AI标识无效"); case "14": - throw "invalid AI after '40'"; + throw Error.InvalidNumAI(14, "在'40'后面的AI标识无效"); case "15": - throw "invalid AI after '41'"; + throw Error.InvalidNumAI(15, "在'41'后面的AI标识无效"); case "16": - throw "invalid AI after '42'"; + throw Error.InvalidNumAI(16, "在'42'后面的AI标识无效"); case "17": - throw "invalid AI after '4'"; + throw Error.InvalidNumAI(17, "在'4'后面的AI标识无效"); case "18": - throw "invalid AI after '700'"; + throw Error.InvalidNumAI(18, "在'700'后面的AI标识无效"); case "19": - throw "invalid AI after '70'"; + throw Error.InvalidNumAI(19, "在'70'后面的AI标识无效"); case "20": - throw "invalid AI after '71'"; + throw Error.InvalidNumAI(20, "在'71'后面的AI标识无效"); case "21": - throw "invalid AI after '7'"; + throw Error.InvalidNumAI(21, "在'7'后面的AI标识无效"); case "22": - throw "invalid AI after '800'"; + throw Error.InvalidNumAI(22, "在'800'后面的AI标识无效"); case "23": - throw "invalid AI after '801'"; + throw Error.InvalidNumAI(23, "在'801'后面的AI标识无效"); case "24": - throw "invalid AI after '802'"; + throw Error.InvalidNumAI(24, "在'802'后面的AI标识无效"); case "25": - throw "invalid AI after '80'"; + throw Error.InvalidNumAI(25, "在'80'后面的AI标识无效"); case "26": - throw "invalid AI after '810'"; + throw Error.InvalidNumAI(26, "在'810'后面的AI标识无效"); case "27": - throw "invalid AI after '811'"; + throw Error.InvalidNumAI(27, "在'811'后面的AI标识无效"); case "28": - throw "invalid AI after '81'"; + throw Error.InvalidNumAI(28, "在'81'后面的AI标识无效"); case "29": - throw "invalid AI after '82'"; + throw Error.InvalidNumAI(29, "在'82'后面的AI标识无效"); case "30": - throw "invalid AI after '8'"; + throw Error.InvalidNumAI(30, "在'8'后面的AI标识无效"); case "31": - throw "invalid AI after '9'"; + throw Error.InvalidNumAI(31, "在'9'后面的AI标识无效"); case "32": - throw "no valid AI"; + throw Error.InvalidNormalAI; case "33": - throw "invalid year in date"; + throw Error.InvalidDate(33, "日期中的年份有误"); case "34": - throw "invalid month in date"; + throw Error.InvalidDate(34, "日期中的月份有误"); case "35": - throw "invalid day in date"; + throw Error.InvalidDate(35, "日期中日有误"); case "36": - throw "invalid number"; + throw Error.InvalidNum; default: - throw "unknown error"; + throw Error.UnkownErr; } } } @@ -1345,5 +1344,24 @@ var parseBarcode = (function () { */ return answer; } - return parseBarcode; -}()); \ No newline at end of file + +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. + */ +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..5513e1c --- /dev/null +++ b/scripts/gs1Barcode.js @@ -0,0 +1,45 @@ +/** + * 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', //序列号 + ai37: 'count' //物流单位内含商品品项数 +} + +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; + 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..d2bebad --- /dev/null +++ b/scripts/package.json @@ -0,0 +1,19 @@ +{ + "name": "gs1-parser", + "version": "1.0.7", + "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" + } +}