Skip to content

Commit

Permalink
feat(functionValue): format functions output the way you want
Browse files Browse the repository at this point in the history
`functionValue` can now be used to configure the formatting of functions
  • Loading branch information
giedrius-timinskis authored and vvo committed Apr 24, 2017
1 parent 1a99024 commit 460e0cc
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 3 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,13 @@ console.log(reactElementToJSXString(<div a="1" b="2">Hello, world!</div>));

If false, functions bodies are replaced with `function noRefCheck() {}`.

**options.functionValue: function, default `(fn) => fn`**

Allows you to override the default formatting of function values.

`functionValue` receives the original function reference as input
and should send any value as output.

**options.tabStop: number, default 2**

Provide a different number of columns for indentation.
Expand Down
15 changes: 15 additions & 0 deletions index-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -748,4 +748,19 @@ describe('reactElementToJSXString(ReactElement)', () => {
</div>
</div>`);
});
it('should return functionValue result when it returns a string', () => {
expect(
reactElementToJSXString(<div fn={() => 'value'}/>, {showFunctions: true, functionValue: () => '...'})
).toEqual('<div fn={...} />');
});
it('sends the original fn to functionValue', () => {
const fn = () => {};
const functionValue = receivedFn => expect(receivedFn).toBe(fn);
reactElementToJSXString(<div fn={fn} />, {functionValue});
});
it('should return noRefCheck when "showFunctions" is false and "functionValue" is not provided', () => {
expect(
reactElementToJSXString(<div fn={() => {}}/>)
).toEqual('<div fn={function noRefCheck() {}} />');
});
});
18 changes: 15 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@ import sortobject from 'sortobject';
import traverse from 'traverse';
import {fill} from 'lodash';

const defaultFunctionValue = fn => fn;

export default function reactElementToJSXString(
ReactElement, {
displayName,
filterProps = [],
showDefaultProps = true,
showFunctions = false,
functionValue = defaultFunctionValue,
tabStop = 2,
useBooleanShorthandSyntax = true,
maxInlineAttributesLineLength,
Expand Down Expand Up @@ -190,12 +193,21 @@ got \`${typeof Element}\``
return typeof value;
}

function isFunction (value) {
return typeof value === 'function';
}

function formatValue(value, inline, lvl) {
const wrapper = '__reactElementToJSXString__Wrapper__';
if (isFunction(value)) {
return functionValue(
showFunctions === false && functionValue === defaultFunctionValue ?
function noRefCheck() {} : // eslint-disable-line prefer-arrow-callback
value
);
}

if (typeof value === 'function' && !showFunctions) {
return function noRefCheck() {};
} else if (isElement(value)) {
if (isElement(value)) {
// we use this delimiter hack in cases where the react element is a property
// of an object from a root prop
// i.e.
Expand Down

0 comments on commit 460e0cc

Please sign in to comment.