-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
support for iterable structures within state #79
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
56de89c
support for state with iterable structures
danielkcz c76f755
safer type checking for iterable structures
danielkcz 04f15f6
arrays shouldn't be treated as an iterable
danielkcz 11af53c
counting entries of iterable structure
danielkcz fff7e39
using Number.isSafeInteger for iterable size (+ fixed some comments)
danielkcz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
import React from 'react'; | ||
import reactMixin from 'react-mixin'; | ||
import { ExpandedStateHandlerMixin } from './mixins'; | ||
import JSONArrow from './JSONArrow'; | ||
import grabNode from './grab-node'; | ||
|
||
const styles = { | ||
base: { | ||
position: 'relative', | ||
paddingTop: 3, | ||
paddingBottom: 3, | ||
paddingRight: 0, | ||
marginLeft: 14 | ||
}, | ||
label: { | ||
margin: 0, | ||
padding: 0, | ||
display: 'inline-block' | ||
}, | ||
span: { | ||
cursor: 'default' | ||
}, | ||
spanType: { | ||
marginLeft: 5, | ||
marginRight: 5 | ||
} | ||
}; | ||
|
||
@reactMixin.decorate(ExpandedStateHandlerMixin) | ||
export default class JSONIterableNode extends React.Component { | ||
defaultProps = { | ||
data: [], | ||
initialExpanded: false | ||
}; | ||
|
||
// flag to see if we still need to render our child nodes | ||
needsChildNodes = true; | ||
|
||
// cache store for our child nodes | ||
renderedChildren = []; | ||
|
||
// cache store for the number of items string we display | ||
itemString = false; | ||
|
||
constructor(props) { | ||
super(props); | ||
this.state = { | ||
expanded: this.props.initialExpanded, | ||
createdChildNodes: false | ||
}; | ||
} | ||
|
||
// Returns the child nodes for each entry in iterable. If we have | ||
// generated them previously, we return from cache, otherwise we create | ||
// them. | ||
getChildNodes() { | ||
if (this.state.expanded && this.needsChildNodes) { | ||
let childNodes = []; | ||
for (const entry of this.props.data) { | ||
let key = null; | ||
let value = null; | ||
if (Array.isArray(entry)) { | ||
[key, value] = entry; | ||
} else { | ||
key = childNodes.length; | ||
value = entry; | ||
} | ||
|
||
let prevData; | ||
if (typeof this.props.previousData !== 'undefined') { | ||
prevData = this.props.previousData[key]; | ||
} | ||
const node = grabNode(key, value, prevData, this.props.theme); | ||
if (node !== false) { | ||
childNodes.push(node); | ||
} | ||
} | ||
this.needsChildNodes = false; | ||
this.renderedChildren = childNodes; | ||
} | ||
return this.renderedChildren; | ||
} | ||
|
||
// Returns the "n entries" string for this node, generating and | ||
// caching it if it hasn't been created yet. | ||
getItemString() { | ||
if (!this.itemString) { | ||
const { data } = this.props; | ||
let count = 0; | ||
if (Number.isSafeInteger(data.size)) { | ||
count = data.size; | ||
} else { | ||
for (const entry of data) { // eslint-disable-line no-unused-vars | ||
count += 1; | ||
} | ||
} | ||
this.itemString = count + ' entr' + (count !== 1 ? 'ies' : 'y'); | ||
} | ||
return this.itemString; | ||
} | ||
|
||
render() { | ||
const childNodes = this.getChildNodes(); | ||
const childListStyle = { | ||
padding: 0, | ||
margin: 0, | ||
listStyle: 'none', | ||
display: (this.state.expanded) ? 'block' : 'none' | ||
}; | ||
let containerStyle; | ||
let spanStyle = { | ||
...styles.span, | ||
color: this.props.theme.base0E | ||
}; | ||
containerStyle = { | ||
...styles.base | ||
}; | ||
if (this.state.expanded) { | ||
spanStyle = { | ||
...spanStyle, | ||
color: this.props.theme.base03 | ||
}; | ||
} | ||
return ( | ||
<li style={containerStyle}> | ||
<JSONArrow theme={this.props.theme} open={this.state.expanded} onClick={::this.handleClick}/> | ||
<label style={{ | ||
...styles.label, | ||
color: this.props.theme.base0D | ||
}} onClick={::this.handleClick}> | ||
{this.props.keyName}: | ||
</label> | ||
<span style={spanStyle} onClick={::this.handleClick}> | ||
<span style={styles.spanType}>()</span> | ||
{this.getItemString()} | ||
</span> | ||
<ol style={childListStyle}> | ||
{childNodes} | ||
</ol> | ||
</li> | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,8 @@ | ||
export default function(obj) { | ||
if (obj !== null && typeof obj === 'object' && !Array.isArray(obj) && | ||
typeof obj[Symbol.iterator] === 'function' | ||
) { | ||
return 'Iterable'; | ||
} | ||
return Object.prototype.toString.call(obj).slice(8, -1); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Change to the following
To handle the length of a
Map()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I agree. I had the
size
there before, but then I've noticed that MoriJS has onlycount
method.I am thinking if it wouldn't be better to use iterator even here to count entries. That would be unified way. I would like to avoid adding another property/method in a future if some other data structure implementing iterator would use different name for this. Some library could even have a like
size
being method orcount
being property.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any conclusion here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alright, I've change it. It uses the
size
property as it's the most common (only mori doesn't have it), otherwise it counts entries through the iterator. I am not usinglength
because it's deprecated in Immutable anyway and others doesn't have it at all.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Another small change to use Number.isSafeInteger for the
size
property. It should be safer way in case some library hassize
as a function.