Skip to content

Commit e8f05e7

Browse files
author
Gabriela Seabra
committed
feat(options) more flexible function options
- Pass prop name as second argument in `functionValue` option - Accept filter function in `showFunctions`
1 parent afe55e4 commit e8f05e7

10 files changed

+137
-73
lines changed

src/formatter/formatComplexDataStructure.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import type { Options } from './../options';
1111

1212
export default (
1313
value: Object | Array<any>,
14+
name: string,
1415
inline: boolean,
1516
lvl: number,
1617
options: Options
@@ -31,7 +32,7 @@ export default (
3132
}
3233

3334
if (typeof currentValue === 'function') {
34-
return formatFunction(currentValue, true, lvl, options);
35+
return formatFunction(currentValue, name, true, lvl, options);
3536
}
3637

3738
return originalResult;

src/formatter/formatComplexDataStructure.spec.js

Lines changed: 35 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ describe('formatComplexDataStructure', () => {
1515
it('should format an object', () => {
1616
const fixture = { a: 1, b: { c: 'ccc' } };
1717

18-
expect(formatComplexDataStructure(fixture, false, 0, options)).toEqual(
18+
expect(
19+
formatComplexDataStructure(fixture, 'foo', false, 0, options)
20+
).toEqual(
1921
`{
2022
a: 1,
2123
b: {
@@ -28,19 +30,23 @@ describe('formatComplexDataStructure', () => {
2830
it('should format inline an object', () => {
2931
const fixture = { a: 1, b: { c: 'ccc' } };
3032

31-
expect(formatComplexDataStructure(fixture, true, 0, options)).toEqual(
32-
"{a: 1, b: {c: 'ccc'}}"
33-
);
33+
expect(
34+
formatComplexDataStructure(fixture, 'foo', true, 0, options)
35+
).toEqual("{a: 1, b: {c: 'ccc'}}");
3436
});
3537

3638
it('should format an empty object', () => {
37-
expect(formatComplexDataStructure({}, false, 0, options)).toEqual('{}');
39+
expect(formatComplexDataStructure({}, 'foo', false, 0, options)).toEqual(
40+
'{}'
41+
);
3842
});
3943

4044
it('should order the object keys', () => {
4145
const fixture = { b: { d: 'ddd', c: 'ccc' }, a: 1 };
4246

43-
expect(formatComplexDataStructure(fixture, false, 0, options)).toEqual(
47+
expect(
48+
formatComplexDataStructure(fixture, 'foo', false, 0, options)
49+
).toEqual(
4450
`{
4551
a: 1,
4652
b: {
@@ -54,7 +60,9 @@ describe('formatComplexDataStructure', () => {
5460
it('should format an array', () => {
5561
const fixture = [1, '2', true, false, null];
5662

57-
expect(formatComplexDataStructure(fixture, false, 0, options)).toEqual(
63+
expect(
64+
formatComplexDataStructure(fixture, 'foo', false, 0, options)
65+
).toEqual(
5866
`[
5967
1,
6068
'2',
@@ -68,39 +76,43 @@ describe('formatComplexDataStructure', () => {
6876
it('should format inline an array ', () => {
6977
const fixture = [1, '2', true, false, null];
7078

71-
expect(formatComplexDataStructure(fixture, true, 0, options)).toEqual(
72-
"[1, '2', true, false, null]"
73-
);
79+
expect(
80+
formatComplexDataStructure(fixture, 'foo', true, 0, options)
81+
).toEqual("[1, '2', true, false, null]");
7482
});
7583

7684
it('should format an object that contains a react element', () => {
7785
const fixture = { a: createFakeReactElement('BarBar') };
7886

79-
expect(formatComplexDataStructure(fixture, false, 0, options)).toEqual(
87+
expect(
88+
formatComplexDataStructure(fixture, 'foo', false, 0, options)
89+
).toEqual(
8090
`{
8191
a: <BarBar />
8292
}`
8393
);
8494
});
8595

8696
it('should format an empty array', () => {
87-
expect(formatComplexDataStructure([], false, 0, options)).toEqual('[]');
97+
expect(formatComplexDataStructure([], 'foo', false, 0, options)).toEqual(
98+
'[]'
99+
);
88100
});
89101

90102
it('should format an object that contains a date', () => {
91103
const fixture = { a: new Date('2017-11-13T00:00:00.000Z') };
92104

93-
expect(formatComplexDataStructure(fixture, true, 0, options)).toEqual(
94-
`{a: new Date('2017-11-13T00:00:00.000Z')}`
95-
);
105+
expect(
106+
formatComplexDataStructure(fixture, 'foo', true, 0, options)
107+
).toEqual(`{a: new Date('2017-11-13T00:00:00.000Z')}`);
96108
});
97109

98110
it('should format an object that contains a regexp', () => {
99111
const fixture = { a: /test/g };
100112

101-
expect(formatComplexDataStructure(fixture, true, 0, options)).toEqual(
102-
`{a: /test/g}`
103-
);
113+
expect(
114+
formatComplexDataStructure(fixture, 'foo', true, 0, options)
115+
).toEqual(`{a: /test/g}`);
104116
});
105117

106118
it('should replace a function with noRefCheck', () => {
@@ -110,9 +122,9 @@ describe('formatComplexDataStructure', () => {
110122
},
111123
};
112124

113-
expect(formatComplexDataStructure(fixture, true, 0, options)).toEqual(
114-
'{a: function noRefCheck() {}}'
115-
);
125+
expect(
126+
formatComplexDataStructure(fixture, 'foo', true, 0, options)
127+
).toEqual('{a: function noRefCheck() {}}');
116128
});
117129

118130
it('should format a function', () => {
@@ -123,7 +135,7 @@ describe('formatComplexDataStructure', () => {
123135
};
124136

125137
expect(
126-
formatComplexDataStructure(fixture, true, 0, {
138+
formatComplexDataStructure(fixture, 'foo', true, 0, {
127139
...options,
128140
showFunctions: true,
129141
})
@@ -138,7 +150,7 @@ describe('formatComplexDataStructure', () => {
138150
};
139151

140152
expect(
141-
formatComplexDataStructure(fixture, true, 0, {
153+
formatComplexDataStructure(fixture, 'foo', true, 0, {
142154
...options,
143155
functionValue: () => '<Test />',
144156
})

src/formatter/formatFunction.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,22 @@ export const preserveFunctionLineBreak = (fn: any): string => fn.toString();
1414

1515
export default (
1616
fn: Function,
17+
prop: string,
1718
inline: boolean,
1819
lvl: number,
1920
options: Options
2021
): string => {
2122
const { functionValue, showFunctions } = options;
2223
const functionFn =
2324
functionValue || (inline ? inlineFunction : preserveFunctionLineBreak);
24-
const shouldShowFunction = showFunctions || functionValue;
25+
const shouldShowFunction = Boolean(
26+
functionValue ||
27+
(typeof showFunctions === 'function'
28+
? showFunctions(fn, prop)
29+
: showFunctions)
30+
);
2531

26-
return String(functionFn(shouldShowFunction ? fn : noRefCheck))
32+
return String(functionFn(shouldShowFunction ? fn : noRefCheck, prop))
2733
.split('\n')
2834
.map(ln => spacer(lvl, options.tabStop) + ln)
2935
.join('\n')

src/formatter/formatFunction.spec.js

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,38 +12,40 @@ function hello() {
1212

1313
describe('formatFunction', () => {
1414
it('should replace a function with noRefCheck without showFunctions option', () => {
15-
expect(formatFunction(hello, true, 0, {})).toEqual(
15+
expect(formatFunction(hello, 'prop', true, 0, {})).toEqual(
1616
'function noRefCheck() {}'
1717
);
1818
});
1919

2020
it('should replace a function with noRefCheck if showFunctions is false', () => {
21-
expect(formatFunction(hello, true, 0, { showFunctions: false })).toEqual(
22-
'function noRefCheck() {}'
23-
);
21+
expect(
22+
formatFunction(hello, 'prop', true, 0, { showFunctions: false })
23+
).toEqual('function noRefCheck() {}');
2424
});
2525

2626
it('should format a function if showFunctions is true', () => {
27-
expect(formatFunction(hello, true, 0, { showFunctions: true })).toEqual(
28-
'function hello() {return 1;}'
29-
);
27+
expect(
28+
formatFunction(hello, 'prop', true, 0, { showFunctions: true })
29+
).toEqual('function hello() {return 1;}');
3030
});
3131

3232
it('should format a function without name if showFunctions is true', () => {
33-
expect(formatFunction(() => 1, true, 0, { showFunctions: true })).toEqual(
34-
'function () {return 1;}'
35-
);
33+
expect(
34+
formatFunction(() => 1, 'prop', true, 0, { showFunctions: true })
35+
).toEqual('function () {return 1;}');
3636
});
3737

3838
it('should use the functionValue option', () => {
3939
expect(
40-
formatFunction(hello, true, 0, { functionValue: () => '<Test />' })
40+
formatFunction(hello, 'prop', true, 0, {
41+
functionValue: () => '<Test />',
42+
})
4143
).toEqual('<Test />');
4244
});
4345

4446
it('should use the functionValue option even if showFunctions is true', () => {
4547
expect(
46-
formatFunction(hello, true, 0, {
48+
formatFunction(hello, 'prop', true, 0, {
4749
showFunctions: true,
4850
functionValue: () => '<Test />',
4951
})
@@ -52,7 +54,7 @@ describe('formatFunction', () => {
5254

5355
it('should use the functionValue option even if showFunctions is false', () => {
5456
expect(
55-
formatFunction(hello, true, 0, {
57+
formatFunction(hello, 'prop', true, 0, {
5658
showFunctions: false,
5759
functionValue: () => '<Test />',
5860
})
@@ -61,13 +63,19 @@ describe('formatFunction', () => {
6163

6264
it('should format multi-line function', () => {
6365
expect(
64-
formatFunction(hello, false, 0, { showFunctions: true, tabStop: 2 })
66+
formatFunction(hello, 'prop', false, 0, {
67+
showFunctions: true,
68+
tabStop: 2,
69+
})
6570
).toEqual('function hello() {\n return 1;\n}');
6671
});
6772

6873
it('should format multi-line function with indentation', () => {
6974
expect(
70-
formatFunction(hello, false, 1, { showFunctions: true, tabStop: 2 })
75+
formatFunction(hello, 'prop', false, 1, {
76+
showFunctions: true,
77+
tabStop: 2,
78+
})
7179
).toEqual('function hello() {\n return 1;\n }');
7280
});
7381
});

src/formatter/formatProp.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,13 @@ export default (
2828

2929
const { useBooleanShorthandSyntax, tabStop } = options;
3030

31-
const formattedPropValue = formatPropValue(usedValue, inline, lvl, options);
31+
const formattedPropValue = formatPropValue(
32+
usedValue,
33+
name,
34+
inline,
35+
lvl,
36+
options
37+
);
3238

3339
let attributeFormattedInline = ' ';
3440
let attributeFormattedMultiline = `\n${spacer(lvl + 1, tabStop)}`;

src/formatter/formatProp.spec.js

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ describe('formatProp', () => {
3030

3131
expect(formatPropValue).toHaveBeenCalledWith(
3232
'bar',
33+
'foo',
3334
true,
3435
0,
3536
defaultOptions
@@ -50,6 +51,7 @@ describe('formatProp', () => {
5051

5152
expect(formatPropValue).toHaveBeenCalledWith(
5253
'baz',
54+
'foo',
5355
true,
5456
0,
5557
defaultOptions
@@ -70,6 +72,7 @@ describe('formatProp', () => {
7072

7173
expect(formatPropValue).toHaveBeenCalledWith(
7274
'bar',
75+
'foo',
7376
true,
7477
0,
7578
defaultOptions
@@ -93,7 +96,7 @@ describe('formatProp', () => {
9396
isMultilineAttribute: false,
9497
});
9598

96-
expect(formatPropValue).toHaveBeenCalledWith(true, true, 0, options);
99+
expect(formatPropValue).toHaveBeenCalledWith(true, 'foo', true, 0, options);
97100
});
98101

99102
it('should ignore a falsy boolean prop (with short syntax)', () => {
@@ -112,7 +115,13 @@ describe('formatProp', () => {
112115
isMultilineAttribute: false,
113116
});
114117

115-
expect(formatPropValue).toHaveBeenCalledWith(false, true, 0, options);
118+
expect(formatPropValue).toHaveBeenCalledWith(
119+
false,
120+
'foo',
121+
true,
122+
0,
123+
options
124+
);
116125
});
117126

118127
it('should format a truthy boolean prop (with explicit syntax)', () => {
@@ -132,7 +141,7 @@ describe('formatProp', () => {
132141
isMultilineAttribute: false,
133142
});
134143

135-
expect(formatPropValue).toHaveBeenCalledWith(true, true, 0, options);
144+
expect(formatPropValue).toHaveBeenCalledWith(true, 'foo', true, 0, options);
136145
});
137146

138147
it('should format a falsy boolean prop (with explicit syntax)', () => {
@@ -152,7 +161,13 @@ describe('formatProp', () => {
152161
isMultilineAttribute: false,
153162
});
154163

155-
expect(formatPropValue).toHaveBeenCalledWith(false, true, 0, options);
164+
expect(formatPropValue).toHaveBeenCalledWith(
165+
false,
166+
'foo',
167+
true,
168+
0,
169+
options
170+
);
156171
});
157172

158173
it('should format a mulitline props', () => {
@@ -187,6 +202,7 @@ describe('formatProp', () => {
187202

188203
expect(formatPropValue).toHaveBeenCalledWith(
189204
['a', 'b'],
205+
'foo',
190206
false,
191207
0,
192208
defaultOptions
@@ -217,6 +233,12 @@ describe('formatProp', () => {
217233
isMultilineAttribute: false,
218234
});
219235

220-
expect(formatPropValue).toHaveBeenCalledWith('bar', true, 4, options);
236+
expect(formatPropValue).toHaveBeenCalledWith(
237+
'bar',
238+
'foo',
239+
true,
240+
4,
241+
options
242+
);
221243
});
222244
});

0 commit comments

Comments
 (0)