From 460e0cc5f09a8d2c093df598c488f06347b5365c Mon Sep 17 00:00:00 2001 From: Giedrius Timinskis Date: Mon, 24 Apr 2017 17:51:48 +1000 Subject: [PATCH] feat(functionValue): format functions output the way you want `functionValue` can now be used to configure the formatting of functions --- README.md | 7 +++++++ index-test.js | 15 +++++++++++++++ index.js | 18 +++++++++++++++--- 3 files changed, 37 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index fca60b0cc..1b859440f 100644 --- a/README.md +++ b/README.md @@ -86,6 +86,13 @@ console.log(reactElementToJSXString(
Hello, world!
)); 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. diff --git a/index-test.js b/index-test.js index d6e1c4296..d9d725c14 100644 --- a/index-test.js +++ b/index-test.js @@ -748,4 +748,19 @@ describe('reactElementToJSXString(ReactElement)', () => { `); }); + it('should return functionValue result when it returns a string', () => { + expect( + reactElementToJSXString(
'value'}/>, {showFunctions: true, functionValue: () => '...'}) + ).toEqual('
'); + }); + it('sends the original fn to functionValue', () => { + const fn = () => {}; + const functionValue = receivedFn => expect(receivedFn).toBe(fn); + reactElementToJSXString(
, {functionValue}); + }); + it('should return noRefCheck when "showFunctions" is false and "functionValue" is not provided', () => { + expect( + reactElementToJSXString(
{}}/>) + ).toEqual('
'); + }); }); diff --git a/index.js b/index.js index 6e69d4cdd..e335f65a4 100644 --- a/index.js +++ b/index.js @@ -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, @@ -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.