Skip to content

doc: document object iterators #1090

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

Merged
merged 1 commit into from
Oct 25, 2021
Merged
Changes from all commits
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
98 changes: 98 additions & 0 deletions doc/object.md
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,34 @@ Napi::Value Napi::Object::operator[] (uint32_t index) const;

Returns an indexed property or array element as a [`Napi::Value`](value.md).

### begin()

```cpp
Napi::Object::iterator Napi::Object::begin() const;
```

Returns a constant iterator to the beginning of the object.

```cpp
Napi::Object::iterator Napi::Object::begin();
```

Returns a non constant iterator to the beginning of the object.

### end()

```cpp
Napi::Object::iterator Napi::Object::end() const;
```

Returns a constant iterator to the end of the object.

```cpp
Napi::Object::iterator Napi::Object::end();
```

Returns a non constant iterator to the end of the object.

## Iterator

Iterators expose an `std::pair<...>`, where the `first` property is a
Expand All @@ -300,6 +328,41 @@ exceptions are enabled (by defining `NAPI_CPP_EXCEPTIONS` during the build).

In constant iterators, the iterated values are immutable.

#### operator++()

```cpp
inline Napi::Object::const_iterator& Napi::Object::const_iterator::operator++();
```

Moves the iterator one step forward.

#### operator==

```cpp
inline bool Napi::Object::const_iterator::operator==(const Napi::Object::const_iterator& other) const;
```
- `[in] other`: Another iterator to compare the current iterator to.

Returns whether both iterators are at the same index.

#### operator!=

```cpp
inline bool Napi::Object::const_iterator::operator!=(const Napi::Object::const_iterator& other) const;
```
- `[in] other`: Another iterator to compare the current iterator to.

Returns whether both iterators are at different indices.

#### operator*()

```cpp
inline const std::pair<Napi::Value, Napi::Object::PropertyLValue<Napi::Value>> Napi::Object::const_iterator::operator*() const;
```

Returns the currently iterated key and value.

#### Example
```cpp
Value Sum(const CallbackInfo& info) {
Object object = info[0].As<Object>();
Expand All @@ -317,6 +380,41 @@ Value Sum(const CallbackInfo& info) {

In non constant iterators, the iterated values are mutable.

#### operator++()

```cpp
inline Napi::Object::iterator& Napi::Object::iterator::operator++();
```

Moves the iterator one step forward.

#### operator==

```cpp
inline bool Napi::Object::iterator::operator==(const Napi::Object::iterator& other) const;
```
- `[in] other`: Another iterator to compare the current iterator to.

Returns whether both iterators are at the same index.

#### operator!=

```cpp
inline bool Napi::Object::iterator::operator!=(const Napi::Object::iterator& other) const;
```
- `[in] other`: Another iterator to compare the current iterator to.

Returns whether both iterators are at different indices.

#### operator*()

```cpp
inline std::pair<Napi::Value, Napi::Object::PropertyLValue<Napi::Value>> Napi::Object::iterator::operator*();
```

Returns the currently iterated key and value.

#### Example
```cpp
void Increment(const CallbackInfo& info) {
Env env = info.Env();
Expand Down