Skip to content

Commit

Permalink
Uses a dictionary to look up the macros.
Browse files Browse the repository at this point in the history
  • Loading branch information
zonble committed Dec 11, 2023
1 parent 95854f1 commit 7e19809
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions Source/InputMacro.swift
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ fileprivate func getYearBase(year: Int) -> (Int, Int) {
return (base % 10, base % 12)
}

fileprivate func ganshi(year: Int) -> String {
fileprivate func ganzhi(year: Int) -> String {
let gan = ["", "", "", "", "", "", "", "", "", ""]
let zhi = ["", "", "", "", "", "", "", "", "", "", "", ""]
let (ganBase, zhiBase) = getYearBase(year: year)
Expand All @@ -231,7 +231,7 @@ fileprivate struct InputMacroThisYearGanZhi: InputMacro {

var replacement: String {
let year = getCurrentYear()
return ganshi(year: year)
return ganzhi(year: year)
}
}

Expand All @@ -240,7 +240,7 @@ fileprivate struct InputMacroLastYearGanZhi: InputMacro {

var replacement: String {
let year = getCurrentYear()
return ganshi(year: year - 1)
return ganzhi(year: year - 1)
}
}

Expand All @@ -249,7 +249,7 @@ fileprivate struct InputMacroNextYearGanZhi: InputMacro {

var replacement: String {
let year = getCurrentYear()
return ganshi(year: year + 1)
return ganzhi(year: year + 1)
}
}

Expand Down Expand Up @@ -286,8 +286,8 @@ class InputMacroController: NSObject {
@objc
static let shared = InputMacroController()

private var macros: [InputMacro] =
[
private var macros: [String:InputMacro] = {
let macros: [InputMacro] = [
InputMacroDateTodayShort(),
InputMacroDateYesterdayShort(),
InputMacroDateTomorrowShort(),
Expand All @@ -311,13 +311,18 @@ class InputMacroController: NSObject {
InputMacroLastYearChineseZodiac(),
InputMacroNextYearChineseZodiac(),
]
var map:[String:InputMacro] = [:]
macros.forEach { macro in
map[macro.name] = macro
}
return map
} ()


@objc
func handle(_ input: String) -> String {
for inputMacro in macros {
if inputMacro.name == input {
return inputMacro.replacement
}
if let macro = macros[input] {
return macro.replacement
}
return input
}
Expand Down

0 comments on commit 7e19809

Please sign in to comment.