Skip to content
This repository has been archived by the owner on Jul 5, 2023. It is now read-only.

Commit

Permalink
Fixed bug with transactions that had no reference text
Browse files Browse the repository at this point in the history
  • Loading branch information
paulofierro committed Oct 25, 2022
1 parent 2d78878 commit 78977b2
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 30 deletions.
29 changes: 25 additions & 4 deletions Butterfield2FreeAgent.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

/* Begin PBXBuildFile section */
3AC77C6922CA4B35008F20A0 /* B2FA.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3AC77C6822CA4B35008F20A0 /* B2FA.swift */; };
3AE1ED4F29072AAA00AA8247 /* PFToolbox in Frameworks */ = {isa = PBXBuildFile; productRef = 3AE1ED4E29072AAA00AA8247 /* PFToolbox */; };
3AF9A0A724ABA1B800D7455F /* Transaction.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3AF9A0A624ABA1B800D7455F /* Transaction.swift */; };
3AF9A0AA24ABA24B00D7455F /* String.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3AF9A0A924ABA24B00D7455F /* String.swift */; };
3AF9A0AD24ABA5A600D7455F /* ArgumentParser in Frameworks */ = {isa = PBXBuildFile; productRef = 3AF9A0AC24ABA5A600D7455F /* ArgumentParser */; };
Expand Down Expand Up @@ -39,6 +40,7 @@
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
3AE1ED4F29072AAA00AA8247 /* PFToolbox in Frameworks */,
3AF9A0AD24ABA5A600D7455F /* ArgumentParser in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down Expand Up @@ -99,6 +101,7 @@
name = b2fa;
packageProductDependencies = (
3AF9A0AC24ABA5A600D7455F /* ArgumentParser */,
3AE1ED4E29072AAA00AA8247 /* PFToolbox */,
);
productName = Butterfield2FreeAgent;
productReference = 3AC77C6522CA4B35008F20A0 /* b2fa */;
Expand All @@ -111,7 +114,7 @@
isa = PBXProject;
attributes = {
LastSwiftUpdateCheck = 1100;
LastUpgradeCheck = 1200;
LastUpgradeCheck = 1410;
ORGANIZATIONNAME = "Jadehopper Ltd";
TargetAttributes = {
3AC77C6422CA4B35008F20A0 = {
Expand All @@ -130,6 +133,7 @@
mainGroup = 3AC77C5C22CA4B35008F20A0;
packageReferences = (
3AF9A0AB24ABA5A600D7455F /* XCRemoteSwiftPackageReference "swift-argument-parser" */,
3AE1ED4D29072AAA00AA8247 /* XCRemoteSwiftPackageReference "PFToolbox" */,
);
productRefGroup = 3AC77C6622CA4B35008F20A0 /* Products */;
projectDirPath = "";
Expand Down Expand Up @@ -189,6 +193,7 @@
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
COPY_PHASE_STRIP = NO;
DEAD_CODE_STRIPPING = YES;
DEBUG_INFORMATION_FORMAT = dwarf;
ENABLE_STRICT_OBJC_MSGSEND = YES;
ENABLE_TESTABILITY = YES;
Expand All @@ -206,7 +211,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
MACOSX_DEPLOYMENT_TARGET = 10.14;
MACOSX_DEPLOYMENT_TARGET = 11.0;
MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE;
MTL_FAST_MATH = YES;
ONLY_ACTIVE_ARCH = YES;
Expand Down Expand Up @@ -251,6 +256,7 @@
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
COPY_PHASE_STRIP = NO;
DEAD_CODE_STRIPPING = YES;
DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
ENABLE_NS_ASSERTIONS = NO;
ENABLE_STRICT_OBJC_MSGSEND = YES;
Expand All @@ -262,7 +268,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
MACOSX_DEPLOYMENT_TARGET = 10.14;
MACOSX_DEPLOYMENT_TARGET = 11.0;
MTL_ENABLE_DEBUG_INFO = NO;
MTL_FAST_MATH = YES;
SDKROOT = macosx;
Expand All @@ -277,6 +283,7 @@
buildSettings = {
CODE_SIGN_IDENTITY = "-";
CODE_SIGN_STYLE = Automatic;
DEAD_CODE_STRIPPING = YES;
DEVELOPMENT_TEAM = NG69HBMD6R;
ENABLE_HARDENED_RUNTIME = YES;
PRODUCT_NAME = "$(TARGET_NAME)";
Expand All @@ -288,6 +295,7 @@
buildSettings = {
CODE_SIGN_IDENTITY = "-";
CODE_SIGN_STYLE = Automatic;
DEAD_CODE_STRIPPING = YES;
DEVELOPMENT_TEAM = NG69HBMD6R;
ENABLE_HARDENED_RUNTIME = YES;
PRODUCT_NAME = "$(TARGET_NAME)";
Expand Down Expand Up @@ -318,17 +326,30 @@
/* End XCConfigurationList section */

/* Begin XCRemoteSwiftPackageReference section */
3AE1ED4D29072AAA00AA8247 /* XCRemoteSwiftPackageReference "PFToolbox" */ = {
isa = XCRemoteSwiftPackageReference;
repositoryURL = "https://github.com/paulofierro/PFToolbox";
requirement = {
branch = main;
kind = branch;
};
};
3AF9A0AB24ABA5A600D7455F /* XCRemoteSwiftPackageReference "swift-argument-parser" */ = {
isa = XCRemoteSwiftPackageReference;
repositoryURL = "https://github.com/apple/swift-argument-parser";
requirement = {
kind = upToNextMajorVersion;
minimumVersion = 0.2.0;
minimumVersion = 1.1.4;
};
};
/* End XCRemoteSwiftPackageReference section */

/* Begin XCSwiftPackageProductDependency section */
3AE1ED4E29072AAA00AA8247 /* PFToolbox */ = {
isa = XCSwiftPackageProductDependency;
package = 3AE1ED4D29072AAA00AA8247 /* XCRemoteSwiftPackageReference "PFToolbox" */;
productName = PFToolbox;
};
3AF9A0AC24ABA5A600D7455F /* ArgumentParser */ = {
isa = XCSwiftPackageProductDependency;
package = 3AF9A0AB24ABA5A600D7455F /* XCRemoteSwiftPackageReference "swift-argument-parser" */;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
{
"object": {
"pins": [
{
"package": "swift-argument-parser",
"repositoryURL": "https://github.com/apple/swift-argument-parser",
"state": {
"branch": null,
"revision": "eb51f949cdd0c9d88abba9ce79d37eb7ea1231d0",
"version": "0.2.0"
}
"pins" : [
{
"identity" : "pftoolbox",
"kind" : "remoteSourceControl",
"location" : "https://github.com/paulofierro/PFToolbox",
"state" : {
"branch" : "main",
"revision" : "19c09358c65f5b212afc79304db3f9bddb504a45"
}
]
},
"version": 1
},
{
"identity" : "swift-argument-parser",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-argument-parser",
"state" : {
"revision" : "9f39744e025c7d377987f30b03770805dcb0bcd1",
"version" : "1.1.4"
}
}
],
"version" : 2
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1200"
LastUpgradeVersion = "1410"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down Expand Up @@ -52,7 +52,7 @@
</BuildableProductRunnable>
<CommandLineArguments>
<CommandLineArgument
argument = "/Users/paulo/Development/B2FA/sample.csv"
argument = "/Users/paulo/Desktop/butterfield/1666630872264.csv"
isEnabled = "YES">
</CommandLineArgument>
</CommandLineArguments>
Expand Down
15 changes: 11 additions & 4 deletions Butterfield2FreeAgent/B2FA.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import ArgumentParser
import Foundation
import PFToolbox

@main
struct B2FA: ParsableCommand {
Expand All @@ -28,7 +29,7 @@ struct B2FA: ParsableCommand {
.converted()
.write(to: outputPath)

print("Converted file and saved as \(outputPath).")
log.info("Converted file and saved as \(outputPath).")
}
}

Expand All @@ -42,9 +43,10 @@ extension B2FA {
let lines = contents.split(separator: "\n")

// For some reason I can't get this to match quite right, as \\d+ doesn't work for the thousands
// so we do two expressions, one for single and another for double. Not been paid a triple yet...
// so we do three expressions.
let singleExpression = try NSRegularExpression(pattern: "\"\\d,\\d+\\.\\d\\d\"", options: [])
let doubleExpression = try NSRegularExpression(pattern: "\"\\d\\d,\\d+\\.\\d\\d\"", options: [])
let tripleExpression = try NSRegularExpression(pattern: "\"\\d\\d\\d,\\d+\\.\\d\\d\"", options: [])
let quoteExpression = try NSRegularExpression(pattern: "\"(.*)\"", options: [])

// Remove the non-transaction lines, and create a list of Transaction objects
Expand All @@ -63,7 +65,7 @@ extension B2FA {
// Convert "XY,ABC.DE" to "XYABC.DE", as otherwise
// this will screw up the comma-based separation later.
var string = String(line)
[singleExpression, doubleExpression, quoteExpression].forEach { expression in
[singleExpression, doubleExpression, tripleExpression, quoteExpression].forEach { expression in
let range = NSRange(location: 0, length: string.count)
expression
.enumerateMatches(in: string, options:[], range: range) { result, flags, pointer in
Expand All @@ -90,7 +92,12 @@ extension B2FA {
// If lines don't have a debit or crebit the value is empty which results in ",,"
// in the transaction line. Unfortunately split() removes this element which results
// in out of bounds exceptsions, so insert a 0
let values = line.replacingOccurrences(of: ",,", with: ",0.00,").split(separator: ",")
let values = line
.replacingOccurrences(of: ",,", with: ",0.00,")
.split(separator: ",")
.map {
String($0)
}
return try Transaction(with: values)
}
return transactions
Expand Down
4 changes: 2 additions & 2 deletions Butterfield2FreeAgent/Extensions/String.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ extension String {
}


extension Substring {
extension String {
/// Remove whitespace, newlines and quotes
var trimmed: String {
return String(self)
return self
.trimmingCharacters(in: .whitespacesAndNewlines)
.replacingOccurrences(of: "\"", with: "")
}
Expand Down
21 changes: 16 additions & 5 deletions Butterfield2FreeAgent/Transaction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//

import Foundation
import PFToolbox

/// The structure of a parsed transaction object
struct Transaction {
Expand Down Expand Up @@ -36,11 +37,19 @@ struct Transaction {
return Self.outputDateFormatter.string(from: valueDate)
}

init(with values: [String.SubSequence]) throws {
init(with values: [String]) throws {

// Trim the value for a specific column
let trimValue: ((Column)) -> String = { column in
return values[column.rawValue].trimmed
let trimValue: (Column) -> String = { column in
guard let value = values[safeIndex: column.rawValue] else {
switch column {
case .reference:
return "No reference"
default:
fatalError("Column \(column) has no data")
}
}
return value.trimmed
}

// Format the dates
Expand All @@ -62,17 +71,19 @@ struct Transaction {

private extension Transaction {

private static let locale = Locale(identifier: "en_US_POSIX")

static var inputDateFormatter: DateFormatter {
let formatter = DateFormatter()
formatter.dateFormat = "dd MM yyyy"
formatter.locale = Locale(identifier: "en_US_POSIX")
formatter.locale = locale
return formatter
}

static var outputDateFormatter: DateFormatter {
let formatter = DateFormatter()
formatter.dateFormat = "dd/MM/yyyy"
formatter.locale = Locale(identifier: "en_US_POSIX")
formatter.locale = locale
return formatter
}
}

0 comments on commit 78977b2

Please sign in to comment.