-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCamelCaseConfig.swift
More file actions
143 lines (122 loc) · 3.86 KB
/
CamelCaseConfig.swift
File metadata and controls
143 lines (122 loc) · 3.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
extension String.Casification.Configuration {
public struct CamelCase: Equatable, Sendable {
public static var current: Self { .init() }
public static let `default`: Self = .init(
numbers: .default,
acronyms: .default
)
public var numbers: Numbers
public var acronyms: Acronyms
public init(
numbers: Numbers = _Configuration.current.camelCase.numbers,
acronyms: Acronyms = _Configuration.current.camelCase.acronyms
) {
self.numbers = numbers
self.acronyms = acronyms
}
public enum Mode: Equatable, Sendable {
public static let `default`: Self = .automatic
case automatic
case camel
case pascal
}
public struct Numbers: Equatable, Sendable {
public static var current: Self { .init() }
public static let `default`: Self = .init(
nextTokenMode: .default,
separator: "_"
)
public var nextTokenMode: NextTokenMode
public var separator: Substring
public init(
nextTokenMode: NextTokenMode = .current,
separator: Substring = CamelCase.current.numbers.separator
) {
self.nextTokenMode = nextTokenMode
self.separator = separator
}
public enum NextTokenMode: Equatable, Sendable {
public static var current: Self { CamelCase.current.numbers.nextTokenMode }
public static let `default`: Self = .inherit
case inherit
case override(Mode = .automatic)
public var overridenValue: Mode? {
switch self {
case let .override(mode): mode
default: nil
}
}
}
}
public struct Acronyms: Equatable, Sendable {
public static var current: Self { .init() }
public static let `default`: Self = .init(processingPolicy: .default)
public var processingPolicy: ProcessingPolicy
public init(
processingPolicy: ProcessingPolicy = .current
) {
self.processingPolicy = processingPolicy
}
public enum ProcessingPolicy: Equatable, Sendable {
public static var current: Self { CamelCase.current.acronyms.processingPolicy }
public static let `default`: Self = .alwaysMatchCase
/// Keep acronyms as parsed
///
/// Examples:
/// - `"ID"` → `"ID"`
/// - `"Id"` → `"Id"`
/// - `"id"` → `"id"`
///
/// - Warning: Overrides camel case mode when the first token is acronym
///
/// Examples:
/// - `"someString"` → `"someString"`
/// - `"uuidString"` → `"uuidString"`
/// - `"UuidString"` → `"UuidString"`
/// - `"UUIDString"` → `"UUIDString"`
case preserve
/// Always uppercase or lowercase acronyms
///
/// **Standard processing policy**
///
/// Examples:
/// - `"ID"` → `"ID"`, or `"id"` if first token in camel case
/// - `"Id"` → `"ID"`, or `"id"` if first token in camel case
/// - `"id"` → `"ID"`, or `"id"` if first token in camel case
case alwaysMatchCase
/// Always capitalize acronyms
///
/// Examples:
/// - `"ID"` → `"Id"`
/// - `"Id"` → `"Id"`
/// - `"id"` → `"Id"`
///
/// - Warning: Overrides `Mode.camel` when the first token is acronym
///
/// Examples:
/// - `"someString"` → `"someString"`
/// - `"uuidString"` → `"UuidString"`
case alwaysCapitalize
/// Always capitalize acronyms
///
/// First token behaves like `.alwaysMatchCase`, rest tokens are processed like `.alwaysCapitalize`
///
/// Examples:
/// - `"ID"` → `"Id"`, or `"id"` if first token in camel case
/// - `"Id"` → `"Id"`, or `"id"` if first token in camel case
/// - `"id"` → `"Id"`, or `"id"` if first token in camel case
case conditionalCapitalization
}
}
}
}
// MARK: - ConfigurationKey
extension String.Casification.Configuration {
private enum CamelCaseKey: String.Casification.ConfigurationKey {
static var `default`: CamelCase { .default }
}
public var camelCase: CamelCase {
get { self[CamelCaseKey.self] }
set { self[CamelCaseKey.self] = newValue }
}
}