Skip to content

Commit

Permalink
Add new extensions and modify readme
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonL committed Jul 19, 2019
1 parent 5748787 commit c75a874
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 14 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
# TymateSwiftExtensions

Just add "https://github.com/tymate/tymate-swift-extensions.git" as source and the pod 'TymateSwiftExtensions' to your Podfile
[![Platforms](https://img.shields.io/badge/platforms-iOS%20%7C%20tvOS%20%7C%20macOS%20%7C%20watchOS-lightgrey.svg)](https://github.com/tymate/tymate-swift-extensions)
[![Swift](https://img.shields.io/badge/Swift-5.0-orange.svg)](https://swift.org)
[![Xcode](https://img.shields.io/badge/Xcode-10.2-blue.svg)](https://developer.apple.com/xcode)
[![MIT](https://img.shields.io/badge/License-MIT-red.svg)](https://opensource.org/licenses/MIT)

This pod have some useful functions and extensions that are often use in swift projects
For example you can check if a string is a valid email easily or design an imageView in circle

## String Extensions

Expand Down Expand Up @@ -62,7 +68,7 @@ To integrate Extensions into your Xcode project using CocoaPods, specify it in y

```ruby
source 'https://github.com/tymate/tymate-swift-extensions.git'
platform :ios, '8.0'

use_frameworks!

pod 'TymateSwiftExtensions'
Expand Down
2 changes: 1 addition & 1 deletion TymateSwiftExtensions.podspec
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Pod::Spec.new do |spec|

spec.name = "TymateSwiftExtensions"
spec.version = "0.0.5"
spec.version = "0.0.6"
spec.summary = "A CocoaPods library written in Swift"

spec.description = <<-DESC
Expand Down
4 changes: 4 additions & 0 deletions TymateSwiftExtensions.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
/* Begin PBXBuildFile section */
5B6AA39F467E0BF2D262468F /* Pods_TymateSwiftExtensions.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 6685F60706721708C67C8CF6 /* Pods_TymateSwiftExtensions.framework */; };
F403403022D76E4500784A3C /* UIViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = F403402F22D76E4500784A3C /* UIViewController.swift */; };
F437662222E1FB7C004D24BD /* DateExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = F437662122E1FB7C004D24BD /* DateExtensions.swift */; };
F464DAE522D39F2C002413BD /* TymateSwiftExtensions.h in Headers */ = {isa = PBXBuildFile; fileRef = F464DAE322D39F2C002413BD /* TymateSwiftExtensions.h */; settings = {ATTRIBUTES = (Public, ); }; };
F464DB1322D730A5002413BD /* StringExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = F464DB1222D730A5002413BD /* StringExtensions.swift */; };
F464DB3322D76917002413BD /* DateFormatterExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = F464DB3222D76917002413BD /* DateFormatterExtensions.swift */; };
Expand All @@ -20,6 +21,7 @@
6685F60706721708C67C8CF6 /* Pods_TymateSwiftExtensions.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_TymateSwiftExtensions.framework; sourceTree = BUILT_PRODUCTS_DIR; };
78A9AA7B809D409F49089628 /* Pods-TymateSwiftExtensions.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-TymateSwiftExtensions.release.xcconfig"; path = "Target Support Files/Pods-TymateSwiftExtensions/Pods-TymateSwiftExtensions.release.xcconfig"; sourceTree = "<group>"; };
F403402F22D76E4500784A3C /* UIViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UIViewController.swift; sourceTree = "<group>"; };
F437662122E1FB7C004D24BD /* DateExtensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DateExtensions.swift; sourceTree = "<group>"; };
F464DAE022D39F2C002413BD /* TymateSwiftExtensions.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = TymateSwiftExtensions.framework; sourceTree = BUILT_PRODUCTS_DIR; };
F464DAE322D39F2C002413BD /* TymateSwiftExtensions.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = TymateSwiftExtensions.h; sourceTree = "<group>"; };
F464DAE422D39F2C002413BD /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
Expand Down Expand Up @@ -84,6 +86,7 @@
F464DB3222D76917002413BD /* DateFormatterExtensions.swift */,
F464DB3422D769E3002413BD /* UIImageViewExtensions.swift */,
F403402F22D76E4500784A3C /* UIViewController.swift */,
F437662122E1FB7C004D24BD /* DateExtensions.swift */,
);
path = TymateSwiftExtensions;
sourceTree = "<group>";
Expand Down Expand Up @@ -194,6 +197,7 @@
buildActionMask = 2147483647;
files = (
F464DB3322D76917002413BD /* DateFormatterExtensions.swift in Sources */,
F437662222E1FB7C004D24BD /* DateExtensions.swift in Sources */,
F464DB3522D769E3002413BD /* UIImageViewExtensions.swift in Sources */,
F403403022D76E4500784A3C /* UIViewController.swift in Sources */,
F464DB1322D730A5002413BD /* StringExtensions.swift in Sources */,
Expand Down
Binary file not shown.
22 changes: 22 additions & 0 deletions TymateSwiftExtensions/DateExtensions.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//
// DateExtensions.swift
// TymateSwiftExtensions
//
// Created by Simon on 19/07/2019.
// Copyright © 2019 Tymate. All rights reserved.
//

import Foundation

extension Date {
func days(between otherDate: Date) -> Int {
let calendar = Calendar.current

let startOfSelf = calendar.startOfDay(for: self)
let startOfOther = calendar.startOfDay(for: otherDate)
let components = calendar.dateComponents([.day], from: startOfSelf, to: startOfOther)

return abs(components.day ?? 0)
}
}

13 changes: 11 additions & 2 deletions TymateSwiftExtensions/StringExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ import Foundation

public extension String {
func isValidEmail() -> Bool {
// print("validate calendar: \(testStr)")
let emailRegEx = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,}"
let emailRegEx = "(?:[a-zA-Z0-9!#$%\\&‘*+/=?\\^_`{|}~-]+(?:\\.[a-zA-Z0-9!#$%\\&'*+/=?\\^_`{|}” + “~-]+)*|\"(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21\\x23-\\x5b\\x5d-\\” + “x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])*\")@(?:(?:[a-z0-9](?:[a-” + “z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\\[(?:(?:25[0-5” + “]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-” + “9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21” + “-\\x5a\\x53-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])+)\\])"

let emailTest = NSPredicate(format: "SELF MATCHES %@", emailRegEx)
return emailTest.evaluate(with: self)
Expand All @@ -32,6 +31,11 @@ public extension String {
return "https://" + withoutHttps
}

func withPrefix(_ prefix: String) -> String {
if self.hasPrefix(prefix) { return self }
return "\(prefix)\(self)"
}

func phoneCall() {
if let url = URL(string: "tel://\(self)"), UIApplication.shared.canOpenURL(url) {
if #available(iOS 10, *) {
Expand Down Expand Up @@ -80,4 +84,9 @@ public extension String {
func isEqualToZero() -> Bool {
return self.toFloat() == 0
}

func wordCount() -> Int {
let regex = try? NSRegularExpression(pattern: "\\w+")
return regex?.numberOfMatches(in: self, range: NSRange(location: 0, length: self.utf16.count)) ?? 0
}
}

0 comments on commit c75a874

Please sign in to comment.