MCPcopy Create free account
hub / github.com/DavidHDev/react-bits / formatPropValue

Function formatPropValue

src/utils/codeGeneration.js:13–49  ·  view source on GitHub ↗
(value, propName)

Source from the content-addressed store, hash-verified

11 * - Functions: shown as reference
12 */
13export function formatPropValue(value, propName) {
14 if (value === undefined || value === null) {
15 return null;
16 }
17
18 const type = typeof value;
19
20 if (type === 'string') {
21 return `"${value}"`;
22 }
23
24 if (type === 'number') {
25 return `{${value}}`;
26 }
27
28 if (type === 'boolean') {
29 return value ? '' : `{${value}}`; // true props can be just the prop name
30 }
31
32 if (type === 'function') {
33 // Check if it's a named function
34 if (value.name && value.name !== 'anonymous') {
35 return `{${value.name}}`;
36 }
37 return `{handle${propName.charAt(0).toUpperCase() + propName.slice(1)}}`;
38 }
39
40 if (Array.isArray(value)) {
41 return `{${JSON.stringify(value)}}`;
42 }
43
44 if (type === 'object') {
45 return `{${JSON.stringify(value)}}`;
46 }
47
48 return `{${value}}`;
49}
50
51/**
52 * Generates JSX props string from a props object.

Callers 2

injectPropsIntoCodeFunction · 0.90
generatePropsStringFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected