This file contains hidden or 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
import java.text.BreakIterator | |
fun String.countWords(): Int { | |
fun isWord(word: String): Boolean { | |
if (word.length == 1) { | |
return word[0].isLetterOrDigit() | |
} | |
return "" != word.trim() | |
} |
This file contains hidden or 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
/// Offsets every character in iso String by a fixed amount to get the regional indicator symbol. | |
/// iso needs to be uppercase. 'A' for example is then 🇦. | |
/// For example an iso "DE" returns "🇩🇪" | |
fun getUnicodeFlag(iso: String): String { | |
var flag = "" | |
val offset = 0x1F1A5 | |
(0 until iso.length) | |
.map { Character.codePointAt(iso, it) + offset } | |
.map { Character.toChars(it) } | |
.forEach { flag += String(it) } |
This file contains hidden or 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
extension UIImage { | |
func resized(to maxSize: CGFloat) -> UIImage? { | |
var size = self.size | |
if size.width <= maxSize && size.height <= maxSize { | |
return self | |
} | |
let scaleFactor = maxSize / max(size.width, size.height) | |
size.width *= scaleFactor | |
size.height *= scaleFactor | |
This file contains hidden or 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
import UIKit | |
extension Sequence { | |
func groupByDay(dateKey: (Iterator.Element) -> Date, order: ComparisonResult = .orderedAscending) -> [[Iterator.Element]] { | |
var groups: [[Iterator.Element]] = [] | |
for element in self { | |
let key = dateKey(element) | |
guard let dayIndex = groups.firstIndex(where: { $0.contains(where: { Calendar.current.isDate(dateKey($0), inSameDayAs: key) }) }) else { | |
guard let nextIndex = groups.firstIndex(where: { $0.contains(where: { dateKey($0).compare(key) == order }) }) else { | |
groups.append([element]) |
This file contains hidden or 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
extension String { | |
/// Returns a new string containing the characters of the String from the one at a given index to the end. If 'from' exceeds endIndex it returns an empty string. | |
func substring(from: Int) -> String { | |
guard let offsetStartIndex = index(startIndex, offsetBy: from, limitedBy: endIndex) else { return "" } | |
return self[offsetStartIndex..<endIndex] | |
} | |
/// Returns a new string containing the characters of the String up to the one at a given index. If 'to' exceeds endIndex it returns the string as it was. | |
func substring(to: Int) -> String { | |
return self[startIndex..<(index(startIndex, offsetBy: to, limitedBy: endIndex) ?? endIndex)] |
This file contains hidden or 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
import CoreLocation | |
static func vCardURL(from coordinate: CLLocationCoordinate2D, with name: String?) -> URL { | |
let vCardFileURL = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent("vCard.loc.vcf") | |
let vCardString = [ | |
"BEGIN:VCARD", | |
"VERSION:4.0", | |
"FN:\(name ?? "Shared Location")", | |
"item1.URL;type=pref:http://maps.apple.com/?ll=\(coordinate.latitude),\(coordinate.longitude)", |
This file contains hidden or 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
import UIKit | |
enum SeparatorType { | |
case horizontal | |
case vertical | |
} | |
let kTotalSize: CGFloat = 18 // the total height of the separator (including parts that are not visible) | |
let kVisibleSize: CGFloat = 8 // the height of the visible portion of the separator | |
let kMargin: CGFloat = (kTotalSize - kVisibleSize) / 2 // the height of the non-visible portions of the separator (i.e. above and below the visible portion) |