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

Commit 6fabc3c

Browse files
committed
v0.1.4
- **Internal** + Optimized `integer` type (@chriskjaer idea)
1 parent 3908fd5 commit 6fabc3c

File tree

6 files changed

+45
-25
lines changed

6 files changed

+45
-25
lines changed

CHANGELOG.md

+7-2
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,16 @@
1111
1212
**Note**: Gaps between patch versions are faulty/broken releases.
1313

14+
## v0.1.4
15+
16+
- **Internal**
17+
+ Optimized `integer` type (@chriskjaer idea)
18+
1419
## v0.1.3
1520

1621
- **New Feature**
17-
+ Support format property for string types #6
18-
+ Support integer type #5
22+
+ Support format property for string types #6 (@oliger idea)
23+
+ Support integer type #5 (@chriskjaer idea)
1924
- **Bug Fix**
2025
+ Removed `t.util.format`
2126
- **Internal**

README.md

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Converts a JSON Schema to a [tcomb](https://github.com/gcanti/tcomb) type.
1+
Transforms a JSON Schema to a type [tcomb](https://github.com/gcanti/tcomb) type.
22

33
# API
44

@@ -36,4 +36,8 @@ var TcombType = transform({
3636

3737
## resetFormats(): void
3838

39-
Removes all registered formats.
39+
Removes all registered formats.
40+
41+
```js
42+
transform.resetFormats();
43+
```

index.js

+5-11
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
var t = require('tcomb');
44
var fcomb = require('fcomb');
5+
var util = require('./util');
56

67
var Str = t.Str;
78
var Num = t.Num;
@@ -12,18 +13,11 @@ var subtype = t.subtype;
1213
var enums = t.enums;
1314

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

1917
function and(f, g) {
2018
return f ? fcomb.and(f, g) : g;
2119
}
2220

23-
function isInteger(n) {
24-
return n % 1 === 0;
25-
}
26-
2721
var types = {
2822

2923
string: function (s) {
@@ -60,13 +54,13 @@ var types = {
6054
and(predicate, fcomb.lte(s.maximum));
6155
}
6256
if (s.hasOwnProperty('integer') && s.integer) {
63-
predicate = and(predicate, isInteger);
57+
predicate = and(predicate, util.isInteger);
6458
}
6559
return predicate ? subtype(Num, predicate) : Num;
6660
},
6761

6862
integer: function (s) {
69-
var predicate = isInteger;
63+
var predicate;
7064
if (s.hasOwnProperty('minimum')) {
7165
predicate = s.exclusiveMinimum ?
7266
and(predicate, fcomb.gt(s.minimum)) :
@@ -77,7 +71,7 @@ var types = {
7771
and(predicate, fcomb.lt(s.maximum)) :
7872
and(predicate, fcomb.lte(s.maximum));
7973
}
80-
return predicate ? subtype(Num, predicate) : Num;
74+
return predicate ? subtype(util.Int, predicate) : util.Int;
8175
},
8276

8377
boolean: function (s) {
@@ -122,7 +116,7 @@ var types = {
122116
},
123117

124118
null: function () {
125-
return Null;
119+
return util.Null;
126120
}
127121

128122
};

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "tcomb-json-schema",
3-
"version": "0.1.3",
4-
"description": "Converts a JSON Schema to tcomb types",
3+
"version": "0.1.4",
4+
"description": "Transforms a JSON Schema to a tcomb type",
55
"main": "index.js",
66
"scripts": {
77
"test": "mocha"

test/test.js

+9-8
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
var assert = require('assert');
33
var t = require('tcomb');
44
var transform = require('../index');
5+
var util = require('../util');
56

67
var Str = t.Str;
78
var Num = t.Num;
@@ -153,10 +154,9 @@ describe('transform', function () {
153154
var Type = transform({
154155
type: 'integer'
155156
});
156-
eq(getKind(Type), 'subtype');
157-
eq(Type.meta.type, Num);
158-
eq(Type.meta.predicate(1), true);
159-
eq(Type.meta.predicate(1.1), false);
157+
ok(Type === util.Int);
158+
eq(Type.is(1), true);
159+
eq(Type.is(1.1), false);
160160
});
161161

162162
it('should handle minimum', function () {
@@ -165,7 +165,7 @@ describe('transform', function () {
165165
minimum: 2
166166
});
167167
eq(getKind(Type), 'subtype');
168-
eq(Type.meta.type, Num);
168+
eq(Type.meta.type, util.Int);
169169
eq(Type.meta.predicate(1), false);
170170
eq(Type.meta.predicate(2), true);
171171
eq(Type.meta.predicate(3), true);
@@ -178,7 +178,7 @@ describe('transform', function () {
178178
exclusiveMinimum: true
179179
});
180180
eq(getKind(Type), 'subtype');
181-
eq(Type.meta.type, Num);
181+
eq(Type.meta.type, util.Int);
182182
eq(Type.meta.predicate(1), false);
183183
eq(Type.meta.predicate(2), false);
184184
eq(Type.meta.predicate(3), true);
@@ -190,7 +190,7 @@ describe('transform', function () {
190190
maximum: 2
191191
});
192192
eq(getKind(Type), 'subtype');
193-
eq(Type.meta.type, Num);
193+
eq(Type.meta.type, util.Int);
194194
eq(Type.meta.predicate(1), true);
195195
eq(Type.meta.predicate(2), true);
196196
eq(Type.meta.predicate(3), false);
@@ -203,7 +203,7 @@ describe('transform', function () {
203203
exclusiveMaximum: true
204204
});
205205
eq(getKind(Type), 'subtype');
206-
eq(Type.meta.type, Num);
206+
eq(Type.meta.type, util.Int);
207207
eq(Type.meta.predicate(1), true);
208208
eq(Type.meta.predicate(2), false);
209209
eq(Type.meta.predicate(3), false);
@@ -213,6 +213,7 @@ describe('transform', function () {
213213

214214
it('should transform a null schema', function () {
215215
var Type = transform({type: 'null'});
216+
ok(Type === util.Null);
216217
ok(Type.is(null));
217218
ko(Type.is(undefined));
218219
ko(Type.is('a'));

util.js

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
'use strict';
2+
3+
var t = require('tcomb');
4+
5+
function isInteger(n) {
6+
return n % 1 === 0;
7+
}
8+
9+
var Null = t.irreducible('Null', function (x) { return x === null; });
10+
var Int = t.irreducible('Int', isInteger);
11+
12+
module.exports = {
13+
isInteger: isInteger,
14+
Null: Null,
15+
Int: Int
16+
};

0 commit comments

Comments
 (0)