Skip to content

Commit

Permalink
fix: handle null and undefined prop values
Browse files Browse the repository at this point in the history
fixes #1
  • Loading branch information
vvo committed Oct 16, 2015
1 parent 7060f39 commit 9a57a10
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
18 changes: 17 additions & 1 deletion index-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,11 +152,27 @@ describe(`reactElementToJSXString(ReactElement)`, () => {
}).toThrow('react-element-to-jsx-string: Expected a ReactElement');
});

it('ignores object keys order (sortobject)', () => {
it(`ignores object keys order (sortobject)`, () => {

This comment has been minimized.

Copy link
@redox

redox Oct 16, 2015

Contributor

Why backquotes?

This comment has been minimized.

Copy link
@vvo

vvo Oct 16, 2015

Contributor

Well I only use backquotes everywhere now so that I have never to ask myself if I should use single or double or backquotes.

Backquotes allows you to do string interpolation at any moment (template strings):

var world = ', world!';
var hey = `Hello ${world}`;
var well = `But what about ${someFunction(hey)}`;

So instead of switching from one to another and sometime turning '' to for the sake of interpolation, I use everywhere

expect(
reactElementToJSXString(<div o={{a: 1, b: 2}}/>)
).toEqual(
reactElementToJSXString(<div o={{b: 2, a: 1}}/>)
);
});

it(`reactElementToJSXString(<div a={null} />`, () => {
expect(
reactElementToJSXString(<div a={null} />)
).toEqual(
reactElementToJSXString(<div a={null} />)
);
});

it(`reactElementToJSXString(<div a={undefined} />`, () => {
expect(
reactElementToJSXString(<div a={undefined} />)
).toEqual(
reactElementToJSXString(<div a={undefined} />)
);
});
});
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ function recurse({lvl, inline}) {

function stringifyObject(obj) {
// sortobject fails on some types, like regex
if (Object.keys(obj).length > 0) {
if (obj && Object.keys(obj).length > 0) {
obj = sortobject(obj);
}

Expand Down

0 comments on commit 9a57a10

Please sign in to comment.