Cent is a library that extends certain Swift object types using the extension feature and gives its two cents to Swift language.
Dollar is a Swift library that provides useful functional programming helper methods without extending any built in objects. It is similar to Lo-Dash or Underscore.js in Javascript.
Using cocoapods version 0.36.x or greater
Add pod 'Cent' to your Podfile and run pod install. Add use_frameworks! to the end of the Podfile. Requires cocoapod version 0.36.x or greater.
Using Swift Package Manager
Add the following dependency .Package(url: "https://github.com/ankurp/Cent", majorVersion: 6, minor: 0) to your Package.swift file and then run swift build. Requires swift version 2.2 or greater that you can install from https://swift.org
- If you are using git then add Cent as a submodule using
git submodule add https://github.com/ankurp/Cent.git. If not using git download the project usinggit clone https://github.com/ankurp/Cent.gitin your project folder. - Open the
Centfolder. Drag Cent.xcodeproj, inside the Cent folder, into the file navigator of your Xcode project. - In Xcode, navigate to the target configuration window by clicking on the blue project icon, and selecting the application target under the "Targets" heading in the sidebar.
- In the tab bar at the top of that window, open the "Build Phases" panel.
- Expand the "Link Binary with Libraries" group, and add Cent.framework.
- In your project file
import Centand you can call all of the helper functions.
- For Xcode 9 (Swift 4) user version
7.0.0 - For Xcode 8 (Swift 3) user version
6.0.4 - For Xcode 7 (Swift 2) use version
4.1.0or5.2.0 - For Xcode 6.3 (Swift 1.2) use version
3.0.3 - For Xcode 6.1 and 6.2 (Swift 1.1) use version
2.2.0
- If you need help, use gitter.im or post a question on Stack Overflow with tag
dollar.swift. - If you'd like to ask a general question, use Stack Overflow.
- If you found a bug, open an issue.
- If you have a feature request, open an issue.
- If you want to contribute, submit a pull request.
Overloaded operator to append element to an array or append elements from another array into the first array. Return array with the element appended in the end.
vararray=[1,2,3]
array << 4=>[1,2,3,4]
array << [5,6]=>[1,2,3,4,5,6]Creates an array of elements from the specified indexes, or keys, of the collection.
letarray=["foo","spam","bar","eggs"]letsome= array.at(1,3)=>["spam","eggs"]For each item in the array invoke the callback by passing the elem
letarray=["foo","spam","bar","eggs"]
array.each{print($0)}=>["foo","spam","bar","eggs"]For each item in the array invoke the callback by passing the elem along with the index
letarray=["foo","spam","bar","eggs"]
array.each{(index, elem)print("\(index) - \(elem)")}=>["foo","spam","bar","eggs"]Cycles through the array definetly or indefinetly passing each element into the callback function. The second parameter is to specify how many times to cycle through the array. If left out it will cycle indefinetly.
[1,2,3].cycle(2){print($0)}
// Prints the following
123123[1,2,3].cycle{print($0)}
// Cycles in an infinite loopChecks if the given callback returns true value for all items in the array.
["angry","hungry"].every{(a:String)->(Bool)in a.hasSuffix("gry")}=>trueGets the index at which the first occurrence of value is found.
letarray=["foo","spam","bar","eggs"]
array.indexOf("spam")=>1
array.indexOf("None")=>nilGet element from an array at the given index which can be negative to find elements from the end of the array. A default value can be returned if indexing out of bounds.
letarr=[1,2,3,4,5,6,7,8]
arr.fetch(100)=>nil
arr.fetch(100, orElse:42)=>42
arr.fetch(-1)=>8This method is like find except that it returns the index of the first element that passes the callback check.
letind:int?=["foo","bar","spam","eggs"].findIndex{
$0.length ==4}
ind! ==2=>trueThis method is like findIndex except that it iterates over elements of the array from right to left.
letind:int?=["foo","bar","spam","eggs"].findLastIndex{
$0.length ==4}
ind! ==3=>trueGets the first element in the array.
letfirst=["foo","bar"].first()=>"foo"Flattens a nested array of any depth.
letunFlattened=["foo",["bar"],[["spam"]],[[["eggs"]]]]letflattened= unFlattened.flatten()=>["foo","bar","spam","eggs"]Get element at index
letelement=["foo","bar"].get(0)
element!
=>"foo"letnothing=["foo","bar"].get(1000)=>nilGets all but the last element or last n elements of an array.
letinitial=["foo","bar","spam"].initial(2)=>["foo"]Gets the last element from the array.
letlast=["foo","bar"].last()=>"bar"The opposite of initial this method gets all but the first element or first n elements of an array.
letrest=["foo","bar","spam"].rest(2)=>["spam"]Retrieves the minimum value in an array.
letmin=[0,1,2].min()=>0Retrieves the maximum value in an array.
letmax=[0,1,2].max()=>2Remove element from array
vararr=["A","B","C","D","E"]
arr.remove("B")=>["A","C","D","E"]Checks if a given value is present in the array.
vararr=["A","B","C","D","E"]
arr.contains("C")=>true
arr.contains("Z")=>falseCreates an object composed from arrays of keys and values.
letkeys=["A","B","C"]letvals=[1,2,3]
keys.zipObject(vals)=>["A":1,"B":2,"C":3]Checks if the array has one or more elements.
letarr=[1,2,3]
arr.isNotEmpty
=>trueGet string description of Character
letch:Character="A"letstr= ch.description
=>"A"Get int representation of character
Character("A").ord
=>65Returns a new Date given the year month and day
letdate=Date.from(2014,1,1)=>"Jan 1, 2014, 12:00 AM"Returns a new Date given the unix timestamp (timeIntervalSince1970)
letdate=Date.from(unix:1_388_552_400.0)=>"Jan 1, 2014, 12:00 AM"Parses the date based on the format and return a new Date
letparsedDate=Date.parse("2014-01-01", format:"yyyy-MM-dd")=>"Jan 1, 2014, 12:00 AM"Returns the unix timestamp of the date passed in or the current unix timestamp
letcurrentUnix=Date.unix()=>1,388,552,400.0varotherNSDate=Date()letotherUnix=Date.unix(otherDate)=>1,388,552,400.0Use the following syntax to calculate dates and times based on the user's current calendar.
1.day.ago
=>"Apr 10, 2015, 11:51 AM"4.hours.fromNow
=>"Apr 11, 2015, 3:51 PM"Merges the dictionary with dictionaries passed. The latter dictionaries will override values of the keys that are already set
vardic=["foo":"bar"]letanotherDic=["foo":"baz","spam":"eggs"]
dic.merge(anotherDic)=>["foo":"baz","spam":"eggs"]Invoke a callback n times with callback that takes index
5.times{(a:Int)->()inprint("\(a)")}=>01234Invoke a callback n times
5.times{print("Na")}=>"NaNaNaNaNa"Get ASCII character from integer
65.char
=>"A"Check if int is even
2.isEven
=>true1.isEven
=>falseCheck if int is odd
3.isOdd
=>true2.isOdd
=>falseSplits the int into array of digits
4208.digits()=>[4,2,0,8]LCM method return least common multiple with number passed
3.lcm(10)=>303.lcm(9)=>9GCD method return greatest common denominator with number passed
3.gcd(10)=>13.gcd(9)=>3Returns random number from 0 upto but not including value of integer
3.random()=>23.random()=>1Returns factorial of integer
3.factorial()=>60.factorial()=>1Returns true if i is in interval or range
5.isIn(1...10)=>true10.isIn(1..<10)=>falseGet the next int
10.next()=>11Get the previous int
10.prev()=>9Invoke the callback from int up to and including limit
3.upTo(5){print("Hi")}
Prints "HiHiHi"Invoke the callback from int down to and including limit
3.downTo(0){print("Hi")}
Prints "HiHiHiHi"Get the length of the string
"Hello".length
=>5Get the camel case representation of the string
"__Dollar and cent-- dollarANDCent".camelCase
=>"dollarAndCentDollarAndCent"Get the kebab case representation of the string
"__Dollar and cent-- dollarANDCent".kebabCase
=>"dollar-and-cent-dollar-and-cent"Get the snake case representation of the string
"__Dollar and cent-- dollarANDCent".snakeCase
=>"dollar_and_cent_dollar_and_cent"Get the start case representation of the string
"__Dollar and cent-- dollarANDCent".startCase
=>"Dollar And Cent Dollar And Cent"Does a regex match of whether regex string on the right is matches the string on the left
"Dollar"=~"oll"=>trueGet string concatenated n times
"Hi Swift! "*3=>"Hi Swift! Hi Swift! Hi Swift! "Get character at a subscript
"Hello World"[6]=="W"=>true"Hi"[5]=>nilReturns the substring based on the first regex match passed in the subscript
letproj="Dollar and Cent"proj["^.+[^and Cent]"]=>{Some:"Dollar"}Get substring using subscript notation and by passing a range
"Hello World"[0..<5]=="Hello"=>trueGet the start index of character
"hello world".indexOf(Character("o"))!
=>4Get the start index of string
"hello world".indexOf("llo")!
=>2"hello world".indexOf("illo")=>nilGet the start index of regex pattern inside the string
"hello world".indexOf(".llo")!
=>1Get an array from string split using the delimiter character
"Hello World".split("")=>["Hello","World"]Get string without leading spaces
letleadingSpace=" Hello"
leadingSpace.lstrip()=>"Hello"Get string without trailing spaces
lettrailingSpace="Hello "
trailingSpace.rstrip()=>"Hello"Get string without leading or trailing spaces
letspaces=" Hello "
spaces.strip()=>"Hello"Init with regex pattern as string
Regex.init("^Hello.World$") // Regex that matches "Hello World"Return matches based on String passed.
letre=Regex.init("^Hello.World$")
re.matches("Hello World")letre=Regex.init("^Hello.World$")
re.test("Hello World")=>true
re.test("Str")=>falseEscape string with regex characters
Regex.escape("Hello.World")=>"Hello\.World"Check the equality of two ranges
(1...5)==(1...5)=>true(1..<5)==(1...5)=>falseFor each index in the range invoke the callback by passing the item in range
(1...5).eachWithIndex{(a:Int)->()inprint("\(a)")}=>12345For each index in the range invoke the callback
(1...5).each{print("Na")}=>"NaNaNaNaNa"If you are interested in contributing checkout CONTRIBUTING.md