-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
39 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#ifndef SRC_ITERATOR_H_ | ||
#define SRC_ITERATOR_H_ | ||
|
||
#include "src/types.h" | ||
|
||
namespace ki { | ||
|
||
template<typename T> | ||
bool IterateArray(napi_env env, napi_value arr, | ||
const std::function<void(uint32_t i, T value)>& visit) { | ||
if (!IsArray(env, arr)) | ||
return false; | ||
uint32_t length; | ||
if (napi_get_array_length(env, arr, &length) != napi_ok) | ||
return false; | ||
for (uint32_t i = 0; i < length; ++i) { | ||
napi_value el = nullptr; | ||
if (napi_get_element(env, arr, i, &el) != napi_ok) | ||
return false; | ||
std::optional<T> out = FromNodeTo<T>(env, el); | ||
if (!out) | ||
return false; | ||
visit(i, *out); | ||
} | ||
return true; | ||
} | ||
|
||
} // namespace ki | ||
|
||
#endif // SRC_ITERATOR_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters