Skip to content
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
Next Next commit
feat: add IsNullOrUndefined
  • Loading branch information
januwA committed Feb 15, 2021
commit a331a7bf18e87a9673b2412003be694d106c14d3
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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