Repository: plaid/sanctuary
Pull request #29 (2015-05-11 22:08:52)

maybe, either: define "toString"
This grants Maybe objects and Either objects the following property:

    eval(R.toString(x)) ≍ x

While this is primarily useful in the REPL, it also makes these values compatible with [`R.memoize`][1].

This change was made possible by ramda/ramda#924 and the publication of Ramda v0.14.0. I love it when a plan comes together. :grinning:


[1]: http://ramdajs.com/docs/#memoize



Patch:
diff --git a/index.js b/index.js
index 27f2f30..0dd832c 100644
--- a/index.js
+++ b/index.js
@@ -110,6 +110,11 @@
     return false;
   };
 
+  //  Nothing#toString :: -> String
+  Nothing.prototype.toString = function() {
+    return 'Nothing()';
+  };
+
   var Just = S.Just = function Just(value) {
     if (!(this instanceof Just)) {
       return new Just(value);
@@ -149,6 +154,11 @@
     return true;
   };
 
+  //  Just#toString :: -> String
+  Just.prototype.toString = function() {
+    return 'Just(' + R.toString(this.value) + ')';
+  };
+
   //  fromMaybe :: a -> Maybe a -> a
   S.fromMaybe = R.curry(function(x, maybe) {
     switch (true) {
@@ -218,6 +228,11 @@
     return this;
   };
 
+  //  Left#toString :: -> String
+  Left.prototype.toString = function() {
+    return 'Left(' + R.toString(this.value) + ')';
+  };
+
   var Right = S.Right = function Right(value) {
     if (!(this instanceof Right)) {
       return new Right(value);
@@ -246,6 +261,11 @@
     return new Right(f(this.value));
   };
 
+  //  Right#toString :: -> String
+  Right.prototype.toString = function() {
+    return 'Right(' + R.toString(this.value) + ')';
+  };
+
   //  either :: (a -> c) -> (b -> c) -> Either a b -> c
   S.either = R.curry(function(l, r, either) {
     switch (true) {
diff --git a/package.json b/package.json
index bb62e99..1a147de 100644
--- a/package.json
+++ b/package.json
@@ -8,7 +8,7 @@
     "url": "git://github.com/plaid/sanctuary.git"
   },
   "dependencies": {
-    "ramda": "0.13.x"
+    "ramda": "0.14.x"
   },
   "devDependencies": {
     "istanbul": "0.3.x",


Test patch:
diff --git a/test/index.js b/test/index.js
index 786f29e..cec738f 100644
--- a/test/index.js
+++ b/test/index.js
@@ -168,6 +168,12 @@ describe('maybe', function() {
       eq(nothing.toBoolean(), false);
     });
 
+    it('provides a "toString" method', function() {
+      var nothing = S.Nothing();
+      eq(nothing.toString.length, 0);
+      eq(nothing.toString(), 'Nothing()');
+    });
+
     it('implements Semigroup', function() {
       var a = S.Nothing();
       var b = S.Nothing();
@@ -336,6 +342,12 @@ describe('maybe', function() {
       eq(just.toBoolean(), true);
     });
 
+    it('provides a "toString" method', function() {
+      var just = S.Just([1, 2, 3]);
+      eq(just.toString.length, 0);
+      eq(just.toString(), 'Just([1, 2, 3])');
+    });
+
     it('implements Semigroup', function() {
       var a = S.Just('foo');
       var b = S.Just('bar');
@@ -584,6 +596,12 @@ describe('either', function() {
       eq(left.map(square), left);
     });
 
+    it('provides a "toString" method', function() {
+      var left = S.Left('Cannot divide by zero');
+      eq(left.toString.length, 0);
+      eq(left.toString(), 'Left("Cannot divide by zero")');
+    });
+
     it('implements Functor', function() {
       var a = S.Left('Cannot divide by zero');
       var f = R.inc;
@@ -701,6 +719,12 @@ describe('either', function() {
       assert(right.map(square).equals(S.Right(1764)));
     });
 
+    it('provides a "toString" method', function() {
+      var right = S.Right([1, 2, 3]);
+      eq(right.toString.length, 0);
+      eq(right.toString(), 'Right([1, 2, 3])');
+    });
+
     it('implements Functor', function() {
       var a = S.Right(7);
       var f = R.inc;