Skip to content

Commit

Permalink
doc: some fix on Napi::Boolean documentation
Browse files Browse the repository at this point in the history
PR-URL: #354
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
  • Loading branch information
NickNaso authored and mhdawson committed Sep 28, 2018
1 parent 78374f7 commit 4f76262
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 10 deletions.
53 changes: 44 additions & 9 deletions doc/boolean.md
Original file line number Diff line number Diff line change
@@ -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.
19 changes: 18 additions & 1 deletion test/basic_types/boolean.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,27 @@ Value CreateBoolean(const CallbackInfo& info) {
return Boolean::New(info.Env(), info[0].As<Boolean>().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<Boolean>());
return Boolean::New(info.Env(), boolean->Value());
}

Value CreateBooleanFromPrimitive(const CallbackInfo& info) {
bool boolean = info[0].As<Boolean>();
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;
}
16 changes: 16 additions & 0 deletions test/basic_types/boolean.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

}

0 comments on commit 4f76262

Please sign in to comment.