diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..3acb79a --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,239 @@ +# Change Log + +## [Unreleased](https://github.com/pNre/ExSwift/tree/HEAD) + +[Full Changelog](https://github.com/pNre/ExSwift/compare/0.1.9...HEAD) + +**Closed issues:** + +- NSDate addUnit stuff not working in ios7 [\#97](https://github.com/pNre/ExSwift/issues/97) + +- weak support [\#96](https://github.com/pNre/ExSwift/issues/96) + +- @synchronized support [\#95](https://github.com/pNre/ExSwift/issues/95) + +- string.toDouble\(\) should accept "1.23e4" [\#94](https://github.com/pNre/ExSwift/issues/94) + +- Compile fix for Swift 1.2 [\#90](https://github.com/pNre/ExSwift/issues/90) + +- Swift-1.2 branch causing Segmentation fault: 11 [\#81](https://github.com/pNre/ExSwift/issues/81) + +- Support for Swift 1.2 [\#80](https://github.com/pNre/ExSwift/issues/80) + +- Still breaks range literals [\#71](https://github.com/pNre/ExSwift/issues/71) + +- flatMap [\#62](https://github.com/pNre/ExSwift/issues/62) + +- Nil filter method on array [\#59](https://github.com/pNre/ExSwift/issues/59) + +- don't use array extension function [\#54](https://github.com/pNre/ExSwift/issues/54) + +- Documentation small issue [\#53](https://github.com/pNre/ExSwift/issues/53) + +- Spare the TakeSequence and TakeWhileSequence structs [\#52](https://github.com/pNre/ExSwift/issues/52) + +- No one is merging in changes [\#49](https://github.com/pNre/ExSwift/issues/49) + +- Swift Compile Error on Xcode 6.1 [\#38](https://github.com/pNre/ExSwift/issues/38) + +- ExSwift breaks range subscripting [\#32](https://github.com/pNre/ExSwift/issues/32) + +- skipWhile [\#31](https://github.com/pNre/ExSwift/issues/31) + +**Merged pull requests:** + +- Enhance Carthage Support [\#103](https://github.com/pNre/ExSwift/pull/103) ([nebhale](https://github.com/nebhale)) + +- Remove Deprecation Warnings [\#102](https://github.com/pNre/ExSwift/pull/102) ([nebhale](https://github.com/nebhale)) + +- Fix compatibility issue NSDate add [\#101](https://github.com/pNre/ExSwift/pull/101) ([PGLongo](https://github.com/PGLongo)) + +- Ex.cached for 1-arg functions. [\#93](https://github.com/pNre/ExSwift/pull/93) ([hiltonc](https://github.com/hiltonc)) + +- fixed for Xcode Version 6.3 beta 3 [\#89](https://github.com/pNre/ExSwift/pull/89) ([katopz](https://github.com/katopz)) + +- fix compiler error 'missing argument \#2' [\#88](https://github.com/pNre/ExSwift/pull/88) ([sebbean](https://github.com/sebbean)) + +- Format Number [\#86](https://github.com/pNre/ExSwift/pull/86) ([PGLongo](https://github.com/PGLongo)) + +- Fix compiler errors in second Xcode 6.3 beta \(6D532l\). [\#84](https://github.com/pNre/ExSwift/pull/84) ([JakobR](https://github.com/JakobR)) + +- Add Lisp's find method to Arrays. [\#83](https://github.com/pNre/ExSwift/pull/83) ([nickmshelley](https://github.com/nickmshelley)) + +- Add Bool extension [\#79](https://github.com/pNre/ExSwift/pull/79) ([davidman](https://github.com/davidman)) + +- Swift 1.2 \(Xcode 6.3\) support [\#78](https://github.com/pNre/ExSwift/pull/78) ([markusl](https://github.com/markusl)) + +- Made Framework and Unit Test iOS / OS X universal [\#76](https://github.com/pNre/ExSwift/pull/76) ([colemancda](https://github.com/colemancda)) + +- added method and tests [\#73](https://github.com/pNre/ExSwift/pull/73) ([michaeleisel](https://github.com/michaeleisel)) + +- added methods and tests [\#72](https://github.com/pNre/ExSwift/pull/72) ([michaeleisel](https://github.com/michaeleisel)) + +- added method and tests [\#70](https://github.com/pNre/ExSwift/pull/70) ([michaeleisel](https://github.com/michaeleisel)) + +- added method, tests, and whitespace changes [\#69](https://github.com/pNre/ExSwift/pull/69) ([michaeleisel](https://github.com/michaeleisel)) + +- Round to nearest [\#68](https://github.com/pNre/ExSwift/pull/68) ([michaeleisel](https://github.com/michaeleisel)) + +- added arithmetic methods [\#67](https://github.com/pNre/ExSwift/pull/67) ([michaeleisel](https://github.com/michaeleisel)) + +- added strideable methods [\#66](https://github.com/pNre/ExSwift/pull/66) ([michaeleisel](https://github.com/michaeleisel)) + +- removed unnecessary methods [\#65](https://github.com/pNre/ExSwift/pull/65) ([michaeleisel](https://github.com/michaeleisel)) + +- completely redid permutation generating [\#64](https://github.com/pNre/ExSwift/pull/64) ([michaeleisel](https://github.com/michaeleisel)) + +- Add shared schemes for installation by Carthage. [\#63](https://github.com/pNre/ExSwift/pull/63) ([yoichitgy](https://github.com/yoichitgy)) + +- Add getter and comparison to NSDate [\#61](https://github.com/pNre/ExSwift/pull/61) ([PGLongo](https://github.com/PGLongo)) + +- mapAccum for Array [\#60](https://github.com/pNre/ExSwift/pull/60) ([phatmann](https://github.com/phatmann)) + +- Added to\* methods to readme docs. [\#58](https://github.com/pNre/ExSwift/pull/58) ([mikeckennedy](https://github.com/mikeckennedy)) + +- Trimmed methods fail on string that contains only whitespace [\#57](https://github.com/pNre/ExSwift/pull/57) ([pizthewiz](https://github.com/pizthewiz)) + +- Add toNumeric \(e.g. toDouble\) conversions to string extensions [\#56](https://github.com/pNre/ExSwift/pull/56) ([mikeckennedy](https://github.com/mikeckennedy)) + +- Added overloaded ltrimmed and rtrimmed methods [\#55](https://github.com/pNre/ExSwift/pull/55) ([hhoangnl](https://github.com/hhoangnl)) + +- Update Dictionary.swift [\#51](https://github.com/pNre/ExSwift/pull/51) ([davidman](https://github.com/davidman)) + +- Added Charater extension [\#50](https://github.com/pNre/ExSwift/pull/50) ([cennydavidsson](https://github.com/cennydavidsson)) + +- Repeated permutations [\#48](https://github.com/pNre/ExSwift/pull/48) ([michaeleisel](https://github.com/michaeleisel)) + +- Added NSDate methods [\#47](https://github.com/pNre/ExSwift/pull/47) ([PGLongo](https://github.com/PGLongo)) + +- Repeated combinations [\#46](https://github.com/pNre/ExSwift/pull/46) ([michaeleisel](https://github.com/michaeleisel)) + +- Fill [\#45](https://github.com/pNre/ExSwift/pull/45) ([michaeleisel](https://github.com/michaeleisel)) + +- Transpose [\#44](https://github.com/pNre/ExSwift/pull/44) ([michaeleisel](https://github.com/michaeleisel)) + +- Permutations [\#43](https://github.com/pNre/ExSwift/pull/43) ([michaeleisel](https://github.com/michaeleisel)) + +- added combinations method [\#42](https://github.com/pNre/ExSwift/pull/42) ([michaeleisel](https://github.com/michaeleisel)) + +- Unique by method [\#41](https://github.com/pNre/ExSwift/pull/41) ([michaeleisel](https://github.com/michaeleisel)) + +- Added UIColor extension [\#39](https://github.com/pNre/ExSwift/pull/39) ([PGLongo](https://github.com/PGLongo)) + +- Added minBy and maxBy methods [\#37](https://github.com/pNre/ExSwift/pull/37) ([michaeleisel](https://github.com/michaeleisel)) + +- Add installation instruction [\#35](https://github.com/pNre/ExSwift/pull/35) ([kostiakoval](https://github.com/kostiakoval)) + +- the script can generation a Framework of StaticLibrary [\#30](https://github.com/pNre/ExSwift/pull/30) ([zhfish](https://github.com/zhfish)) + +## [0.1.9](https://github.com/pNre/ExSwift/tree/0.1.9) (2014-08-20) + +[Full Changelog](https://github.com/pNre/ExSwift/compare/0.1.8...0.1.9) + +**Merged pull requests:** + +- Added count methods [\#27](https://github.com/pNre/ExSwift/pull/27) ([jmnavarro](https://github.com/jmnavarro)) + +## [0.1.8](https://github.com/pNre/ExSwift/tree/0.1.8) (2014-08-05) + +[Full Changelog](https://github.com/pNre/ExSwift/compare/0.1.7...0.1.8) + +**Closed issues:** + +- ExSwift broken in beta 5 [\#26](https://github.com/pNre/ExSwift/issues/26) + +- Single index string subscripting results in compiler error [\#25](https://github.com/pNre/ExSwift/issues/25) + +- Array.indexOf not working with \[UInt\] [\#21](https://github.com/pNre/ExSwift/issues/21) + +**Merged pull requests:** + +- Changes =~ to take regular expression. [\#22](https://github.com/pNre/ExSwift/pull/22) ([nomothetis](https://github.com/nomothetis)) + +## [0.1.7](https://github.com/pNre/ExSwift/tree/0.1.7) (2014-07-25) + +[Full Changelog](https://github.com/pNre/ExSwift/compare/0.1.6...0.1.7) + +**Closed issues:** + +- Breaks in Xcode6-Beta4 [\#19](https://github.com/pNre/ExSwift/issues/19) + +**Merged pull requests:** + +- Added access modifiers [\#20](https://github.com/pNre/ExSwift/pull/20) ([vmartinelli](https://github.com/vmartinelli)) + +## [0.1.6](https://github.com/pNre/ExSwift/tree/0.1.6) (2014-07-22) + +[Full Changelog](https://github.com/pNre/ExSwift/compare/0.1.5...0.1.6) + +## [0.1.5](https://github.com/pNre/ExSwift/tree/0.1.5) (2014-07-19) + +[Full Changelog](https://github.com/pNre/ExSwift/compare/0.1.4...0.1.5) + +**Closed issues:** + +- Support for Double / Integer [\#18](https://github.com/pNre/ExSwift/issues/18) + +- Help, got so many compile errors [\#17](https://github.com/pNre/ExSwift/issues/17) + +## [0.1.4](https://github.com/pNre/ExSwift/tree/0.1.4) (2014-07-07) + +[Full Changelog](https://github.com/pNre/ExSwift/compare/0.1.3...0.1.4) + +**Merged pull requests:** + +- used shuffle\(\) to shuffle in shuffled\(\) [\#16](https://github.com/pNre/ExSwift/pull/16) ([natecook1000](https://github.com/natecook1000)) + +## [0.1.3](https://github.com/pNre/ExSwift/tree/0.1.3) (2014-07-03) + +[Full Changelog](https://github.com/pNre/ExSwift/compare/0.1.2...0.1.3) + +**Merged pull requests:** + +- Sequence ops [\#15](https://github.com/pNre/ExSwift/pull/15) ([ColinEberhardt](https://github.com/ColinEberhardt)) + +- Added a toDictionary method [\#13](https://github.com/pNre/ExSwift/pull/13) ([ColinEberhardt](https://github.com/ColinEberhardt)) + +- Added a sortBy method [\#12](https://github.com/pNre/ExSwift/pull/12) ([ColinEberhardt](https://github.com/ColinEberhardt)) + +- Define iOS module with the same name as OS X, cleanup duplicate files [\#11](https://github.com/pNre/ExSwift/pull/11) ([garnett](https://github.com/garnett)) + +- Simplify partition tests [\#10](https://github.com/pNre/ExSwift/pull/10) ([zolrath](https://github.com/zolrath)) + +## [0.1.2](https://github.com/pNre/ExSwift/tree/0.1.2) (2014-06-18) + +[Full Changelog](https://github.com/pNre/ExSwift/compare/0.1.1...0.1.2) + +**Fixed bugs:** + +- Compile error: Access String array by subscript [\#6](https://github.com/pNre/ExSwift/issues/6) + +**Closed issues:** + +- Result of indexOf, lastIndexOf should be an optional [\#8](https://github.com/pNre/ExSwift/issues/8) + +**Merged pull requests:** + +- Add .gitignore, add iOS Framework + tests target [\#7](https://github.com/pNre/ExSwift/pull/7) ([garnett](https://github.com/garnett)) + +- return Int? in indexOf, lastIndexOf [\#9](https://github.com/pNre/ExSwift/pull/9) ([garnett](https://github.com/garnett)) + +- Add partition, partitionAll, and partitionBy [\#5](https://github.com/pNre/ExSwift/pull/5) ([zolrath](https://github.com/zolrath)) + +- Add takeWhile and skipWhile to Array [\#4](https://github.com/pNre/ExSwift/pull/4) ([zolrath](https://github.com/zolrath)) + +## [0.1.1](https://github.com/pNre/ExSwift/tree/0.1.1) (2014-06-11) + +[Full Changelog](https://github.com/pNre/ExSwift/compare/0.1.0...0.1.1) + +**Merged pull requests:** + +- Use if let syntax for Dictionary {group, count}by [\#3](https://github.com/pNre/ExSwift/pull/3) ([zolrath](https://github.com/zolrath)) + +- Use if let syntax for groupBy and countBy [\#2](https://github.com/pNre/ExSwift/pull/2) ([zolrath](https://github.com/zolrath)) + +## [0.1.0](https://github.com/pNre/ExSwift/tree/0.1.0) (2014-06-10) + + + +\* *This Change Log was automatically generated by [github_changelog_generator](https://github.com/skywinder/Github-Changelog-Generator)* \ No newline at end of file diff --git a/ExSwift.xcodeproj/project.pbxproj b/ExSwift.xcodeproj/project.pbxproj index d70b12f..1f5e0e6 100755 --- a/ExSwift.xcodeproj/project.pbxproj +++ b/ExSwift.xcodeproj/project.pbxproj @@ -85,6 +85,13 @@ CC633BA61A37142900341557 /* CharacterExtensionsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = CC633BA51A37142900341557 /* CharacterExtensionsTests.swift */; }; CC633BA71A37144E00341557 /* Character.swift in Sources */ = {isa = PBXBuildFile; fileRef = CC8C1D701A365CA6003D386E /* Character.swift */; }; CC8C1D711A365CA6003D386E /* Character.swift in Sources */ = {isa = PBXBuildFile; fileRef = CC8C1D701A365CA6003D386E /* Character.swift */; }; + 3C0AB97E195C7FC20009BDA0 /* ExSwiftSequenceTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3C0AB97C195C7FBC0009BDA0 /* ExSwiftSequenceTests.swift */; }; + 6CB1F15F1A8AB867002EA767 /* Bool.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6CB1F15E1A8AB867002EA767 /* Bool.swift */; }; + 6CB1F1611A8AB8D3002EA767 /* ExSwiftBoolTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6CB1F1601A8AB8D3002EA767 /* ExSwiftBoolTests.swift */; }; + 6CB1F1621A8AB979002EA767 /* ExSwiftBoolTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6CB1F1601A8AB8D3002EA767 /* ExSwiftBoolTests.swift */; }; + 6CB1F1631A8ABA1A002EA767 /* Bool.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6CB1F15E1A8AB867002EA767 /* Bool.swift */; }; + 6CB1F1641A8ABA1A002EA767 /* Bool.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6CB1F15E1A8AB867002EA767 /* Bool.swift */; }; + 6CB1F1661A8ABA1C002EA767 /* Bool.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6CB1F15E1A8AB867002EA767 /* Bool.swift */; }; /* End PBXBuildFile section */ /* Begin PBXContainerItemProxy section */ @@ -135,6 +142,7 @@ CC633BA51A37142900341557 /* CharacterExtensionsTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CharacterExtensionsTests.swift; sourceTree = ""; }; CC8C1D701A365CA6003D386E /* Character.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Character.swift; sourceTree = ""; }; FA8EE3398AF8D20E3CE7A698 /* Pods-ExSwiftTests-OSX.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-ExSwiftTests-OSX.debug.xcconfig"; path = "Pods/Target Support Files/Pods-ExSwiftTests-OSX/Pods-ExSwiftTests-OSX.debug.xcconfig"; sourceTree = ""; }; + 6CB1F15E1A8AB867002EA767 /* Bool.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Bool.swift; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -210,6 +218,7 @@ 1E11AFB11943225B006BCE48 /* String.swift */, 1EC241FB1946E91B0047109A /* NSArray.swift */, 12168FC91A22852300ED4105 /* NSDate.swift */, + 6CB1F15E1A8AB867002EA767 /* Bool.swift */, 1E11AF891943222D006BCE48 /* ExSwift.h */, 1E11AF871943222D006BCE48 /* Supporting Files */, ); @@ -550,6 +559,7 @@ 3C0AB978195C7FAF0009BDA0 /* Sequence.swift in Sources */, 1E11AFB21943225B006BCE48 /* Array.swift in Sources */, 1ED536841943863100BDA94E /* ExSwift.swift in Sources */, + 6CB1F15F1A8AB867002EA767 /* Bool.swift in Sources */, 1E11AFB41943225B006BCE48 /* Dictionary.swift in Sources */, 1E11AFB61943225B006BCE48 /* Float.swift in Sources */, 1E11AFB81943225B006BCE48 /* Int.swift in Sources */, @@ -579,6 +589,7 @@ 2EB34F0C195473AC00A8D2AF /* NSArray.swift in Sources */, CC633BA71A37144E00341557 /* Character.swift in Sources */, 2EB34F05195473AC00A8D2AF /* ExSwift.swift in Sources */, + 6CB1F1631A8ABA1A002EA767 /* Bool.swift in Sources */, 2EB34F08195473AC00A8D2AF /* Float.swift in Sources */, 12168FCD1A2285A900ED4105 /* NSDateExtensionsTests.swift in Sources */, 1E11AFAB19432236006BCE48 /* StringExtensionsTests.swift in Sources */, diff --git a/ExSwift/Array.swift b/ExSwift/Array.swift index af24b6d..7aaa316 100644 --- a/ExSwift/Array.swift +++ b/ExSwift/Array.swift @@ -124,6 +124,30 @@ internal extension Array { @availability(*, unavailable, message="use the 'last' property instead") func last () -> Element? { return last } + + /** + First occurrence of item, if found. + + :param: item The item to search for + :returns: Matched item or nil + */ + func find (item: U) -> T? { + if let index = indexOf(item) { + return self[index] + } + + return nil + } + + /** + First item that meets the condition. + + :param: condition A function which returns a boolean if an element satisfies a given condition or not. + :returns: First matched item or nil + */ + func find (condition: Element -> Bool) -> Element? { + return takeFirst(condition) + } /** Index of the first occurrence of item, if found. @@ -133,7 +157,7 @@ internal extension Array { */ func indexOf (item: U) -> Int? { if item is Element { - return find(unsafeBitCast(self, [U].self), item) + return Swift.find(unsafeBitCast(self, [U].self), item) } return nil diff --git a/ExSwift/Bool.swift b/ExSwift/Bool.swift new file mode 100644 index 0000000..7071efe --- /dev/null +++ b/ExSwift/Bool.swift @@ -0,0 +1,18 @@ +// +// Bool.swift +// ExSwift +// +// Created by Hernandez Alvarez, David on 2/10/15. +// Copyright (c) 2015 pNre. All rights reserved. +// + +import Foundation + +extension Bool { + + mutating func toggle() -> Bool { + self = !self + return self + } + +} \ No newline at end of file diff --git a/ExSwift/ExSwift.swift b/ExSwift/ExSwift.swift index d5776d3..82c5dc0 100644 --- a/ExSwift/ExSwift.swift +++ b/ExSwift/ExSwift.swift @@ -140,6 +140,28 @@ public class ExSwift { } } + /** + Creates a wrapper for function that caches the result of function's invocations. + + :param: function Function with one parameter to cache + :returns: Wrapper function + */ + public class func cached (function: P -> R) -> (P -> R) { + var cache = [P:R]() + + return { (param: P) -> R in + let key = param + + if let cachedValue = cache[key] { + return cachedValue + } else { + let value = function(param) + cache[key] = value + return value + } + } + } + /** Creates a wrapper for function that caches the result of function's invocations. @@ -154,7 +176,6 @@ public class ExSwift { var cache = [P:R]() return { (params: P...) -> R in - let adaptedFunction = unsafeBitCast(function, Function.self) let adaptedHash = unsafeBitCast(hash, Hash.self) @@ -162,11 +183,11 @@ public class ExSwift { if let cachedValue = cache[key] { return cachedValue + } else { + let value = adaptedFunction(params) + cache[key] = value + return value } - - cache[key] = adaptedFunction(params) - - return cache[key]! } } diff --git a/ExSwift/NSDate.swift b/ExSwift/NSDate.swift index f45a907..db1a49b 100644 --- a/ExSwift/NSDate.swift +++ b/ExSwift/NSDate.swift @@ -24,9 +24,41 @@ public extension NSDate { :param: years number of years to add :returns: the NSDate computed */ - public func add(seconds: Int = 0, minutes: Int = 0, hours: Int = 0, days: Int = 0, weeks: Int = 0, months: Int = 0, years: Int = 0) -> NSDate { + public func add(seconds: Int = 0, minutes: Int = 0, hours: Int = 0, days: Int = 0, weeks: Int = 0, months: Int = 0, years: Int = 0) -> NSDate { var calendar = NSCalendar.currentCalendar() - var date = calendar.dateByAddingUnit(.CalendarUnitSecond, value: seconds, toDate: self, options: nil) as NSDate! + let version = floor(NSFoundationVersionNumber) + if ( version <= NSFoundationVersionNumber_iOS_7_1 || version <= NSFoundationVersionNumber10_9_2 ) { + var component = NSDateComponents() + component.setValue(seconds, forComponent: .CalendarUnitSecond) + + var date : NSDate! = calendar.dateByAddingComponents(component, toDate: self, options: nil)! + component = NSDateComponents() + component.setValue(minutes, forComponent: .CalendarUnitMinute) + date = calendar.dateByAddingComponents(component, toDate: date, options: nil)! + + component = NSDateComponents() + component.setValue(hours, forComponent: .CalendarUnitHour) + date = calendar.dateByAddingComponents(component, toDate: date, options: nil)! + + component = NSDateComponents() + component.setValue(days, forComponent: .CalendarUnitDay) + date = calendar.dateByAddingComponents(component, toDate: date, options: nil)! + + component = NSDateComponents() + component.setValue(weeks, forComponent: .CalendarUnitWeekOfMonth) + date = calendar.dateByAddingComponents(component, toDate: date, options: nil)! + + component = NSDateComponents() + component.setValue(months, forComponent: .CalendarUnitMonth) + date = calendar.dateByAddingComponents(component, toDate: date, options: nil)! + + component = NSDateComponents() + component.setValue(years, forComponent: .CalendarUnitYear) + date = calendar.dateByAddingComponents(component, toDate: date, options: nil)! + return date + } + + var date : NSDate! = calendar.dateByAddingUnit(.CalendarUnitSecond, value: seconds, toDate: self, options: nil) date = calendar.dateByAddingUnit(.CalendarUnitMinute, value: minutes, toDate: date, options: nil) date = calendar.dateByAddingUnit(.CalendarUnitDay, value: days, toDate: date, options: nil) date = calendar.dateByAddingUnit(.CalendarUnitHour, value: hours, toDate: date, options: nil) diff --git a/ExSwift/String.swift b/ExSwift/String.swift index f0cfdce..2c7f1b6 100644 --- a/ExSwift/String.swift +++ b/ExSwift/String.swift @@ -14,15 +14,15 @@ public extension String { String length */ var length: Int { return count(self) } - + /** self.capitalizedString shorthand */ var capitalized: String { return capitalizedString } - + /** Returns the substring in the given range - + :param: range :returns: Substring in range */ @@ -39,7 +39,7 @@ public extension String { /** Equivalent to at. Takes a list of indexes and returns an Array containing the elements at the given indexes in self. - + :param: firstIndex :param: secondIndex :param: restOfIndexes @@ -50,9 +50,9 @@ public extension String { } /** - Gets the character at the specified index as String. + Gets the character at the specified index as String. If index is negative it is assumed to be relative to the end of the String. - + :param: index Position of the character to get :returns: Character as String or nil if the index is out of bounds */ @@ -66,7 +66,7 @@ public extension String { /** Takes a list of indexes and returns an Array containing the elements at the given indexes in self. - + :param: indexes Positions of the elements to get :returns: Array of characters (as String) */ @@ -76,7 +76,7 @@ public extension String { /** Takes a list of indexes and returns an Array containing the elements at the given indexes in self. - + :param: indexes Positions of the elements to get :returns: Array of characters (as String) */ @@ -86,7 +86,7 @@ public extension String { /** Returns an array of strings, each of which is a substring of self formed by splitting it on separator. - + :param: separator Character used to split the string :returns: Array of substrings */ @@ -98,7 +98,7 @@ public extension String { /** Finds any match in self for pattern. - + :param: pattern Pattern to match :param: ignoreCase true for case insensitive matching :returns: Matches found (as [NSTextCheckingResult]) @@ -115,7 +115,7 @@ public extension String { /** Inserts a substring at the given index in self. - + :param: index Where the new string is inserted :param: string String to insert :returns: String formed from self inserting string at index @@ -127,47 +127,47 @@ public extension String { } else if index < 0 { return string + self } - + return self[0.. String { if let range = rangeOfCharacterFromSet(set.invertedSet) { return self[range.startIndex.. String { return trimmedLeft(characterSet: set) } - + /** Strips the specified characters from the end of self. - + :returns: Stripped string */ func trimmedRight (characterSet set: NSCharacterSet = NSCharacterSet.whitespaceAndNewlineCharacterSet()) -> String { if let range = rangeOfCharacterFromSet(set.invertedSet, options: NSStringCompareOptions.BackwardsSearch) { return self[startIndex.. String { return trimmedRight(characterSet: set) } - + /** Strips whitespaces from both the beginning and the end of self. - + :returns: Stripped string */ func trimmed () -> String { @@ -176,7 +176,7 @@ public extension String { /** Costructs a string using random chars from a given set. - + :param: length String length. If < 1, it's randomly selected in the range 0..16 :param: charset Chars to use in the random string :returns: Random string @@ -206,19 +206,15 @@ public extension String { */ func toDouble() -> Double? { - let pattern = "^[-+]?[0-9]*\\.?[0-9]+$" + let scanner = NSScanner(string: self) + var double: Double = 0 - if let regex = ExSwift.regex(pattern, ignoreCase: true) { - let text = self.trimmed() - let matches = regex.matchesInString(text, options: nil, range: NSMakeRange(0, count(text))) - if matches.isEmpty { - return nil - } - - return (self as NSString).doubleValue + if scanner.scanDouble(&double) { + return double } return nil + } /** @@ -227,11 +223,16 @@ public extension String { :returns: A float parsed from the string or nil if it cannot be parsed. */ func toFloat() -> Float? { - if let val = self.toDouble() { - return Float(val) + + let scanner = NSScanner(string: self) + var float: Float = 0 + + if scanner.scanFloat(&float) { + return float } return nil + } /** @@ -305,7 +306,7 @@ public func * (first: String, n: Int) -> String { } return result - + } // Pattern matching using a regular expression @@ -313,18 +314,18 @@ public func =~ (string: String, pattern: String) -> Bool { let regex = ExSwift.regex(pattern, ignoreCase: false)! let matches = regex.numberOfMatchesInString(string, options: nil, range: NSMakeRange(0, string.length)) - + return matches > 0 - + } // Pattern matching using a regular expression public func =~ (string: String, regex: NSRegularExpression) -> Bool { let matches = regex.numberOfMatchesInString(string, options: nil, range: NSMakeRange(0, string.length)) - + return matches > 0 - + } // This version also allowes to specify case sentitivity @@ -335,35 +336,35 @@ public func =~ (string: String, options: (pattern: String, ignoreCase: Bool)) -> } return false - + } // Match against all the alements in an array of String public func =~ (strings: [String], pattern: String) -> Bool { let regex = ExSwift.regex(pattern, ignoreCase: false)! - + return strings.all { $0 =~ regex } - + } public func =~ (strings: [String], options: (pattern: String, ignoreCase: Bool)) -> Bool { return strings.all { $0 =~ options } - + } // Match against any element in an array of String public func |~ (strings: [String], pattern: String) -> Bool { let regex = ExSwift.regex(pattern, ignoreCase: false)! - + return strings.any { $0 =~ regex } - + } public func |~ (strings: [String], options: (pattern: String, ignoreCase: Bool)) -> Bool { return strings.any { $0 =~ options } - + } diff --git a/ExSwiftTests/ExSwiftBoolTests.swift b/ExSwiftTests/ExSwiftBoolTests.swift new file mode 100644 index 0000000..6696c1f --- /dev/null +++ b/ExSwiftTests/ExSwiftBoolTests.swift @@ -0,0 +1,23 @@ +// +// ExSwiftBoolTests.swift +// ExSwift +// +// Created by Hernandez Alvarez, David on 2/10/15. +// Copyright (c) 2015 pNre. All rights reserved. +// + +import XCTest + +class ExSwiftBoolTests: XCTestCase { + + func testToogleTrue() { + var bool: Bool = false + XCTAssertTrue(bool.toggle(), "Bool did not toogle to true") + } + + func testToogleFalse() { + var bool: Bool = true + XCTAssertFalse(bool.toggle(), "Bool did not toogle to false") + } + +} \ No newline at end of file diff --git a/ExSwiftTests/ExSwiftTests.swift b/ExSwiftTests/ExSwiftTests.swift index 9bd20bd..b92ab05 100644 --- a/ExSwiftTests/ExSwiftTests.swift +++ b/ExSwiftTests/ExSwiftTests.swift @@ -10,115 +10,114 @@ import Quick import Nimble class ExSwiftSpec: QuickSpec { - + override func spec() { - + it("after") { - + let f = ExSwift.after(2, function: { () -> Bool in return true }) - + expect(f()).to(beNil()) expect(f()).to(beNil()) - + expect(f()).to(beTrue()) - + var called = false let g = ExSwift.after(2, function: { called = true }) - + g() - + expect(called).to(beFalse()) - + g() - + expect(called).to(beFalse()) - + g() - + expect(called).to(beTrue()) - + } - + it("once") { - + var seq = [1, 2, 3, 4].generate() - + let g = Ex.once { Void -> Int in return seq.next()! } - + expect(g()) == 1 expect(g()) == 1 - + } - + it("partial") { - + let add = { (params: Int...) -> Int in return params.reduce(0, combine: +) } - + let add5 = ExSwift.partial(add, 5) - + expect(add5(10)) == 15 expect(add5(1, 2)) == 8 - + } - + it("bind") { - + let concat = { (params: String...) -> String in return params.implode(" ")! } - + let helloWorld = ExSwift.bind(concat, "Hello", "World") - + expect(helloWorld()) == "Hello World" - + } - + it("cached") { - + var calls = 0 - + // Slow Fibonacci var fib: ((Int...) -> Int)! fib = { (params: Int...) -> Int in let n = params[0] - + calls++ - + if n <= 1 { return n } return fib(n - 1) + fib(n - 2) } - + let fibonacci = Ex.cached(fib) - + // This one is computed (fib is called 465 times) fibonacci(12) expect(calls) == 465 - + // The results is taken from the cache (fib is not called again) fibonacci(12) expect(calls) == 465 } - + describe("operators") { - + it("spaceship") { - + expect(4 <=> 5) == -1 expect(5 <=> 4) == 1 expect(4 <=> 4) == 0 - + } - + } - + } } - diff --git a/README.md b/README.md index 015872c..9b72642 100644 --- a/README.md +++ b/README.md @@ -428,7 +428,7 @@ Name | Signatures **`once`**|`once (function: P -> T) -> (P -> T?)`
`once (call: Void -> T) -> (Void -> T?)` **`partial`**|`partial (function: (P...) -> T, _ parameters: P...) -> ((P...) -> T?)` **`bind`**|`bind (function: (P...) -> T, _ parameters: P...) -> (() -> T)` -**`cached`**|`cached (function: (P...) -> R) -> ((P...) -> R)`
`cached (function: (P...) -> R, hash: ((P...) -> P)) -> ((P...) -> R)` +**`cached`**|`cached (function: P -> R) -> (P -> R)`
`cached (function: (P...) -> R) -> ((P...) -> R)`
`cached (function: (P...) -> R, hash: ((P...) -> P)) -> ((P...) -> R)` #### Operators #### Name | Signatures