Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcSteven committed Nov 11, 2023
1 parent 617aa98 commit e4285f3
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,30 +73,4 @@ public extension AVAsset {
return isPortrait
}
}
import Foundation
import UIKit

extension PHAsset {
var originalFilename: NSString {
if let resource = PHAssetResource.assetResources(for: self).first {
return resource.originalFilename as NSString
} else {
return self.value(forKey: "filename") as? NSString
?? ("IMG_" + CCUtility.getIncrementalNumber() + getExtension()) as NSString
}
}

private func getExtension() -> String {
switch mediaType {
case .video:
return ".mp4"
case .image:
return ".jpg"
case .audio:
return ".mp3"
default:
return ".unknownType"
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,15 @@ public extension NSManagedObject {
didAccessValue(forKey: key)
return result.flatMap({ValueType(rawValue: $0)})
}

/// Lookup an object by its NSManagedObjectID
///
/// - Parameters:
/// - objectID: The `NSManagedObject` subclass' objectID as defined by Core Data.
/// - context: An NSManagedObjectContext that contains the associated object.
/// - Returns: The `NSManagedObject` subclass associated with the given `objectID`, if it exists.
static func lookup(withObjectID objectID: NSManagedObjectID, in context: NSManagedObjectContext) -> Self? {
return try? context.existingObject(with: objectID) as? Self
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,7 @@

import UIKit
import Foundation
public extension NotificationationCenter {
func postOnMainThread(name: String, object anObject: Any? = nil, userInfo aUserInfo: [AnyHashable: Any]? = nil, second: Double = 0) {
DispatchQueue.main.asyncAfter(deadline: .now() + second) {
NotificationCenter.default.post(name: Notification.Name(rawValue: name), object: anObject, userInfo: aUserInfo)
}
}
}

public extension NotificationCenter {
func postOnMainThread(name: String, object anObject: Any? = nil, userInfo aUserInfo: [AnyHashable : Any]? = nil) {
// if UIApplication.shared.applicationState == .background {
Expand Down
74 changes: 74 additions & 0 deletions MemoryChainKit/Sources/Extensions/Foudantion/StringExtension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -831,4 +831,78 @@ public extension String {
guard let message = data(using: .utf8) else { return nil }
return message.hashed(type, output: output)
}

///
/// Attempts to remove excessive whitespace in text by replacing multiple new lines with just 2.
/// This first trims whitespace and newlines from the ends
/// Then normalizes the newlines by replacing {Space}{Newline} with a single newline char
/// Then finally it looks for any newlines that are 3 or more and replaces them with 2 newlines.
///
/// Example:
/// ```
/// This is the first line
///
///
///
///
/// This is the last line
/// ```
/// Turns into:
/// ```
/// This is the first line
///
/// This is the last line
/// ```
///
func condenseWhitespace() -> String {
return self.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)
.replacingOccurrences(of: "\\s\n", with: "\n", options: .regularExpression, range: nil)
.replacingOccurrences(of: "[\n]{3,}", with: "\n\n", options: .regularExpression, range: nil)
}
}

//MARK: - regex
public extension String {

/// Find all matches of the specified regex.
///
/// - Parameters:
/// - regex: the regex to use.
/// - options: the regex options.
///
/// - Returns: the requested matches.
///
func matches(regex: String, options: NSRegularExpression.Options = []) -> [NSTextCheckingResult] {
let regex = try! NSRegularExpression(pattern: regex, options: options)
let fullRange = NSRange(location: 0, length: count)

return regex.matches(in: self, options: [], range: fullRange)
}

/// Replaces all matches of a given RegEx, with a template String.
///
/// - Parameters:
/// - regex: the regex to use.
/// - template: the template string to use for the replacement.
/// - options: the regex options.
///
/// - Returns: a new string after replacing all matches with the specified template.
///
func replacingMatches(of regex: String, with template: String, options: NSRegularExpression.Options = []) -> String {

let regex = try! NSRegularExpression(pattern: regex, options: options)
let fullRange = NSRange(location: 0, length: count)

return regex.stringByReplacingMatches(in: self,
options: [],
range: fullRange,
withTemplate: template)
}


/// Returns a NSRange instance starting at position 0, with the entire String's Length
///
var foundationRangeOfEntireString: NSRange {
return NSRange(location: 0, length: utf16.count)
}
}

0 comments on commit e4285f3

Please sign in to comment.