-
Notifications
You must be signed in to change notification settings - Fork 209
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 just-order-by
package
#530
Conversation
Thanks Maxim! I like this but can we drop the |
I do not quite understand. Are you suggesting to change // from
type OrderParam<T> = (
| OrderParamField<T>
| { field: OrderParamField<T>; order?: 'asc' | 'desc' }
);
// to
type OrderParam<T> = (
| OrderParamField<T>
| { order?: 'asc' | 'desc' }
); |
I might be wrong but it seems there only needs to be two possible argument formats (and I think this how lodash works too, although their documentation is not clear) Sort definition is an array of keys, order is an array of strings Sort definition is a function, order is a string |
In fact, this is only a part of all possible argument formats (as far as I can see from their source code) I purposely did not copy api from lodash. It seems more convenient to me to use an object as a parameter (it is immediately visible by which field and in what order sorting is performed) Do you think the lodash api is better or is there some other reason to use it? |
Not wedded to lodash API While I do like the idea of only having only two args, it just seemed a little confusing having an I'm not really sure why we need an option for function type params at all. Either the function just points to the property name (don't need function), or if it's more complex it could just take the place of the entire sort function and user could use My personal preference is either: |
As I see it: orderBy(arr, [
(a) => a.b.c.toLowerCase(),
'f',
/* ^^^^^^^^^^^^^^^^^^^^^^^^
Variant 1:
Use an element as a function or a string, order is always asc
String is name of top level field of object
Inside function you can use nested fields of object and transform value
*/
{ field: 'd' },
{ field: 'e', order: 'desc' },
{ field: (a) => a.b['g.g'] },
{ field: (a) => a.b.h, order: 'desc' },
/* ^^^^^^^^^^^^^^^^^^^^^^^^
Variant 2:
Use an element as object
Field named 'field' is required and behavior is identical to Variant 1
Field named 'order' is optional and defaults to asc
*/
]); I think it is as simple as possible As for string as a path to field: using a string not as a field name, but as a path to field is harder to implement, harder to declare types, and ultimately inferior to capabilities of function |
I don't think it should support function args. I like variant 2. We use path to field in https://github.com/angus-c/just#just-safe-get and it's not too difficult. |
I agree to leave only variant 2. But I do not agree with using string as path to field Problems:
This means that we will have to copy code from
The And as I wrote above, function still provides more features Also as an example: |
First of all my apologies. I completely forgot about the existence of In this case it makes perfect sense to follow a similar API. So yes let's keep functions, and no string to path. I would say for functional arg we should use your variant 1 style above (I don't think variant 2 is necessary for functional args since the function itself can specify the sort order—and I always want to have just one way to do things). I would prefer A general note about occasional duplication of code in Just. It is not ideal but IMO it is better than the alternative. The purpose of Just is to make modules as tiny as possible so they a) do not cause a long network fetch b) do not bloat memory on older devices. Lodash modular version auto-inlines all dependencies into one module. It may be a little easier to maintain but it makes the JS much bigger since because it almost always inlines much more code than it actually needs. I would prefer to occasionally rewrite the same code in the interests of legibility and file size. |
The final api looks like this: orderBy(arr, [
{ property: 'd' },
{ property: 'e', order: 'desc' },
{ property: (a) => a.b['g.g'] },
{ property: (a) => a.b.h, order: 'desc' },
]); Correct me if something is wrong. If you agree, I will rewrite code as we agreed |
Looks good except I don't believe functional version needs |
Thank you for your patience.
I made a couple of changes in master (removed 12.x test, updated version in lerna config to match package.json version) and that worked locally. Would you mind rebasing from master to see if the checks now pass? |
@klaseca We're getting 2 test errors |
Fixed |
Thanks! Published as |
Adding a
orderBy
function similar toorderBy
fromlodash