Coder Social home page Coder Social logo

Comments (9)

TkDodo avatar TkDodo commented on May 20, 2024

here's a diff that I think would accomplish this:

diff --git a/src/isEmpty.ts b/src/isEmpty.ts
index b42b70c..fc73d57 100644
--- a/src/isEmpty.ts
+++ b/src/isEmpty.ts
@@ -22,6 +22,7 @@ export function isEmpty(data: ReadonlyArray<unknown> | []): data is [];
@@ -22,6 +22,7 @@ export function isEmpty(data: ReadonlyArray<unknown> | []): data is [];
 export function isEmpty<T extends Readonly<Record<PropertyKey, unknown>>>(
   data: T
 ): data is Record<keyof T, never>;
+export function isEmpty(data: null | undefined): true;
 export function isEmpty(data: unknown): boolean {
   if (isArray(data) || isString(data)) {
     return data.length === 0;
@@ -29,5 +30,5 @@ export function isEmpty(data: unknown): boolean {
   if (isObject(data)) {
     return Object.keys(data).length === 0;
   }
-  return false;
+  return data == null;
 }

from remeda.

eranhirsch avatar eranhirsch commented on May 20, 2024

When I reviewed the original PR my fear was that any relaxation of typing would allow some bugs to fall through, this is the reasoning behind typescripts strictest configs which are not enabled by default (like exactOptionalPropertyTypes). Zod have the same issue where optional() object properties return a type which has undefined in it.

Because remeda isn't configurable, we need to make a decision on how strict our types should be. I prefer strictness, but understand this isn't everyone's cup of tea.

Your example could be shortened to:

const hasFirstName = user === undefined ? false : isEmpty(user.firstName);

and I think that's descriptive enough, think about a different use-case. It's not always obvious what the undefined case should return, it's not always going to be false.

const hideWelcomeGreeting = user === undefined ? true : isEmpty(user.firstName);

from remeda.

TkDodo avatar TkDodo commented on May 20, 2024

Your example could be shortened to:

const hasFirstName = user === undefined ? false : isEmpty(user.firstName);

not exactly because the firstName itself is also optional.

Because remeda isn't configurable, we need to make a decision on how strict our types should be.

we always have the option of adding stricter types to the .strict namespace?

from remeda.

eranhirsch avatar eranhirsch commented on May 20, 2024

true, but that doesn't address what should be the default returned value for isEmpty(undefined), is it true or false?

from remeda.

TkDodo avatar TkDodo commented on May 20, 2024

I think it should be true, given that we agree that undefined and null can both be considered empty (for whatever empty means in that terminology 😅 )

from remeda.

eranhirsch avatar eranhirsch commented on May 20, 2024

Facebook's Hack standard library define null as true for strings (and strings only) and doesn't accept nulls for collections. I don't mind going with this...

https://docs.hhvm.com/hsl/reference/function/HH.Lib.C.is_empty/
https://docs.hhvm.com/hsl/reference/function/HH.Lib.Str.is_empty/

from remeda.

TkDodo avatar TkDodo commented on May 20, 2024

yeah I would also not allow null for collection-only methods like flatMap (I think we had a separate discussion about this), but here, were strings are also allowed, I think it would be a better DX.

from remeda.

eranhirsch avatar eranhirsch commented on May 20, 2024

Checkout the #377 that solves this. My only issue is that we will now accept isEmpty(undefined) too, and I couldn't find a way around it so users lose some protection against cases like: isEmpty(obj["typo"])

from remeda.

TkDodo avatar TkDodo commented on May 20, 2024

🎉 This issue has been resolved in version 1.26.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

from remeda.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.