]> granicus.if.org Git - icinga2/commitdiff
Implement the Array#any and Array#all protoype functions
authorGunnar Beutner <gunnar.beutner@icinga.com>
Mon, 15 May 2017 13:59:44 +0000 (15:59 +0200)
committerGunnar Beutner <gunnar.beutner@icinga.com>
Mon, 15 May 2017 13:59:44 +0000 (15:59 +0200)
doc/18-library-reference.md
lib/base/array-script.cpp

index 9d8cbc0ca36677726c9659cace549dc816f5d1a4..2dd41d69f7b68c3d49fba2d4f36f45de0f61b20b 100644 (file)
@@ -1148,6 +1148,24 @@ Signature:
 Returns a copy of the array containing only the elements for which `func(element)`
 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
 
 Signature:
index 03ae2cae8d83ae2a6ea9e0b148c8d95993d57a94..568037d9ed9f3142866226e38420866d9ad3e9e0 100644 (file)
@@ -207,6 +207,43 @@ static Array::Ptr ArrayFilter(const Function::Ptr& function)
        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)
 {
        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("reduce", new Function("Array#reduce", WrapFunction(ArrayReduce), { "reduce" }, 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));
        }