File tree Expand file tree Collapse file tree 3 files changed +9
-6
lines changed
Expand file tree Collapse file tree 3 files changed +9
-6
lines changed Original file line number Diff line number Diff line change 77 * onmouseenter="handleMouseEnter({'#root', title: 'title content', event: event})">title content </span>
88 */
99
10- import { getStrWidthPx } from './string ' ;
10+ import { getStrWidthPx } from './dom ' ;
1111
1212interface ITooltipParams {
1313 rootElId : string ;
Original file line number Diff line number Diff line change @@ -54,14 +54,14 @@ export const isDate = (any: unknown): any is Date => typeIs(any) === 'Date';
5454export const isError = ( any : unknown ) : any is Error => typeIs ( any ) === 'Error' ;
5555export const isRegExp = ( any : unknown ) : any is RegExp => typeIs ( any ) === 'RegExp' ;
5656/**
57- * 判断一个字符串是否为有效的 JSON
57+ * 判断一个字符串是否为有效的 JSON, 若有效则返回有效的JSON对象,否则false
5858 * @param {string } str
59- * @return {boolean }
59+ * @return {Object | boolean }
6060 */
61- export function isJsonString ( str : string ) : boolean {
61+ export function isJsonString ( str : string ) : Object | boolean {
6262 try {
6363 const parsed = JSON . parse ( str ) ;
64- return typeof parsed === 'object' && parsed !== null ;
64+ return typeof parsed === 'object' && parsed !== null ? parsed : false ;
6565 } catch ( e ) {
6666 return false ;
6767 }
Original file line number Diff line number Diff line change @@ -95,6 +95,9 @@ test('isRegExp', () => {
9595test ( 'isJsonString' , ( ) => {
9696 const jsonString = '{"name": "John", "age": 30}' ;
9797 const invalidJsonString = '{"name": "John", "age": 30' ;
98- expect ( isJsonString ( jsonString ) ) . toBe ( true ) ;
98+ expect ( isJsonString ( jsonString ) ) . toEqual ( {
99+ name : 'John' ,
100+ age : 30
101+ } ) ;
99102 expect ( isJsonString ( invalidJsonString ) ) . toBe ( false ) ;
100103} ) ;
You can’t perform that action at this time.
0 commit comments