Skip to content

Commit 55e1ba9

Browse files
polok“Marcin
andauthored
refactor: Handle matrix variable declarations with and without white spaces (#86)
Co-authored-by: “Marcin <“[email protected]”>
1 parent a53e904 commit 55e1ba9

File tree

1 file changed

+38
-1
lines changed

1 file changed

+38
-1
lines changed

Sources/Featurevisor/Mappers/AssertionMapper.swift

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,43 @@ enum AssertionMapper {
7070
extension AssertionMapper {
7171

7272
fileprivate static func replace(source: String, key: String, value: String) -> String {
73-
return source.replacingOccurrences(of: "${{ \(key) }}", with: value)
73+
return removeAllWhitespaceInsidePlaceholders(in: source)
74+
.replacingOccurrences(of: "${{\(key)}}", with: value)
75+
}
76+
77+
fileprivate static func removeAllWhitespaceInsidePlaceholders(in input: String) -> String {
78+
let pattern = "\\$\\{\\{\\s*(.*?)\\s*\\}\\}"
79+
let regex = try! NSRegularExpression(pattern: pattern, options: [])
80+
81+
let nsrange = NSRange(input.startIndex..<input.endIndex, in: input)
82+
var result = input
83+
var offset = 0
84+
85+
regex.enumerateMatches(in: input, options: [], range: nsrange) { match, _, _ in
86+
guard let match = match, match.numberOfRanges == 2,
87+
let innerRange = Range(match.range(at: 1), in: input)
88+
else {
89+
return
90+
}
91+
92+
let originalInner = String(input[innerRange])
93+
let cleaned = originalInner.replacingOccurrences(
94+
of: #"\s+"#,
95+
with: "",
96+
options: .regularExpression
97+
)
98+
99+
// Rebuild the full match range considering offset due to replacements
100+
let fullMatchRange = match.range(at: 0)
101+
if let rangeToReplace = Range(
102+
NSRange(location: fullMatchRange.location + offset, length: fullMatchRange.length),
103+
in: result
104+
) {
105+
result.replaceSubrange(rangeToReplace, with: "${{\(cleaned)}}")
106+
offset += "${{\(cleaned)}}".count - fullMatchRange.length
107+
}
108+
}
109+
110+
return result
74111
}
75112
}

0 commit comments

Comments
 (0)