-
Notifications
You must be signed in to change notification settings - Fork 1.1k
feat(filter): implement ensureFields utility #6495
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
feat(filter): implement ensureFields utility #6495
Conversation
14cb5bd to
37254b7
Compare
Signed-off-by: InvictusMB <invictusmb@gmail.com>
1346a2d to
09c29bd
Compare
|
@achrinza Would you like to review it? |
|
@mdbetancourt could you please review the changes proposed in this pull request? I feel they may be related to the work you contributed in #6517. |
mdbetancourt
left a comment
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.
@bajtos It looks good to me, @InvictusMB I think you can make some changes to make the code more understandable and avoid adding a library like lodash, I made the suggestions quickly so maybe you have to make some final adjustments
| fieldsAdded: [] as (keyof T)[], | ||
| }; | ||
| } | ||
| const isDisablingOnly = _.size(fields) > 0 && !_.some(fields, Boolean); |
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.
this is some hard to understand a suggestion could be uses every instead of some negation a vanilla version:
| const isDisablingOnly = _.size(fields) > 0 && !_.some(fields, Boolean); | |
| const isDisablingOnly = Object.values(fields).length > 0 && Object.values(fields).every(f => !f); |
| export function matchesFields<T extends object = AnyObject>( | ||
| fields: (keyof T)[], | ||
| filter?: Filter<T>, | ||
| ) { | ||
| const normalized = new FilterBuilder(filter).build(); | ||
| const targetFields = normalized.fields; | ||
| if (!targetFields) { | ||
| return true; | ||
| } | ||
| const isDisablingOnly = | ||
| _.size(targetFields) > 0 && !_.some(targetFields, Boolean); | ||
| for (const f of fields) { | ||
| if (!targetFields[f] && (f in targetFields || !isDisablingOnly)) { | ||
| return false; | ||
| } | ||
| } | ||
| return true; | ||
| } |
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.
| export function matchesFields<T extends object = AnyObject>( | |
| fields: (keyof T)[], | |
| filter?: Filter<T>, | |
| ) { | |
| const normalized = new FilterBuilder(filter).build(); | |
| const targetFields = normalized.fields; | |
| if (!targetFields) { | |
| return true; | |
| } | |
| const isDisablingOnly = | |
| _.size(targetFields) > 0 && !_.some(targetFields, Boolean); | |
| for (const f of fields) { | |
| if (!targetFields[f] && (f in targetFields || !isDisablingOnly)) { | |
| return false; | |
| } | |
| } | |
| return true; | |
| } | |
| function isSuperset(set, subset) { | |
| for (let elem of subset) { | |
| if (!set.has(elem)) { | |
| return false | |
| } | |
| } | |
| return true | |
| } | |
| function difference(setA, setB) { | |
| let _difference = new Set(setA) | |
| for (let elem of setB) { | |
| _difference.delete(elem) | |
| } | |
| return _difference | |
| } |
| const fields = builder.build().fields; | ||
| if (!fields || matchesFields(targetFields, filter)) { | ||
| return { | ||
| filter: builder.build(), | ||
| fieldsAdded: [] as (keyof T)[], | ||
| }; | ||
| } |
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.
| const fields = builder.build().fields; | |
| if (!fields || matchesFields(targetFields, filter)) { | |
| return { | |
| filter: builder.build(), | |
| fieldsAdded: [] as (keyof T)[], | |
| }; | |
| } | |
| const fields = new Set(Object.keys(builder.build().fields)); | |
| const target = new Set(Object.keys(targetFields)) | |
| if (!fields.size || isSuperset(target, fields)) { | |
| return { | |
| filter: builder.build(), | |
| fieldsAdded: [], | |
| }; | |
| } |
| const fieldsAdded = (isDisablingOnly ? _.keys(fields) : []) as (keyof T)[]; | ||
| targetFields.forEach(f => { | ||
| if (!fields[f]) { | ||
| fieldsAdded.push(f); | ||
| builder.fields(f); | ||
| } | ||
| }); | ||
|
|
||
| const newFilter = builder.build(); | ||
| // if the filter only hides the fields, unset the entire fields clause | ||
| if (isDisablingOnly) { | ||
| delete filter.fields; | ||
| } | ||
| return { | ||
| filter: newFilter, | ||
| fieldsAdded: _.uniq(fieldsAdded) as (keyof T)[], | ||
| } as const; | ||
| } |
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.
| const fieldsAdded = (isDisablingOnly ? _.keys(fields) : []) as (keyof T)[]; | |
| targetFields.forEach(f => { | |
| if (!fields[f]) { | |
| fieldsAdded.push(f); | |
| builder.fields(f); | |
| } | |
| }); | |
| const newFilter = builder.build(); | |
| // if the filter only hides the fields, unset the entire fields clause | |
| if (isDisablingOnly) { | |
| delete filter.fields; | |
| } | |
| return { | |
| filter: newFilter, | |
| fieldsAdded: _.uniq(fieldsAdded) as (keyof T)[], | |
| } as const; | |
| } | |
| const fieldsAdded = difference(target, fields) | |
| fieldsAdded.forEach(builder.fields) | |
| return { | |
| filter: builder.build(), | |
| fieldsAdded | |
| } as const; | |
| } |
agnes512
left a comment
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.
Almost LGTM 👍 I will review it again once those comments from @mdbetancourt are addressed.
|
@agnes512, assigning this PR to you since you're helping it to land. Thanks. |
|
@InvictusMB any updates? |
|
This pull request has been marked stale because it has not seen activity within two months. It will be closed within 14 days of being stale unless there is new activity. |
|
Not stale. I'll have time to refresh this soon. |
|
This pull request has been marked stale because it has not seen activity within two months. It will be closed within 14 days of being stale unless there is new activity. |
|
This pull request has been closed due to continued inactivity. If you are interested in finishing the proposed changes, then feel free to re-open this pull request or open a new one. |
Split from #5592
Checklist
npm testpasses on your machinepackages/cliwere updatedexamples/*were updated👉 Check out how to submit a PR 👈