Skip to content

Commit

Permalink
Replace 'operator <==>' with Equatable
Browse files Browse the repository at this point in the history
  • Loading branch information
CrazyFanFan committed Nov 14, 2020
1 parent 29bf9ca commit fd15be9
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 44 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -210,3 +210,5 @@ fastlane/test_output

# pod
**/Pods/*

.build
4 changes: 2 additions & 2 deletions K3Pinyin.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = "K3Pinyin"
s.version = "1.0.0"
s.version = "2.0.0"
s.summary = "a simple wap to use pinyin with swift."

s.description = <<-DESC
Expand All @@ -15,5 +15,5 @@ Pod::Spec.new do |s|
s.source = { :git => "https://github.com/CrazyFanFan/K3Pinyin.git", :tag => "#{s.version}" }
s.source_files = "Sources/**/*.{swift}"
s.requires_arc = true
s.swift_version = '5.0'
s.swift_version = '5.3'
end
3 changes: 2 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,6 @@ let package = Package(
.testTarget(
name: "K3PinyinTests",
dependencies: ["K3Pinyin"]),
]
],
swiftLanguageVersions: [.v5]
)
5 changes: 4 additions & 1 deletion Sources/K3Pinyin/K3Pinyin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ public extension K3Pinyin where BaseType == String {
let cases = options ?? []

// get only first char when case onlyFirst...
let source = (cases.onlyFirstCharacter || cases.onlyFirstLetter ? "\(base.prefix(1))" : base)
var source = base
if cases.onlyFirstCharacter || cases.onlyFirstLetter {
source = String(base.prefix(1))
}

// get pinyin
let cfString = CFStringCreateMutableCopy(nil, 0, source as CFString)
Expand Down
57 changes: 17 additions & 40 deletions Sources/K3Pinyin/K3PinyinOptions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import Foundation

public typealias K3PinyinOptions = [K3PinyinOption]

public enum K3PinyinOption {
public enum K3PinyinOption: Equatable {
case stripCombiningMarks
case separator(String)
case onlyFirstCharacter
Expand All @@ -19,53 +19,30 @@ public enum K3PinyinOption {
case capitalized
}

precedencegroup OptionComparisonPrecedence {
associativity: none
higherThan: LogicalConjunctionPrecedence
}

infix operator <==>: OptionComparisonPrecedence

/// This operator returns true if two `K3PinyinOption` enum is the same,
/// without considering the associated values.
func <==> (lhs: K3PinyinOption, rhs: K3PinyinOption) -> Bool {
switch (lhs, rhs) {
case (.stripCombiningMarks, .stripCombiningMarks): return true
case (.separator, .separator): return true
case (.onlyFirstCharacter, .onlyFirstCharacter): return true
case (.allFirstLetter, .allFirstLetter): return true
case (.onlyFirstLetter, .onlyFirstLetter): return true
case (.capitalized, .capitalized): return true
default: return false
}
}

public extension Collection where Iterator.Element == K3PinyinOption {
var stripCombiningMarks: Bool {
return contains { $0 <==> .stripCombiningMarks }
}
@inlinable
var stripCombiningMarks: Bool { contains(.stripCombiningMarks) }

var separator: String? {
if let item = reversed().first(where: { $0 <==> .separator("") }),
case .separator(let separator) = item {
return separator
for option in reversed() {
switch option {
case .separator(let separator):
return separator
default: break
}
}
return nil
}

var onlyFirstCharacter: Bool {
return contains { $0 <==> .onlyFirstCharacter }
}
@inlinable
var onlyFirstCharacter: Bool { contains(.onlyFirstCharacter) }

var allFirstLetter: Bool {
return contains { $0 <==> .allFirstLetter }
}
@inlinable
var allFirstLetter: Bool { contains(.allFirstLetter) }

var onlyFirstLetter: Bool {
return contains { $0 <==> .onlyFirstLetter }
}
@inlinable
var onlyFirstLetter: Bool { contains(.onlyFirstLetter) }

var capitalized: Bool {
return contains { $0 <==> .capitalized }
}
@inlinable
var capitalized: Bool { contains(.capitalized) }
}

0 comments on commit fd15be9

Please sign in to comment.