Skip to content
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

Merged

Conversation

mum-never-proud
Copy link
Contributor

@mum-never-proud mum-never-proud commented Mar 21, 2020

Fixes #244

index.js Outdated
@@ -263,6 +269,14 @@ function parse(input, options) {
}, Object.create(null));
}

function isEmptyString(value) {
return value === '';
}
Copy link
Owner

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@sindresorhus
Copy link
Owner

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) => {
Copy link
Owner

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.

Copy link
Contributor Author

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?

Copy link
Owner

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).

Copy link
Contributor Author

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?

Copy link
Owner

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.

readme.md Outdated Show resolved Hide resolved
@sindresorhus sindresorhus changed the title add support to skip empty string Add skipEmptyString option Apr 4, 2020
mum-never-proud and others added 3 commits April 5, 2020 10:21
Co-Authored-By: Sindre Sorhus <sindresorhus@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add parameter skipEmptyString
2 participants