forked from monitoror/monitoror
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(ui): move all helper functions into multiple files
- Loading branch information
Showing
9 changed files
with
139 additions
and
122 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export default function ellipsisUnnecessaryParams(jsonString: string): string { | ||
const cleanedJsonString = jsonString.replace(/"params":[^}]*}/g, '"params": { ... }') | ||
|
||
return cleanedJsonString | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export default function extractFieldValue(jsonString: string, fieldName: string): string | undefined { | ||
try { | ||
return JSON.stringify(JSON.parse(jsonString)[fieldName]) | ||
} catch (e) { | ||
return | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import extractFieldValue from '@/helpers/extractFieldValue' | ||
import jsonSyntaxColor from '@/helpers/jsonSyntaxColor' | ||
import ConfigError from '@/interfaces/configError' | ||
|
||
export default function formatConfigExtract(configError: ConfigError): string { | ||
if (configError.data.configExtract === undefined) { | ||
return '' | ||
} | ||
|
||
const formattedConfigExtract = JSON.stringify(JSON.parse(configError.data.configExtract), null, 2) | ||
let html = formattedConfigExtract | ||
|
||
let configExtractHighlight = configError.data.configExtractHighlight | ||
let patternPrefix = '' | ||
|
||
if (configExtractHighlight === undefined && configError.data.fieldName !== undefined) { | ||
patternPrefix = `"${configError.data.fieldName}":\\s+` | ||
configExtractHighlight = extractFieldValue(configError.data.configExtract, configError.data.fieldName) | ||
} | ||
|
||
if (configExtractHighlight !== undefined) { | ||
const formattedConfigExtractHighlight = JSON.stringify(JSON.parse(configExtractHighlight), null, 2) | ||
const isHighlightMultiline = formattedConfigExtractHighlight.includes('\n') | ||
const multilinePrefix = isHighlightMultiline ? ' *' : '' | ||
const multilineSuffix = isHighlightMultiline ? ',?' : '' | ||
const pattern = [ | ||
multilinePrefix, | ||
patternPrefix, | ||
formattedConfigExtractHighlight.replace(/\s+/g, '\\s+'), | ||
multilineSuffix, | ||
].join('') | ||
const find = new RegExp(pattern) | ||
const matches = formattedConfigExtract.match(find) | ||
|
||
if (matches === null) { | ||
return html | ||
} | ||
|
||
const match = matches[0] | ||
const markClassAttr = isHighlightMultiline ? 'class="multiline-mark"' : '' | ||
|
||
html = formattedConfigExtract.replace(match, `<mark ${markClassAttr}>${match}</mark>`) | ||
} | ||
|
||
if (html.includes('</mark>')) { | ||
html = `<span class="has-mark">${html}</span>` | ||
} | ||
|
||
return jsonSyntaxColor(html) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import extractFieldValue from '@/helpers/extractFieldValue' | ||
import ConfigError from '@/interfaces/configError' | ||
|
||
export default function getTileDocUrl(error: ConfigError): string | undefined { | ||
const tileType = extractFieldValue(error.data.configExtract as string, 'type') | ||
|
||
if (tileType === undefined) { | ||
return | ||
} | ||
|
||
const url = 'https://monitoror.com/documentation/#tile-' + JSON.parse(tileType).toLowerCase() | ||
|
||
return url | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import parsedExtractFieldValue from '@/helpers/parsedExpectedValue' | ||
import {findBestMatch} from 'string-similarity' | ||
|
||
export default function guessExpectedValue( | ||
configExtract: string, | ||
fieldName: string, | ||
expectedValues: string[], | ||
): string | undefined { | ||
const currentValue = parsedExtractFieldValue(configExtract, fieldName) | ||
|
||
if (currentValue === undefined) { | ||
return | ||
} | ||
|
||
const bestMatch = findBestMatch(currentValue, expectedValues).bestMatch | ||
|
||
return bestMatch.target | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export default function jsonSyntaxColor(jsonString: string): string { | ||
const coloredJson = jsonString | ||
.replace(/:\s+"(.*)"/g, ': <span class="code-string">"$1"</span>') | ||
.replace(/:\s+([.\d]+)/g, ': <span class="code-number">$1</span>') | ||
|
||
return coloredJson | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import extractFieldValue from '@/helpers/extractFieldValue' | ||
|
||
export default function parsedExtractFieldValue(jsonString: string, fieldName: string): string | undefined { | ||
const fieldValue = extractFieldValue(jsonString, fieldName) | ||
|
||
if (fieldValue === undefined) { | ||
return | ||
} | ||
|
||
return JSON.parse(fieldValue) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export default function splitList(list: string): string[] { | ||
try { | ||
return list.split(',').map((item) => item.trim()).sort() | ||
} catch (e) { | ||
return [list] | ||
} | ||
} |