From 4f76262a1083fe856412bf2b3a4f0dba8b0ff965 Mon Sep 17 00:00:00 2001 From: NickNaso Date: Mon, 24 Sep 2018 23:25:17 +0200 Subject: [PATCH] doc: some fix on `Napi::Boolean` documentation PR-URL: https://github.com/nodejs/node-addon-api/pull/354 Reviewed-By: Michael Dawson --- doc/boolean.md | 53 ++++++++++++++++++++++++++++++------- test/basic_types/boolean.cc | 19 ++++++++++++- test/basic_types/boolean.js | 16 +++++++++++ 3 files changed, 78 insertions(+), 10 deletions(-) diff --git a/doc/boolean.md b/doc/boolean.md index 2886b1d0c..01b6a4c0a 100644 --- a/doc/boolean.md +++ b/doc/boolean.md @@ -1,29 +1,64 @@ # Boolean -# Methods +`Napi::Boolean` class is a representation of the JavaScript `Boolean` object. The +`Napi::Boolean` class inherits its behavior from the `Napi::Value` class +(for more info see: [`Napi::Value`](value.md)). + +## Methods ### Constructor +Creates a new empty instance of an `Napi::Boolean` object. + ```cpp -Napi::Boolean::New(Napi::Env env, bool value); +Napi::Boolean::Boolean(); ``` - - `[in] env`: The `napi_env` Environment - - `[in] value`: The Javascript boolean value + +Returns a new _empty_ `Napi::Boolean` object. + +### Contructor + +Creates a new instance of the `Napi::Boolean` object. ```cpp -Napi::Boolean::Boolean(); +Napi::Boolean(napi_env env, napi_value value); ``` -returns a new empty Javascript Boolean value type. -### operator bool -Converts a `Napi::Boolean` value to a boolean primitive. +- `[in] env`: The `napi_env` environment in which to construct the `Napi::Boolean` object. +- `[in] value`: The `napi_value` which is a handle for a JavaScript `Boolean`. + +Returns a non-empty `Napi::Boolean` object. + +### New + +Initializes a new instance of the `Napi::Boolean` object. + ```cpp -Napi::Boolean::operator bool() const; +Napi::Boolean Napi::Boolean::New(napi_env env, bool value); ``` +- `[in] env`: The `napi_env` environment in which to construct the `Napi::Boolean` object. +- `[in] value`: The primitive boolean value (`true` or `false`). + +Returns a new instance of the `Napi::Boolean` object. ### Value + Converts a `Napi::Boolean` value to a boolean primitive. ```cpp bool Napi::Boolean::Value() const; ``` + +Returns the boolean primitive type of the corresponding `Napi::Boolean` object. + +## Operators + +### operator bool + +Converts a `Napi::Boolean` value to a boolean primitive. + +```cpp +Napi::Boolean::operator bool() const; +``` + +Returns the boolean primitive type of the corresponding `Napi::Boolean` object. diff --git a/test/basic_types/boolean.cc b/test/basic_types/boolean.cc index c874845eb..900438f62 100644 --- a/test/basic_types/boolean.cc +++ b/test/basic_types/boolean.cc @@ -6,10 +6,27 @@ Value CreateBoolean(const CallbackInfo& info) { return Boolean::New(info.Env(), info[0].As().Value()); } +Value CreateEmptyBoolean(const CallbackInfo& info) { + Boolean* boolean = new Boolean(); + return Boolean::New(info.Env(), boolean->IsEmpty()); +} + +Value CreateBooleanFromExistingValue(const CallbackInfo& info) { + Boolean* boolean = new Boolean(info.Env(), info[0].As()); + return Boolean::New(info.Env(), boolean->Value()); +} + +Value CreateBooleanFromPrimitive(const CallbackInfo& info) { + bool boolean = info[0].As(); + return Boolean::New(info.Env(), boolean); +} + Object InitBasicTypesBoolean(Env env) { Object exports = Object::New(env); exports["createBoolean"] = Function::New(env, CreateBoolean); - + exports["createEmptyBoolean"] = Function::New(env, CreateEmptyBoolean); + exports["createBooleanFromExistingValue"] = Function::New(env, CreateBooleanFromExistingValue); + exports["createBooleanFromPrimitive"] = Function::New(env, CreateBooleanFromPrimitive); return exports; } diff --git a/test/basic_types/boolean.js b/test/basic_types/boolean.js index 3a9c88da8..1c27664f1 100644 --- a/test/basic_types/boolean.js +++ b/test/basic_types/boolean.js @@ -11,4 +11,20 @@ function test(binding) { const bool2 = binding.basic_types_boolean.createBoolean(false); assert.strictEqual(bool2, false); + + const emptyBoolean = binding.basic_types_boolean.createEmptyBoolean(); + assert.strictEqual(emptyBoolean, true); + + const bool3 = binding.basic_types_boolean.createBooleanFromExistingValue(true); + assert.strictEqual(bool3, true); + + const bool4 = binding.basic_types_boolean.createBooleanFromExistingValue(false); + assert.strictEqual(bool4, false); + + const bool5 = binding.basic_types_boolean.createBooleanFromPrimitive(true); + assert.strictEqual(bool5, true); + + const bool6 = binding.basic_types_boolean.createBooleanFromPrimitive(false); + assert.strictEqual(bool6, false); + }