Repository: gcanti/tcomb
Pull request #213 (2016-06-06 11:13:47)

fix missing path argument in FuncType


Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 05ef5ab..dbef166 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -12,6 +12,13 @@
 **Note**: Gaps between patch versions are faulty/broken releases.
 **Note**: A feature tagged as Experimental is in a high state of flux, you're at risk of it changing without notice.
 
+# v3.2.1
+
+- **Bug Fix**
+  - fix missing path argument in FuncType
+- **Polish**
+  - better stringify serialization for functions
+
 # v3.2.0
 
 - **New Feature**
diff --git a/lib/func.js b/lib/func.js
index 67dc105..10e0f02 100644
--- a/lib/func.js
+++ b/lib/func.js
@@ -48,14 +48,15 @@ function func(domain, codomain, name) {
   var domainLength = domain.length;
   var optionalArgumentsIndex = getOptionalArgumentsIndex(domain);
 
-  function FuncType(value, curried) {
+  function FuncType(value, path) {
 
     if (!isInstrumented(value)) { // automatically instrument the function
-      return FuncType.of(value, curried);
+      return FuncType.of(value);
     }
 
     if (process.env.NODE_ENV !== 'production') {
-      assert(FuncType.is(value), function () { return 'Invalid value ' + assert.stringify(value) + ' supplied to ' + displayName; });
+      path = path || [displayName];
+      assert(FuncType.is(value), function () { return 'Invalid value ' + assert.stringify(value) + ' supplied to ' + path.join('/'); });
     }
 
     return value;
diff --git a/lib/stringify.js b/lib/stringify.js
index 3b2b3c6..6085ec9 100644
--- a/lib/stringify.js
+++ b/lib/stringify.js
@@ -1,6 +1,15 @@
+var getFunctionName = require('./getFunctionName');
+
+function replacer(key, value) {
+  if (typeof value === 'function') {
+    return getFunctionName(value);
+  }
+  return value;
+}
+
 module.exports = function stringify(x) {
   try { // handle "Converting circular structure to JSON" error
-    return JSON.stringify(x, null, 2);
+    return JSON.stringify(x, replacer, 2);
   }
   catch (e) {
     return String(x);
diff --git a/package.json b/package.json
index ecc2e90..4fe4e21 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
 {
   "name": "tcomb",
-  "version": "3.2.0",
+  "version": "3.2.1",
   "description": "Type checking and DDD for JavaScript",
   "main": "index.js",
   "typings": "index.d.ts",


Test patch:
diff --git a/test/declare.js b/test/declare.js
index 99c0b38..1dbe44f 100644
--- a/test/declare.js
+++ b/test/declare.js
@@ -92,7 +92,7 @@ describe('t.declare([name])', function () {
       throwsWithMessage(function () {
         var ANum = t.declare();
         ANum.define(t.Number);
-      }, '[tcomb] Invalid argument type undefined supplied to define(type) (expected a fresh, unnamed type)');
+      }, '[tcomb] Invalid argument type "Number" supplied to define(type) (expected a fresh, unnamed type)');
     });
 
     it('should play well with identity', function () {
diff --git a/test/func.js b/test/func.js
index 388721f..7c3998b 100644
--- a/test/func.js
+++ b/test/func.js
@@ -24,6 +24,15 @@ describe('t.func(domain, codomain, [name])', function () {
     assert.ok(T.is(T(f)));
   });
 
+  it('should throw if arguments are wrong', function () {
+    var T1 = t.func(t.Number, t.Number);
+    var T2 = t.func(t.Number, t.String);
+    var f = T2.of(function () { return 'hi'; });
+    util.throwsWithMessage(function () {
+      T1(f);
+    }, '[tcomb] Invalid value "<function0>" supplied to (Number) => Number');
+  });
+
   describe('of', function () {
 
     it('should check the arguments', function () {
@@ -118,7 +127,7 @@ describe('t.func(domain, codomain, [name])', function () {
 
     it('should throw if no arguments are passed in', function () {
       var Type = t.func([t.Number, t.Number, t.Number], t.Number);
-      var sum = Type(function (a, b, c) {
+      var sum = Type.of(function (a, b, c) {
         return a + b + c;
       }, true);
       util.throwsWithMessage(function () {
@@ -128,7 +137,7 @@ describe('t.func(domain, codomain, [name])', function () {
 
     it('should curry functions', function () {
       var Type = t.func([t.Number, t.Number, t.Number], t.Number);
-      var sum = Type(function (a, b, c) {
+      var sum = Type.of(function (a, b, c) {
         return a + b + c;
       }, true);
       assert.deepEqual(sum(1, 2, 3), 6);