-
Notifications
You must be signed in to change notification settings - Fork 3.7k
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
Add 'toHierarchy()' method equivalent to 'toArray()' method #1790
base: master
Are you sure you want to change the base?
Conversation
'npm audit fix' and 'npm audit' preparing for building.
Such as missing closing parenthases, forgotten commas, wrong assignments, etc.
Could you please write some tests for this? We shouldn't accept PR's without tests. I think that instead of recursive arrays, we should have this as recursive object-arrays. This way the data type will always be the same, instead of the array element being of type const hierarchy = [
{ value: "a", children: [ { value: "b", children: [ ] } ] },
{ value: "b", children: [ /* we could go on forever... */ ] },
{ value: "c", children: [ ] },
{ value: "d", children: [ ]}
] |
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.
The package-lock.json file looks like it hasn't been gitted correctly.
We can fix this later. Focus on the comments I mentioned earlier instead.
@waynevanson @LavaSlider I wrote the code for what Wayne suggested in the answer to a question on stack overflow: I also think it would be a less confusing approach. |
I considered using an array of objects but went with the way it is implemented because it is most consistent with the current functionality of 'toArray'. It is also much lighter weight. The array types are either String or Array, no null's. I am happy to add another method that would loop through the current return and convert it to the structure you describe. What would you suggest the name for that method? I have seen both structures of return from other Sortable/Draggable packages. I will work on developing some tests and updating the README file as well. |
@LavaSlider I think it is less confusing to just use one method, and to me the object with Sortable is a big library with a lot of users and it isn't a library that doesn't requires much programming experience to use, so inevitably there will be lots of questions over any bit of possible confusion. |
I am attempting to write tests but when I run
FAILED COUNT 1 npm ERR! A complete log of this run can be found in: I've looked in the -debug.log file in the _logs directory but it does not have the information. Any suggestions? |
Not sure what to do. I'll write a more helpful test runner soon. |
@LavaSlider If you commit your test to the repo, we can have a look |
This method is similar to a recursive 'toString'. It has three modes of operation. Mode 2 is most similar to 'toString' and returns an array of 'id's with interspersed arrays of 'id's for the sublists. The default, mode 0, returns an array of object with 'id' and 'children' properties. Mode 1 is similar to mode 0 except omits the 'children' property if there are no children.
These tests piggy-back on the already existing 'nested' tests by assessing the output of 'toHierarchy()' after the row-dragging. This is done by adding an 'onEnd' event handler to update the DOM based on current row structure and then compare that text to the expected text.
Ok, it is your library and you can do what you want but I whole heartedly disagree about the structure and think your statement is inconsistent with how the toArray works. Users will use what is provided and want their tools to do what they say they do as efficiently and effectively as possible. Changing to objects with children more than quadruples the data size. To me they seem virtually identical in use. Below is the same function written for each data array format:
To compromise I have merged them into one method that I renamed to getHierarchy() with an optional "mode" parameter and set the default mode to the maximally verbose method with an 'id' and a 'children' property for every node. Mode 1 will strip the 'children' property if there are no children and mode 2 will just return the id strings and arrays as I originally implemented it. I implemented tests as part of the current nested test suite by putting the stringified version of the returned values into the DOM and comparing that to the expected strings. This will fail if I also added the sections to the README.md file describing the operations. |
Oops, had forgot to remove a 60 second delay in the test script I had added for figuring out what was going on. |
Sometimes specific use cases should transform the data to a structure they need with a single function of their own.
I'd rather have more data and a clear way of navigating it than trying to navigate nested arrays.
I'd recommend that we get the hierarchy using one method only, then transform that data to it's various different 'shapes'. That way if any changes are made in future, there will only have to be one 'getter' function from the DOM. type Hierarchy = { id: string, children: Hierarchy[] }
function getHierarchy() :Hierarchy I will do a review in more detail when I get the time over the next week. I need more time to process the code. |
I was using nested Sortable elements and needed a way to easily dump the hierarchy. The added method is analogous to 'toArray()' except that it includes both ids of the Sortable element and all its nested lists.
This is only new code, three new options and three new methods so should not change other functionality.