| 5 | namespace NHelper { |
| 6 | |
| 7 | bool CheckIsMatrix(Napi::Env env, const Napi::Value& value, ENApiType type, const std::string& errorPrefix) { |
| 8 | if (!Check(env, value.IsArray(), errorPrefix + "is not an array")) { |
| 9 | return false; |
| 10 | } |
| 11 | const Napi::Array matrix = value.As<Napi::Array>(); |
| 12 | const uint32_t rowsCount = matrix.Length(); |
| 13 | if (rowsCount == 0) { |
| 14 | return true; |
| 15 | } |
| 16 | |
| 17 | if (!Check(env, matrix[0u].IsArray(), errorPrefix + "the first element of the matrix is not an array")) { |
| 18 | return false; |
| 19 | } |
| 20 | const uint32_t columnsCount = matrix[0u].As<Napi::Array>().Length(); |
| 21 | size_t numberCount = 0; |
| 22 | size_t strCount = 0; |
| 23 | |
| 24 | std::function<bool(const Napi::Value&)> checkElement; |
| 25 | |
| 26 | switch (type) { |
| 27 | case ENApiType::NAT_NUMBER: |
| 28 | checkElement = [&] (const Napi::Value& value) -> bool { |
| 29 | return Check( |
| 30 | env, |
| 31 | value.IsNumber(), |
| 32 | "non-numeric type in the matrix elements" |
| 33 | ); |
| 34 | }; |
| 35 | break; |
| 36 | case ENApiType::NAT_STRING: |
| 37 | checkElement = [&] (const Napi::Value& value) -> bool { |
| 38 | return Check( |
| 39 | env, |
| 40 | value.IsString(), |
| 41 | "non-string type in the matrix elements" |
| 42 | ); |
| 43 | }; |
| 44 | break; |
| 45 | case ENApiType::NAT_NUMBER_OR_STRING: |
| 46 | checkElement = [&] (const Napi::Value& value) -> bool { |
| 47 | if (value.IsNumber()) { |
| 48 | ++numberCount; |
| 49 | } else if (value.IsString()) { |
| 50 | ++strCount; |
| 51 | } else { |
| 52 | Check(env, false, errorPrefix + "invalid type found: " + std::to_string(value.Type())); |
| 53 | return false; |
| 54 | } |
| 55 | return true; |
| 56 | }; |
| 57 | break; |
| 58 | case ENApiType::NAT_ARRAY_OR_NUMBERS: |
| 59 | checkElement = [&] (const Napi::Value& value) -> bool { |
| 60 | if (!Check( |
| 61 | env, |
| 62 | value.IsArray(), |
| 63 | "the matrix contains non-array elements" |
| 64 | )) |