Last active
September 2, 2015 09:32
-
-
Save kaunteya/97bc86eb20c5387b1794 to your computer and use it in GitHub Desktop.
This file contains 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 Bool { | |
init<T : IntegerType>(_ integer: T){ | |
self.init(integer != 0) | |
} | |
/// True => 1 False => 0 | |
var toInt: Int { | |
return self ? 1 : 0 | |
} | |
init(val: String) { | |
self.init(val.lowercaseString == "true") | |
} | |
} | |
extension Int { | |
var even: Bool { | |
return self % 2 == 0 | |
} | |
var odd: Bool { | |
return self % 2 == 1 | |
} | |
var toBool: Bool { | |
return self != 0 | |
} | |
} | |
extension Array { | |
func contains <T: Equatable> (element: T) -> Bool { | |
return map{ $0 as! T}.filter{ $0 == element }.first != nil | |
} | |
func indexOf <T: Equatable> (element: T) -> Int? { | |
for (index, eachElement) in enumerate(map{ $0 as! T}) { | |
if element == eachElement { | |
return index | |
} | |
} | |
return .None | |
} | |
mutating func remove <T: Equatable> (element: T) -> T? { | |
if let index = self.indexOf(element) { | |
return self.removeAtIndex(index) as? T | |
} | |
return .None | |
} | |
} | |
extension String { | |
var length: Int { | |
return count(self) | |
} | |
func split(separator: String) -> [String] { | |
return self.componentsSeparatedByString(separator) | |
} | |
func beginsWith (str: String) -> Bool { | |
if let range = self.rangeOfString(str) { | |
return range.startIndex == self.startIndex | |
} | |
return false | |
} | |
func endsWith (str: String) -> Bool { | |
if let range = self.rangeOfString(str) { | |
return range.endIndex == self.endIndex | |
} | |
return false | |
} | |
func replace(target: String, withString: String) -> String | |
{ | |
return self.stringByReplacingOccurrencesOfString(target, withString: withString, options: NSStringCompareOptions.LiteralSearch, range: nil) | |
} | |
func rW(width: Int) -> String { | |
let whiteSpaceCount = max(0, width - count(self)) | |
var finalString = "" | |
for var i = 0 ; i < whiteSpaceCount; i++ { | |
finalString += " " | |
} | |
finalString += self | |
finalString = finalString.substringWithRange(Range<String.Index>(start: finalString.startIndex, end: advance(finalString.startIndex, width))) | |
return finalString | |
} | |
func lW(width: Int) -> String { | |
let whiteSpaceCount = max(0, width - count(self)) | |
var finalString = "" | |
for var i = 0 ; i < whiteSpaceCount; i++ { | |
finalString += " " | |
} | |
finalString = self + finalString | |
finalString = finalString.substringWithRange(Range<String.Index>(start: finalString.startIndex, end: advance(finalString.startIndex, width))) | |
return finalString | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment