From a1bcc5f5715286c6da20ecb8f1fa260f51068f59 Mon Sep 17 00:00:00 2001 From: Oktawian Chojnacki Date: Fri, 20 Nov 2015 19:19:02 +0100 Subject: [PATCH 1/3] Improved time helpers. --- README.md | 2 + Src/SwiftyTimer.swift | 10 +--- .../project.pbxproj | 4 ++ Tests/SwiftyTimerTests/SwiftyTime.swift | 54 +++++++++++++++++++ Tests/SwiftyTimerTests/main.swift | 4 ++ 5 files changed, 65 insertions(+), 9 deletions(-) create mode 100644 Tests/SwiftyTimerTests/SwiftyTime.swift diff --git a/README.md b/README.md index 016f8a1..637f784 100644 --- a/README.md +++ b/README.md @@ -23,11 +23,13 @@ SwiftyTimer uses closures instead of target/selector/userInfo. You can specify time intervals with intuitive [Ruby on Rails](http://rubyonrails.org)-like helpers: ```swift +100.miliseconds 1.second 2.5.seconds 5.seconds 10.minutes 1.hour +2.days ``` You can pass method references instead of closures: diff --git a/Src/SwiftyTimer.swift b/Src/SwiftyTimer.swift index 47f0081..dc79eff 100644 --- a/Src/SwiftyTimer.swift +++ b/Src/SwiftyTimer.swift @@ -89,12 +89,4 @@ extension NSTimer { } } -extension Double { - public var ms: NSTimeInterval { return self / 1000 } - public var second: NSTimeInterval { return self } - public var seconds: NSTimeInterval { return self } - public var minute: NSTimeInterval { return self * 60 } - public var minutes: NSTimeInterval { return self * 60 } - public var hour: NSTimeInterval { return self * 3600 } - public var hours: NSTimeInterval { return self * 3600 } -} + diff --git a/Tests/SwiftyTimerTests.xcodeproj/project.pbxproj b/Tests/SwiftyTimerTests.xcodeproj/project.pbxproj index 1c3f8ff..35646b8 100644 --- a/Tests/SwiftyTimerTests.xcodeproj/project.pbxproj +++ b/Tests/SwiftyTimerTests.xcodeproj/project.pbxproj @@ -7,11 +7,13 @@ objects = { /* Begin PBXBuildFile section */ + 1AF1E4861BFE6E40004F5DEC /* SwiftyTime.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1AF1E4851BFE6E40004F5DEC /* SwiftyTime.swift */; }; 6EE9C1591B00BB5B00D6B91C /* main.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6EE9C1581B00BB5B00D6B91C /* main.swift */; }; 6EE9C1741B00BBB700D6B91C /* SwiftyTimer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6EE9C1731B00BBB700D6B91C /* SwiftyTimer.swift */; }; /* End PBXBuildFile section */ /* Begin PBXFileReference section */ + 1AF1E4851BFE6E40004F5DEC /* SwiftyTime.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SwiftyTime.swift; sourceTree = ""; }; 6EE9C1531B00BB5B00D6B91C /* SwiftyTimerTests.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = SwiftyTimerTests.app; sourceTree = BUILT_PRODUCTS_DIR; }; 6EE9C1571B00BB5B00D6B91C /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 6EE9C1581B00BB5B00D6B91C /* main.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = main.swift; sourceTree = ""; }; @@ -49,6 +51,7 @@ isa = PBXGroup; children = ( 6EE9C1731B00BBB700D6B91C /* SwiftyTimer.swift */, + 1AF1E4851BFE6E40004F5DEC /* SwiftyTime.swift */, 6EE9C1581B00BB5B00D6B91C /* main.swift */, 6EE9C1561B00BB5B00D6B91C /* Supporting Files */, ); @@ -130,6 +133,7 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( + 1AF1E4861BFE6E40004F5DEC /* SwiftyTime.swift in Sources */, 6EE9C1591B00BB5B00D6B91C /* main.swift in Sources */, 6EE9C1741B00BBB700D6B91C /* SwiftyTimer.swift in Sources */, ); diff --git a/Tests/SwiftyTimerTests/SwiftyTime.swift b/Tests/SwiftyTimerTests/SwiftyTime.swift new file mode 100644 index 0000000..1fd7020 --- /dev/null +++ b/Tests/SwiftyTimerTests/SwiftyTime.swift @@ -0,0 +1,54 @@ +// +// SwiftyTimer +// +// Copyright (c) 2015 Oktawian Chojnacki and Radosław Pietruszewski +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. +// + +import Foundation + +private let miliDivider: Double = 1000 +private let secondsInMinute: Double = 60 +private let minutesInHour: Double = 60 +private let hoursInDay: Double = 24 + +extension Double { + + private func assertOneOrLess(value: Double) -> Double { + assert(self <= 1, "🤓 Use plural property for numbers above 1.") + return value + } + + public var milisecond: NSTimeInterval { return assertOneOrLess(miliseconds) } + public var miliseconds: NSTimeInterval { return self / miliDivider } + public var ms: NSTimeInterval { return miliseconds } + + public var second: NSTimeInterval { return assertOneOrLess(seconds) } + public var seconds: NSTimeInterval { return self } + + public var minute: NSTimeInterval { return assertOneOrLess(minutes) } + public var minutes: NSTimeInterval { return self * secondsInMinute } + + public var hour: NSTimeInterval { return assertOneOrLess(hours)} + public var hours: NSTimeInterval { return minutes * minutesInHour } + + public var day: NSTimeInterval { return assertOneOrLess(days) } + public var days: NSTimeInterval { return hours * hoursInDay } +} diff --git a/Tests/SwiftyTimerTests/main.swift b/Tests/SwiftyTimerTests/main.swift index 7c835fb..012c8d8 100644 --- a/Tests/SwiftyTimerTests/main.swift +++ b/Tests/SwiftyTimerTests/main.swift @@ -14,6 +14,10 @@ class AppDelegate: NSObject, NSApplicationDelegate { assert(1.2.seconds == 1.2) assert(1.5.minutes == 90.0) assert(1.5.hours == 5400.0) + assert(1.3.miliseconds == 0.0013) + assert(0.5.day == 43_200 ) + assert(1.day == 86_400 ) + assert(2.days == 172_800) test2() } From 91eee7cdac1fafbea5c113b428d9dc0c6a836f88 Mon Sep 17 00:00:00 2001 From: Oktawian Chojnacki Date: Thu, 10 Dec 2015 16:29:02 +0100 Subject: [PATCH 2/3] MiLLi. --- README.md | 2 +- .../SwiftyTimerTests => Src}/SwiftyTime.swift | 24 +++++++++---------- .../project.pbxproj | 8 +++---- Tests/SwiftyTimerTests/main.swift | 2 +- 4 files changed, 18 insertions(+), 18 deletions(-) rename {Tests/SwiftyTimerTests => Src}/SwiftyTime.swift (63%) diff --git a/README.md b/README.md index 637f784..13c4e13 100644 --- a/README.md +++ b/README.md @@ -23,7 +23,7 @@ SwiftyTimer uses closures instead of target/selector/userInfo. You can specify time intervals with intuitive [Ruby on Rails](http://rubyonrails.org)-like helpers: ```swift -100.miliseconds +100.milliseconds 1.second 2.5.seconds 5.seconds diff --git a/Tests/SwiftyTimerTests/SwiftyTime.swift b/Src/SwiftyTime.swift similarity index 63% rename from Tests/SwiftyTimerTests/SwiftyTime.swift rename to Src/SwiftyTime.swift index 1fd7020..e53fe3c 100644 --- a/Tests/SwiftyTimerTests/SwiftyTime.swift +++ b/Src/SwiftyTime.swift @@ -24,7 +24,7 @@ import Foundation -private let miliDivider: Double = 1000 +private let milliDivider: Double = 1000 private let secondsInMinute: Double = 60 private let minutesInHour: Double = 60 private let hoursInDay: Double = 24 @@ -36,19 +36,19 @@ extension Double { return value } - public var milisecond: NSTimeInterval { return assertOneOrLess(miliseconds) } - public var miliseconds: NSTimeInterval { return self / miliDivider } - public var ms: NSTimeInterval { return miliseconds } + public var millisecond: NSTimeInterval { return assertOneOrLess(milliseconds) } + public var milliseconds: NSTimeInterval { return self / milliDivider } + public var ms: NSTimeInterval { return milliseconds } - public var second: NSTimeInterval { return assertOneOrLess(seconds) } - public var seconds: NSTimeInterval { return self } + public var second: NSTimeInterval { return assertOneOrLess(seconds) } + public var seconds: NSTimeInterval { return self } - public var minute: NSTimeInterval { return assertOneOrLess(minutes) } - public var minutes: NSTimeInterval { return self * secondsInMinute } + public var minute: NSTimeInterval { return assertOneOrLess(minutes) } + public var minutes: NSTimeInterval { return self * secondsInMinute } - public var hour: NSTimeInterval { return assertOneOrLess(hours)} - public var hours: NSTimeInterval { return minutes * minutesInHour } + public var hour: NSTimeInterval { return assertOneOrLess(hours)} + public var hours: NSTimeInterval { return minutes * minutesInHour } - public var day: NSTimeInterval { return assertOneOrLess(days) } - public var days: NSTimeInterval { return hours * hoursInDay } + public var day: NSTimeInterval { return assertOneOrLess(days) } + public var days: NSTimeInterval { return hours * hoursInDay } } diff --git a/Tests/SwiftyTimerTests.xcodeproj/project.pbxproj b/Tests/SwiftyTimerTests.xcodeproj/project.pbxproj index 35646b8..5ba7122 100644 --- a/Tests/SwiftyTimerTests.xcodeproj/project.pbxproj +++ b/Tests/SwiftyTimerTests.xcodeproj/project.pbxproj @@ -7,13 +7,13 @@ objects = { /* Begin PBXBuildFile section */ - 1AF1E4861BFE6E40004F5DEC /* SwiftyTime.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1AF1E4851BFE6E40004F5DEC /* SwiftyTime.swift */; }; + 1A4561A11C19D12600313D20 /* SwiftyTime.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1A4561A01C19D12600313D20 /* SwiftyTime.swift */; }; 6EE9C1591B00BB5B00D6B91C /* main.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6EE9C1581B00BB5B00D6B91C /* main.swift */; }; 6EE9C1741B00BBB700D6B91C /* SwiftyTimer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6EE9C1731B00BBB700D6B91C /* SwiftyTimer.swift */; }; /* End PBXBuildFile section */ /* Begin PBXFileReference section */ - 1AF1E4851BFE6E40004F5DEC /* SwiftyTime.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SwiftyTime.swift; sourceTree = ""; }; + 1A4561A01C19D12600313D20 /* SwiftyTime.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = SwiftyTime.swift; path = ../../Src/SwiftyTime.swift; sourceTree = ""; }; 6EE9C1531B00BB5B00D6B91C /* SwiftyTimerTests.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = SwiftyTimerTests.app; sourceTree = BUILT_PRODUCTS_DIR; }; 6EE9C1571B00BB5B00D6B91C /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 6EE9C1581B00BB5B00D6B91C /* main.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = main.swift; sourceTree = ""; }; @@ -50,8 +50,8 @@ 6EE9C1551B00BB5B00D6B91C /* SwiftyTimerTests */ = { isa = PBXGroup; children = ( + 1A4561A01C19D12600313D20 /* SwiftyTime.swift */, 6EE9C1731B00BBB700D6B91C /* SwiftyTimer.swift */, - 1AF1E4851BFE6E40004F5DEC /* SwiftyTime.swift */, 6EE9C1581B00BB5B00D6B91C /* main.swift */, 6EE9C1561B00BB5B00D6B91C /* Supporting Files */, ); @@ -133,7 +133,7 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( - 1AF1E4861BFE6E40004F5DEC /* SwiftyTime.swift in Sources */, + 1A4561A11C19D12600313D20 /* SwiftyTime.swift in Sources */, 6EE9C1591B00BB5B00D6B91C /* main.swift in Sources */, 6EE9C1741B00BBB700D6B91C /* SwiftyTimer.swift in Sources */, ); diff --git a/Tests/SwiftyTimerTests/main.swift b/Tests/SwiftyTimerTests/main.swift index 012c8d8..e33639e 100644 --- a/Tests/SwiftyTimerTests/main.swift +++ b/Tests/SwiftyTimerTests/main.swift @@ -14,7 +14,7 @@ class AppDelegate: NSObject, NSApplicationDelegate { assert(1.2.seconds == 1.2) assert(1.5.minutes == 90.0) assert(1.5.hours == 5400.0) - assert(1.3.miliseconds == 0.0013) + assert(1.3.milliseconds == 0.0013) assert(0.5.day == 43_200 ) assert(1.day == 86_400 ) assert(2.days == 172_800) From 4dfa99a327842a8865359a1693725965fcac8689 Mon Sep 17 00:00:00 2001 From: Oktawian Chojnacki Date: Thu, 10 Dec 2015 16:48:00 +0100 Subject: [PATCH 3/3] Formatting improvement. --- Src/SwiftyTime.swift | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/Src/SwiftyTime.swift b/Src/SwiftyTime.swift index e53fe3c..303f764 100644 --- a/Src/SwiftyTime.swift +++ b/Src/SwiftyTime.swift @@ -36,19 +36,19 @@ extension Double { return value } - public var millisecond: NSTimeInterval { return assertOneOrLess(milliseconds) } + public var millisecond: NSTimeInterval { return assertOneOrLess(milliseconds) } public var milliseconds: NSTimeInterval { return self / milliDivider } - public var ms: NSTimeInterval { return milliseconds } + public var ms: NSTimeInterval { return milliseconds } - public var second: NSTimeInterval { return assertOneOrLess(seconds) } - public var seconds: NSTimeInterval { return self } + public var second: NSTimeInterval { return assertOneOrLess(seconds) } + public var seconds: NSTimeInterval { return self } - public var minute: NSTimeInterval { return assertOneOrLess(minutes) } - public var minutes: NSTimeInterval { return self * secondsInMinute } + public var minute: NSTimeInterval { return assertOneOrLess(minutes) } + public var minutes: NSTimeInterval { return self * secondsInMinute } - public var hour: NSTimeInterval { return assertOneOrLess(hours)} - public var hours: NSTimeInterval { return minutes * minutesInHour } + public var hour: NSTimeInterval { return assertOneOrLess(hours)} + public var hours: NSTimeInterval { return minutes * minutesInHour } - public var day: NSTimeInterval { return assertOneOrLess(days) } - public var days: NSTimeInterval { return hours * hoursInDay } + public var day: NSTimeInterval { return assertOneOrLess(days) } + public var days: NSTimeInterval { return hours * hoursInDay } }