Skip to content
This repository has been archived by the owner on Feb 16, 2021. It is now read-only.

Commit

Permalink
v0.1.4
Browse files Browse the repository at this point in the history
- **Internal**
  + Optimized `integer` type (@chriskjaer idea)
  • Loading branch information
gcanti committed Apr 20, 2015
1 parent 3908fd5 commit 6fabc3c
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 25 deletions.
9 changes: 7 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,16 @@
**Note**: Gaps between patch versions are faulty/broken releases.

## v0.1.4

- **Internal**
+ Optimized `integer` type (@chriskjaer idea)

## v0.1.3

- **New Feature**
+ Support format property for string types #6
+ Support integer type #5
+ Support format property for string types #6 (@oliger idea)
+ Support integer type #5 (@chriskjaer idea)
- **Bug Fix**
+ Removed `t.util.format`
- **Internal**
Expand Down
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Converts a JSON Schema to a [tcomb](https://github.com/gcanti/tcomb) type.
Transforms a JSON Schema to a type [tcomb](https://github.com/gcanti/tcomb) type.

# API

Expand Down Expand Up @@ -36,4 +36,8 @@ var TcombType = transform({

## resetFormats(): void

Removes all registered formats.
Removes all registered formats.

```js
transform.resetFormats();
```
16 changes: 5 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

var t = require('tcomb');
var fcomb = require('fcomb');
var util = require('./util');

var Str = t.Str;
var Num = t.Num;
Expand All @@ -12,18 +13,11 @@ var subtype = t.subtype;
var enums = t.enums;

var SchemaType = enums.of('null string number integer boolean object array', 'SchemaType');
var Null = t.irreducible('Null', function (x) {
return x === null;
});

function and(f, g) {
return f ? fcomb.and(f, g) : g;
}

function isInteger(n) {
return n % 1 === 0;
}

var types = {

string: function (s) {
Expand Down Expand Up @@ -60,13 +54,13 @@ var types = {
and(predicate, fcomb.lte(s.maximum));
}
if (s.hasOwnProperty('integer') && s.integer) {
predicate = and(predicate, isInteger);
predicate = and(predicate, util.isInteger);
}
return predicate ? subtype(Num, predicate) : Num;
},

integer: function (s) {
var predicate = isInteger;
var predicate;
if (s.hasOwnProperty('minimum')) {
predicate = s.exclusiveMinimum ?
and(predicate, fcomb.gt(s.minimum)) :
Expand All @@ -77,7 +71,7 @@ var types = {
and(predicate, fcomb.lt(s.maximum)) :
and(predicate, fcomb.lte(s.maximum));
}
return predicate ? subtype(Num, predicate) : Num;
return predicate ? subtype(util.Int, predicate) : util.Int;
},

boolean: function (s) {
Expand Down Expand Up @@ -122,7 +116,7 @@ var types = {
},

null: function () {
return Null;
return util.Null;
}

};
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "tcomb-json-schema",
"version": "0.1.3",
"description": "Converts a JSON Schema to tcomb types",
"version": "0.1.4",
"description": "Transforms a JSON Schema to a tcomb type",
"main": "index.js",
"scripts": {
"test": "mocha"
Expand Down
17 changes: 9 additions & 8 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
var assert = require('assert');
var t = require('tcomb');
var transform = require('../index');
var util = require('../util');

var Str = t.Str;
var Num = t.Num;
Expand Down Expand Up @@ -153,10 +154,9 @@ describe('transform', function () {
var Type = transform({
type: 'integer'
});
eq(getKind(Type), 'subtype');
eq(Type.meta.type, Num);
eq(Type.meta.predicate(1), true);
eq(Type.meta.predicate(1.1), false);
ok(Type === util.Int);
eq(Type.is(1), true);
eq(Type.is(1.1), false);
});

it('should handle minimum', function () {
Expand All @@ -165,7 +165,7 @@ describe('transform', function () {
minimum: 2
});
eq(getKind(Type), 'subtype');
eq(Type.meta.type, Num);
eq(Type.meta.type, util.Int);
eq(Type.meta.predicate(1), false);
eq(Type.meta.predicate(2), true);
eq(Type.meta.predicate(3), true);
Expand All @@ -178,7 +178,7 @@ describe('transform', function () {
exclusiveMinimum: true
});
eq(getKind(Type), 'subtype');
eq(Type.meta.type, Num);
eq(Type.meta.type, util.Int);
eq(Type.meta.predicate(1), false);
eq(Type.meta.predicate(2), false);
eq(Type.meta.predicate(3), true);
Expand All @@ -190,7 +190,7 @@ describe('transform', function () {
maximum: 2
});
eq(getKind(Type), 'subtype');
eq(Type.meta.type, Num);
eq(Type.meta.type, util.Int);
eq(Type.meta.predicate(1), true);
eq(Type.meta.predicate(2), true);
eq(Type.meta.predicate(3), false);
Expand All @@ -203,7 +203,7 @@ describe('transform', function () {
exclusiveMaximum: true
});
eq(getKind(Type), 'subtype');
eq(Type.meta.type, Num);
eq(Type.meta.type, util.Int);
eq(Type.meta.predicate(1), true);
eq(Type.meta.predicate(2), false);
eq(Type.meta.predicate(3), false);
Expand All @@ -213,6 +213,7 @@ describe('transform', function () {

it('should transform a null schema', function () {
var Type = transform({type: 'null'});
ok(Type === util.Null);
ok(Type.is(null));
ko(Type.is(undefined));
ko(Type.is('a'));
Expand Down
16 changes: 16 additions & 0 deletions util.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
'use strict';

var t = require('tcomb');

function isInteger(n) {
return n % 1 === 0;
}

var Null = t.irreducible('Null', function (x) { return x === null; });
var Int = t.irreducible('Int', isInteger);

module.exports = {
isInteger: isInteger,
Null: Null,
Int: Int
};

2 comments on commit 6fabc3c

@chriskjaer
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for adding this! I never got around to make a pull request.

@gcanti
Copy link
Owner Author

@gcanti gcanti commented on 6fabc3c Apr 20, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks to you for the good ideas!

Giulio

Please sign in to comment.