From d930e3f2fa154aaee68a43b1ee13fb902624ffef Mon Sep 17 00:00:00 2001 From: Thomas Geymayer Date: Tue, 23 Dec 2014 14:40:29 +0100 Subject: [PATCH] Nasal: std.Vector: add 'contains' method (by onox). --- Nasal/std/Vector.nas | 9 +++++++++ Nasal/std/Vector.nas.test | 4 ++++ 2 files changed, 13 insertions(+) diff --git a/Nasal/std/Vector.nas b/Nasal/std/Vector.nas index 2b81dbb49..ac93aaa71 100644 --- a/Nasal/std/Vector.nas +++ b/Nasal/std/Vector.nas @@ -120,6 +120,15 @@ var Vector = { die("ValueError: item not in the vector"); }, + contains: func (item) { + # Return true if the vector contains the item, false otherwise + + var err = []; + call(Vector.index, [item], me, err); + + return size(err) == 0; + }, + remove: func (item) { # Remove the first occurrence of the given item. Raises a # ValueError if the item is not present in the vector. diff --git a/Nasal/std/Vector.nas.test b/Nasal/std/Vector.nas.test index 78dc257e3..0f590324d 100644 --- a/Nasal/std/Vector.nas.test +++ b/Nasal/std/Vector.nas.test @@ -108,6 +108,10 @@ REQUIRE_EQUAL(x.size(), 4); # index(): REQUIRE_EQUAL(x.index("c"), 2); +# contains(): +REQUIRE_EQUAL(x.contains("c"), 1); +REQUIRE_EQUAL(x.contains("e"), 0); + # remove(): x.remove("c"); REQUIRE_EQUAL(x.vector, ["a", "b", "d"]);