Skip to content

Commit

Permalink
prevent bugs when enums are defined through t.declare
Browse files Browse the repository at this point in the history
  • Loading branch information
gcanti committed Jul 9, 2016
1 parent d3314e8 commit bfac713
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 5 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
**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.5

- **Polish**
- prevent bugs when enums are defined through `t.declare` (@gcanti)

# v3.2.4

- **Polish**
Expand Down
2 changes: 1 addition & 1 deletion lib/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ var stringify = require('./stringify');
// creates an instance of a type, handling the optional new operator
module.exports = function create(type, value, path) {
if (isType(type)) {
return type.meta.identity ? type(value, path): new type(value, path);
return !type.meta.identity && typeof value === 'object' ? new type(value, path): type(value, path);
}

if (process.env.NODE_ENV !== 'production') {
Expand Down
3 changes: 1 addition & 2 deletions lib/declare.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ module.exports = function declare(name) {
type.displayName = Declare.displayName = name;
Declare.meta.name = name;
}
// ensure identity is still false
Declare.meta.identity = false;
Declare.meta.identity = type.meta.identity;
Declare.prototype = type.prototype;
return Declare;
};
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tcomb",
"version": "3.2.4",
"version": "3.2.5",
"description": "Type checking and DDD for JavaScript",
"main": "index.js",
"typings": "index.d.ts",
Expand Down
13 changes: 12 additions & 1 deletion test/declare.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
var assert = require('assert');
var t = require('../index');
var throwsWithMessage = require('./util').throwsWithMessage;
var create = require('../lib/create');

var A = t.declare('A');

Expand Down Expand Up @@ -101,7 +102,7 @@ describe('t.declare([name])', function () {
assert.equal(Tuple.meta && Tuple.meta.identity, false);
assert.equal(Result.meta && Result.meta.identity, false);
Tuple.define(t.tuple([t.String]));
assert.equal(Tuple.meta && Tuple.meta.identity, false);
assert.equal(Tuple.meta && Tuple.meta.identity, true);
assert.equal(Result.meta && Result.meta.identity, false);

Tuple = t.declare('Tuple');
Expand All @@ -113,6 +114,16 @@ describe('t.declare([name])', function () {
assert.equal(Result.meta && Result.meta.identity, false);
});

it('should play well with enums', function () {
var A = t.declare('A');
A.define(t.enums.of(['a']));
assert.strictEqual(create(A, 'a'), 'a');

var B = t.declare('B');
B.define(t.refinement(t.String, function () { return true; }));
assert.strictEqual(create(B, 'a string'), 'a string');
});

});

describe('constructor', function () {
Expand Down

0 comments on commit bfac713

Please sign in to comment.