Skip to content

Commit 622655e

Browse files
author
andypalmi
committed
Address review feedback on looksLikeJson and tryPrettify
- Allow length >= 2 so {} and [] are valid - Trim whitespace and check matching closing bracket - Reuse looksLikeJson in tryPrettify instead of duplicating logic
1 parent 2abeedc commit 622655e

1 file changed

Lines changed: 6 additions & 4 deletions

File tree

frontend/src/components/dialogs/SnapshotDiffChangePanel.vue

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -176,14 +176,16 @@ export default {
176176
}
177177
},
178178
looksLikeJson (v) {
179-
if (typeof v === 'string' && v.length > 2) {
180-
const c = v[0]
181-
return c === '{' || c === '['
179+
if (typeof v === 'string' && v.length >= 2) {
180+
const trimmed = v.trim()
181+
const first = trimmed[0]
182+
const last = trimmed[trimmed.length - 1]
183+
return (first === '{' && last === '}') || (first === '[' && last === ']')
182184
}
183185
return false
184186
},
185187
tryPrettify (v) {
186-
if (typeof v === 'string' && v.length > 2 && (v[0] === '{' || v[0] === '[')) {
188+
if (this.looksLikeJson(v)) {
187189
try { return JSON.stringify(JSON.parse(v), null, 2) } catch (_) { /* not valid JSON */ }
188190
}
189191
return null

0 commit comments

Comments
 (0)