Skip to content

Commit c79349e

Browse files
committed
adds ability to render fields with type "function"
does not render body of the function but just static string indicating the function type
1 parent 0937746 commit c79349e

File tree

5 files changed

+54
-3
lines changed

5 files changed

+54
-3
lines changed

src/DataRenderer.test.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,12 @@ describe('DataRender', () => {
148148
expect(screen.getByText('42n')).toBeInTheDocument();
149149
});
150150

151+
it('should render functions', () => {
152+
render(<WrappedDataRenderer value={{ func: () => {} }} />);
153+
expect(screen.getByText(/func:/)).toBeInTheDocument();
154+
expect(screen.getByText('function() { }')).toBeInTheDocument();
155+
});
156+
151157
it('should render dates', () => {
152158
render(<WrappedDataRenderer value={{ test: new Date(0) }} />);
153159
expect(screen.getByText(/test:/)).toBeInTheDocument();

src/DataRenderer.tsx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ function JsonPrimitiveValue({
286286
value,
287287
style,
288288
lastElement
289-
}: JsonRenderProps<string | number | boolean | Date | null | undefined>) {
289+
}: JsonRenderProps<string | number | boolean | Date | Function | null | undefined>) {
290290
let stringValue;
291291
let valueStyle = style.otherValue;
292292

@@ -310,6 +310,8 @@ function JsonPrimitiveValue({
310310
valueStyle = style.numberValue;
311311
} else if (DataTypeDetection.isDate(value)) {
312312
stringValue = value.toISOString();
313+
} else if (DataTypeDetection.isFunction(value)) {
314+
stringValue = 'function() { }';
313315
} else {
314316
stringValue = (value as any).toString();
315317
}
@@ -331,7 +333,11 @@ export default function DataRender(props: JsonRenderProps<any>) {
331333
return <JsonArray {...props} />;
332334
}
333335

334-
if (DataTypeDetection.isObject(value) && !DataTypeDetection.isDate(value)) {
336+
if (
337+
DataTypeDetection.isObject(value) &&
338+
!DataTypeDetection.isDate(value) &&
339+
!DataTypeDetection.isFunction(value)
340+
) {
335341
return <JsonObject {...props} />;
336342
}
337343

src/DataTypeDetection.test.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ const errorValue = new Error();
1414
const regExValue = /test/;
1515
const symbolValue = Symbol('s');
1616
const bigintValue = BigInt(42);
17+
const func = function (param: number): number {
18+
return param * 2;
19+
};
1720

1821
describe('isBoolean', () => {
1922
it('should return `true` for boolean values', () => {
@@ -35,6 +38,7 @@ describe('isBoolean', () => {
3538
expect(DataTypeDetection.isBoolean(regExValue)).toBe(false);
3639
expect(DataTypeDetection.isBoolean(symbolValue)).toBe(false);
3740
expect(DataTypeDetection.isBoolean(bigintValue)).toBe(false);
41+
expect(DataTypeDetection.isBoolean(func)).toBe(false);
3842
});
3943
});
4044

@@ -59,6 +63,7 @@ describe('isNumber', () => {
5963
expect(DataTypeDetection.isNumber(regExValue)).toBe(false);
6064
expect(DataTypeDetection.isNumber(symbolValue)).toBe(false);
6165
expect(DataTypeDetection.isNumber(bigintValue)).toBe(false);
66+
expect(DataTypeDetection.isNumber(func)).toBe(false);
6267
});
6368
});
6469

@@ -82,6 +87,7 @@ describe('isString', () => {
8287
expect(DataTypeDetection.isString(regExValue)).toBe(false);
8388
expect(DataTypeDetection.isString(symbolValue)).toBe(false);
8489
expect(DataTypeDetection.isString(bigintValue)).toBe(false);
90+
expect(DataTypeDetection.isString(func)).toBe(false);
8591
});
8692
});
8793

@@ -103,6 +109,7 @@ describe('isNull', () => {
103109
expect(DataTypeDetection.isNull(regExValue)).toBe(false);
104110
expect(DataTypeDetection.isNull(symbolValue)).toBe(false);
105111
expect(DataTypeDetection.isNull(bigintValue)).toBe(false);
112+
expect(DataTypeDetection.isNull(func)).toBe(false);
106113
});
107114
});
108115

@@ -124,6 +131,7 @@ describe('isUndefined', () => {
124131
expect(DataTypeDetection.isUndefined(regExValue)).toBe(false);
125132
expect(DataTypeDetection.isUndefined(symbolValue)).toBe(false);
126133
expect(DataTypeDetection.isUndefined(bigintValue)).toBe(false);
134+
expect(DataTypeDetection.isUndefined(func)).toBe(false);
127135
});
128136
});
129137

@@ -145,6 +153,7 @@ describe('isArray', () => {
145153
expect(DataTypeDetection.isArray(regExValue)).toBe(false);
146154
expect(DataTypeDetection.isArray(symbolValue)).toBe(false);
147155
expect(DataTypeDetection.isArray(bigintValue)).toBe(false);
156+
expect(DataTypeDetection.isArray(func)).toBe(false);
148157
});
149158
});
150159

@@ -187,6 +196,7 @@ describe('isBigInt', () => {
187196
expect(DataTypeDetection.isBigInt(nullValue)).toBe(false);
188197
expect(DataTypeDetection.isBigInt(undefinedValue)).toBe(false);
189198
expect(DataTypeDetection.isBigInt(symbolValue)).toBe(false);
199+
expect(DataTypeDetection.isBigInt(func)).toBe(false);
190200
});
191201
});
192202

@@ -209,5 +219,29 @@ describe('isDate', () => {
209219
expect(DataTypeDetection.isDate(regExValue)).toBe(false);
210220
expect(DataTypeDetection.isDate(symbolValue)).toBe(false);
211221
expect(DataTypeDetection.isDate(bigintValue)).toBe(false);
222+
expect(DataTypeDetection.isDate(func)).toBe(false);
223+
});
224+
});
225+
226+
describe('isFunc', () => {
227+
it('should return `true` for function values', () => {
228+
expect(DataTypeDetection.isFunction(func)).toBe(true);
229+
});
230+
231+
it('should return `false` for non-function values', () => {
232+
expect(DataTypeDetection.isFunction(stringValue)).toBe(false);
233+
expect(DataTypeDetection.isFunction(numberValue)).toBe(false);
234+
expect(DataTypeDetection.isFunction(floatNumberValue)).toBe(false);
235+
expect(DataTypeDetection.isFunction(trueValue)).toBe(false);
236+
expect(DataTypeDetection.isFunction(falseValue)).toBe(false);
237+
expect(DataTypeDetection.isFunction(objectValue)).toBe(false);
238+
expect(DataTypeDetection.isFunction(arrayValue)).toBe(false);
239+
expect(DataTypeDetection.isFunction(nullValue)).toBe(false);
240+
expect(DataTypeDetection.isFunction(undefinedValue)).toBe(false);
241+
expect(DataTypeDetection.isFunction(errorValue)).toBe(false);
242+
expect(DataTypeDetection.isFunction(regExValue)).toBe(false);
243+
expect(DataTypeDetection.isFunction(symbolValue)).toBe(false);
244+
expect(DataTypeDetection.isFunction(bigintValue)).toBe(false);
245+
expect(DataTypeDetection.isFunction(dateValue)).toBe(false);
212246
});
213247
});

src/DataTypeDetection.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,7 @@ export const isNull = (data: any): data is null => {
3333
export const isUndefined = (data: any): data is undefined => {
3434
return data === undefined;
3535
};
36+
37+
export const isFunction = (data: unknown): data is Function => {
38+
return !!data && data instanceof Object && typeof data === 'function';
39+
};

src/stories/JsonView.stories.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ const jsonData = {
6767
longText:
6868
' Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam pharetra at dolor eu egestas. Mauris bibendum a sem vel euismod. Proin vitae imperdiet diam. In sed gravida nisi, in convallis felis. Fusce convallis dapibus molestie. In tristique dapibus velit et rutrum. Nam vestibulum sodales tortor. Integer gravida aliquet sollicitudin. Duis at nulla varius, congue risus sit amet, gravida ipsum. Cras placerat pellentesque ipsum, a consequat magna pretium et. Duis placerat dui nisi, eget varius dui egestas eget. Etiam leo mauris, mattis et aliquam hendrerit, dapibus eu massa. Phasellus vitae vestibulum elit. Nulla congue eleifend massa at efficitur. '
6969
}
70-
}
70+
},
71+
func: () => {}
7172
};
7273

7374
export const Basic = Template.bind({});

0 commit comments

Comments
 (0)