Cent 7.0.0

Cent 7.0.0

测试已测试
语言语言 SwiftSwift
许可证 MIT
发布最后发布2018年2月
SPM支持SPM

Ankur Patel维护。



Cent 7.0.0

  • 作者:
  • Ankur Patel

Cent 是一个库,它使用扩展功能扩展某些 Swift 对象类型,并对 Swift 语言给出自己的见解。

Dollar 是一个 Swift 库,它提供了有用的函数式编程辅助方法,而不扩展任何内置对象。它类似于 Javascript 中的 Lo-DashUnderscore.js

内容

设置

使用 Swift 包管理器

将以下依赖项 .Package(url: "https://github.com/ankurp/Cent", majorVersion: 6, minor: 0) 添加到您的 Package.swift 文件中,然后运行 swift build。需要版本 2.2 或更高版本的 Swift,您可以从 https://swiftlang.cn 安装。

使用 git submodule

  1. 如果您使用 git,则使用 git submodule add https://github.com/ankurp/Cent.git 将 Cent 添加为子模块。如果不使用 git,请使用 git clone https://github.com/ankurp/Cent.git 在您的项目文件夹中下载项目。
  2. 打开 Cent 文件夹。将 Cent.xcodeproj 文件(位于 Cent 文件夹中)拖入 Xcode 项目文件导航器。
  3. 在 Xcode 中,通过单击蓝色项目图标并从侧边栏的“目标”标题下选择应用程序目标来导航到目标配置窗口。
  4. 在该窗口的顶部标签栏中,打开“构建阶段”面板。
  5. 展开“链接二进制与库”组,并添加 Cent.framework。
  6. 在您的项目文件中 import Cent,然后您可以调用所有辅助函数。

Xcode 和 Swift 支持

  • 对于 Xcode 8(Swift 3),使用版本 6.0.4
  • 对于 Xcode 7(Swift 2),请使用版本 4.1.05.2.0
  • 对于 Xcode 6.3(Swift 1.2),请使用版本 3.0.3
  • 对于 Xcode 6.1 和 6.2(Swift 1.1),请使用版本 2.2.0

通讯

  • 如果您需要帮助,请访问 gitter.im 或在带有 dollar.swift 标签的 Stack Overflow 上提问。
  • 如果您想提问,请使用 Stack Overflow
  • 如果您发现了错误,请开启一个问题。
  • 如果您有功能请求,请开启一个问题。
  • 如果您想做出贡献,请提交一个合并请求数据。

Cent 用法

数组扩展

<< elem: Element -> [Element]

重载运算符,将元素追加到数组中,或从另一个数组中追加元素到第一个数组。返回带有元素追加在末尾的数组。

var array = [1, 2, 3]
array << 4
=> [1, 2, 3, 4]
array << [5, 6]
=> [1, 2, 3, 4, 5, 6]

at(indexes: Int...) -> [Element]

基于指定索引或键创建从集合中的元素组成的数组。

let array = ["foo", "spam", "bar", "eggs"]
let some = array.at(1, 3)
=> ["spam", "eggs"]

each(callback: (Element) -> ()) -> [Element]

对数组的每个项目调用回调,并传递元素。

let array = ["foo", "spam", "bar", "eggs"]
array.each {
  print($0)
}
=> ["foo", "spam", "bar", "eggs"]

eachWithIndex(callback: (Int, Element) -> ()) -> [Element]

对数组中的每个项目调用回调,同时传递元素及其索引。

let array = ["foo", "spam", "bar", "eggs"]
array.each { (index, elem)
  print("\(index) - \(elem)")
}
=> ["foo", "spam", "bar", "eggs"]

cycle<U>(times: Int, callback: (Element) -> U)

依次通过数组将每个元素传递给回调函数。第二个参数用于指定遍历数组的次数。如果省略,它将无限次遍历。

[1, 2, 3].cycle(2) {
  print($0)
}
// Prints the following
123123

[1, 2, 3].cycle {
  print($0)
}
// Cycles in an infinite loop

every(callback: (Element) -> Bool) -> Bool

检查给定的回调是否对数组中的所有项目返回 true。

["angry", "hungry"].every { (a: String) -> (Bool) in 
  a.hasSuffix("gry") 
}
=> true

indexOf<T: Equatable>(value: T) -> Int

获取第一个发生 value 的索引值。

let array = ["foo", "spam", "bar", "eggs"]
array.indexOf("spam")
=> 1
array.indexOf("None")
=> nil

fetch(index: Int, orElse: T? = .None) -> T!

从给定索引尝试获取数组中的元素,该索引可以是负数,以便从数组末尾查找元素。如果没有越界,则可以返回一个默认值。

let arr = [1, 2, 3, 4, 5, 6, 7, 8]
arr.fetch(100)
=> nil

arr.fetch(100, orElse: 42)
=> 42

arr.fetch(-1)
=> 8

findIndex(callback: (Element) -> Bool) -> Int?

此方法类似于 find,但它返回第一个通过回调检查的元素的索引。

let ind: int? = ["foo", "bar", "spam", "eggs"].findIndex {
  $0.length == 4
}
ind! == 2 
=> true

findLastIndex(callback: (Element) -> Bool) -> Int?

此方法类似于 findIndex,但它从右到左遍历数组的元素。

let ind: int? = ["foo", "bar", "spam", "eggs"].findLastIndex {
  $0.length == 4 
}
ind! == 3 
=> true

first() -> Element?

获取数组中的第一个元素。

let first = ["foo", "bar"].first()
=> "foo"

flatten() -> [Element]

递归扁平化任意深度的嵌套数组。

let unFlattened = ["foo", ["bar"], [["spam"]], [[["eggs"]]] ]
let flattened = unFlattened.flatten() 
=> ["foo", "bar", "spam", "eggs"]

get(index: Int) -> Element?

获取索引处的元素

let element = ["foo", "bar"].get(0)
element!
=> "foo"

let nothing = ["foo", "bar"].get(1000)
=> nil

initial(numElements: Int? = 1) -> [Element]

获取数组除了最后一个元素或最后n个元素以外的所有元素。

let initial = ["foo", "bar", "spam"].initial(2) 
=> ["foo"]

last() -> Element?

从数组中获取最后一个元素。

let last = ["foo", "bar"].last() 
=> "bar"

rest(numElements: Int? = 1) -> [Element]

与initial相反的方法,获取数组中的除第一个元素或前n个元素以外的所有元素。

let rest = ["foo", "bar", "spam"].rest(2)
=> ["spam"]

min<T: Comparable>() -> T?

从数组中检索最小值。

let min = [ 0, 1, 2 ].min()
=> 0

max<T: Comparable>() -> T?

从数组中检索最大值。

let max = [ 0, 1, 2].max()
=> 2

remove<T: Equatable>(value: T) -> T?

从数组中移除元素

var arr = ["A", "B", "C", "D", "E"]
arr.remove("B")
=> ["A", "C", "D", "E"]

contains<T:Equatable>(value: T) -> Bool

检查给定值是否存在于数组中。

var arr = ["A", "B", "C", "D", "E"]
arr.contains("C")
=> true
arr.contains("Z")
=> false

zipObject<T>(values: [T]) -> [Element:T]

从键和值的数组创建一个对象。

let keys = ["A", "B", "C"]
let vals = [1,2,3]
keys.zipObject(vals)
=> ["A":1,"B":2,"C":3]

isNotEmpty

检查数组是否有一个或多个元素。

let arr = [1,2,3]
arr.isNotEmpty
=> true

字符扩展

"A".description -> String

获取Character的字符串描述

let ch: Character = "A"
let str = ch.description
=> "A"

"A".ord -> Int

获取字符的int表示

Character("A").ord
=> 65

日期扩展

Date.from(#year: Int, month: Int, day: Int) -> NSDate

给定年、月和日返回一个新的Date对象

let date = Date.from(2014, 1, 1) 
=> "Jan 1, 2014, 12:00 AM"

Date.from(#unix: Double) -> NSDate

给定Unix时间戳返回一个新的Date对象(timeIntervalSince1970)

let date = Date.from(unix: 1_388_552_400.0)
=> "Jan 1, 2014, 12:00 AM"

Date.parse(dateStr: String, format: String = "yyyy-MM-dd") -> NSDate

根据格式解析日期并返回一个新的 Date

let parsedDate = Date.parse("2014-01-01", format: "yyyy-MM-dd")
=> "Jan 1, 2014, 12:00 AM"

Date.unix(date: NSDate = NSDate()) -> Double

返回传入日期的 Unix 时间戳或当前的 Unix 时间戳

let currentUnix = Date.unix()
=> 1,388,552,400.0

var otherNSDate = Date()
let otherUnix = Date.unix(otherDate)
=> 1,388,552,400.0

Int.hour.fromNow 等。

使用以下语法根据用户的当前日历计算日期和时间。

1.day.ago
=> "Apr 10, 2015, 11:51 AM"
4.hours.fromNow
=> "Apr 11, 2015, 3:51 PM"

字典扩展

merge<K, V>(dictionaries: Dictionary<K, V>...)

将字典与传入的字典合并。后面的字典将覆盖已设置的键的值。

var dic = ["foo": "bar"] 
let anotherDic = ["foo": "baz", "spam": "eggs"]
dic.merge(anotherDic)
=> ["foo": "baz", "spam": "eggs"]

整型扩展

times(callback: (Int) -> ())

调用 callback n 次,其中 callback 接收索引。

5.times { print("Na") } 
=> "NaNaNaNaNa"

times (function: () -> ())

调用 callback n 次。

5.times { (a: Int) -> () in print("\(a) ") } 
=> 0 1 2 3 4  

char -> Character

从整数获取 ASCII 字符。

65.char
=> "A"

isEven

检查 int 是否为偶数。

2.isEven
=> true

1.isEven
=> false

isOdd

检查 int 是否为奇数。

3.isOdd
=> true

2.isOdd
=> false

digits() -> [Int]

将 int 分割成数字数组。

4208.digits()
=> [4, 2, 0, 8]

lcm() -> Int

LCM 方法返回与传入数字的最小公倍数。

3.lcm(10)
=> 30

3.lcm(9)
=> 9

gcd() -> Int

GCD 方法返回与传入数字的最大公约数。

3.gcd(10)
=> 1

3.gcd(9)
=> 3

random() -> Int

返回 0 到整数值(不含)之间的随机数。

3.random()
=> 2

3.random()
=> 1

factorial() -> Int

返回整数的阶乘。

3.factorial()
=> 6

0.factorial()
=> 1

isIn(interval) -> Bool

如果 i 在区间或范围内,则返回 true。

5.isIn(1...10)
=> true

10.isIn(1..<10)
=> false

next() -> Int

获取下一个整数值。

10.next()
=> 11

prev() -> Int

获取前一个整数值。

10.prev()
=> 9

upTo(limit: Int, callback: () -> ())

从 int 到 limit(包括)调用 callback。

3.upTo(5) {
  print("Hi")
}
Prints "HiHiHi"

downTo(limit: Int, callback: () -> ())

从整数 int 依次回调到 limit 并包括 limit

3.downTo(0) {
  print("Hi")
}
Prints "HiHiHiHi"

字符串扩展

.length

获取字符串的长度

"Hello".length
=> 5

.camelCase

获取字符串的驼峰表示形式

"__Dollar and cent-- dollarANDCent".camelCase
=> "dollarAndCentDollarAndCent"

.kebabCase

获取字符串的短横线表示形式

"__Dollar and cent-- dollarANDCent".kebabCase
=> "dollar-and-cent-dollar-and-cent"

.snakeCase

获取字符串的蛇形表示形式

"__Dollar and cent-- dollarANDCent".snakeCase
=> "dollar_and_cent_dollar_and_cent"

.startCase

获取字符串的初始表示形式

"__Dollar and cent-- dollarANDCent".startCase
=> "Dollar And Cent Dollar And Cent"

=~ str: String -> Bool

是否与右侧的正则表达式字符串匹配左侧的字符串

"Dollar" =~ "oll"
=> true

* n: Int -> String

获取连字符 n 次的字符串

"Hi Swift! " * 3
=> "Hi Swift! Hi Swift! Hi Swift! "

[i: Int] -> Character?

通过索引获取字符

"Hello World"[6] == "W"
=> true

"Hi"[5]
=> nil

[str: String] -> String?

根据传入子字符串的第一个正则表达式匹配返回子字符串

let proj = "Dollar and Cent"
proj["^.+[^and Cent]"]
=> {Some: "Dollar"}

[r: Range<Int>] -> String

使用索引表示法并通过传递一个范围来获取子字符串

"Hello World"[0..<5] == "Hello" 
=> true

indexOf(char: Character) -> Int?

获取字符的起始索引

"hello world".indexOf(Character("o"))!
=> 4

indexOf(str: String) -> Int?

获取字符串的起始索引

"hello world".indexOf("llo")!
=> 2

"hello world".indexOf("illo")
=> nil

indexOf(pattern: String) -> Int?

获取字符串中正则表达式的起始索引

"hello world".indexOf(".llo")!
=> 1

split(delimiter: Character) -> [String]

使用分隔符字符从字符串获取数组

"Hello World".split(" ") 
=> ["Hello", "World"]

lstring() -> String

获取没有前导空格的字符串

let leadingSpace = "  Hello"
leadingSpace.lstrip()
=> "Hello"

rstring() -> String

获取没有尾随空格的字符串

let trailingSpace = "Hello   "
trailingSpace.rstrip()
=> "Hello"

strip() -> String

获取没有前导或尾随空格的字符串

let spaces = "   Hello   "
spaces.strip()
=> "Hello"

正则表达式

init

使用字符串作为正则表达式模式的初始化

Regex.init("^Hello.World$") // Regex that matches "Hello World"

matches(testStr: String) -> [AnyObject]

根据传入的字符串返回匹配项。

let re = Regex.init("^Hello.World$")
re.matches("Hello World")

test(testStr: String) -> Bool

let re = Regex.init("^Hello.World$")
re.test("Hello World")
=> true

re.test("Str")
=> false

escapeStr(str: String) -> String

使用正则表达式字符转义字符串

Regex.escape("Hello.World")
=> "Hello\.World"

范围扩展

equals - ==

检查两个范围的等价性

(1...5) == (1...5) 
=> true

(1..<5) == (1...5) 
=> false

eachWithIndex(callback: (T) -> ())

对于范围内的每个索引,通过传递范围中的项来调用回调

(1...5).eachWithIndex { (a: Int) -> () in print("\(a)") } 
=> 12345

each(callback: () -> ())

对于范围中的每个索引调用回调

(1...5).each { print("Na") } 
=> "NaNaNaNaNa"

贡献

如果您有兴趣贡献,请查看 CONTRIBUTING.md