From a925f094781704355c7341dd0d8effca8f4067de Mon Sep 17 00:00:00 2001 From: Toni Hoffmann Date: Mon, 26 Jun 2017 22:33:39 +0200 Subject: [PATCH 01/13] implementing further nodes, reorganizing logic --- SwiftGS1Barcode/GS1Barcode.swift | 63 +++++++++--- SwiftGS1BarcodeTests/BarcodeParserTests.swift | 1 - SwiftGS1BarcodeTests/GS1BarcodeTests.swift | 96 +++++++++++++++---- 3 files changed, 129 insertions(+), 31 deletions(-) diff --git a/SwiftGS1Barcode/GS1Barcode.swift b/SwiftGS1Barcode/GS1Barcode.swift index f9cfb94..680e9b6 100644 --- a/SwiftGS1Barcode/GS1Barcode.swift +++ b/SwiftGS1Barcode/GS1Barcode.swift @@ -16,6 +16,17 @@ struct GS1Nodes{ var expirationDateNode = GS1Node(dateIdentifier: "17") var serialNumberNode = GS1Node("21", length: 20, type: .String, dynamicLength: true) var amountNode = GS1Node("30", length: 8, type: .Int, dynamicLength: true) + + // Experimental Support + // var serialShippingContainerCodeNode = GS1Node("0", length: 18, type: .String) + // var gtinOfContainedTradeItemsNode = GS1Node("2", length: 14, type: .String) + var productionDateNode = GS1Node(dateIdentifier: "11") + var dueDateNode = GS1Node(dateIdentifier: "12") + var packagingDateNode = GS1Node(dateIdentifier: "13") + var bestBeforeDateNode = GS1Node(dateIdentifier: "15") + var productVariantNode = GS1Node("20", length: 2, type: .String) + var secondaryDataFieldsNode = GS1Node("22", length:29, type: .String, dynamicLength:true) + var numberOfUnitsContainedNode = GS1Node("37", length:8, type: .String, dynamicLength:true) } public class GS1Barcode: NSObject, Barcode { @@ -31,6 +42,19 @@ public class GS1Barcode: NSObject, Barcode { public var amount: Int?{ get {return nodes.amountNode.intValue} } public var gtinIndicatorDigit: Int? {get {return nodes.gtinIndicatorDigitNode.intValue}} + // Experimental Support + public var productionDate: NSDate? {get{return nodes.productionDateNode.dateValue}} + public var dueDate: NSDate? {get{return nodes.dueDateNode.dateValue}} + public var packagingDate: NSDate? {get{return nodes.packagingDateNode.dateValue}} + public var bestBeforeDate: NSDate? {get{return nodes.bestBeforeDateNode.dateValue}} + public var productVariant: String? {get{return nodes.productVariantNode.stringValue}} + public var secondaryDataFields: String? {get{return nodes.secondaryDataFieldsNode.stringValue}} + public var numberOfUnitsContained: String? {get{return nodes.numberOfUnitsContainedNode.stringValue}} + + // Super Experimental Support + // public var serialShippingContainerCode: String? {get{return nodes.serialShippingContainerCodeNode.stringValue}} + // public var gtinOfContainedTradeItems: String? {get{return nodes.gtinOfContainedTradeItemsNode.stringValue}} + required override public init() { super.init() } @@ -45,6 +69,15 @@ public class GS1Barcode: NSObject, Barcode { return parseSuccessFull && raw != "" && raw != nil } + func parseNode(node: inout GS1Node, data: inout String)->Bool{ + if(data.startsWith(node.identifier)){ + node = GS1BarcodeParser.parseGS1Node(node: node, data: data) + data = GS1BarcodeParser.reduce(data: data, by: node)! + return true + } + return false + } + func parse() ->Bool{ self.parseSuccessFull = false var data = raw @@ -59,19 +92,23 @@ public class GS1Barcode: NSObject, Barcode { nodes.gtinNode = GS1BarcodeParser.parseGS1Node(node: nodes.gtinNode, data: data!) nodes.gtinIndicatorDigitNode = GS1BarcodeParser.parseGS1Node(node: nodes.gtinIndicatorDigitNode, data: data!) data = GS1BarcodeParser.reduce(data: data, by: nodes.gtinNode) - }else if(data!.startsWith(nodes.lotNumberNode.identifier)){ - nodes.lotNumberNode = GS1BarcodeParser.parseGS1Node(node: nodes.lotNumberNode, data: data!) - data = GS1BarcodeParser.reduce(data: data, by: nodes.lotNumberNode) - }else if(data!.startsWith(nodes.expirationDateNode.identifier)){ - nodes.expirationDateNode = GS1BarcodeParser.parseGS1Node(node: nodes.expirationDateNode, data: data!) - data = GS1BarcodeParser.reduce(data: data, by: nodes.expirationDateNode) - }else if(data!.startsWith(nodes.serialNumberNode.identifier)){ - nodes.serialNumberNode = GS1BarcodeParser.parseGS1Node(node: nodes.serialNumberNode, data: data!) - data = GS1BarcodeParser.reduce(data: data, by: nodes.serialNumberNode) - }else if(data!.startsWith(nodes.amountNode.identifier)){ - nodes.amountNode = GS1BarcodeParser.parseGS1Node(node: nodes.amountNode, data: data!) - data = GS1BarcodeParser.reduce(data: data, by: nodes.amountNode) - }else{ + } + else if(parseNode(node: &nodes.lotNumberNode, data: &data!)){} + else if parseNode(node: &nodes.expirationDateNode, data: &data!){} + else if parseNode(node: &nodes.serialNumberNode, data: &data!){} + else if parseNode(node: &nodes.amountNode, data: &data!){} + // Experimental Support + else if(parseNode(node: &nodes.productionDateNode, data: &data!)){} + else if(parseNode(node: &nodes.dueDateNode, data: &data!)){} + else if(parseNode(node: &nodes.packagingDateNode, data: &data!)){} + else if(parseNode(node: &nodes.bestBeforeDateNode, data: &data!)){} + else if(parseNode(node: &nodes.productVariantNode, data: &data!)){} + else if(parseNode(node: &nodes.secondaryDataFieldsNode, data: &data!)){} + else if(parseNode(node: &nodes.numberOfUnitsContainedNode, data: &data!)){} + // Supper Experimental Support + // else if(parseNode(node: &nodes.serialShippingContainerCodeNode, data: &data!)){} + // else if(parseNode(node: &nodes.gtinOfContainedTradeItemsNode, data: &data!)){} + else{ print("Do not know identifier. Canceling Parsing") return false } diff --git a/SwiftGS1BarcodeTests/BarcodeParserTests.swift b/SwiftGS1BarcodeTests/BarcodeParserTests.swift index 2b00360..075a0b4 100644 --- a/SwiftGS1BarcodeTests/BarcodeParserTests.swift +++ b/SwiftGS1BarcodeTests/BarcodeParserTests.swift @@ -57,5 +57,4 @@ class BarcodeParserTests: XCTestCase { XCTAssertEqual(node.intValue, nil) XCTAssertEqual(node.dateValue, nil) } - } diff --git a/SwiftGS1BarcodeTests/GS1BarcodeTests.swift b/SwiftGS1BarcodeTests/GS1BarcodeTests.swift index 2aece8c..66844bd 100644 --- a/SwiftGS1BarcodeTests/GS1BarcodeTests.swift +++ b/SwiftGS1BarcodeTests/GS1BarcodeTests.swift @@ -21,7 +21,7 @@ class GS1BarcodeTests: XCTestCase { super.tearDown() barcode = nil } - + func testExample() { let barcode = GS1Barcode(raw: "01001234670210133001\u{1D}2110066600") XCTAssert(barcode.validate()) @@ -36,7 +36,7 @@ class GS1BarcodeTests: XCTestCase { XCTAssertEqual(barcode.gtin, "00123467021013") XCTAssertEqual(barcode.serialNumber, "10066600") } - + func testEmptyBarcode(){ barcode = GS1Barcode(raw: "") @@ -55,30 +55,17 @@ class GS1BarcodeTests: XCTestCase { XCTAssertNotEqual(barcode.gtin, "") XCTAssertEqual(barcode.gtin, "10123467042022") } - + func testGETINIndicator(){ XCTAssertNotNil(barcode.gtinIndicatorDigit) XCTAssertEqual(barcode.gtinIndicatorDigit, 1) } - func testGETINIndicatorDifferentValue(){ - barcode = GS1Barcode(raw: "01201234670420223005\u{1d}172101311010022247") - XCTAssertNotNil(barcode.gtinIndicatorDigit) - XCTAssertEqual(barcode.gtinIndicatorDigit, 2) - } - func testGETINIndicatorEmpty(){ - barcode = GS1Barcode(raw: "01001234670420223005\u{1d}172101311010022247") - XCTAssertNotNil(barcode.gtinIndicatorDigit) - XCTAssertEqual(barcode.gtinIndicatorDigit, 0) - } - func testGETINIndicatorEmptyBarcode(){ - barcode = GS1Barcode(raw: "") - XCTAssertNil(barcode.gtinIndicatorDigit) - } func testLot(){ XCTAssertNotNil(barcode.lotNumber) XCTAssertEqual(barcode.lotNumber, "10022247") } + func testSerial(){ XCTAssertNil(barcode.serialNumber) } @@ -93,6 +80,23 @@ class GS1BarcodeTests: XCTestCase { XCTAssertNotNil(barcode.expirationDate) XCTAssertEqual(barcode.expirationDate, NSDate.from(year: 2021, month: 1, day: 31)) } + + + func testGETINIndicatorDifferentValue(){ + barcode = GS1Barcode(raw: "01201234670420223005\u{1d}172101311010022247") + XCTAssertNotNil(barcode.gtinIndicatorDigit) + XCTAssertEqual(barcode.gtinIndicatorDigit, 2) + } + func testGETINIndicatorEmpty(){ + barcode = GS1Barcode(raw: "01001234670420223005\u{1d}172101311010022247") + XCTAssertNotNil(barcode.gtinIndicatorDigit) + XCTAssertEqual(barcode.gtinIndicatorDigit, 0) + } + func testGETINIndicatorEmptyBarcode(){ + barcode = GS1Barcode(raw: "") + XCTAssertNil(barcode.gtinIndicatorDigit) + } + func testValidate(){ XCTAssert(barcode.validate()) } @@ -106,4 +110,62 @@ class GS1BarcodeTests: XCTestCase { XCTAssertFalse(b.validate()) } + + // Experimental Support + +// func testserialShippingContainerCodeNode(){ +// //var serialShippingContainerCodeNode = GS1Node("0", length: 18, type: .String) +// barcode = GS1Barcode(raw: "02123456789012345678901234567890") +// XCTAssertNotNil(barcode.serialShippingContainerCode) +// // XCTAssertEqual(barcode.serialShippingContainerCode, "") +// } +// func testgtinOfContainedTradeItemsNode(){ +// //var gtinOfContainedTradeItemsNode = GS1Node("2", length: 14, type: .String) +// barcode = GS1Barcode(raw: "22123456789012345678901234567890") +// XCTAssertNotNil(barcode.gtinOfContainedTradeItems) +// // XCTAssertEqual(barcode.gtinOfContainedTradeItems, "") +// } + func testproductionDateNode(){ + //var productionDateNode = GS1Node(dateIdentifier: "11") + barcode = GS1Barcode(raw: "11210110") + XCTAssertNotNil(barcode.productionDate) + XCTAssertEqual(barcode.productionDate, NSDate.from(year: 2021, month: 1, day: 10)) + } + func testdueDateNode(){ + //var dueDateNode = GS1Node(dateIdentifier: "12") + barcode = GS1Barcode(raw: "12210110") + XCTAssertNotNil(barcode.dueDate) + XCTAssertEqual(barcode.dueDate, NSDate.from(year: 2021, month: 1, day: 10)) + } + func testpackagingDateNode(){ + //var packagingDateNode = GS1Node(dateIdentifier: "13") + barcode = GS1Barcode(raw: "13210110") + XCTAssertNotNil(barcode.packagingDate) + XCTAssertEqual(barcode.packagingDate, NSDate.from(year: 2021, month: 1, day: 10)) + } + func testbestBeforeDateNode(){ + //var bestBeforeDateNode = GS1Node(dateIdentifier: "15") + barcode = GS1Barcode(raw: "15210110") + XCTAssertNotNil(barcode.bestBeforeDate) + XCTAssertEqual(barcode.bestBeforeDate, NSDate.from(year: 2021, month: 1, day: 10)) + } + func testproductVariantNode(){ + //var productVariantNode = GS1Node("20", length: 2, type: .String) + barcode = GS1Barcode(raw: "2023456789012345678901234567890") + XCTAssertNotNil(barcode.productVariant) + // XCTAssertEqual(barcode.productVariant, "") + } + + func testsecondaryDataFieldsNode(){ + //var secondaryDataFieldsNode = GS1Node("22", length:29, type: .String, dynamicLength:true) + barcode = GS1Barcode(raw: "22123456789012345678901234567890") + XCTAssertNotNil(barcode.secondaryDataFields) + // XCTAssertEqual(barcode.secondaryDataFields, "") + } + func testnumberOfUnitsContainedNode(){ + //var numberOfUnitsContainedNode = GS1Node("37", length:8, type: .String, dynamicLength:true) + barcode = GS1Barcode(raw: "37123456789012345678901234567890") + XCTAssertNotNil(barcode.numberOfUnitsContained) + // XCTAssertEqual(barcode.numberOfUnitsContained, "") + } } From 44f9b436400841435cd49756a5afd23cf4e15f67 Mon Sep 17 00:00:00 2001 From: Toni Hoffmann Date: Mon, 26 Jun 2017 22:35:12 +0200 Subject: [PATCH 02/13] updating docs to fit planned values --- README.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/README.md b/README.md index f285fb9..a1fb016 100644 --- a/README.md +++ b/README.md @@ -29,6 +29,18 @@ print(barcode.lotNumber) // S123456 - `serialNumber` - `amount` +Currently implementing: +- `productionDate` +- `dueDate` +- `packagingDate` +- `bestBeforeDate` +- `productVariant` +- `secondaryDataFields` +- `numberOfUnitsContained` +- `serialShippingContainerCode` +- `gtinOfContainedTradeItems` + + Other properties can be extended pretty easily. **You** can contribute yourself, or open an [issue](https://github.com/xremix/SwiftGS1Barcode/issues/new). From a05d034b8c524c0e4db87b779fc33038463cf5c0 Mon Sep 17 00:00:00 2001 From: Toni Hoffmann Date: Mon, 26 Jun 2017 22:41:16 +0200 Subject: [PATCH 03/13] fixing bugs --- SwiftGS1Barcode/GS1Barcode.swift | 17 +++++++------- SwiftGS1BarcodeTests/GS1BarcodeTests.swift | 26 +++++++++++----------- 2 files changed, 22 insertions(+), 21 deletions(-) diff --git a/SwiftGS1Barcode/GS1Barcode.swift b/SwiftGS1Barcode/GS1Barcode.swift index 680e9b6..e9ae23b 100644 --- a/SwiftGS1Barcode/GS1Barcode.swift +++ b/SwiftGS1Barcode/GS1Barcode.swift @@ -18,8 +18,6 @@ struct GS1Nodes{ var amountNode = GS1Node("30", length: 8, type: .Int, dynamicLength: true) // Experimental Support - // var serialShippingContainerCodeNode = GS1Node("0", length: 18, type: .String) - // var gtinOfContainedTradeItemsNode = GS1Node("2", length: 14, type: .String) var productionDateNode = GS1Node(dateIdentifier: "11") var dueDateNode = GS1Node(dateIdentifier: "12") var packagingDateNode = GS1Node(dateIdentifier: "13") @@ -27,6 +25,9 @@ struct GS1Nodes{ var productVariantNode = GS1Node("20", length: 2, type: .String) var secondaryDataFieldsNode = GS1Node("22", length:29, type: .String, dynamicLength:true) var numberOfUnitsContainedNode = GS1Node("37", length:8, type: .String, dynamicLength:true) + + var serialShippingContainerCodeNode = GS1Node("00", length: 18, type: .String) + var gtinOfContainedTradeItemsNode = GS1Node("02", length: 14, type: .String) } public class GS1Barcode: NSObject, Barcode { @@ -43,6 +44,8 @@ public class GS1Barcode: NSObject, Barcode { public var gtinIndicatorDigit: Int? {get {return nodes.gtinIndicatorDigitNode.intValue}} // Experimental Support + public var serialShippingContainerCode: String? {get{return nodes.serialShippingContainerCodeNode.stringValue}} + public var gtinOfContainedTradeItems: String? {get{return nodes.gtinOfContainedTradeItemsNode.stringValue}} public var productionDate: NSDate? {get{return nodes.productionDateNode.dateValue}} public var dueDate: NSDate? {get{return nodes.dueDateNode.dateValue}} public var packagingDate: NSDate? {get{return nodes.packagingDateNode.dateValue}} @@ -51,9 +54,6 @@ public class GS1Barcode: NSObject, Barcode { public var secondaryDataFields: String? {get{return nodes.secondaryDataFieldsNode.stringValue}} public var numberOfUnitsContained: String? {get{return nodes.numberOfUnitsContainedNode.stringValue}} - // Super Experimental Support - // public var serialShippingContainerCode: String? {get{return nodes.serialShippingContainerCodeNode.stringValue}} - // public var gtinOfContainedTradeItems: String? {get{return nodes.gtinOfContainedTradeItemsNode.stringValue}} required override public init() { super.init() @@ -88,6 +88,7 @@ public class GS1Barcode: NSObject, Barcode { data = data!.substring(from: 1) } + // Do not change the order! if(data!.startsWith(nodes.gtinNode.identifier)){ nodes.gtinNode = GS1BarcodeParser.parseGS1Node(node: nodes.gtinNode, data: data!) nodes.gtinIndicatorDigitNode = GS1BarcodeParser.parseGS1Node(node: nodes.gtinIndicatorDigitNode, data: data!) @@ -98,6 +99,9 @@ public class GS1Barcode: NSObject, Barcode { else if parseNode(node: &nodes.serialNumberNode, data: &data!){} else if parseNode(node: &nodes.amountNode, data: &data!){} // Experimental Support + + else if(parseNode(node: &nodes.serialShippingContainerCodeNode, data: &data!)){} + else if(parseNode(node: &nodes.gtinOfContainedTradeItemsNode, data: &data!)){} else if(parseNode(node: &nodes.productionDateNode, data: &data!)){} else if(parseNode(node: &nodes.dueDateNode, data: &data!)){} else if(parseNode(node: &nodes.packagingDateNode, data: &data!)){} @@ -105,9 +109,6 @@ public class GS1Barcode: NSObject, Barcode { else if(parseNode(node: &nodes.productVariantNode, data: &data!)){} else if(parseNode(node: &nodes.secondaryDataFieldsNode, data: &data!)){} else if(parseNode(node: &nodes.numberOfUnitsContainedNode, data: &data!)){} - // Supper Experimental Support - // else if(parseNode(node: &nodes.serialShippingContainerCodeNode, data: &data!)){} - // else if(parseNode(node: &nodes.gtinOfContainedTradeItemsNode, data: &data!)){} else{ print("Do not know identifier. Canceling Parsing") return false diff --git a/SwiftGS1BarcodeTests/GS1BarcodeTests.swift b/SwiftGS1BarcodeTests/GS1BarcodeTests.swift index 66844bd..16adbc2 100644 --- a/SwiftGS1BarcodeTests/GS1BarcodeTests.swift +++ b/SwiftGS1BarcodeTests/GS1BarcodeTests.swift @@ -112,19 +112,19 @@ class GS1BarcodeTests: XCTestCase { // Experimental Support - -// func testserialShippingContainerCodeNode(){ -// //var serialShippingContainerCodeNode = GS1Node("0", length: 18, type: .String) -// barcode = GS1Barcode(raw: "02123456789012345678901234567890") -// XCTAssertNotNil(barcode.serialShippingContainerCode) -// // XCTAssertEqual(barcode.serialShippingContainerCode, "") -// } -// func testgtinOfContainedTradeItemsNode(){ -// //var gtinOfContainedTradeItemsNode = GS1Node("2", length: 14, type: .String) -// barcode = GS1Barcode(raw: "22123456789012345678901234567890") -// XCTAssertNotNil(barcode.gtinOfContainedTradeItems) -// // XCTAssertEqual(barcode.gtinOfContainedTradeItems, "") -// } + //TODO There should be a test with the identifier starting, but also another one starting with a different identifier at first + func testserialShippingContainerCodeNode(){ + //var serialShippingContainerCodeNode = GS1Node("00", length: 18, type: .String) + barcode = GS1Barcode(raw: "002123456789012345678901234567890") + XCTAssertNotNil(barcode.serialShippingContainerCode) + // XCTAssertEqual(barcode.serialShippingContainerCode, "") + } + func testgtinOfContainedTradeItemsNode(){ + //var gtinOfContainedTradeItemsNode = GS1Node("02", length: 14, type: .String) + barcode = GS1Barcode(raw: "022123456789012345678901234567890") + XCTAssertNotNil(barcode.gtinOfContainedTradeItems) + // XCTAssertEqual(barcode.gtinOfContainedTradeItems, "") + } func testproductionDateNode(){ //var productionDateNode = GS1Node(dateIdentifier: "11") barcode = GS1Barcode(raw: "11210110") From 4a7473940ffbf5e5072eb18aecc2f19734ba20f2 Mon Sep 17 00:00:00 2001 From: Toni Hoffmann Date: Mon, 26 Jun 2017 22:46:10 +0200 Subject: [PATCH 04/13] implementing extended test --- SwiftGS1BarcodeTests/GS1BarcodeTests.swift | 83 ++++++++++++++++++++-- 1 file changed, 76 insertions(+), 7 deletions(-) diff --git a/SwiftGS1BarcodeTests/GS1BarcodeTests.swift b/SwiftGS1BarcodeTests/GS1BarcodeTests.swift index 16adbc2..4338826 100644 --- a/SwiftGS1BarcodeTests/GS1BarcodeTests.swift +++ b/SwiftGS1BarcodeTests/GS1BarcodeTests.swift @@ -112,60 +112,129 @@ class GS1BarcodeTests: XCTestCase { // Experimental Support - //TODO There should be a test with the identifier starting, but also another one starting with a different identifier at first func testserialShippingContainerCodeNode(){ //var serialShippingContainerCodeNode = GS1Node("00", length: 18, type: .String) barcode = GS1Barcode(raw: "002123456789012345678901234567890") XCTAssertNotNil(barcode.serialShippingContainerCode) - // XCTAssertEqual(barcode.serialShippingContainerCode, "") + XCTAssertEqual(barcode.serialShippingContainerCode, "212345678901234567") } + func testserialShippingContainerCodeNodeExtended(){ + //var serialShippingContainerCodeNode = GS1Node("00", length: 18, type: .String) + barcode = GS1Barcode(raw: "30888888888002123456789012345678901234567890") + XCTAssertNotNil(barcode.serialShippingContainerCode) + XCTAssertEqual(barcode.serialShippingContainerCode, "212345678901234567") + } + + func testgtinOfContainedTradeItemsNode(){ //var gtinOfContainedTradeItemsNode = GS1Node("02", length: 14, type: .String) barcode = GS1Barcode(raw: "022123456789012345678901234567890") XCTAssertNotNil(barcode.gtinOfContainedTradeItems) - // XCTAssertEqual(barcode.gtinOfContainedTradeItems, "") + XCTAssertEqual(barcode.gtinOfContainedTradeItems, "21234567890123") + } + func testgtinOfContainedTradeItemsNodeExtended(){ + //var gtinOfContainedTradeItemsNode = GS1Node("02", length: 14, type: .String) + barcode = GS1Barcode(raw: "30888888888022123456789012345678901234567890") + XCTAssertNotNil(barcode.gtinOfContainedTradeItems) + XCTAssertEqual(barcode.gtinOfContainedTradeItems, "21234567890123") } + + func testproductionDateNode(){ //var productionDateNode = GS1Node(dateIdentifier: "11") barcode = GS1Barcode(raw: "11210110") XCTAssertNotNil(barcode.productionDate) XCTAssertEqual(barcode.productionDate, NSDate.from(year: 2021, month: 1, day: 10)) } + func testproductionDateNodeExtended(){ + //var productionDateNode = GS1Node(dateIdentifier: "11") + barcode = GS1Barcode(raw: "3088888888811210110") + XCTAssertNotNil(barcode.productionDate) + XCTAssertEqual(barcode.productionDate, NSDate.from(year: 2021, month: 1, day: 10)) + } + + func testdueDateNode(){ //var dueDateNode = GS1Node(dateIdentifier: "12") barcode = GS1Barcode(raw: "12210110") XCTAssertNotNil(barcode.dueDate) XCTAssertEqual(barcode.dueDate, NSDate.from(year: 2021, month: 1, day: 10)) } + func testdueDateNodeExtended(){ + //var dueDateNode = GS1Node(dateIdentifier: "12") + barcode = GS1Barcode(raw: "3088888888812210110") + XCTAssertNotNil(barcode.dueDate) + XCTAssertEqual(barcode.dueDate, NSDate.from(year: 2021, month: 1, day: 10)) + } + + func testpackagingDateNode(){ //var packagingDateNode = GS1Node(dateIdentifier: "13") barcode = GS1Barcode(raw: "13210110") XCTAssertNotNil(barcode.packagingDate) XCTAssertEqual(barcode.packagingDate, NSDate.from(year: 2021, month: 1, day: 10)) } + func testpackagingDateNodeExtended(){ + //var packagingDateNode = GS1Node(dateIdentifier: "13") + barcode = GS1Barcode(raw: "3088888888813210110") + XCTAssertNotNil(barcode.packagingDate) + XCTAssertEqual(barcode.packagingDate, NSDate.from(year: 2021, month: 1, day: 10)) + } + + func testbestBeforeDateNode(){ //var bestBeforeDateNode = GS1Node(dateIdentifier: "15") barcode = GS1Barcode(raw: "15210110") XCTAssertNotNil(barcode.bestBeforeDate) XCTAssertEqual(barcode.bestBeforeDate, NSDate.from(year: 2021, month: 1, day: 10)) } + func testbestBeforeDateNodeExtended(){ + //var bestBeforeDateNode = GS1Node(dateIdentifier: "15") + barcode = GS1Barcode(raw: "3088888888815210110") + XCTAssertNotNil(barcode.bestBeforeDate) + XCTAssertEqual(barcode.bestBeforeDate, NSDate.from(year: 2021, month: 1, day: 10)) + } + + func testproductVariantNode(){ //var productVariantNode = GS1Node("20", length: 2, type: .String) barcode = GS1Barcode(raw: "2023456789012345678901234567890") XCTAssertNotNil(barcode.productVariant) - // XCTAssertEqual(barcode.productVariant, "") + XCTAssertEqual(barcode.productVariant, "23") + } + func testproductVariantNodeExtended(){ + //var productVariantNode = GS1Node("20", length: 2, type: .String) + barcode = GS1Barcode(raw: "308888888882023456789012345678901234567890") + XCTAssertNotNil(barcode.productVariant) + XCTAssertEqual(barcode.productVariant, "23") } - + + func testsecondaryDataFieldsNode(){ //var secondaryDataFieldsNode = GS1Node("22", length:29, type: .String, dynamicLength:true) barcode = GS1Barcode(raw: "22123456789012345678901234567890") XCTAssertNotNil(barcode.secondaryDataFields) - // XCTAssertEqual(barcode.secondaryDataFields, "") + XCTAssertEqual(barcode.secondaryDataFields, "12345678901234567890123456789") + } + func testsecondaryDataFieldsNodeExtended(){ + //var secondaryDataFieldsNode = GS1Node("22", length:29, type: .String, dynamicLength:true) + barcode = GS1Barcode(raw: "3088888888822123456789012345678901234567890") + XCTAssertNotNil(barcode.secondaryDataFields) + XCTAssertEqual(barcode.secondaryDataFields, "12345678901234567890123456789") } + + func testnumberOfUnitsContainedNode(){ //var numberOfUnitsContainedNode = GS1Node("37", length:8, type: .String, dynamicLength:true) barcode = GS1Barcode(raw: "37123456789012345678901234567890") XCTAssertNotNil(barcode.numberOfUnitsContained) - // XCTAssertEqual(barcode.numberOfUnitsContained, "") + XCTAssertEqual(barcode.numberOfUnitsContained, "12345678") + } + func testnumberOfUnitsContainedNodeExtended(){ + //var numberOfUnitsContainedNode = GS1Node("37", length:8, type: .String, dynamicLength:true) + barcode = GS1Barcode(raw: "3088888888837123456789012345678901234567890") + XCTAssertNotNil(barcode.numberOfUnitsContained) + XCTAssertEqual(barcode.numberOfUnitsContained, "12345678") } + } From 9633a874c99de0841f2ce6b2621befadb2b3b577 Mon Sep 17 00:00:00 2001 From: Toni Hoffmann Date: Mon, 26 Jun 2017 22:46:43 +0200 Subject: [PATCH 05/13] todo --- SwiftGS1Barcode/GS1Barcode.swift | 1 + 1 file changed, 1 insertion(+) diff --git a/SwiftGS1Barcode/GS1Barcode.swift b/SwiftGS1Barcode/GS1Barcode.swift index e9ae23b..b270ebd 100644 --- a/SwiftGS1Barcode/GS1Barcode.swift +++ b/SwiftGS1Barcode/GS1Barcode.swift @@ -9,6 +9,7 @@ import UIKit // Struct used in the GS1 Barcode Class +// TODO think about getting rid of this and use a key value pair struct GS1Nodes{ var gtinNode = GS1Node("01", length: 14, type: .String) var gtinIndicatorDigitNode = GS1Node("01", length: 1, type: .Int) From 74d7f5103aa3d8a41f4b40cda16efb5b4d8e50fa Mon Sep 17 00:00:00 2001 From: Toni Hoffmann Date: Mon, 26 Jun 2017 22:49:09 +0200 Subject: [PATCH 06/13] make parsing algorithm stronger --- SwiftGS1Barcode/BarcodeParser.swift | 5 +++++ SwiftGS1Barcode/GS1Barcode.swift | 2 ++ SwiftGS1BarcodeTests/BarcodeParserTests.swift | 5 ++--- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/SwiftGS1Barcode/BarcodeParser.swift b/SwiftGS1Barcode/BarcodeParser.swift index 6095f50..da5cc8c 100644 --- a/SwiftGS1Barcode/BarcodeParser.swift +++ b/SwiftGS1Barcode/BarcodeParser.swift @@ -23,6 +23,11 @@ public class GS1BarcodeParser: NSObject { static func parseGS1Node(node: GS1Node, data: String)->GS1Node{ print("Parsing node with identifier \(node.identifier) of type \(String(describing: node.type?.description))") + if !data.startsWith(node.identifier){ + print("Passed invalid Node with wrong node identifier") + return node + } + // Get Pure Data by removing the identifier var nodeData = data nodeData = nodeData.substring(from: node.identifier.length) diff --git a/SwiftGS1Barcode/GS1Barcode.swift b/SwiftGS1Barcode/GS1Barcode.swift index b270ebd..d3589ec 100644 --- a/SwiftGS1Barcode/GS1Barcode.swift +++ b/SwiftGS1Barcode/GS1Barcode.swift @@ -89,6 +89,8 @@ public class GS1Barcode: NSObject, Barcode { data = data!.substring(from: 1) } + // Checking the nodes by it's identifier and passing it to the Barcode Parser to get the value and cut the data + // Do not change the order! if(data!.startsWith(nodes.gtinNode.identifier)){ nodes.gtinNode = GS1BarcodeParser.parseGS1Node(node: nodes.gtinNode, data: data!) diff --git a/SwiftGS1BarcodeTests/BarcodeParserTests.swift b/SwiftGS1BarcodeTests/BarcodeParserTests.swift index 075a0b4..668fd12 100644 --- a/SwiftGS1BarcodeTests/BarcodeParserTests.swift +++ b/SwiftGS1BarcodeTests/BarcodeParserTests.swift @@ -41,8 +41,7 @@ class BarcodeParserTests: XCTestCase { XCTAssertEqual(node.dateValue, nil) } func testGroupSeperatorBased(){ - // TODO What? why is this 03 and not 30? - var node = GS1Node("03", length: 8, type: .String, dynamicLength: true) + var node = GS1Node("30", length: 8, type: .String, dynamicLength: true) node = GS1BarcodeParser.parseGS1Node(node: node, data: "3001\u{1D}12341234") XCTAssertEqual(node.originalValue, "01") XCTAssertEqual(node.stringValue, "01") @@ -50,7 +49,7 @@ class BarcodeParserTests: XCTestCase { XCTAssertEqual(node.dateValue, nil) } func testGroupSeperatorBasedEndOfString(){ - var node = GS1Node("03", length: 8, type: .String, dynamicLength: true) + var node = GS1Node("30", length: 8, type: .String, dynamicLength: true) node = GS1BarcodeParser.parseGS1Node(node: node, data: "3001") XCTAssertEqual(node.originalValue, "01") XCTAssertEqual(node.stringValue, "01") From bceb88629cc66e266bab7698b01489ccecc81579 Mon Sep 17 00:00:00 2001 From: Toni Hoffmann Date: Mon, 26 Jun 2017 22:55:11 +0200 Subject: [PATCH 07/13] split tests into multiple files --- SwiftGS1Barcode.xcodeproj/project.pbxproj | 4 + .../GS1BarcodeNodeTests.swift | 142 ++++++++++++++++++ SwiftGS1BarcodeTests/GS1BarcodeTests.swift | 132 +--------------- 3 files changed, 154 insertions(+), 124 deletions(-) create mode 100644 SwiftGS1BarcodeTests/GS1BarcodeNodeTests.swift diff --git a/SwiftGS1Barcode.xcodeproj/project.pbxproj b/SwiftGS1Barcode.xcodeproj/project.pbxproj index bdd0bd1..39acee4 100644 --- a/SwiftGS1Barcode.xcodeproj/project.pbxproj +++ b/SwiftGS1Barcode.xcodeproj/project.pbxproj @@ -24,6 +24,7 @@ 64AE61241F01081800F3B9C0 /* SwiftGS1Barcode.h in Headers */ = {isa = PBXBuildFile; fileRef = 64AE61161F01081800F3B9C0 /* SwiftGS1Barcode.h */; settings = {ATTRIBUTES = (Public, ); }; }; 64DD17361F014E1400F10103 /* GS1Node.swift in Sources */ = {isa = PBXBuildFile; fileRef = 64DD17351F014E1400F10103 /* GS1Node.swift */; }; 64DD17381F014F8700F10103 /* GS1NodeTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 64DD17371F014F8700F10103 /* GS1NodeTests.swift */; }; + E28E99891F01ABC50093C02B /* GS1BarcodeNodeTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = E28E99881F01ABC50093C02B /* GS1BarcodeNodeTests.swift */; }; /* End PBXBuildFile section */ /* Begin PBXContainerItemProxy section */ @@ -57,6 +58,7 @@ 64AE61231F01081800F3B9C0 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 64DD17351F014E1400F10103 /* GS1Node.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = GS1Node.swift; sourceTree = ""; }; 64DD17371F014F8700F10103 /* GS1NodeTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = GS1NodeTests.swift; sourceTree = ""; }; + E28E99881F01ABC50093C02B /* GS1BarcodeNodeTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = GS1BarcodeNodeTests.swift; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -124,6 +126,7 @@ 64DD17371F014F8700F10103 /* GS1NodeTests.swift */, 64754D0F1F01106500B22B62 /* SimpleBarcodeTests.swift */, 64754D131F01107500B22B62 /* StringTests.swift */, + E28E99881F01ABC50093C02B /* GS1BarcodeNodeTests.swift */, ); path = SwiftGS1BarcodeTests; sourceTree = ""; @@ -260,6 +263,7 @@ 64754D161F01107B00B22B62 /* DateTests.swift in Sources */, 64754D141F01107500B22B62 /* StringTests.swift in Sources */, 64754D0C1F01103C00B22B62 /* BarcodeParserTests.swift in Sources */, + E28E99891F01ABC50093C02B /* GS1BarcodeNodeTests.swift in Sources */, 64DD17381F014F8700F10103 /* GS1NodeTests.swift in Sources */, 64754D121F01106D00B22B62 /* GS1BarcodeTests.swift in Sources */, 64754D101F01106500B22B62 /* SimpleBarcodeTests.swift in Sources */, diff --git a/SwiftGS1BarcodeTests/GS1BarcodeNodeTests.swift b/SwiftGS1BarcodeTests/GS1BarcodeNodeTests.swift new file mode 100644 index 0000000..1f2a6a6 --- /dev/null +++ b/SwiftGS1BarcodeTests/GS1BarcodeNodeTests.swift @@ -0,0 +1,142 @@ +// +// GS1BarcodeNodeTests.swift +// SwiftGS1Barcode +// +// Created by Toni Hoffmann on 26.06.17. +// Copyright © 2017 Toni Hoffmann. All rights reserved. +// + +import XCTest +@testable import SwiftGS1Barcode + + +class GS1BarcodeNodeTests: XCTestCase { + + // TODO Write test for the Main Nodes as well + + + // Experimental Support + func testserialShippingContainerCodeNode(){ + //var serialShippingContainerCodeNode = GS1Node("00", length: 18, type: .String) + let barcode = GS1Barcode(raw: "002123456789012345678901234567890") + XCTAssertNotNil(barcode.serialShippingContainerCode) + XCTAssertEqual(barcode.serialShippingContainerCode, "212345678901234567") + } + func testserialShippingContainerCodeNodeExtended(){ + //var serialShippingContainerCodeNode = GS1Node("00", length: 18, type: .String) + let barcode = GS1Barcode(raw: "30888888888002123456789012345678901234567890") + XCTAssertNotNil(barcode.serialShippingContainerCode) + XCTAssertEqual(barcode.serialShippingContainerCode, "212345678901234567") + } + + + func testgtinOfContainedTradeItemsNode(){ + //var gtinOfContainedTradeItemsNode = GS1Node("02", length: 14, type: .String) + let barcode = GS1Barcode(raw: "022123456789012345678901234567890") + XCTAssertNotNil(barcode.gtinOfContainedTradeItems) + XCTAssertEqual(barcode.gtinOfContainedTradeItems, "21234567890123") + } + func testgtinOfContainedTradeItemsNodeExtended(){ + //var gtinOfContainedTradeItemsNode = GS1Node("02", length: 14, type: .String) + let barcode = GS1Barcode(raw: "30888888888022123456789012345678901234567890") + XCTAssertNotNil(barcode.gtinOfContainedTradeItems) + XCTAssertEqual(barcode.gtinOfContainedTradeItems, "21234567890123") + } + + + func testproductionDateNode(){ + //var productionDateNode = GS1Node(dateIdentifier: "11") + let barcode = GS1Barcode(raw: "11210110") + XCTAssertNotNil(barcode.productionDate) + XCTAssertEqual(barcode.productionDate, NSDate.from(year: 2021, month: 1, day: 10)) + } + func testproductionDateNodeExtended(){ + //var productionDateNode = GS1Node(dateIdentifier: "11") + let barcode = GS1Barcode(raw: "3088888888811210110") + XCTAssertNotNil(barcode.productionDate) + XCTAssertEqual(barcode.productionDate, NSDate.from(year: 2021, month: 1, day: 10)) + } + + + func testdueDateNode(){ + //var dueDateNode = GS1Node(dateIdentifier: "12") + let barcode = GS1Barcode(raw: "12210110") + XCTAssertNotNil(barcode.dueDate) + XCTAssertEqual(barcode.dueDate, NSDate.from(year: 2021, month: 1, day: 10)) + } + func testdueDateNodeExtended(){ + //var dueDateNode = GS1Node(dateIdentifier: "12") + let barcode = GS1Barcode(raw: "3088888888812210110") + XCTAssertNotNil(barcode.dueDate) + XCTAssertEqual(barcode.dueDate, NSDate.from(year: 2021, month: 1, day: 10)) + } + + + func testpackagingDateNode(){ + //var packagingDateNode = GS1Node(dateIdentifier: "13") + let barcode = GS1Barcode(raw: "13210110") + XCTAssertNotNil(barcode.packagingDate) + XCTAssertEqual(barcode.packagingDate, NSDate.from(year: 2021, month: 1, day: 10)) + } + func testpackagingDateNodeExtended(){ + //var packagingDateNode = GS1Node(dateIdentifier: "13") + let barcode = GS1Barcode(raw: "3088888888813210110") + XCTAssertNotNil(barcode.packagingDate) + XCTAssertEqual(barcode.packagingDate, NSDate.from(year: 2021, month: 1, day: 10)) + } + + + func testbestBeforeDateNode(){ + //var bestBeforeDateNode = GS1Node(dateIdentifier: "15") + let barcode = GS1Barcode(raw: "15210110") + XCTAssertNotNil(barcode.bestBeforeDate) + XCTAssertEqual(barcode.bestBeforeDate, NSDate.from(year: 2021, month: 1, day: 10)) + } + func testbestBeforeDateNodeExtended(){ + //var bestBeforeDateNode = GS1Node(dateIdentifier: "15") + let barcode = GS1Barcode(raw: "3088888888815210110") + XCTAssertNotNil(barcode.bestBeforeDate) + XCTAssertEqual(barcode.bestBeforeDate, NSDate.from(year: 2021, month: 1, day: 10)) + } + + + func testproductVariantNode(){ + //var productVariantNode = GS1Node("20", length: 2, type: .String) + let barcode = GS1Barcode(raw: "2023456789012345678901234567890") + XCTAssertNotNil(barcode.productVariant) + XCTAssertEqual(barcode.productVariant, "23") + } + func testproductVariantNodeExtended(){ + //var productVariantNode = GS1Node("20", length: 2, type: .String) + let barcode = GS1Barcode(raw: "308888888882023456789012345678901234567890") + XCTAssertNotNil(barcode.productVariant) + XCTAssertEqual(barcode.productVariant, "23") + } + + + func testsecondaryDataFieldsNode(){ + //var secondaryDataFieldsNode = GS1Node("22", length:29, type: .String, dynamicLength:true) + let barcode = GS1Barcode(raw: "22123456789012345678901234567890") + XCTAssertNotNil(barcode.secondaryDataFields) + XCTAssertEqual(barcode.secondaryDataFields, "12345678901234567890123456789") + } + func testsecondaryDataFieldsNodeExtended(){ + //var secondaryDataFieldsNode = GS1Node("22", length:29, type: .String, dynamicLength:true) + let barcode = GS1Barcode(raw: "3088888888822123456789012345678901234567890") + XCTAssertNotNil(barcode.secondaryDataFields) + XCTAssertEqual(barcode.secondaryDataFields, "12345678901234567890123456789") + } + + + func testnumberOfUnitsContainedNode(){ + //var numberOfUnitsContainedNode = GS1Node("37", length:8, type: .String, dynamicLength:true) + let barcode = GS1Barcode(raw: "37123456789012345678901234567890") + XCTAssertNotNil(barcode.numberOfUnitsContained) + XCTAssertEqual(barcode.numberOfUnitsContained, "12345678") + } + func testnumberOfUnitsContainedNodeExtended(){ + //var numberOfUnitsContainedNode = GS1Node("37", length:8, type: .String, dynamicLength:true) + let barcode = GS1Barcode(raw: "3088888888837123456789012345678901234567890") + XCTAssertNotNil(barcode.numberOfUnitsContained) + XCTAssertEqual(barcode.numberOfUnitsContained, "12345678") + }} diff --git a/SwiftGS1BarcodeTests/GS1BarcodeTests.swift b/SwiftGS1BarcodeTests/GS1BarcodeTests.swift index 4338826..c921b52 100644 --- a/SwiftGS1BarcodeTests/GS1BarcodeTests.swift +++ b/SwiftGS1BarcodeTests/GS1BarcodeTests.swift @@ -110,131 +110,15 @@ class GS1BarcodeTests: XCTestCase { XCTAssertFalse(b.validate()) } - - // Experimental Support - func testserialShippingContainerCodeNode(){ - //var serialShippingContainerCodeNode = GS1Node("00", length: 18, type: .String) - barcode = GS1Barcode(raw: "002123456789012345678901234567890") - XCTAssertNotNil(barcode.serialShippingContainerCode) - XCTAssertEqual(barcode.serialShippingContainerCode, "212345678901234567") - } - func testserialShippingContainerCodeNodeExtended(){ - //var serialShippingContainerCodeNode = GS1Node("00", length: 18, type: .String) - barcode = GS1Barcode(raw: "30888888888002123456789012345678901234567890") - XCTAssertNotNil(barcode.serialShippingContainerCode) - XCTAssertEqual(barcode.serialShippingContainerCode, "212345678901234567") - } - - - func testgtinOfContainedTradeItemsNode(){ - //var gtinOfContainedTradeItemsNode = GS1Node("02", length: 14, type: .String) - barcode = GS1Barcode(raw: "022123456789012345678901234567890") - XCTAssertNotNil(barcode.gtinOfContainedTradeItems) - XCTAssertEqual(barcode.gtinOfContainedTradeItems, "21234567890123") - } - func testgtinOfContainedTradeItemsNodeExtended(){ - //var gtinOfContainedTradeItemsNode = GS1Node("02", length: 14, type: .String) - barcode = GS1Barcode(raw: "30888888888022123456789012345678901234567890") - XCTAssertNotNil(barcode.gtinOfContainedTradeItems) - XCTAssertEqual(barcode.gtinOfContainedTradeItems, "21234567890123") - } - - - func testproductionDateNode(){ - //var productionDateNode = GS1Node(dateIdentifier: "11") - barcode = GS1Barcode(raw: "11210110") - XCTAssertNotNil(barcode.productionDate) - XCTAssertEqual(barcode.productionDate, NSDate.from(year: 2021, month: 1, day: 10)) - } - func testproductionDateNodeExtended(){ - //var productionDateNode = GS1Node(dateIdentifier: "11") - barcode = GS1Barcode(raw: "3088888888811210110") - XCTAssertNotNil(barcode.productionDate) - XCTAssertEqual(barcode.productionDate, NSDate.from(year: 2021, month: 1, day: 10)) - } - - - func testdueDateNode(){ - //var dueDateNode = GS1Node(dateIdentifier: "12") - barcode = GS1Barcode(raw: "12210110") - XCTAssertNotNil(barcode.dueDate) - XCTAssertEqual(barcode.dueDate, NSDate.from(year: 2021, month: 1, day: 10)) - } - func testdueDateNodeExtended(){ - //var dueDateNode = GS1Node(dateIdentifier: "12") - barcode = GS1Barcode(raw: "3088888888812210110") - XCTAssertNotNil(barcode.dueDate) - XCTAssertEqual(barcode.dueDate, NSDate.from(year: 2021, month: 1, day: 10)) - } - - - func testpackagingDateNode(){ - //var packagingDateNode = GS1Node(dateIdentifier: "13") - barcode = GS1Barcode(raw: "13210110") - XCTAssertNotNil(barcode.packagingDate) - XCTAssertEqual(barcode.packagingDate, NSDate.from(year: 2021, month: 1, day: 10)) - } - func testpackagingDateNodeExtended(){ - //var packagingDateNode = GS1Node(dateIdentifier: "13") - barcode = GS1Barcode(raw: "3088888888813210110") - XCTAssertNotNil(barcode.packagingDate) - XCTAssertEqual(barcode.packagingDate, NSDate.from(year: 2021, month: 1, day: 10)) - } - - - func testbestBeforeDateNode(){ - //var bestBeforeDateNode = GS1Node(dateIdentifier: "15") - barcode = GS1Barcode(raw: "15210110") - XCTAssertNotNil(barcode.bestBeforeDate) - XCTAssertEqual(barcode.bestBeforeDate, NSDate.from(year: 2021, month: 1, day: 10)) - } - func testbestBeforeDateNodeExtended(){ - //var bestBeforeDateNode = GS1Node(dateIdentifier: "15") - barcode = GS1Barcode(raw: "3088888888815210110") - XCTAssertNotNil(barcode.bestBeforeDate) - XCTAssertEqual(barcode.bestBeforeDate, NSDate.from(year: 2021, month: 1, day: 10)) - } - - - func testproductVariantNode(){ - //var productVariantNode = GS1Node("20", length: 2, type: .String) - barcode = GS1Barcode(raw: "2023456789012345678901234567890") - XCTAssertNotNil(barcode.productVariant) - XCTAssertEqual(barcode.productVariant, "23") - } - func testproductVariantNodeExtended(){ - //var productVariantNode = GS1Node("20", length: 2, type: .String) - barcode = GS1Barcode(raw: "308888888882023456789012345678901234567890") - XCTAssertNotNil(barcode.productVariant) - XCTAssertEqual(barcode.productVariant, "23") - } - - - func testsecondaryDataFieldsNode(){ - //var secondaryDataFieldsNode = GS1Node("22", length:29, type: .String, dynamicLength:true) - barcode = GS1Barcode(raw: "22123456789012345678901234567890") - XCTAssertNotNil(barcode.secondaryDataFields) - XCTAssertEqual(barcode.secondaryDataFields, "12345678901234567890123456789") - } - func testsecondaryDataFieldsNodeExtended(){ - //var secondaryDataFieldsNode = GS1Node("22", length:29, type: .String, dynamicLength:true) - barcode = GS1Barcode(raw: "3088888888822123456789012345678901234567890") - XCTAssertNotNil(barcode.secondaryDataFields) - XCTAssertEqual(barcode.secondaryDataFields, "12345678901234567890123456789") + + func testPerformance(){ + + measure { + for _ in 0...1000{ + _ = GS1Barcode(raw: "01101234670420223005\u{1d}172101311010022247") + } + } } - func testnumberOfUnitsContainedNode(){ - //var numberOfUnitsContainedNode = GS1Node("37", length:8, type: .String, dynamicLength:true) - barcode = GS1Barcode(raw: "37123456789012345678901234567890") - XCTAssertNotNil(barcode.numberOfUnitsContained) - XCTAssertEqual(barcode.numberOfUnitsContained, "12345678") - } - func testnumberOfUnitsContainedNodeExtended(){ - //var numberOfUnitsContainedNode = GS1Node("37", length:8, type: .String, dynamicLength:true) - barcode = GS1Barcode(raw: "3088888888837123456789012345678901234567890") - XCTAssertNotNil(barcode.numberOfUnitsContained) - XCTAssertEqual(barcode.numberOfUnitsContained, "12345678") - } - } From a99a07f84cfe4ba49cf0cf59535e72d371e193d3 Mon Sep 17 00:00:00 2001 From: Toni Hoffmann Date: Mon, 26 Jun 2017 23:12:27 +0200 Subject: [PATCH 08/13] super big logic rewrite --- README.md | 5 +- SwiftGS1Barcode/GS1Barcode.swift | 114 ++++++++++----------- SwiftGS1BarcodeTests/GS1BarcodeTests.swift | 4 +- 3 files changed, 61 insertions(+), 62 deletions(-) diff --git a/README.md b/README.md index a1fb016..4644f23 100644 --- a/README.md +++ b/README.md @@ -44,6 +44,9 @@ Currently implementing: Other properties can be extended pretty easily. **You** can contribute yourself, or open an [issue](https://github.com/xremix/SwiftGS1Barcode/issues/new). +#### Speed +Parsing 500 barcodes takes 0.258ms right now. + ## Installation ### CocoaPods You can install the library to you project using [CocoaPods](https://cocoapods.org). Add the following code to your `Podfile`: @@ -79,4 +82,4 @@ https://www.gs1.at/fileadmin/user_upload/Liste_GS1_Austria_Application_Identifie https://www.appcoda.com/cocoapods-making-guide/ -![Analytics](https://ga-beacon.appspot.com/UA-40522413-9/SwiftGS1Barcode/readme?pixel) \ No newline at end of file +![Analytics](https://ga-beacon.appspot.com/UA-40522413-9/SwiftGS1Barcode/readme?pixel) diff --git a/SwiftGS1Barcode/GS1Barcode.swift b/SwiftGS1Barcode/GS1Barcode.swift index d3589ec..a4be67b 100644 --- a/SwiftGS1Barcode/GS1Barcode.swift +++ b/SwiftGS1Barcode/GS1Barcode.swift @@ -10,50 +10,49 @@ import UIKit // Struct used in the GS1 Barcode Class // TODO think about getting rid of this and use a key value pair -struct GS1Nodes{ - var gtinNode = GS1Node("01", length: 14, type: .String) - var gtinIndicatorDigitNode = GS1Node("01", length: 1, type: .Int) - var lotNumberNode = GS1Node("10", length: 20, type: .String, dynamicLength: true) - var expirationDateNode = GS1Node(dateIdentifier: "17") - var serialNumberNode = GS1Node("21", length: 20, type: .String, dynamicLength: true) - var amountNode = GS1Node("30", length: 8, type: .Int, dynamicLength: true) - - // Experimental Support - var productionDateNode = GS1Node(dateIdentifier: "11") - var dueDateNode = GS1Node(dateIdentifier: "12") - var packagingDateNode = GS1Node(dateIdentifier: "13") - var bestBeforeDateNode = GS1Node(dateIdentifier: "15") - var productVariantNode = GS1Node("20", length: 2, type: .String) - var secondaryDataFieldsNode = GS1Node("22", length:29, type: .String, dynamicLength:true) - var numberOfUnitsContainedNode = GS1Node("37", length:8, type: .String, dynamicLength:true) - - var serialShippingContainerCodeNode = GS1Node("00", length: 18, type: .String) - var gtinOfContainedTradeItemsNode = GS1Node("02", length: 14, type: .String) -} public class GS1Barcode: NSObject, Barcode { public var raw: String? private var parseSuccessFull: Bool = false - var nodes = GS1Nodes() - + // var nodes = GS1Nodes() + var nodeDictionary = [ + "gtin": GS1Node("01", length: 14, type: .String), + "gtinIndicatorDigit": GS1Node("01", length: 1, type: .Int), + "lotNumber": GS1Node("10", length: 20, type: .String, dynamicLength: true), + "expirationDate": GS1Node(dateIdentifier: "17"), + "serialNumber": GS1Node("21", length: 20, type: .String, dynamicLength: true), + "amount": GS1Node("30", length: 8, type: .Int, dynamicLength: true), + // Experimental Support + "productionDate": GS1Node(dateIdentifier: "11"), + "dueDate": GS1Node(dateIdentifier: "12"), + "packagingDate": GS1Node(dateIdentifier: "13"), + "bestBeforeDate": GS1Node(dateIdentifier: "15"), + "productVariant": GS1Node("20", length: 2, type: .String), + "secondaryDataFields": GS1Node("22", length:29, type: .String, dynamicLength:true), + "numberOfUnitsContained": GS1Node("37", length:8, type: .String, dynamicLength:true), + + "serialShippingContainerCode": GS1Node("00", length: 18, type: .String), + "gtinOfContainedTradeItems": GS1Node("02", length: 14, type: .String), + + ] // Mapping for User Friendly Usage - public var gtin: String?{ get {return nodes.gtinNode.stringValue} } - public var lotNumber: String?{ get {return nodes.lotNumberNode.stringValue} } - public var expirationDate: NSDate?{ get {return nodes.expirationDateNode.dateValue} } - public var serialNumber: String?{ get {return nodes.serialNumberNode.stringValue} } - public var amount: Int?{ get {return nodes.amountNode.intValue} } - public var gtinIndicatorDigit: Int? {get {return nodes.gtinIndicatorDigitNode.intValue}} + public var gtin: String?{ get {return nodeDictionary["gtin"]!.stringValue} } + public var lotNumber: String?{ get {return nodeDictionary["lotNumber"]!.stringValue} } + public var expirationDate: NSDate?{ get {return nodeDictionary["expirationDate"]!.dateValue} } + public var serialNumber: String?{ get {return nodeDictionary["serialNumber"]!.stringValue} } + public var amount: Int?{ get {return nodeDictionary["amount"]!.intValue} } + public var gtinIndicatorDigit: Int? {get {return nodeDictionary["gtinIndicatorDigit"]!.intValue}} // Experimental Support - public var serialShippingContainerCode: String? {get{return nodes.serialShippingContainerCodeNode.stringValue}} - public var gtinOfContainedTradeItems: String? {get{return nodes.gtinOfContainedTradeItemsNode.stringValue}} - public var productionDate: NSDate? {get{return nodes.productionDateNode.dateValue}} - public var dueDate: NSDate? {get{return nodes.dueDateNode.dateValue}} - public var packagingDate: NSDate? {get{return nodes.packagingDateNode.dateValue}} - public var bestBeforeDate: NSDate? {get{return nodes.bestBeforeDateNode.dateValue}} - public var productVariant: String? {get{return nodes.productVariantNode.stringValue}} - public var secondaryDataFields: String? {get{return nodes.secondaryDataFieldsNode.stringValue}} - public var numberOfUnitsContained: String? {get{return nodes.numberOfUnitsContainedNode.stringValue}} + public var serialShippingContainerCode: String? {get{return nodeDictionary["serialShippingContainerCode"]!.stringValue}} + public var gtinOfContainedTradeItems: String? {get{return nodeDictionary["gtinOfContainedTradeItems"]!.stringValue}} + public var productionDate: NSDate? {get{return nodeDictionary["productionDate"]!.dateValue}} + public var dueDate: NSDate? {get{return nodeDictionary["dueDate"]!.dateValue}} + public var packagingDate: NSDate? {get{return nodeDictionary["packagingDate"]!.dateValue}} + public var bestBeforeDate: NSDate? {get{return nodeDictionary["bestBeforeDate"]!.dateValue}} + public var productVariant: String? {get{return nodeDictionary["productVariant"]!.stringValue}} + public var secondaryDataFields: String? {get{return nodeDictionary["secondaryDataFields"]!.stringValue}} + public var numberOfUnitsContained: String? {get{return nodeDictionary["numberOfUnitsContained"]!.stringValue}} required override public init() { @@ -89,30 +88,27 @@ public class GS1Barcode: NSObject, Barcode { data = data!.substring(from: 1) } - // Checking the nodes by it's identifier and passing it to the Barcode Parser to get the value and cut the data + // Checking the nodes by it's identifier and passing it to the Barcode Parser to get the value and cut the data - // Do not change the order! - if(data!.startsWith(nodes.gtinNode.identifier)){ - nodes.gtinNode = GS1BarcodeParser.parseGS1Node(node: nodes.gtinNode, data: data!) - nodes.gtinIndicatorDigitNode = GS1BarcodeParser.parseGS1Node(node: nodes.gtinIndicatorDigitNode, data: data!) - data = GS1BarcodeParser.reduce(data: data, by: nodes.gtinNode) + var foundOne = false + if(data!.startsWith(nodeDictionary["gtin"]!.identifier)){ + nodeDictionary["gtin"]! = GS1BarcodeParser.parseGS1Node(node: nodeDictionary["gtin"]!, data: data!) + nodeDictionary["gtinIndicatorDigit"]! = GS1BarcodeParser.parseGS1Node(node: nodeDictionary["gtinIndicatorDigit"]!, data: data!) + data = GS1BarcodeParser.reduce(data: data, by: nodeDictionary["gtin"]!) + foundOne = true + }else{ + for nodeKey in nodeDictionary.keys{ + if nodeKey != "gtin" && nodeKey != "gtinIndicatorDigit"{ + if(parseNode(node: &nodeDictionary[nodeKey]!, data: &data!)){ + foundOne = true + continue + } + } + } } - else if(parseNode(node: &nodes.lotNumberNode, data: &data!)){} - else if parseNode(node: &nodes.expirationDateNode, data: &data!){} - else if parseNode(node: &nodes.serialNumberNode, data: &data!){} - else if parseNode(node: &nodes.amountNode, data: &data!){} - // Experimental Support - - else if(parseNode(node: &nodes.serialShippingContainerCodeNode, data: &data!)){} - else if(parseNode(node: &nodes.gtinOfContainedTradeItemsNode, data: &data!)){} - else if(parseNode(node: &nodes.productionDateNode, data: &data!)){} - else if(parseNode(node: &nodes.dueDateNode, data: &data!)){} - else if(parseNode(node: &nodes.packagingDateNode, data: &data!)){} - else if(parseNode(node: &nodes.bestBeforeDateNode, data: &data!)){} - else if(parseNode(node: &nodes.productVariantNode, data: &data!)){} - else if(parseNode(node: &nodes.secondaryDataFieldsNode, data: &data!)){} - else if(parseNode(node: &nodes.numberOfUnitsContainedNode, data: &data!)){} - else{ + + + if !foundOne{ print("Do not know identifier. Canceling Parsing") return false } diff --git a/SwiftGS1BarcodeTests/GS1BarcodeTests.swift b/SwiftGS1BarcodeTests/GS1BarcodeTests.swift index c921b52..66f78c6 100644 --- a/SwiftGS1BarcodeTests/GS1BarcodeTests.swift +++ b/SwiftGS1BarcodeTests/GS1BarcodeTests.swift @@ -76,7 +76,7 @@ class GS1BarcodeTests: XCTestCase { } func testExpirationDate(){ - XCTAssertNotNil(barcode.nodes.expirationDateNode.originalValue) + XCTAssertNotNil(barcode.nodeDictionary["expirationDate"]!.originalValue) XCTAssertNotNil(barcode.expirationDate) XCTAssertEqual(barcode.expirationDate, NSDate.from(year: 2021, month: 1, day: 31)) } @@ -114,7 +114,7 @@ class GS1BarcodeTests: XCTestCase { func testPerformance(){ measure { - for _ in 0...1000{ + for _ in 0...500{ _ = GS1Barcode(raw: "01101234670420223005\u{1d}172101311010022247") } } From e07a4607483306008da24ff967eaa00943382ff3 Mon Sep 17 00:00:00 2001 From: Toni Hoffmann Date: Mon, 26 Jun 2017 23:30:07 +0200 Subject: [PATCH 09/13] ok, this is getting better but has some bugs --- SwiftGS1Barcode/GS1Barcode.swift | 25 ++++++++++++---------- SwiftGS1BarcodeTests/GS1BarcodeTests.swift | 16 +++++++++++++- 2 files changed, 29 insertions(+), 12 deletions(-) diff --git a/SwiftGS1Barcode/GS1Barcode.swift b/SwiftGS1Barcode/GS1Barcode.swift index a4be67b..8a0587b 100644 --- a/SwiftGS1Barcode/GS1Barcode.swift +++ b/SwiftGS1Barcode/GS1Barcode.swift @@ -16,8 +16,9 @@ public class GS1Barcode: NSObject, Barcode { private var parseSuccessFull: Bool = false // var nodes = GS1Nodes() var nodeDictionary = [ - "gtin": GS1Node("01", length: 14, type: .String), + // ATTENTION! NEVER CHANGE THE ORDER "gtinIndicatorDigit": GS1Node("01", length: 1, type: .Int), + "gtin": GS1Node("01", length: 14, type: .String), "lotNumber": GS1Node("10", length: 20, type: .String, dynamicLength: true), "expirationDate": GS1Node(dateIdentifier: "17"), "serialNumber": GS1Node("21", length: 20, type: .String, dynamicLength: true), @@ -72,7 +73,9 @@ public class GS1Barcode: NSObject, Barcode { func parseNode(node: inout GS1Node, data: inout String)->Bool{ if(data.startsWith(node.identifier)){ node = GS1BarcodeParser.parseGS1Node(node: node, data: data) - data = GS1BarcodeParser.reduce(data: data, by: node)! + if node.identifier != "gtinIndicatorDigit"{ + data = GS1BarcodeParser.reduce(data: data, by: node)! + } return true } return false @@ -91,21 +94,21 @@ public class GS1Barcode: NSObject, Barcode { // Checking the nodes by it's identifier and passing it to the Barcode Parser to get the value and cut the data var foundOne = false - if(data!.startsWith(nodeDictionary["gtin"]!.identifier)){ - nodeDictionary["gtin"]! = GS1BarcodeParser.parseGS1Node(node: nodeDictionary["gtin"]!, data: data!) - nodeDictionary["gtinIndicatorDigit"]! = GS1BarcodeParser.parseGS1Node(node: nodeDictionary["gtinIndicatorDigit"]!, data: data!) - data = GS1BarcodeParser.reduce(data: data, by: nodeDictionary["gtin"]!) - foundOne = true - }else{ +// if(data!.startsWith(nodeDictionary["gtin"]!.identifier)){ +// nodeDictionary["gtin"]! = GS1BarcodeParser.parseGS1Node(node: nodeDictionary["gtin"]!, data: data!) +// nodeDictionary["gtinIndicatorDigit"]! = GS1BarcodeParser.parseGS1Node(node: nodeDictionary["gtinIndicatorDigit"]!, data: data!) +// data = GS1BarcodeParser.reduce(data: data, by: nodeDictionary["gtin"]!) +// foundOne = true +// }else{ for nodeKey in nodeDictionary.keys{ - if nodeKey != "gtin" && nodeKey != "gtinIndicatorDigit"{ +// if nodeKey != "gtin" && nodeKey != "gtinIndicatorDigit"{ if(parseNode(node: &nodeDictionary[nodeKey]!, data: &data!)){ foundOne = true continue } - } +// } } - } +// } if !foundOne{ diff --git a/SwiftGS1BarcodeTests/GS1BarcodeTests.swift b/SwiftGS1BarcodeTests/GS1BarcodeTests.swift index 66f78c6..42d7e8a 100644 --- a/SwiftGS1BarcodeTests/GS1BarcodeTests.swift +++ b/SwiftGS1BarcodeTests/GS1BarcodeTests.swift @@ -110,7 +110,21 @@ class GS1BarcodeTests: XCTestCase { XCTAssertFalse(b.validate()) } - + func testGtinNodeOrder(){ + var index = 0 + var indexGtin = 0 + + for node in GS1Barcode().nodeDictionary{ + index += 1 + if node.key == "gtinIndicatorDigit"{ + indexGtin = index + }else if node.key == "gtin"{ + XCTAssert(indexGtin < index) + } + + } + } + func testPerformance(){ measure { From bcfa3a6423b0e905fb8c82694486d6aabc3796c8 Mon Sep 17 00:00:00 2001 From: thoffmann Date: Tue, 27 Jun 2017 08:29:41 +0200 Subject: [PATCH 10/13] refactoring --- SwiftGS1Barcode.podspec | 2 +- SwiftGS1Barcode.xcodeproj/project.pbxproj | 8 +- SwiftGS1Barcode/BarcodeParser.swift | 4 +- ...e.swift => GS1ApplicationIdentifier.swift} | 4 +- SwiftGS1Barcode/GS1Barcode.swift | 115 +++++++++--------- SwiftGS1Barcode/Info.plist | 2 +- SwiftGS1BarcodeTests/BarcodeParserTests.swift | 24 ++-- .../GS1BarcodeNodeTests.swift | 36 +++--- SwiftGS1BarcodeTests/GS1BarcodeTests.swift | 4 +- SwiftGS1BarcodeTests/GS1NodeTests.swift | 10 +- 10 files changed, 102 insertions(+), 107 deletions(-) rename SwiftGS1Barcode/{GS1Node.swift => GS1ApplicationIdentifier.swift} (93%) diff --git a/SwiftGS1Barcode.podspec b/SwiftGS1Barcode.podspec index a5bfae9..d3eec87 100644 --- a/SwiftGS1Barcode.podspec +++ b/SwiftGS1Barcode.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |s| s.name = 'SwiftGS1Barcode' - s.version = '0.2.0' + s.version = '0.2.1' s.summary = 'A GS1 Barcode Library and Parser for Swift' s.description = <<-DESC diff --git a/SwiftGS1Barcode.xcodeproj/project.pbxproj b/SwiftGS1Barcode.xcodeproj/project.pbxproj index 39acee4..79155da 100644 --- a/SwiftGS1Barcode.xcodeproj/project.pbxproj +++ b/SwiftGS1Barcode.xcodeproj/project.pbxproj @@ -22,7 +22,7 @@ 64754D181F011B9700B22B62 /* README.md in Sources */ = {isa = PBXBuildFile; fileRef = 64754D171F011B9700B22B62 /* README.md */; }; 64AE611D1F01081800F3B9C0 /* SwiftGS1Barcode.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 64AE61131F01081800F3B9C0 /* SwiftGS1Barcode.framework */; }; 64AE61241F01081800F3B9C0 /* SwiftGS1Barcode.h in Headers */ = {isa = PBXBuildFile; fileRef = 64AE61161F01081800F3B9C0 /* SwiftGS1Barcode.h */; settings = {ATTRIBUTES = (Public, ); }; }; - 64DD17361F014E1400F10103 /* GS1Node.swift in Sources */ = {isa = PBXBuildFile; fileRef = 64DD17351F014E1400F10103 /* GS1Node.swift */; }; + 64DD17361F014E1400F10103 /* GS1ApplicationIdentifier.swift in Sources */ = {isa = PBXBuildFile; fileRef = 64DD17351F014E1400F10103 /* GS1ApplicationIdentifier.swift */; }; 64DD17381F014F8700F10103 /* GS1NodeTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 64DD17371F014F8700F10103 /* GS1NodeTests.swift */; }; E28E99891F01ABC50093C02B /* GS1BarcodeNodeTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = E28E99881F01ABC50093C02B /* GS1BarcodeNodeTests.swift */; }; /* End PBXBuildFile section */ @@ -56,7 +56,7 @@ 64AE61171F01081800F3B9C0 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 64AE611C1F01081800F3B9C0 /* SwiftGS1BarcodeTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = SwiftGS1BarcodeTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; 64AE61231F01081800F3B9C0 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; - 64DD17351F014E1400F10103 /* GS1Node.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = GS1Node.swift; sourceTree = ""; }; + 64DD17351F014E1400F10103 /* GS1ApplicationIdentifier.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = GS1ApplicationIdentifier.swift; sourceTree = ""; }; 64DD17371F014F8700F10103 /* GS1NodeTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = GS1NodeTests.swift; sourceTree = ""; }; E28E99881F01ABC50093C02B /* GS1BarcodeNodeTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = GS1BarcodeNodeTests.swift; sourceTree = ""; }; /* End PBXFileReference section */ @@ -108,8 +108,8 @@ 64754D011F010E9100B22B62 /* Barcode.swift */, 64754CFF1F010E8400B22B62 /* BarcodeParser.swift */, 64754D071F010ECA00B22B62 /* DateExtension.swift */, + 64DD17351F014E1400F10103 /* GS1ApplicationIdentifier.swift */, 64754CFD1F010A1000B22B62 /* GS1Barcode.swift */, - 64DD17351F014E1400F10103 /* GS1Node.swift */, 64754D031F010EA000B22B62 /* SimpleBarcode.swift */, 64754D051F010EBB00B22B62 /* StringExtension.swift */, ); @@ -246,7 +246,7 @@ buildActionMask = 2147483647; files = ( 64754D041F010EA000B22B62 /* SimpleBarcode.swift in Sources */, - 64DD17361F014E1400F10103 /* GS1Node.swift in Sources */, + 64DD17361F014E1400F10103 /* GS1ApplicationIdentifier.swift in Sources */, 64754D081F010ECA00B22B62 /* DateExtension.swift in Sources */, 64754CFE1F010A1000B22B62 /* GS1Barcode.swift in Sources */, 64754D181F011B9700B22B62 /* README.md in Sources */, diff --git a/SwiftGS1Barcode/BarcodeParser.swift b/SwiftGS1Barcode/BarcodeParser.swift index da5cc8c..bb0f8eb 100644 --- a/SwiftGS1Barcode/BarcodeParser.swift +++ b/SwiftGS1Barcode/BarcodeParser.swift @@ -9,7 +9,7 @@ import UIKit public class GS1BarcodeParser: NSObject { - static func reduce(data: String?, by node: GS1Node)->String?{ + static func reduce(data: String?, by node: GS1ApplicationIdentifier)->String?{ if data == nil{ return data } @@ -20,7 +20,7 @@ public class GS1BarcodeParser: NSObject { } return data!.substring(from: length) } - static func parseGS1Node(node: GS1Node, data: String)->GS1Node{ + static func parseGS1ApplicationIdentifier(node: GS1ApplicationIdentifier, data: String)->GS1ApplicationIdentifier{ print("Parsing node with identifier \(node.identifier) of type \(String(describing: node.type?.description))") if !data.startsWith(node.identifier){ diff --git a/SwiftGS1Barcode/GS1Node.swift b/SwiftGS1Barcode/GS1ApplicationIdentifier.swift similarity index 93% rename from SwiftGS1Barcode/GS1Node.swift rename to SwiftGS1Barcode/GS1ApplicationIdentifier.swift index 1c673bd..9670a0d 100644 --- a/SwiftGS1Barcode/GS1Node.swift +++ b/SwiftGS1Barcode/GS1ApplicationIdentifier.swift @@ -1,5 +1,5 @@ // -// GS1Node.swift +// GS1ApplicationIdentifier.swift // SwiftGS1Barcode // // Created by Toni Hoffmann on 26.06.17. @@ -17,7 +17,7 @@ enum GS1Type: String{ } } -class GS1Node: NSObject{ +class GS1ApplicationIdentifier: NSObject{ var identifier: String var maxLength: Int var dynamicLength: Bool = false diff --git a/SwiftGS1Barcode/GS1Barcode.swift b/SwiftGS1Barcode/GS1Barcode.swift index 8a0587b..77d4cc6 100644 --- a/SwiftGS1Barcode/GS1Barcode.swift +++ b/SwiftGS1Barcode/GS1Barcode.swift @@ -9,51 +9,49 @@ import UIKit // Struct used in the GS1 Barcode Class -// TODO think about getting rid of this and use a key value pair public class GS1Barcode: NSObject, Barcode { public var raw: String? - private var parseSuccessFull: Bool = false - // var nodes = GS1Nodes() - var nodeDictionary = [ - // ATTENTION! NEVER CHANGE THE ORDER - "gtinIndicatorDigit": GS1Node("01", length: 1, type: .Int), - "gtin": GS1Node("01", length: 14, type: .String), - "lotNumber": GS1Node("10", length: 20, type: .String, dynamicLength: true), - "expirationDate": GS1Node(dateIdentifier: "17"), - "serialNumber": GS1Node("21", length: 20, type: .String, dynamicLength: true), - "amount": GS1Node("30", length: 8, type: .Int, dynamicLength: true), + private var lastParseSuccessfull: Bool = false + + // All + var applicationIdentifiers = [ + "gtinIndicatorDigit": GS1ApplicationIdentifier("01", length: 1, type: .Int), + "gtin": GS1ApplicationIdentifier("01", length: 14, type: .String), + "lotNumber": GS1ApplicationIdentifier("10", length: 20, type: .String, dynamicLength: true), + "expirationDate": GS1ApplicationIdentifier(dateIdentifier: "17"), + "serialNumber": GS1ApplicationIdentifier("21", length: 20, type: .String, dynamicLength: true), + "amount": GS1ApplicationIdentifier("30", length: 8, type: .Int, dynamicLength: true), // Experimental Support - "productionDate": GS1Node(dateIdentifier: "11"), - "dueDate": GS1Node(dateIdentifier: "12"), - "packagingDate": GS1Node(dateIdentifier: "13"), - "bestBeforeDate": GS1Node(dateIdentifier: "15"), - "productVariant": GS1Node("20", length: 2, type: .String), - "secondaryDataFields": GS1Node("22", length:29, type: .String, dynamicLength:true), - "numberOfUnitsContained": GS1Node("37", length:8, type: .String, dynamicLength:true), - - "serialShippingContainerCode": GS1Node("00", length: 18, type: .String), - "gtinOfContainedTradeItems": GS1Node("02", length: 14, type: .String), + "serialShippingContainerCode": GS1ApplicationIdentifier("00", length: 18, type: .String), + "gtinOfContainedTradeItems": GS1ApplicationIdentifier("02", length: 14, type: .String), + "productionDate": GS1ApplicationIdentifier(dateIdentifier: "11"), + "dueDate": GS1ApplicationIdentifier(dateIdentifier: "12"), + "packagingDate": GS1ApplicationIdentifier(dateIdentifier: "13"), + "bestBeforeDate": GS1ApplicationIdentifier(dateIdentifier: "15"), + "productVariant": GS1ApplicationIdentifier("20", length: 2, type: .String), + "secondaryDataFields": GS1ApplicationIdentifier("22", length:29, type: .String, dynamicLength:true), + "numberOfUnitsContained": GS1ApplicationIdentifier("37", length:8, type: .String, dynamicLength:true), ] // Mapping for User Friendly Usage - public var gtin: String?{ get {return nodeDictionary["gtin"]!.stringValue} } - public var lotNumber: String?{ get {return nodeDictionary["lotNumber"]!.stringValue} } - public var expirationDate: NSDate?{ get {return nodeDictionary["expirationDate"]!.dateValue} } - public var serialNumber: String?{ get {return nodeDictionary["serialNumber"]!.stringValue} } - public var amount: Int?{ get {return nodeDictionary["amount"]!.intValue} } - public var gtinIndicatorDigit: Int? {get {return nodeDictionary["gtinIndicatorDigit"]!.intValue}} + public var gtin: String?{ get {return applicationIdentifiers["gtin"]!.stringValue} } + public var lotNumber: String?{ get {return applicationIdentifiers["lotNumber"]!.stringValue} } + public var expirationDate: NSDate?{ get {return applicationIdentifiers["expirationDate"]!.dateValue} } + public var serialNumber: String?{ get {return applicationIdentifiers["serialNumber"]!.stringValue} } + public var amount: Int?{ get {return applicationIdentifiers["amount"]!.intValue} } + public var gtinIndicatorDigit: Int? {get {return applicationIdentifiers["gtinIndicatorDigit"]!.intValue}} // Experimental Support - public var serialShippingContainerCode: String? {get{return nodeDictionary["serialShippingContainerCode"]!.stringValue}} - public var gtinOfContainedTradeItems: String? {get{return nodeDictionary["gtinOfContainedTradeItems"]!.stringValue}} - public var productionDate: NSDate? {get{return nodeDictionary["productionDate"]!.dateValue}} - public var dueDate: NSDate? {get{return nodeDictionary["dueDate"]!.dateValue}} - public var packagingDate: NSDate? {get{return nodeDictionary["packagingDate"]!.dateValue}} - public var bestBeforeDate: NSDate? {get{return nodeDictionary["bestBeforeDate"]!.dateValue}} - public var productVariant: String? {get{return nodeDictionary["productVariant"]!.stringValue}} - public var secondaryDataFields: String? {get{return nodeDictionary["secondaryDataFields"]!.stringValue}} - public var numberOfUnitsContained: String? {get{return nodeDictionary["numberOfUnitsContained"]!.stringValue}} + public var serialShippingContainerCode: String? {get{return applicationIdentifiers["serialShippingContainerCode"]!.stringValue}} + public var gtinOfContainedTradeItems: String? {get{return applicationIdentifiers["gtinOfContainedTradeItems"]!.stringValue}} + public var productionDate: NSDate? {get{return applicationIdentifiers["productionDate"]!.dateValue}} + public var dueDate: NSDate? {get{return applicationIdentifiers["dueDate"]!.dateValue}} + public var packagingDate: NSDate? {get{return applicationIdentifiers["packagingDate"]!.dateValue}} + public var bestBeforeDate: NSDate? {get{return applicationIdentifiers["bestBeforeDate"]!.dateValue}} + public var productVariant: String? {get{return applicationIdentifiers["productVariant"]!.stringValue}} + public var secondaryDataFields: String? {get{return applicationIdentifiers["secondaryDataFields"]!.stringValue}} + public var numberOfUnitsContained: String? {get{return applicationIdentifiers["numberOfUnitsContained"]!.stringValue}} required override public init() { @@ -67,57 +65,54 @@ public class GS1Barcode: NSObject, Barcode { // Validating if the barcode got parsed correctly func validate() -> Bool { - return parseSuccessFull && raw != "" && raw != nil + return lastParseSuccessfull && raw != "" && raw != nil } - func parseNode(node: inout GS1Node, data: inout String)->Bool{ + func parseNode(node: inout GS1ApplicationIdentifier, data: inout String)->Bool{ if(data.startsWith(node.identifier)){ - node = GS1BarcodeParser.parseGS1Node(node: node, data: data) - if node.identifier != "gtinIndicatorDigit"{ - data = GS1BarcodeParser.reduce(data: data, by: node)! + node = GS1BarcodeParser.parseGS1ApplicationIdentifier(node: node, data: data) + // Fixes issue where two nodes have the same identifier + if node.identifier == "01"{ + applicationIdentifiers["gtinIndicatorDigit"] = GS1BarcodeParser.parseGS1ApplicationIdentifier(node: applicationIdentifiers["gtinIndicatorDigit"]!, data: data) } + data = GS1BarcodeParser.reduce(data: data, by: node)! + return true } return false } func parse() ->Bool{ - self.parseSuccessFull = false + self.lastParseSuccessfull = false var data = raw if data != nil{ while data!.characters.count > 0 { + // Removing Group Seperator from the beginning of the string if(data!.startsWith("\u{1D}")){ data = data!.substring(from: 1) } // Checking the nodes by it's identifier and passing it to the Barcode Parser to get the value and cut the data - var foundOne = false -// if(data!.startsWith(nodeDictionary["gtin"]!.identifier)){ -// nodeDictionary["gtin"]! = GS1BarcodeParser.parseGS1Node(node: nodeDictionary["gtin"]!, data: data!) -// nodeDictionary["gtinIndicatorDigit"]! = GS1BarcodeParser.parseGS1Node(node: nodeDictionary["gtinIndicatorDigit"]!, data: data!) -// data = GS1BarcodeParser.reduce(data: data, by: nodeDictionary["gtin"]!) -// foundOne = true -// }else{ - for nodeKey in nodeDictionary.keys{ -// if nodeKey != "gtin" && nodeKey != "gtinIndicatorDigit"{ - if(parseNode(node: &nodeDictionary[nodeKey]!, data: &data!)){ - foundOne = true - continue - } -// } + for nodeKey in applicationIdentifiers.keys{ + // Exclude the gtinIndicatorDigit, because it get's added later for the gtin identifier + if nodeKey != "gtinIndicatorDigit"{ + // If could parse node, continue and do the loop once again + if(parseNode(node: &applicationIdentifiers[nodeKey]!, data: &data!)){ + foundOne = true + continue + } } -// } - - + } + // If no node was found return false and keep the lastParseSuccessfull to false if !foundOne{ print("Do not know identifier. Canceling Parsing") return false } } } - self.parseSuccessFull = true + self.lastParseSuccessfull = true return true } } diff --git a/SwiftGS1Barcode/Info.plist b/SwiftGS1Barcode/Info.plist index badd676..600e701 100644 --- a/SwiftGS1Barcode/Info.plist +++ b/SwiftGS1Barcode/Info.plist @@ -15,7 +15,7 @@ CFBundlePackageType FMWK CFBundleShortVersionString - 0.2.0 + 0.2.1 CFBundleVersion $(CURRENT_PROJECT_VERSION) NSPrincipalClass diff --git a/SwiftGS1BarcodeTests/BarcodeParserTests.swift b/SwiftGS1BarcodeTests/BarcodeParserTests.swift index 668fd12..97e875e 100644 --- a/SwiftGS1BarcodeTests/BarcodeParserTests.swift +++ b/SwiftGS1BarcodeTests/BarcodeParserTests.swift @@ -22,35 +22,35 @@ class BarcodeParserTests: XCTestCase { } func testGtinPraser(){ - var node = GS1Node("01", length:14, type: .String) - // var node = GS1Node(identifier: "01", type: .FixedLengthBased, fixedValue: 14) - node = GS1BarcodeParser.parseGS1Node(node: node, data: "010012349993333001") + var node = GS1ApplicationIdentifier("01", length:14, type: .String) + // var node = GS1ApplicationIdentifier(identifier: "01", type: .FixedLengthBased, fixedValue: 14) + node = GS1BarcodeParser.parseGS1ApplicationIdentifier(node: node, data: "010012349993333001") XCTAssertEqual(node.stringValue, "00123499933330") } func testDatePraser(){ - var node = GS1Node("17", length:6, type: .Date) - // var node = GS1Node(identifier: "01", type: .Date) - node = GS1BarcodeParser.parseGS1Node(node: node, data: "17210228") + var node = GS1ApplicationIdentifier("17", length:6, type: .Date) + // var node = GS1ApplicationIdentifier(identifier: "01", type: .Date) + node = GS1BarcodeParser.parseGS1ApplicationIdentifier(node: node, data: "17210228") XCTAssertEqual(node.dateValue, NSDate.from(year: 2021, month: 2, day: 28)) // 17 } func testGroupSeperatorBasedInt(){ - var node = GS1Node("30", length: 99, type: .Int, dynamicLength: true) - node = GS1BarcodeParser.parseGS1Node(node: node, data: "3001\u{1D}12341234") + var node = GS1ApplicationIdentifier("30", length: 99, type: .Int, dynamicLength: true) + node = GS1BarcodeParser.parseGS1ApplicationIdentifier(node: node, data: "3001\u{1D}12341234") XCTAssertEqual(node.originalValue, "01") XCTAssertEqual(node.intValue, 1) XCTAssertEqual(node.dateValue, nil) } func testGroupSeperatorBased(){ - var node = GS1Node("30", length: 8, type: .String, dynamicLength: true) - node = GS1BarcodeParser.parseGS1Node(node: node, data: "3001\u{1D}12341234") + var node = GS1ApplicationIdentifier("30", length: 8, type: .String, dynamicLength: true) + node = GS1BarcodeParser.parseGS1ApplicationIdentifier(node: node, data: "3001\u{1D}12341234") XCTAssertEqual(node.originalValue, "01") XCTAssertEqual(node.stringValue, "01") XCTAssertEqual(node.intValue, nil) XCTAssertEqual(node.dateValue, nil) } func testGroupSeperatorBasedEndOfString(){ - var node = GS1Node("30", length: 8, type: .String, dynamicLength: true) - node = GS1BarcodeParser.parseGS1Node(node: node, data: "3001") + var node = GS1ApplicationIdentifier("30", length: 8, type: .String, dynamicLength: true) + node = GS1BarcodeParser.parseGS1ApplicationIdentifier(node: node, data: "3001") XCTAssertEqual(node.originalValue, "01") XCTAssertEqual(node.stringValue, "01") XCTAssertEqual(node.intValue, nil) diff --git a/SwiftGS1BarcodeTests/GS1BarcodeNodeTests.swift b/SwiftGS1BarcodeTests/GS1BarcodeNodeTests.swift index 1f2a6a6..90f6e3d 100644 --- a/SwiftGS1BarcodeTests/GS1BarcodeNodeTests.swift +++ b/SwiftGS1BarcodeTests/GS1BarcodeNodeTests.swift @@ -17,13 +17,13 @@ class GS1BarcodeNodeTests: XCTestCase { // Experimental Support func testserialShippingContainerCodeNode(){ - //var serialShippingContainerCodeNode = GS1Node("00", length: 18, type: .String) + //var serialShippingContainerCodeNode = GS1ApplicationIdentifier("00", length: 18, type: .String) let barcode = GS1Barcode(raw: "002123456789012345678901234567890") XCTAssertNotNil(barcode.serialShippingContainerCode) XCTAssertEqual(barcode.serialShippingContainerCode, "212345678901234567") } func testserialShippingContainerCodeNodeExtended(){ - //var serialShippingContainerCodeNode = GS1Node("00", length: 18, type: .String) + //var serialShippingContainerCodeNode = GS1ApplicationIdentifier("00", length: 18, type: .String) let barcode = GS1Barcode(raw: "30888888888002123456789012345678901234567890") XCTAssertNotNil(barcode.serialShippingContainerCode) XCTAssertEqual(barcode.serialShippingContainerCode, "212345678901234567") @@ -31,13 +31,13 @@ class GS1BarcodeNodeTests: XCTestCase { func testgtinOfContainedTradeItemsNode(){ - //var gtinOfContainedTradeItemsNode = GS1Node("02", length: 14, type: .String) + //var gtinOfContainedTradeItemsNode = GS1ApplicationIdentifier("02", length: 14, type: .String) let barcode = GS1Barcode(raw: "022123456789012345678901234567890") XCTAssertNotNil(barcode.gtinOfContainedTradeItems) XCTAssertEqual(barcode.gtinOfContainedTradeItems, "21234567890123") } func testgtinOfContainedTradeItemsNodeExtended(){ - //var gtinOfContainedTradeItemsNode = GS1Node("02", length: 14, type: .String) + //var gtinOfContainedTradeItemsNode = GS1ApplicationIdentifier("02", length: 14, type: .String) let barcode = GS1Barcode(raw: "30888888888022123456789012345678901234567890") XCTAssertNotNil(barcode.gtinOfContainedTradeItems) XCTAssertEqual(barcode.gtinOfContainedTradeItems, "21234567890123") @@ -45,13 +45,13 @@ class GS1BarcodeNodeTests: XCTestCase { func testproductionDateNode(){ - //var productionDateNode = GS1Node(dateIdentifier: "11") + //var productionDateNode = GS1ApplicationIdentifier(dateIdentifier: "11") let barcode = GS1Barcode(raw: "11210110") XCTAssertNotNil(barcode.productionDate) XCTAssertEqual(barcode.productionDate, NSDate.from(year: 2021, month: 1, day: 10)) } func testproductionDateNodeExtended(){ - //var productionDateNode = GS1Node(dateIdentifier: "11") + //var productionDateNode = GS1ApplicationIdentifier(dateIdentifier: "11") let barcode = GS1Barcode(raw: "3088888888811210110") XCTAssertNotNil(barcode.productionDate) XCTAssertEqual(barcode.productionDate, NSDate.from(year: 2021, month: 1, day: 10)) @@ -59,13 +59,13 @@ class GS1BarcodeNodeTests: XCTestCase { func testdueDateNode(){ - //var dueDateNode = GS1Node(dateIdentifier: "12") + //var dueDateNode = GS1ApplicationIdentifier(dateIdentifier: "12") let barcode = GS1Barcode(raw: "12210110") XCTAssertNotNil(barcode.dueDate) XCTAssertEqual(barcode.dueDate, NSDate.from(year: 2021, month: 1, day: 10)) } func testdueDateNodeExtended(){ - //var dueDateNode = GS1Node(dateIdentifier: "12") + //var dueDateNode = GS1ApplicationIdentifier(dateIdentifier: "12") let barcode = GS1Barcode(raw: "3088888888812210110") XCTAssertNotNil(barcode.dueDate) XCTAssertEqual(barcode.dueDate, NSDate.from(year: 2021, month: 1, day: 10)) @@ -73,13 +73,13 @@ class GS1BarcodeNodeTests: XCTestCase { func testpackagingDateNode(){ - //var packagingDateNode = GS1Node(dateIdentifier: "13") + //var packagingDateNode = GS1ApplicationIdentifier(dateIdentifier: "13") let barcode = GS1Barcode(raw: "13210110") XCTAssertNotNil(barcode.packagingDate) XCTAssertEqual(barcode.packagingDate, NSDate.from(year: 2021, month: 1, day: 10)) } func testpackagingDateNodeExtended(){ - //var packagingDateNode = GS1Node(dateIdentifier: "13") + //var packagingDateNode = GS1ApplicationIdentifier(dateIdentifier: "13") let barcode = GS1Barcode(raw: "3088888888813210110") XCTAssertNotNil(barcode.packagingDate) XCTAssertEqual(barcode.packagingDate, NSDate.from(year: 2021, month: 1, day: 10)) @@ -87,13 +87,13 @@ class GS1BarcodeNodeTests: XCTestCase { func testbestBeforeDateNode(){ - //var bestBeforeDateNode = GS1Node(dateIdentifier: "15") + //var bestBeforeDateNode = GS1ApplicationIdentifier(dateIdentifier: "15") let barcode = GS1Barcode(raw: "15210110") XCTAssertNotNil(barcode.bestBeforeDate) XCTAssertEqual(barcode.bestBeforeDate, NSDate.from(year: 2021, month: 1, day: 10)) } func testbestBeforeDateNodeExtended(){ - //var bestBeforeDateNode = GS1Node(dateIdentifier: "15") + //var bestBeforeDateNode = GS1ApplicationIdentifier(dateIdentifier: "15") let barcode = GS1Barcode(raw: "3088888888815210110") XCTAssertNotNil(barcode.bestBeforeDate) XCTAssertEqual(barcode.bestBeforeDate, NSDate.from(year: 2021, month: 1, day: 10)) @@ -101,13 +101,13 @@ class GS1BarcodeNodeTests: XCTestCase { func testproductVariantNode(){ - //var productVariantNode = GS1Node("20", length: 2, type: .String) + //var productVariantNode = GS1ApplicationIdentifier("20", length: 2, type: .String) let barcode = GS1Barcode(raw: "2023456789012345678901234567890") XCTAssertNotNil(barcode.productVariant) XCTAssertEqual(barcode.productVariant, "23") } func testproductVariantNodeExtended(){ - //var productVariantNode = GS1Node("20", length: 2, type: .String) + //var productVariantNode = GS1ApplicationIdentifier("20", length: 2, type: .String) let barcode = GS1Barcode(raw: "308888888882023456789012345678901234567890") XCTAssertNotNil(barcode.productVariant) XCTAssertEqual(barcode.productVariant, "23") @@ -115,13 +115,13 @@ class GS1BarcodeNodeTests: XCTestCase { func testsecondaryDataFieldsNode(){ - //var secondaryDataFieldsNode = GS1Node("22", length:29, type: .String, dynamicLength:true) + //var secondaryDataFieldsNode = GS1ApplicationIdentifier("22", length:29, type: .String, dynamicLength:true) let barcode = GS1Barcode(raw: "22123456789012345678901234567890") XCTAssertNotNil(barcode.secondaryDataFields) XCTAssertEqual(barcode.secondaryDataFields, "12345678901234567890123456789") } func testsecondaryDataFieldsNodeExtended(){ - //var secondaryDataFieldsNode = GS1Node("22", length:29, type: .String, dynamicLength:true) + //var secondaryDataFieldsNode = GS1ApplicationIdentifier("22", length:29, type: .String, dynamicLength:true) let barcode = GS1Barcode(raw: "3088888888822123456789012345678901234567890") XCTAssertNotNil(barcode.secondaryDataFields) XCTAssertEqual(barcode.secondaryDataFields, "12345678901234567890123456789") @@ -129,13 +129,13 @@ class GS1BarcodeNodeTests: XCTestCase { func testnumberOfUnitsContainedNode(){ - //var numberOfUnitsContainedNode = GS1Node("37", length:8, type: .String, dynamicLength:true) + //var numberOfUnitsContainedNode = GS1ApplicationIdentifier("37", length:8, type: .String, dynamicLength:true) let barcode = GS1Barcode(raw: "37123456789012345678901234567890") XCTAssertNotNil(barcode.numberOfUnitsContained) XCTAssertEqual(barcode.numberOfUnitsContained, "12345678") } func testnumberOfUnitsContainedNodeExtended(){ - //var numberOfUnitsContainedNode = GS1Node("37", length:8, type: .String, dynamicLength:true) + //var numberOfUnitsContainedNode = GS1ApplicationIdentifier("37", length:8, type: .String, dynamicLength:true) let barcode = GS1Barcode(raw: "3088888888837123456789012345678901234567890") XCTAssertNotNil(barcode.numberOfUnitsContained) XCTAssertEqual(barcode.numberOfUnitsContained, "12345678") diff --git a/SwiftGS1BarcodeTests/GS1BarcodeTests.swift b/SwiftGS1BarcodeTests/GS1BarcodeTests.swift index 42d7e8a..c41ba9d 100644 --- a/SwiftGS1BarcodeTests/GS1BarcodeTests.swift +++ b/SwiftGS1BarcodeTests/GS1BarcodeTests.swift @@ -76,7 +76,7 @@ class GS1BarcodeTests: XCTestCase { } func testExpirationDate(){ - XCTAssertNotNil(barcode.nodeDictionary["expirationDate"]!.originalValue) + XCTAssertNotNil(barcode.applicationIdentifiers["expirationDate"]!.originalValue) XCTAssertNotNil(barcode.expirationDate) XCTAssertEqual(barcode.expirationDate, NSDate.from(year: 2021, month: 1, day: 31)) } @@ -114,7 +114,7 @@ class GS1BarcodeTests: XCTestCase { var index = 0 var indexGtin = 0 - for node in GS1Barcode().nodeDictionary{ + for node in GS1Barcode().applicationIdentifiers{ index += 1 if node.key == "gtinIndicatorDigit"{ indexGtin = index diff --git a/SwiftGS1BarcodeTests/GS1NodeTests.swift b/SwiftGS1BarcodeTests/GS1NodeTests.swift index 65bf70d..68f86b7 100644 --- a/SwiftGS1BarcodeTests/GS1NodeTests.swift +++ b/SwiftGS1BarcodeTests/GS1NodeTests.swift @@ -1,5 +1,5 @@ // -// GS1NodeTests.swift +// GS1ApplicationIdentifierTests.swift // SwiftGS1Barcode // // Created by Toni Hoffmann on 26.06.17. @@ -9,24 +9,24 @@ import XCTest @testable import SwiftGS1Barcode -class GS1NodeTests: XCTestCase { +class GS1ApplicationIdentifierTests: XCTestCase { func testInitIdMaxLength() { - let node = GS1Node("id", length: 1) + let node = GS1ApplicationIdentifier("id", length: 1) XCTAssertEqual(node.identifier, "id") XCTAssertEqual(node.maxLength, 1) XCTAssertEqual(node.type, nil) } func testInitIdMaxLengthType() { - let node = GS1Node("id", length: 1, type: .Int) + let node = GS1ApplicationIdentifier("id", length: 1, type: .Int) XCTAssertEqual(node.identifier, "id") XCTAssertEqual(node.maxLength, 1) XCTAssertEqual(node.type, .Int) } func testInitIdMaxLengthTypeDynamicLength() { - let node = GS1Node("id", length: 1, type: .Int, dynamicLength: true) + let node = GS1ApplicationIdentifier("id", length: 1, type: .Int, dynamicLength: true) XCTAssertEqual(node.identifier, "id") XCTAssertEqual(node.maxLength, 1) XCTAssertEqual(node.type, .Int) From 9733bd3f5341f2e0eb5078c70b0c8f082eacc90d Mon Sep 17 00:00:00 2001 From: thoffmann Date: Tue, 27 Jun 2017 08:30:04 +0200 Subject: [PATCH 11/13] cleanup --- SwiftGS1Barcode.xcodeproj/project.pbxproj | 8 ++++---- ...odeTests.swift => GS1ApplicationIdentifierTests.swift} | 0 2 files changed, 4 insertions(+), 4 deletions(-) rename SwiftGS1BarcodeTests/{GS1NodeTests.swift => GS1ApplicationIdentifierTests.swift} (100%) diff --git a/SwiftGS1Barcode.xcodeproj/project.pbxproj b/SwiftGS1Barcode.xcodeproj/project.pbxproj index 79155da..64cb8a7 100644 --- a/SwiftGS1Barcode.xcodeproj/project.pbxproj +++ b/SwiftGS1Barcode.xcodeproj/project.pbxproj @@ -23,7 +23,7 @@ 64AE611D1F01081800F3B9C0 /* SwiftGS1Barcode.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 64AE61131F01081800F3B9C0 /* SwiftGS1Barcode.framework */; }; 64AE61241F01081800F3B9C0 /* SwiftGS1Barcode.h in Headers */ = {isa = PBXBuildFile; fileRef = 64AE61161F01081800F3B9C0 /* SwiftGS1Barcode.h */; settings = {ATTRIBUTES = (Public, ); }; }; 64DD17361F014E1400F10103 /* GS1ApplicationIdentifier.swift in Sources */ = {isa = PBXBuildFile; fileRef = 64DD17351F014E1400F10103 /* GS1ApplicationIdentifier.swift */; }; - 64DD17381F014F8700F10103 /* GS1NodeTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 64DD17371F014F8700F10103 /* GS1NodeTests.swift */; }; + 64DD17381F014F8700F10103 /* GS1ApplicationIdentifierTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 64DD17371F014F8700F10103 /* GS1ApplicationIdentifierTests.swift */; }; E28E99891F01ABC50093C02B /* GS1BarcodeNodeTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = E28E99881F01ABC50093C02B /* GS1BarcodeNodeTests.swift */; }; /* End PBXBuildFile section */ @@ -57,7 +57,7 @@ 64AE611C1F01081800F3B9C0 /* SwiftGS1BarcodeTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = SwiftGS1BarcodeTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; 64AE61231F01081800F3B9C0 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 64DD17351F014E1400F10103 /* GS1ApplicationIdentifier.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = GS1ApplicationIdentifier.swift; sourceTree = ""; }; - 64DD17371F014F8700F10103 /* GS1NodeTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = GS1NodeTests.swift; sourceTree = ""; }; + 64DD17371F014F8700F10103 /* GS1ApplicationIdentifierTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = GS1ApplicationIdentifierTests.swift; sourceTree = ""; }; E28E99881F01ABC50093C02B /* GS1BarcodeNodeTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = GS1BarcodeNodeTests.swift; sourceTree = ""; }; /* End PBXFileReference section */ @@ -123,7 +123,7 @@ 64754D0B1F01103C00B22B62 /* BarcodeParserTests.swift */, 64754D151F01107B00B22B62 /* DateTests.swift */, 64754D111F01106D00B22B62 /* GS1BarcodeTests.swift */, - 64DD17371F014F8700F10103 /* GS1NodeTests.swift */, + 64DD17371F014F8700F10103 /* GS1ApplicationIdentifierTests.swift */, 64754D0F1F01106500B22B62 /* SimpleBarcodeTests.swift */, 64754D131F01107500B22B62 /* StringTests.swift */, E28E99881F01ABC50093C02B /* GS1BarcodeNodeTests.swift */, @@ -264,7 +264,7 @@ 64754D141F01107500B22B62 /* StringTests.swift in Sources */, 64754D0C1F01103C00B22B62 /* BarcodeParserTests.swift in Sources */, E28E99891F01ABC50093C02B /* GS1BarcodeNodeTests.swift in Sources */, - 64DD17381F014F8700F10103 /* GS1NodeTests.swift in Sources */, + 64DD17381F014F8700F10103 /* GS1ApplicationIdentifierTests.swift in Sources */, 64754D121F01106D00B22B62 /* GS1BarcodeTests.swift in Sources */, 64754D101F01106500B22B62 /* SimpleBarcodeTests.swift in Sources */, ); diff --git a/SwiftGS1BarcodeTests/GS1NodeTests.swift b/SwiftGS1BarcodeTests/GS1ApplicationIdentifierTests.swift similarity index 100% rename from SwiftGS1BarcodeTests/GS1NodeTests.swift rename to SwiftGS1BarcodeTests/GS1ApplicationIdentifierTests.swift From 729d7f2d5352d6e140db086d59b3802fcaa72498 Mon Sep 17 00:00:00 2001 From: thoffmann Date: Tue, 27 Jun 2017 08:31:40 +0200 Subject: [PATCH 12/13] further cleanup --- SwiftGS1Barcode/GS1Barcode.swift | 4 ++-- SwiftGS1Barcode/StringExtension.swift | 1 - 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/SwiftGS1Barcode/GS1Barcode.swift b/SwiftGS1Barcode/GS1Barcode.swift index 77d4cc6..b087703 100644 --- a/SwiftGS1Barcode/GS1Barcode.swift +++ b/SwiftGS1Barcode/GS1Barcode.swift @@ -13,8 +13,8 @@ import UIKit public class GS1Barcode: NSObject, Barcode { public var raw: String? private var lastParseSuccessfull: Bool = false - - // All + + // All var applicationIdentifiers = [ "gtinIndicatorDigit": GS1ApplicationIdentifier("01", length: 1, type: .Int), "gtin": GS1ApplicationIdentifier("01", length: 14, type: .String), diff --git a/SwiftGS1Barcode/StringExtension.swift b/SwiftGS1Barcode/StringExtension.swift index 849a0ad..07b157b 100644 --- a/SwiftGS1Barcode/StringExtension.swift +++ b/SwiftGS1Barcode/StringExtension.swift @@ -14,7 +14,6 @@ extension String{ let end = self.index(start, offsetBy: length) let range = start..String{ From f382eaade5f1c3ecc973b11f57d32a6deccfbcc3 Mon Sep 17 00:00:00 2001 From: thoffmann Date: Tue, 27 Jun 2017 08:33:44 +0200 Subject: [PATCH 13/13] updating version --- SwiftGS1Barcode.podspec | 2 +- SwiftGS1Barcode/Info.plist | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/SwiftGS1Barcode.podspec b/SwiftGS1Barcode.podspec index d3eec87..9dcba7c 100644 --- a/SwiftGS1Barcode.podspec +++ b/SwiftGS1Barcode.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |s| s.name = 'SwiftGS1Barcode' - s.version = '0.2.1' + s.version = '0.3.0' s.summary = 'A GS1 Barcode Library and Parser for Swift' s.description = <<-DESC diff --git a/SwiftGS1Barcode/Info.plist b/SwiftGS1Barcode/Info.plist index 600e701..0d6341f 100644 --- a/SwiftGS1Barcode/Info.plist +++ b/SwiftGS1Barcode/Info.plist @@ -15,7 +15,7 @@ CFBundlePackageType FMWK CFBundleShortVersionString - 0.2.1 + 0.3.0 CFBundleVersion $(CURRENT_PROJECT_VERSION) NSPrincipalClass