-
-
Notifications
You must be signed in to change notification settings - Fork 455
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 skipEmptyString
option
#252
Add skipEmptyString
option
#252
Conversation
index.js
Outdated
@@ -263,6 +269,14 @@ function parse(input, options) { | |||
}, Object.create(null)); | |||
} | |||
|
|||
function isEmptyString(value) { | |||
return value === ''; | |||
} |
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 utility has no value over just checking directly.
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.
done
You also need to update index.d.ts. That one and the readme should be in sync. I would also like to see more tests. |
index.js
Outdated
delete objectCopy[key]; | ||
const objectCopy = Object | ||
.keys(object) | ||
.reduce((objectCopy, key) => { |
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.
Don't do unrelated changes. I prefer this to stay a for-of
loop instead of reduce, for readability.
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.
I didn't refactor, rather a related change
if (options.skipNull) {
for (const key of Object.keys(objectCopy)) {
if (objectCopy[key] === undefined || objectCopy[key] === null) {
delete objectCopy[key];
}
}
}
the same can be achieved with
if (options.skipNull || options. skipEmptyString) {
for (const key of Object.keys(objectCopy)) {
if (objectCopy[key] === undefined || objectCopy[key] === null || objectCopy[key] === '') {
delete objectCopy[key];
}
}
}
suppose in future if we introduce other filters the list will go on...
is there any better way than this?
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.
is there any better way than this?
Do the same as now, but instead of reduce, use for-of
loop, meaning no Object.asssign
. You could maybe move the filtering logic here into a helper function (in this scope, not top-level).
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.
hmm ok, if I remove the reduce this is how the code is (existing) will this work? sorry if I am not getting your point
const objectCopy = Object.assign({}, object);
for (const key of Object.keys(object)) {
if ((options.skipNull && isNullOrUndefined(object[key])) ||
(options.skipEmptyString && object[key] === '')) {
delete objectCopy[key];
}
}
this is another approach
const shouldFilter = key => (
(options.skipNull && isNullOrUndefined(object[key])) ||
(options.skipEmptyString && object[key] === '')
);
const formatter = encoderForArrayFormat(options);
const objectCopy = {};
for (const key of Object.keys(object)) {
if (!shouldFilter(key)) {
objectCopy[key] = object[key];
}
}
we can add as many filters we need in future
your thoughts?
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 another approach
^ The last code block here is what I had in mind. 👌 Sorry for being unclear.
skipEmptyString
option
Co-Authored-By: Sindre Sorhus <sindresorhus@gmail.com>
…-never-proud/query-string into add-skip-empty-string-option
Fixes #244