Skip to content

Commit b7e9d97

Browse files
committed
feat: support view as hex/binary for the key with type string
1 parent dbb5969 commit b7e9d97

File tree

3 files changed

+89
-52
lines changed

3 files changed

+89
-52
lines changed

frontend/src/components/content_value/ContentValueString.vue

Lines changed: 17 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ import Edit from '@/components/icons/Edit.vue'
1111
import { IsJson } from '@/utils/check_string_format.js'
1212
import { types as redisTypes } from '@/consts/support_redis_type.js'
1313
import { ClipboardSetText } from 'wailsjs/runtime/runtime.js'
14-
import { toLower } from 'lodash'
14+
import { map, toLower } from 'lodash'
1515
import useConnectionStore from 'stores/connections.js'
16+
import { fromBase64, fromBase64Json, toBinary, toHex, toJsonText } from '@/utils/string_convert.js'
1617
1718
const i18n = useI18n()
1819
const themeVars = useThemeVars()
@@ -28,53 +29,16 @@ const props = defineProps({
2829
value: String,
2930
})
3031
31-
const viewOption = [
32-
{
33-
value: types.PLAIN_TEXT,
34-
label: types.PLAIN_TEXT,
35-
},
36-
{
37-
value: types.JSON,
38-
label: types.JSON,
39-
},
40-
{
41-
value: types.BASE64_TO_TEXT,
42-
label: types.BASE64_TO_TEXT,
43-
},
44-
{
45-
value: types.BASE64_TO_JSON,
46-
label: types.BASE64_TO_JSON,
47-
},
48-
]
32+
const viewOption = computed(() =>
33+
map(types, (t) => {
34+
return {
35+
value: t,
36+
label: t,
37+
}
38+
}),
39+
)
4940
const viewAs = ref(types.PLAIN_TEXT)
5041
51-
const getJsonValue = () => {
52-
try {
53-
const jsonObj = JSON.parse(props.value)
54-
return JSON.stringify(jsonObj, null, 2)
55-
} catch (e) {
56-
return props.value
57-
}
58-
}
59-
60-
const getBase64Value = () => {
61-
try {
62-
return atob(props.value)
63-
} catch (e) {
64-
return props.value
65-
}
66-
}
67-
68-
const getBase64JsonValue = () => {
69-
try {
70-
const text = atob(props.value)
71-
const jsonObj = JSON.parse(text)
72-
return JSON.stringify(jsonObj, null, 2)
73-
} catch (e) {
74-
return props.value
75-
}
76-
}
77-
7842
const autoDetectFormat = () => {
7943
// auto check format when loaded
8044
if (IsJson(props.value)) {
@@ -104,21 +68,22 @@ const viewValue = computed(() => {
10468
case types.PLAIN_TEXT:
10569
return props.value
10670
case types.JSON:
107-
return getJsonValue()
71+
return toJsonText(props.value)
10872
case types.BASE64_TO_TEXT:
109-
return getBase64Value()
73+
return fromBase64(props.value)
11074
case types.BASE64_TO_JSON:
111-
return getBase64JsonValue()
75+
return fromBase64Json(props.value)
76+
case types.HEX:
77+
return toHex(props.value)
78+
case types.BINARY:
79+
return toBinary(props.value)
11280
default:
11381
return props.value
11482
}
11583
})
11684
11785
const viewLanguage = computed(() => {
11886
switch (viewAs.value) {
119-
case types.PLAIN_TEXT:
120-
case types.BASE64_TO_TEXT:
121-
return 'plaintext'
12287
case types.JSON:
12388
case types.BASE64_TO_JSON:
12489
return 'json'

frontend/src/consts/value_view_type.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,6 @@ export const types = {
33
JSON: 'JSON',
44
BASE64_TO_TEXT: 'Base64 To Text',
55
BASE64_TO_JSON: 'Base64 To JSON',
6+
HEX: 'Hex',
7+
BINARY: 'Binary',
68
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import { map, padStart } from 'lodash'
2+
3+
/**
4+
* convert string to json
5+
* @param str
6+
* @return {string}
7+
*/
8+
export const toJsonText = (str) => {
9+
try {
10+
const jsonObj = JSON.parse(str)
11+
return JSON.stringify(jsonObj, null, 2)
12+
} catch (e) {
13+
return str
14+
}
15+
}
16+
17+
/**
18+
* convert string from base64
19+
* @param str
20+
* @return {string}
21+
*/
22+
export const fromBase64 = (str) => {
23+
try {
24+
return atob(str)
25+
} catch (e) {
26+
return str
27+
}
28+
}
29+
30+
/**
31+
* convert string from base64 to json
32+
* @param str
33+
* @return {string}
34+
*/
35+
export const fromBase64Json = (str) => {
36+
try {
37+
const text = atob(str)
38+
const jsonObj = JSON.parse(text)
39+
return JSON.stringify(jsonObj, null, 2)
40+
} catch (e) {
41+
return str
42+
}
43+
}
44+
45+
/**
46+
* convert string to hex string
47+
* @param str
48+
* @return {string}
49+
*/
50+
export const toHex = (str) => {
51+
const hexArr = map(str, (char) => {
52+
const charCode = char.charCodeAt(0)
53+
return charCode.toString(16)
54+
})
55+
return hexArr.join(' ')
56+
}
57+
58+
/**
59+
* convert string to binary string
60+
* @param str
61+
* @return {string}
62+
*/
63+
export const toBinary = (str) => {
64+
const codeUnits = map(str, (char) => {
65+
let code = char.charCodeAt(0).toString(2)
66+
code = padStart(code, 8, '0')
67+
return code
68+
})
69+
return codeUnits.join(' ')
70+
}

0 commit comments

Comments
 (0)