Implement the Array#any and Array#all protoype functions

This commit is contained in:
Gunnar Beutner 2017-05-15 15:59:44 +02:00
parent 1b77f4b336
commit dc9f129060
2 changed files with 57 additions and 0 deletions

View File

@ -1148,6 +1148,24 @@ Signature:
Returns a copy of the array containing only the elements for which `func(element)` Returns a copy of the array containing only the elements for which `func(element)`
is true. is true.
### <a id="array-any"></a> Array#any
Signature:
function any(func);
Returns true if the array contains at least one element for which `func(element)`
is true, false otherwise.
### <a id="array-all"></a> Array#all
Signature:
function all(func);
Returns true if the array contains only elements for which `func(element)`
is true, false otherwise.
### <a id="array-unique"></a> Array#unique ### <a id="array-unique"></a> Array#unique
Signature: Signature:

View File

@ -207,6 +207,43 @@ static Array::Ptr ArrayFilter(const Function::Ptr& function)
return result; return result;
} }
static bool ArrayAny(const Function::Ptr& function)
{
ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
Array::Ptr self = static_cast<Array::Ptr>(vframe->Self);
if (vframe->Sandboxed && !function->IsSideEffectFree())
BOOST_THROW_EXCEPTION(ScriptError("Filter function must be side-effect free."));
ObjectLock olock(self);
for (const Value& item : self) {
std::vector<Value> args;
args.push_back(item);
if (function->Invoke(args))
return true;
}
return false;
}
static bool ArrayAll(const Function::Ptr& function)
{
ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
Array::Ptr self = static_cast<Array::Ptr>(vframe->Self);
if (vframe->Sandboxed && !function->IsSideEffectFree())
BOOST_THROW_EXCEPTION(ScriptError("Filter function must be side-effect free."));
ObjectLock olock(self);
for (const Value& item : self) {
std::vector<Value> args;
args.push_back(item);
if (!function->Invoke(args))
return false;
}
return true;
}
static Array::Ptr ArrayUnique(void) static Array::Ptr ArrayUnique(void)
{ {
ScriptFrame *vframe = ScriptFrame::GetCurrentFrame(); ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
@ -242,6 +279,8 @@ Object::Ptr Array::GetPrototype(void)
prototype->Set("map", new Function("Array#map", WrapFunction(ArrayMap), { "func" }, true)); prototype->Set("map", new Function("Array#map", WrapFunction(ArrayMap), { "func" }, true));
prototype->Set("reduce", new Function("Array#reduce", WrapFunction(ArrayReduce), { "reduce" }, true)); prototype->Set("reduce", new Function("Array#reduce", WrapFunction(ArrayReduce), { "reduce" }, true));
prototype->Set("filter", new Function("Array#filter", WrapFunction(ArrayFilter), { "func" }, true)); prototype->Set("filter", new Function("Array#filter", WrapFunction(ArrayFilter), { "func" }, true));
prototype->Set("any", new Function("Array#any", WrapFunction(ArrayAny), { "func" }, true));
prototype->Set("all", new Function("Array#all", WrapFunction(ArrayAll), { "func" }, true));
prototype->Set("unique", new Function("Array#unique", WrapFunction(ArrayUnique), {}, true)); prototype->Set("unique", new Function("Array#unique", WrapFunction(ArrayUnique), {}, true));
} }