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

feat: add IsNullOrUndefined #909

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions napi-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,10 @@ inline bool Value::IsNull() const {
return Type() == napi_null;
}

inline bool Value::IsNullOrUndefined() const {
return Type() == napi_undefined || Type() == napi_null;
Copy link
Contributor

@KevinEady KevinEady Feb 24, 2021

Choose a reason for hiding this comment

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

The compiler may optimize this since it is const, but let's store the Type() call in a variable or use a switch statement to ensure it isn't called twice.

}

inline bool Value::IsBoolean() const {
return Type() == napi_boolean;
}
Expand Down
3 changes: 2 additions & 1 deletion napi.h
Original file line number Diff line number Diff line change
Expand Up @@ -269,8 +269,9 @@ namespace Napi {

napi_valuetype Type() const; ///< Gets the type of the value.

bool IsUndefined() const; ///< Tests if a value is an undefined JavaScript value.
bool IsNull() const; ///< Tests if a value is a null JavaScript value.
bool IsUndefined() const; ///< Tests if a value is an undefined JavaScript value.
bool IsNullOrUndefined() const; ///< Tests if a value is a null or undefined JavaScript value.
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe IsNullish would be more concise. With the definition of ?? from MDN as (emphasis mine)

The nullish coalescing operator (??) is a logical operator that returns its right-hand side operand when its left-hand side operand is null or undefined, and otherwise returns its left-hand side operand.

Copy link
Author

Choose a reason for hiding this comment

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

New changes have been submitted

bool IsBoolean() const; ///< Tests if a value is a JavaScript boolean.
bool IsNumber() const; ///< Tests if a value is a JavaScript number.
#if NAPI_VERSION > 5
Expand Down