Skip to content

Commit 2d08a99

Browse files
committed
RI-6231 - addressing feedback
1 parent 238acd9 commit 2d08a99

File tree

3 files changed

+44
-17
lines changed

3 files changed

+44
-17
lines changed

redisinsight/ui/src/pages/browser/modules/key-details/components/rejson-details/rejson-scalar/RejsonScalar.tsx

+2-15
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { bufferToString, createDeleteFieldHeader, createDeleteFieldMessage, Null
1010
import FieldMessage from 'uiSrc/components/field-message/FieldMessage'
1111

1212
import { JSONScalarProps } from '../interfaces'
13-
import { generatePath, getClassNameByValue, isValidJSON } from '../utils'
13+
import { generatePath, getClassNameByValue, isValidJSON, stringifyScalarValue } from '../utils'
1414
import { JSONErrors } from '../constants'
1515

1616
import styles from '../styles.module.scss'
@@ -36,22 +36,9 @@ const RejsonScalar = (props: JSONScalarProps) => {
3636
const dispatch = useDispatch()
3737

3838
useEffect(() => {
39-
setChangedValue(stringify(value))
39+
setChangedValue(stringifyScalarValue(value))
4040
}, [value])
4141

42-
const stringify = (value: string | number | boolean | bigint): string => {
43-
if (typeof value === 'bigint') {
44-
return value.toString()
45-
}
46-
if (isString(value)) {
47-
return `"${value}"`
48-
}
49-
if (isNull(value)) {
50-
return 'null'
51-
}
52-
return String(value)
53-
}
54-
5542
const onDeclineChanges = () => {
5643
setEditing(false)
5744
setError(null)

redisinsight/ui/src/pages/browser/modules/key-details/components/rejson-details/utils/utils.spec.ts

+28-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { generatePath, getBrackets, isRealArray, isRealObject, isScalar, isValidKey, parseJsonData, parseValue, wrapPath } from './utils'
1+
import { generatePath, getBrackets, isRealArray, isRealObject, isScalar, isValidKey, parseJsonData, parseValue, stringifyScalarValue, wrapPath } from './utils'
22
import { ObjectTypes } from '../interfaces'
33

44
describe('JSONUtils', () => {
@@ -177,4 +177,31 @@ describe('JSONUtils', () => {
177177
})
178178
})
179179
})
180+
181+
describe('stringifyScalarValue', () => {
182+
it('should handle bigint values', () => {
183+
const bigIntValue = BigInt('9007199254740991')
184+
expect(stringifyScalarValue(bigIntValue)).toBe('9007199254740991')
185+
})
186+
187+
it('should wrap string values in quotes', () => {
188+
expect(stringifyScalarValue('hello')).toBe('"hello"')
189+
expect(stringifyScalarValue('')).toBe('""')
190+
})
191+
192+
it('should convert null to "null" string', () => {
193+
expect(stringifyScalarValue(null as any)).toBe('null')
194+
})
195+
196+
it('should convert numbers to string representation', () => {
197+
expect(stringifyScalarValue(42)).toBe('42')
198+
expect(stringifyScalarValue(-123.456)).toBe('-123.456')
199+
expect(stringifyScalarValue(0)).toBe('0')
200+
})
201+
202+
it('should convert boolean values to string representation', () => {
203+
expect(stringifyScalarValue(true)).toBe('true')
204+
expect(stringifyScalarValue(false)).toBe('false')
205+
})
206+
})
180207
})

redisinsight/ui/src/pages/browser/modules/key-details/components/rejson-details/utils/utils.ts

+14-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { isArray } from 'lodash'
1+
import { isArray, isNull, isString } from 'lodash'
22
import JSONBigInt from 'json-bigint'
33
import { JSONScalarValue, ObjectTypes } from '../interfaces'
44
import styles from '../styles.module.scss'
@@ -154,3 +154,16 @@ export const parseJsonData = (data: any) => {
154154
return data
155155
}
156156
}
157+
158+
export const stringifyScalarValue = (value: string | number | boolean | bigint): string => {
159+
if (typeof value === 'bigint') {
160+
return value.toString()
161+
}
162+
if (isString(value)) {
163+
return `"${value}"`
164+
}
165+
if (isNull(value)) {
166+
return 'null'
167+
}
168+
return String(value)
169+
}

0 commit comments

Comments
 (0)