Skip to content

Commit

Permalink
Cleaned up the phone URL extractor a bit.
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisMarshallNY committed Jul 22, 2024
1 parent 630e505 commit c783263
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
**1.0.12** *July 22, 2024*

- Added more flexibility, with the phone number parsing.

**1.0.11** *July 5, 2024*

- Added a "cacherizer" to the virtual meeting collection class.
Expand Down
22 changes: 18 additions & 4 deletions Sources/SwiftBMLSDK/SwiftBMLSDK_Parser_UI_Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,20 @@ fileprivate extension StringProtocol {
}
}

/* ################################################################## */
/**
This simply strips out all non-phone characters in the string, leaving only valid phone characters.
*/
var _phoneNumber: String {
let decimalDigits = CharacterSet(charactersIn: "0123456789+-,")
return String(self).filter {
// The higher-order function stuff will convert each character into an aggregate integer, which then becomes a Unicode scalar. Very primitive, but shouldn't be a problem for us, as we only need a very limited ASCII set.
guard let cha = UnicodeScalar($0.unicodeScalars.map { $0.value }.reduce(0, +)) else { return false }

return decimalDigits.contains(cha)
}
}

/* ################################################################## */
/**
This tests a string to see if a given substring is present at the start.
Expand Down Expand Up @@ -258,12 +272,12 @@ extension SwiftBMLSDK_Parser.Meeting: SwiftBMLSDK_MeetingProtocol {
- returns: an optional String. This is the given URI, "cleaned up" ("https://" or "tel:" may be prefixed)
*/
private static func _cleanURI(urlString inURLString: String?) -> String? {
guard var ret: String = inURLString?._urlEncodedString,
guard var ret: String = inURLString?.trimmingCharacters(in: .whitespacesAndNewlines),
let regex = try? NSRegularExpression(pattern: "^(http://|https://|tel://|tel:)", options: .caseInsensitive)
else { return nil }

// We specifically look for tel URIs.
let wasTel = ret.lowercased()._beginsWith("tel:")
// We assume non-HTTP is TEL
let wasTel = !ret.lowercased()._beginsWith("http")

// Yeah, this is pathetic, but it's quick, simple, and works a charm.
ret = regex.stringByReplacingMatches(in: ret, options: [], range: NSRange(location: 0, length: ret.count), withTemplate: "")
Expand All @@ -273,7 +287,7 @@ extension SwiftBMLSDK_Parser.Meeting: SwiftBMLSDK_MeetingProtocol {
}

if wasTel {
ret = "tel:" + ret
ret = "tel:" + ret._phoneNumber
} else {
ret = "https://" + ret
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,7 @@
"$(inherited)",
"@executable_path/Frameworks",
);
MARKETING_VERSION = 1.0.11;
MARKETING_VERSION = 1.0.12;
PRODUCT_BUNDLE_IDENTIFIER = "org.littlegreenviper.SwiftBMLSDK-TestHarness";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_EMIT_LOC_STRINGS = YES;
Expand Down Expand Up @@ -537,7 +537,7 @@
"$(inherited)",
"@executable_path/Frameworks",
);
MARKETING_VERSION = 1.0.11;
MARKETING_VERSION = 1.0.12;
PRODUCT_BUNDLE_IDENTIFIER = "org.littlegreenviper.SwiftBMLSDK-TestHarness";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_EMIT_LOC_STRINGS = YES;
Expand Down

0 comments on commit c783263

Please sign in to comment.