Repository: plaid/sanctuary
Pull request #37 (2015-05-21 23:53:42)

Add indexOf function
As mentioned in #35.

davidchambers: This is great! Have you any interest in defining `S.lastIndexOf` as well?

svozza: Yeah, definitely. Will I add it to this PR or make a new one?
davidchambers: > Will I add it to this PR or make a new one?

That's up to you. I'm happy with either option. :)

svozza: Nice one. Got a better name for the `indexOfToMaybe` function I've added there? Not happy with it at all.
svozza: Did you get my Gitter message btw?
svozza: So I wrote a wrapper function for indexOf methods, I'll add the string ones in a separate PR though.

Patch:
diff --git a/index.js b/index.js
index a5bb346..3b3c1af 100644
--- a/index.js
+++ b/index.js
@@ -343,6 +343,10 @@
     return xBool !== yBool ? or(x, y) : xEmpty;
   });
 
+  var sanctifyIndexOf = function(f) {
+    return R.curry(R.compose(R.ifElse(R.gte(R.__, 0), Just, Nothing), f));
+  };
+
   //  list  //////////////////////////////////////////////////////////////////
 
   //  at :: Number -> [a] -> Maybe a
@@ -374,6 +378,30 @@
     return Nothing();
   });
 
+  //. indexOf :: a -> [a] -> Maybe Number
+  //.
+  //. Returns Just the position of the first occurrence of an
+  //. element in a list, or Nothing if the element does not occur
+  //. in the list.
+  //.
+  //. ```javascript
+  //. > S.indexOf('c', ['a', 'b', 'c', 'd', 'e'])
+  //. Just(2)
+  //. ```
+  S.indexOf = sanctifyIndexOf(R.indexOf);
+
+  //. lastIndexOf :: a -> [a] -> Maybe Number
+  //.
+  //. Returns Just the position of the last occurrence of an
+  //. element in a list, or Nothing if the element does not occur
+  //. in the list.
+  //.
+  //. ```javascript
+  //. > S.lastIndexOf('c', ['a', 'b', 'b', 'c', 'c'])
+  //. Just(4)
+  //. ```
+  S.lastIndexOf = sanctifyIndexOf(R.lastIndexOf);
+
   //. pluck :: String -> [{String: *}] -> [Maybe *]
   //.
   //. Takes a list of objects and plucks the value of the specified key


Test patch:
diff --git a/test/index.js b/test/index.js
index c5198ad..90df157 100644
--- a/test/index.js
+++ b/test/index.js
@@ -1213,6 +1213,40 @@ describe('list', function() {
 
   });
 
+  describe('indexOf', function() {
+
+    it('returns a Nothing for an empty list', function() {
+      assert(S.indexOf(10, []).equals(S.Nothing()));
+    });
+
+    it('returns a Nothing if the element is not found', function() {
+      assert(S.indexOf(10, [1, 2, 3, 4, 5]).equals(S.Nothing()));
+    });
+
+    it('returns Just the index of the element found', function() {
+      assert(S.indexOf(3, [1, 2, 3, 4, 5]).equals(S.Just(2)));
+    });
+
+  });
+
+  describe('lastIndexOf', function() {
+
+    it('returns a Nothing for an empty list', function() {
+      assert(S.lastIndexOf('a', []).equals(S.Nothing()));
+    });
+
+    it('returns a Nothing if the element is not found', function() {
+      assert(S.lastIndexOf('d', ['a', 'b', 'b', 'c', 'c'])
+        .equals(S.Nothing()));
+    });
+
+    it('returns Just the last index of the element found', function() {
+      assert(S.lastIndexOf('c', ['a', 'b', 'b', 'c', 'c'])
+        .equals(S.Just(4)));
+    });
+
+  });
+
   describe('pluck', function() {
 
     it('returns array of Justs for found keys', function() {