Skip to content

Commit 4f76262

Browse files
NickNasomhdawson
authored andcommitted
doc: some fix on Napi::Boolean documentation
PR-URL: #354 Reviewed-By: Michael Dawson <[email protected]>
1 parent 78374f7 commit 4f76262

File tree

3 files changed

+78
-10
lines changed

3 files changed

+78
-10
lines changed

doc/boolean.md

Lines changed: 44 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,64 @@
11
# Boolean
22

3-
# Methods
3+
`Napi::Boolean` class is a representation of the JavaScript `Boolean` object. The
4+
`Napi::Boolean` class inherits its behavior from the `Napi::Value` class
5+
(for more info see: [`Napi::Value`](value.md)).
6+
7+
## Methods
48

59
### Constructor
610

11+
Creates a new empty instance of an `Napi::Boolean` object.
12+
713
```cpp
8-
Napi::Boolean::New(Napi::Env env, bool value);
14+
Napi::Boolean::Boolean();
915
```
10-
- `[in] env`: The `napi_env` Environment
11-
- `[in] value`: The Javascript boolean value
16+
17+
Returns a new _empty_ `Napi::Boolean` object.
18+
19+
### Contructor
20+
21+
Creates a new instance of the `Napi::Boolean` object.
1222

1323
```cpp
14-
Napi::Boolean::Boolean();
24+
Napi::Boolean(napi_env env, napi_value value);
1525
```
16-
returns a new empty Javascript Boolean value type.
1726
18-
### operator bool
19-
Converts a `Napi::Boolean` value to a boolean primitive.
27+
- `[in] env`: The `napi_env` environment in which to construct the `Napi::Boolean` object.
28+
- `[in] value`: The `napi_value` which is a handle for a JavaScript `Boolean`.
29+
30+
Returns a non-empty `Napi::Boolean` object.
31+
32+
### New
33+
34+
Initializes a new instance of the `Napi::Boolean` object.
35+
2036
```cpp
21-
Napi::Boolean::operator bool() const;
37+
Napi::Boolean Napi::Boolean::New(napi_env env, bool value);
2238
```
39+
- `[in] env`: The `napi_env` environment in which to construct the `Napi::Boolean` object.
40+
- `[in] value`: The primitive boolean value (`true` or `false`).
41+
42+
Returns a new instance of the `Napi::Boolean` object.
2343

2444
### Value
45+
2546
Converts a `Napi::Boolean` value to a boolean primitive.
2647

2748
```cpp
2849
bool Napi::Boolean::Value() const;
2950
```
51+
52+
Returns the boolean primitive type of the corresponding `Napi::Boolean` object.
53+
54+
## Operators
55+
56+
### operator bool
57+
58+
Converts a `Napi::Boolean` value to a boolean primitive.
59+
60+
```cpp
61+
Napi::Boolean::operator bool() const;
62+
```
63+
64+
Returns the boolean primitive type of the corresponding `Napi::Boolean` object.

test/basic_types/boolean.cc

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,27 @@ Value CreateBoolean(const CallbackInfo& info) {
66
return Boolean::New(info.Env(), info[0].As<Boolean>().Value());
77
}
88

9+
Value CreateEmptyBoolean(const CallbackInfo& info) {
10+
Boolean* boolean = new Boolean();
11+
return Boolean::New(info.Env(), boolean->IsEmpty());
12+
}
13+
14+
Value CreateBooleanFromExistingValue(const CallbackInfo& info) {
15+
Boolean* boolean = new Boolean(info.Env(), info[0].As<Boolean>());
16+
return Boolean::New(info.Env(), boolean->Value());
17+
}
18+
19+
Value CreateBooleanFromPrimitive(const CallbackInfo& info) {
20+
bool boolean = info[0].As<Boolean>();
21+
return Boolean::New(info.Env(), boolean);
22+
}
23+
924
Object InitBasicTypesBoolean(Env env) {
1025
Object exports = Object::New(env);
1126

1227
exports["createBoolean"] = Function::New(env, CreateBoolean);
13-
28+
exports["createEmptyBoolean"] = Function::New(env, CreateEmptyBoolean);
29+
exports["createBooleanFromExistingValue"] = Function::New(env, CreateBooleanFromExistingValue);
30+
exports["createBooleanFromPrimitive"] = Function::New(env, CreateBooleanFromPrimitive);
1431
return exports;
1532
}

test/basic_types/boolean.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,20 @@ function test(binding) {
1111

1212
const bool2 = binding.basic_types_boolean.createBoolean(false);
1313
assert.strictEqual(bool2, false);
14+
15+
const emptyBoolean = binding.basic_types_boolean.createEmptyBoolean();
16+
assert.strictEqual(emptyBoolean, true);
17+
18+
const bool3 = binding.basic_types_boolean.createBooleanFromExistingValue(true);
19+
assert.strictEqual(bool3, true);
20+
21+
const bool4 = binding.basic_types_boolean.createBooleanFromExistingValue(false);
22+
assert.strictEqual(bool4, false);
23+
24+
const bool5 = binding.basic_types_boolean.createBooleanFromPrimitive(true);
25+
assert.strictEqual(bool5, true);
26+
27+
const bool6 = binding.basic_types_boolean.createBooleanFromPrimitive(false);
28+
assert.strictEqual(bool6, false);
29+
1430
}

0 commit comments

Comments
 (0)