Skip to content

Check for null before stringifying objects #50

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
dextermb opened this issue Dec 13, 2018 · 7 comments
Closed

Check for null before stringifying objects #50

dextermb opened this issue Dec 13, 2018 · 7 comments

Comments

@dextermb
Copy link
Contributor

Somewhat related to #30, as null is an object in JavaScript it gets caught by the object if-statement and then gets stringified which converts it to a string.

screen shot 2018-12-13 at 14 35 53

I think it would be best to check for null, and return a string before rendering the cell. Thoughts?

@DivineOmega
Copy link
Contributor

DivineOmega commented Dec 13, 2018

I don't see this as a problem. In fact, in a way it is an advantage.

It allows you to just use the dataItemManipulator prop to modify the null to a more appropriate value, such as a dash, a blank space, or something similar to 'No end date'.

@dextermb
Copy link
Contributor Author

I don't see this as a problem. In fact, in a way it is an advantage.

It allows you to just use the dataItemManipulator prop to modify the null to a more appropriate value, such as a dash, a blank space, or something similar to 'No end date'.

It's not null, it's 'null'. I think at least the default manipulator should hide it.

@DivineOmega
Copy link
Contributor

Okay. Are you proposing converting null into `` (empty string) before it is displayed?

@dextermb
Copy link
Contributor Author

dextermb commented Dec 13, 2018

Okay. Are you proposing converting null into `` (empty string) before it is displayed?

Yessir

Edit:
You would edit the renderCell function in DataRow:

value = this.props.dataItemManipulator(field.name, value);

if (value === null) {
    value = '';
}

if (typeof value === 'object' || typeof value === 'array') {
    value = JSON.stringify(value);
}

Source. Might even be worth throwing in a safety catch for undefined (event though undefined is not an object), so the new if-statement would become value === null || value === undefined.

@DivineOmega
Copy link
Contributor

That makes sense, assuming you can not think of any scenerios in which we would want to differentiate between null and empty string.

@dextermb
Copy link
Contributor Author

That makes sense, assuming you can not think of any scenerios in which we would want to differentiate between null and empty string.

You could either check for null in the renderCell or maybe have the default dataItemManipulator check for null and return an empty string. This way the user would have the power.

@DivineOmega
Copy link
Contributor

I like the latter suggestion. 👍

[...] have the default dataItemManipulator check for null and return an empty string. This way the user would have the power.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants