mirror of
https://github.com/omnivore-app/omnivore.git
synced 2026-03-11 08:54:26 +00:00
Move NormalizeURL into its own function
This commit is contained in:
parent
7a645489be
commit
2ed1ea29b9
2 changed files with 52 additions and 44 deletions
|
|
@ -10,16 +10,6 @@ import Utils
|
|||
|
||||
let logger = Logger(subsystem: "app.omnivore", category: "data-service")
|
||||
|
||||
private extension String {
|
||||
func replacingRegex(pattern: String, replaceWith: String = "") -> String {
|
||||
do {
|
||||
let regex = try NSRegularExpression(pattern: pattern, options: [.caseInsensitive, .anchorsMatchLines])
|
||||
let range = NSRange(location: 0, length: utf16.count)
|
||||
return regex.stringByReplacingMatches(in: self, options: [], range: range, withTemplate: replaceWith)
|
||||
} catch { return self }
|
||||
}
|
||||
}
|
||||
|
||||
public final class DataService: ObservableObject {
|
||||
public static var registerIntercomUser: ((String) -> Void)?
|
||||
public static var showIntercomMessenger: (() -> Void)?
|
||||
|
|
@ -119,40 +109,6 @@ public final class DataService: ObservableObject {
|
|||
return isFirstRunOfVersion || isFirstRunWithBuildNumber
|
||||
}
|
||||
|
||||
// based losesly on the normalize-url npm package which we use on the backend
|
||||
func normalizeURL(_ dirtyURL: String) -> String {
|
||||
var urlString = dirtyURL
|
||||
|
||||
urlString = urlString.trimmingCharacters(in: .whitespacesAndNewlines)
|
||||
|
||||
if var urlObject = URLComponents(string: urlString) {
|
||||
// Remove auth
|
||||
if /* options.stripAuthentication */ true {
|
||||
urlObject.user = nil
|
||||
urlObject.password = nil
|
||||
}
|
||||
|
||||
// Remove hash
|
||||
if /* options.stripHash */ true {
|
||||
urlObject.fragment = nil
|
||||
}
|
||||
|
||||
urlObject.queryItems = urlObject.queryItems?.filter { item in
|
||||
item.name.starts(with: "utm_")
|
||||
}
|
||||
|
||||
if /* options.removeTrailingSlash */ true {
|
||||
urlObject.path = urlObject.path.replacingRegex(pattern: "/$", replaceWith: "")
|
||||
}
|
||||
|
||||
if let finalUrl = urlObject.url {
|
||||
return finalUrl.absoluteString
|
||||
}
|
||||
}
|
||||
|
||||
return dirtyURL
|
||||
}
|
||||
|
||||
public func persistPageScrapePayload(_ pageScrape: PageScrapePayload, requestId: String) async throws {
|
||||
let normalizedURL = normalizeURL(pageScrape.url)
|
||||
|
||||
|
|
|
|||
52
apple/OmnivoreKit/Sources/Utils/NormalizeURL.swift
Normal file
52
apple/OmnivoreKit/Sources/Utils/NormalizeURL.swift
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
//
|
||||
// NormalizeURL.swift
|
||||
//
|
||||
//
|
||||
// Created by Jackson Harper on 6/2/22.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
// based losesly on the normalize-url npm package which we use on the backend
|
||||
public func normalizeURL(_ dirtyURL: String) -> String {
|
||||
var urlString = dirtyURL
|
||||
|
||||
urlString = urlString.trimmingCharacters(in: .whitespacesAndNewlines)
|
||||
|
||||
if var urlObject = URLComponents(string: urlString) {
|
||||
// Remove auth
|
||||
if /* options.stripAuthentication */ true {
|
||||
urlObject.user = nil
|
||||
urlObject.password = nil
|
||||
}
|
||||
|
||||
// Remove hash
|
||||
if /* options.stripHash */ true {
|
||||
urlObject.fragment = nil
|
||||
}
|
||||
|
||||
urlObject.queryItems = urlObject.queryItems?.filter { item in
|
||||
item.name.starts(with: "utm_")
|
||||
}
|
||||
|
||||
if /* options.removeTrailingSlash */ true {
|
||||
urlObject.path = urlObject.path.replacingRegex(pattern: "/$", replaceWith: "")
|
||||
}
|
||||
|
||||
if let finalUrl = urlObject.url {
|
||||
return finalUrl.absoluteString
|
||||
}
|
||||
}
|
||||
|
||||
return dirtyURL
|
||||
}
|
||||
|
||||
private extension String {
|
||||
func replacingRegex(pattern: String, replaceWith: String = "") -> String {
|
||||
do {
|
||||
let regex = try NSRegularExpression(pattern: pattern, options: [.caseInsensitive, .anchorsMatchLines])
|
||||
let range = NSRange(location: 0, length: utf16.count)
|
||||
return regex.stringByReplacingMatches(in: self, options: [], range: range, withTemplate: replaceWith)
|
||||
} catch { return self }
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue