-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Include Initial Code for Barcode and Barcode Parser
- Loading branch information
Showing
10 changed files
with
304 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,12 @@ | ||
# SwiftGS1Barcode | ||
A GS1 Barcode Library and Parser for Swift Edit | ||
|
||
CocoaPod | ||
https://github.com/xremix/SwiftGS1Barcode | ||
## CocoaPod | ||
https://github.com/xremix/SwiftGS1Barcode | ||
|
||
## Deployment Steps: | ||
- Update Version | ||
- Push Code to Git | ||
- Create Release on Git | ||
- `pod lib lint` | ||
- `pod trunk push SwiftGS1Barcode.podspec` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// | ||
// Barcode.swift | ||
// SwiftGS1Barcode | ||
// | ||
// Created by Toni Hoffmann on 26.06.17. | ||
// Copyright © 2017 Toni Hoffmann. All rights reserved. | ||
// | ||
|
||
import UIKit | ||
|
||
protocol Barcode { | ||
// Properties | ||
var raw: String? {get set} | ||
|
||
// Initializers | ||
init() | ||
init(raw: String) | ||
|
||
// Functions | ||
func validate() -> Bool | ||
func parse() -> Bool | ||
|
||
// Static Functions | ||
// static func tryParse(raw: String) -> Bool | ||
// static func parse(raw:String ) ->Barcode | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// | ||
// BarcodeParser.swift | ||
// SwiftGS1Barcode | ||
// | ||
// Created by Toni Hoffmann on 26.06.17. | ||
// Copyright © 2017 Toni Hoffmann. All rights reserved. | ||
// | ||
|
||
import UIKit | ||
|
||
class GS1BarcodeParser: NSObject { | ||
static func reduce(data: String?, by node: GS1Node)->String?{ | ||
if data == nil{ | ||
return data | ||
} | ||
|
||
var length = (node.value?.length ?? 0) + (node.identifier.length) | ||
if (node.type == .GroupSeperatorBased || node.type == .GroupSeperatorBasedInt) && length < data!.length{ | ||
length += 1 | ||
} | ||
return data!.substring(from: length) | ||
} | ||
static func parseGS1Node(node: GS1Node, data: String)->GS1Node{ | ||
print("Parsing node of type \(node.type.description) with identifier \(node.identifier)") | ||
switch node.type { | ||
case .GTIN: | ||
node.value = data.substring(node.identifier.length, length: node.type.fixedValueLength!) | ||
// GETINIndicatorDigit | ||
break | ||
case .GroupSeperatorBased, .GroupSeperatorBasedInt: | ||
if !data.contains("\u{1D}") { | ||
node.value = data.substring(from: node.identifier.length) | ||
}else{ | ||
let toi = data.index(of: "\u{1D}") | ||
let to = data.distance(from: data.startIndex, to: toi ?? data.startIndex) | ||
|
||
node.value = data.substring(node.identifier.length, to: to) | ||
} | ||
if node.type == .GroupSeperatorBasedInt{ | ||
node.rawValue = Int(node.value!) | ||
} | ||
|
||
case .Date: | ||
node.rawValue = NSDate.from( | ||
year: Int("20" + data.substring(2, length: 2)), | ||
month: Int(data.substring(4, length: 2)), | ||
day: Int(data.substring(6, length: 2)) | ||
) | ||
node.value = data.substring(2, length: 6) | ||
} | ||
if node.rawValue == nil{ | ||
node.rawValue = node.value | ||
} | ||
return node | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// | ||
// DateExtension.swift | ||
// SwiftGS1Barcode | ||
// | ||
// Created by Toni Hoffmann on 26.06.17. | ||
// Copyright © 2017 Toni Hoffmann. All rights reserved. | ||
// | ||
|
||
import UIKit | ||
|
||
extension NSDate{ | ||
static func from(year: Int?, month: Int?, day: Int?)->NSDate{ | ||
|
||
var dateComponents = DateComponents() | ||
dateComponents.year = year | ||
dateComponents.month = month | ||
dateComponents.day = day | ||
|
||
// Create date from components | ||
let userCalendar = NSCalendar.current | ||
let someDateTime = userCalendar.date(from: dateComponents) | ||
return someDateTime! as NSDate | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// | ||
// SimpleBarcode.swift | ||
// SwiftGS1Barcode | ||
// | ||
// Created by Toni Hoffmann on 26.06.17. | ||
// Copyright © 2017 Toni Hoffmann. All rights reserved. | ||
// | ||
|
||
import UIKit | ||
|
||
class SimpleBarcode: NSObject, Barcode { | ||
var raw: String? | ||
required override init() { | ||
super.init() | ||
} | ||
required init(raw: String) { | ||
self.raw = raw | ||
} | ||
func validate() -> Bool { | ||
return raw != nil && raw! != "" | ||
} | ||
func parse()->Bool { | ||
return validate() | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// | ||
// StringExtension.swift | ||
// SwiftGS1Barcode | ||
// | ||
// Created by Toni Hoffmann on 26.06.17. | ||
// Copyright © 2017 Toni Hoffmann. All rights reserved. | ||
// | ||
|
||
import UIKit | ||
|
||
extension String{ | ||
func substring(_ from: Int, length: Int)->String{ | ||
let start = self.index(self.startIndex, offsetBy: from) | ||
let end = self.index(start, offsetBy: length) | ||
let range = start..<end | ||
|
||
|
||
return self.substring(with: range) // play | ||
} | ||
func substring(_ from: Int, to: Int)->String{ | ||
return self.substring(from, length: to-from) | ||
} | ||
func substring(from: Int)->String{ | ||
return self.substring(from, length: self.length - from) | ||
} | ||
|
||
func substring(to: Int)->String{ | ||
return self.substring(0, length: to) | ||
} | ||
func substring(to: String)->String{ | ||
if let index = self.index(of: to){ | ||
return self.substring(to:index) | ||
} | ||
return self | ||
} | ||
func index(of string: String, options: CompareOptions = .literal) -> Index? { | ||
return range(of: string, options: options)?.lowerBound | ||
} | ||
|
||
func startsWith(_ subString: String)->Bool{ | ||
return self.hasPrefix(subString) | ||
} | ||
var length: Int{ | ||
get{ | ||
return self.characters.count | ||
} | ||
} | ||
} |