Skip to content

Commit

Permalink
Merge pull request gcanti#230 from gcanti/fromJSON
Browse files Browse the repository at this point in the history
add support for class constructors, `fromJSON` module
  • Loading branch information
gcanti authored Jul 14, 2016
2 parents fbacefb + 97d03fe commit 6a9234c
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 11 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@
**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.

# 3.2.7

- **Bug Fix**
- add support for class constructors, `fromJSON` module (@gcanti)
- type-check the value returned by a custom reviver, `fromJSON` module (@gcanti)

# v3.2.6

- **Bug Fix**
Expand Down
10 changes: 3 additions & 7 deletions docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -1046,15 +1046,9 @@ import fromJSON from 'tcomb/lib/fromJSON'

const Person = t.struct({
name: t.String,
birthDate: t.Date
birthDate: Date
});

// configure your types
t.Date.fromJSON = function (s) {
t.assert(t.String.is(s));
return new Date(s);
};

const source = {
name: 'Giulio',
birthDate: new Date(1973, 10, 30)
Expand All @@ -1070,6 +1064,8 @@ assert.ok(person instanceof Person); // => true
assert.deepEqual(person, source); // => ok
```

You can add a static `fromJSON: (jsonValue: any) => any` function to your types as a custom reviver.

## The `lib/installTypeFormatter` module

Chrome Dev Tools custom formatter for tcomb types.
Expand Down
7 changes: 6 additions & 1 deletion lib/fromJSON.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ var isNil = require('./isNil');
var getTypeName = require('./getTypeName');
var isObject = require('./isObject');
var isArray = require('./isArray');
var isType = require('./isType');

function fromJSON(value, type) {
if (process.env.NODE_ENV !== 'production') {
Expand All @@ -13,7 +14,11 @@ function fromJSON(value, type) {
}

if (isFunction(type.fromJSON)) {
return type.fromJSON(value);
return type(type.fromJSON(value));
}

if (!isType(type)) {
return value instanceof type ? value : new type(value);
}

var kind = type.meta.kind;
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.6",
"version": "3.2.7",
"description": "Type checking and DDD for JavaScript",
"main": "index.js",
"typings": "index.d.ts",
Expand Down
11 changes: 9 additions & 2 deletions test/fromJSON.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,16 @@ describe('fromJSON', function () {
it('should handle a static fromJSON function attached to the type', function () {
var MyType = t.refinement(t.String, function () { return true; });
MyType.fromJSON = function (s) {
return s.length;
return s + 'ok';
};
assert.equal(fromJSON('aaa', MyType), 3);
assert.equal(fromJSON('aaa', MyType), 'aaaok');
});

it('should handle class constructors', function () {
var expected = '1973-11-30T00:00:00.000Z';
assert.strictEqual(fromJSON('1973-11-30T00:00:00.000Z', Date).toISOString(), expected);
var actual = new RegExp('a');
assert.strictEqual(fromJSON(actual, RegExp), actual);
});

it('should handle maybe', function () {
Expand Down

0 comments on commit 6a9234c

Please sign in to comment.