Comparing version 1.0.2 to 1.0.3
@@ -16,2 +16,7 @@ # Changelog | ||
# 1.0.3 | ||
* **Internal** | ||
* optimizations, https://github.com/gcanti/io-ts/pull/134 (@gcanti, @sledorze) | ||
# 1.0.2 | ||
@@ -18,0 +23,0 @@ |
162
lib/index.js
@@ -46,3 +46,11 @@ "use strict"; | ||
var _this = this; | ||
return new Type(name || "pipe(" + this.name + ", " + ab.name + ")", ab.is, function (i, c) { return _this.validate(i, c).chain(function (a) { return ab.validate(a, c); }); }, this.encode === exports.identity && ab.encode === exports.identity ? exports.identity : function (b) { return _this.encode(ab.encode(b)); }); | ||
return new Type(name || "pipe(" + this.name + ", " + ab.name + ")", ab.is, function (i, c) { | ||
var validation = _this.validate(i, c); | ||
if (validation.isLeft()) { | ||
return validation; | ||
} | ||
else { | ||
return ab.validate(validation.value, c); | ||
} | ||
}, this.encode === exports.identity && ab.encode === exports.identity ? exports.identity : function (b) { return _this.encode(ab.encode(b)); }); | ||
}; | ||
@@ -83,3 +91,8 @@ Type.prototype.asDecoder = function () { | ||
exports.success = function (value) { return new Either_1.Right(value); }; | ||
var pushAll = function (xs, ys) { return Array.prototype.push.apply(xs, ys); }; | ||
var pushAll = function (xs, ys) { | ||
var l = ys.length; | ||
for (var i = 0; i < l; i++) { | ||
xs.push(ys[i]); | ||
} | ||
}; | ||
// | ||
@@ -232,3 +245,12 @@ // basic types | ||
if (name === void 0) { name = "(" + type.name + " | " + exports.getFunctionName(predicate) + ")"; } | ||
return new RefinementType(name, function (m) { return type.is(m) && predicate(m); }, function (i, c) { return type.validate(i, c).chain(function (a) { return (predicate(a) ? exports.success(a) : exports.failure(a, c)); }); }, type.encode, type, predicate); | ||
return new RefinementType(name, function (m) { return type.is(m) && predicate(m); }, function (i, c) { | ||
var validation = type.validate(i, c); | ||
if (validation.isLeft()) { | ||
return validation; | ||
} | ||
else { | ||
var a = validation.value; | ||
return predicate(a) ? exports.success(a) : exports.failure(a, c); | ||
} | ||
}, type.encode, type, predicate); | ||
}; | ||
@@ -323,10 +345,19 @@ exports.Integer = exports.refinement(exports.number, function (n) { return n % 1 === 0; }, 'Integer'); | ||
return new ArrayType(name, function (m) { return arrayType.is(m) && m.every(type.is); }, function (m, c) { | ||
return arrayType.validate(m, c).chain(function (xs) { | ||
var arrayValidation = arrayType.validate(m, c); | ||
if (arrayValidation.isLeft()) { | ||
return arrayValidation; | ||
} | ||
else { | ||
var xs = arrayValidation.value; | ||
var len = xs.length; | ||
var a = xs; | ||
var errors = []; | ||
var _loop_1 = function (i) { | ||
for (var i = 0; i < len; i++) { | ||
var x = xs[i]; | ||
var validation = type.validate(x, exports.appendContext(c, String(i), type)); | ||
validation.fold(function (e) { return pushAll(errors, e); }, function (vx) { | ||
if (validation.isLeft()) { | ||
pushAll(errors, validation.value); | ||
} | ||
else { | ||
var vx = validation.value; | ||
if (vx !== x) { | ||
@@ -338,9 +369,6 @@ if (a === xs) { | ||
} | ||
}); | ||
}; | ||
for (var i = 0; i < len; i++) { | ||
_loop_1(i); | ||
} | ||
} | ||
return errors.length ? exports.failures(errors) : exports.success(a); | ||
}); | ||
} | ||
}, type.encode === exports.identity ? exports.identity : function (a) { return a.map(type.encode); }, type); | ||
@@ -389,10 +417,19 @@ }; | ||
}, function (m, c) { | ||
return exports.Dictionary.validate(m, c).chain(function (o) { | ||
var dictionaryValidation = exports.Dictionary.validate(m, c); | ||
if (dictionaryValidation.isLeft()) { | ||
return dictionaryValidation; | ||
} | ||
else { | ||
var o = dictionaryValidation.value; | ||
var a = o; | ||
var errors = []; | ||
var _loop_2 = function (k) { | ||
for (var k in props) { | ||
var ok = o[k]; | ||
var type_1 = props[k]; | ||
var validation = type_1.validate(ok, exports.appendContext(c, k, type_1)); | ||
validation.fold(function (e) { return pushAll(errors, e); }, function (vok) { | ||
if (validation.isLeft()) { | ||
pushAll(errors, validation.value); | ||
} | ||
else { | ||
var vok = validation.value; | ||
if (vok !== ok) { | ||
@@ -404,9 +441,6 @@ if (a === o) { | ||
} | ||
}); | ||
}; | ||
for (var k in props) { | ||
_loop_2(k); | ||
} | ||
} | ||
return errors.length ? exports.failures(errors) : exports.success(a); | ||
}); | ||
} | ||
}, useIdentity(props) | ||
@@ -477,24 +511,34 @@ ? exports.identity | ||
}, function (m, c) { | ||
return exports.Dictionary.validate(m, c).chain(function (o) { | ||
var dictionaryValidation = exports.Dictionary.validate(m, c); | ||
if (dictionaryValidation.isLeft()) { | ||
return dictionaryValidation; | ||
} | ||
else { | ||
var o = dictionaryValidation.value; | ||
var a = {}; | ||
var errors = []; | ||
var changed = false; | ||
var _loop_3 = function (k) { | ||
for (var k in o) { | ||
var ok = o[k]; | ||
var domainValidation = domain.validate(k, exports.appendContext(c, k, domain)); | ||
var codomainValidation = codomain.validate(ok, exports.appendContext(c, k, codomain)); | ||
domainValidation.fold(function (e) { return pushAll(errors, e); }, function (vk) { | ||
if (domainValidation.isLeft()) { | ||
pushAll(errors, domainValidation.value); | ||
} | ||
else { | ||
var vk = domainValidation.value; | ||
changed = changed || vk !== k; | ||
k = vk; | ||
}); | ||
codomainValidation.fold(function (e) { return pushAll(errors, e); }, function (vok) { | ||
} | ||
if (codomainValidation.isLeft()) { | ||
pushAll(errors, codomainValidation.value); | ||
} | ||
else { | ||
var vok = codomainValidation.value; | ||
changed = changed || vok !== ok; | ||
a[k] = vok; | ||
}); | ||
}; | ||
for (var k in o) { | ||
_loop_3(k); | ||
} | ||
} | ||
return errors.length ? exports.failures(errors) : exports.success((changed ? a : o)); | ||
}); | ||
} | ||
}, domain.encode === exports.identity && codomain.encode === exports.identity | ||
@@ -576,3 +620,8 @@ ? exports.identity | ||
var validation = type_4.validate(a, c); | ||
validation.fold(function (e) { return pushAll(errors, e); }, function (va) { return (a = va); }); | ||
if (validation.isLeft()) { | ||
pushAll(errors, validation.value); | ||
} | ||
else { | ||
a = validation.value; | ||
} | ||
} | ||
@@ -610,10 +659,19 @@ return errors.length ? exports.failures(errors) : exports.success(a); | ||
return new TupleType(name, function (m) { return arrayType.is(m) && m.length === len && types.every(function (type, i) { return type.is(m[i]); }); }, function (m, c) { | ||
return arrayType.validate(m, c).chain(function (as) { | ||
var arrayValidation = arrayType.validate(m, c); | ||
if (arrayValidation.isLeft()) { | ||
return arrayValidation; | ||
} | ||
else { | ||
var as = arrayValidation.value; | ||
var t = as; | ||
var errors = []; | ||
var _loop_4 = function (i) { | ||
for (var i = 0; i < len; i++) { | ||
var a = as[i]; | ||
var type_6 = types[i]; | ||
var validation = type_6.validate(a, exports.appendContext(c, String(i), type_6)); | ||
validation.fold(function (e) { return pushAll(errors, e); }, function (va) { | ||
if (validation.isLeft()) { | ||
pushAll(errors, validation.value); | ||
} | ||
else { | ||
var va = validation.value; | ||
if (va !== a) { | ||
@@ -625,6 +683,3 @@ if (t === as) { | ||
} | ||
}); | ||
}; | ||
for (var i = 0; i < len; i++) { | ||
_loop_4(i); | ||
} | ||
} | ||
@@ -635,3 +690,3 @@ if (as.length > len) { | ||
return errors.length ? exports.failures(errors) : exports.success(t); | ||
}); | ||
} | ||
}, types.every(function (type) { return type.encode === exports.identity; }) ? exports.identity : function (a) { return types.map(function (type, i) { return type.encode(a[i]); }); }, types); | ||
@@ -712,3 +767,8 @@ } | ||
return new StrictType(name, function (m) { return loose.is(m) && Object.getOwnPropertyNames(m).every(function (k) { return props.hasOwnProperty(k); }); }, function (m, c) { | ||
return loose.validate(m, c).chain(function (o) { | ||
var looseValidation = loose.validate(m, c); | ||
if (looseValidation.isLeft()) { | ||
return looseValidation; | ||
} | ||
else { | ||
var o = looseValidation.value; | ||
var keys = Object.getOwnPropertyNames(o); | ||
@@ -724,3 +784,3 @@ var len = keys.length; | ||
return errors.length ? exports.failures(errors) : exports.success(o); | ||
}); | ||
} | ||
}, loose.encode, props); | ||
@@ -795,11 +855,21 @@ }; | ||
}, function (s, c) { | ||
return exports.Dictionary.validate(s, c).chain(function (d) { | ||
return TagValue.validate(d[tag], exports.appendContext(c, tag, TagValue)).chain(function (tagValue) { | ||
var dictionaryValidation = exports.Dictionary.validate(s, c); | ||
if (dictionaryValidation.isLeft()) { | ||
return dictionaryValidation; | ||
} | ||
else { | ||
var d = dictionaryValidation.value; | ||
var tagValueValidation = TagValue.validate(d[tag], exports.appendContext(c, tag, TagValue)); | ||
if (tagValueValidation.isLeft()) { | ||
return tagValueValidation; | ||
} | ||
else { | ||
var tagValue = tagValueValidation.value; | ||
var i = tagValue2Index[tagValue]; | ||
var type = types[i]; | ||
return type.validate(d, exports.appendContext(c, String(i), type)); | ||
}); | ||
}); | ||
var type_8 = types[i]; | ||
return type_8.validate(d, exports.appendContext(c, String(i), type_8)); | ||
} | ||
} | ||
}, types.every(function (type) { return type.encode === exports.identity; }) ? exports.identity : function (a) { return types[tagValue2Index[a[tag]]].encode(a); }, types); | ||
}; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "io-ts", | ||
"version": "1.0.2", | ||
"version": "1.0.3", | ||
"description": "TypeScript compatible runtime type system for IO validation", | ||
@@ -12,3 +12,3 @@ "files": ["lib"], | ||
"typings-checker --allow-expect-error --project typings-checker/tsconfig.json typings-checker/index.ts", | ||
"mocha": "TS_NODE_CACHE=false mocha -r ts-node/register test/*.ts", | ||
"mocha": "TS_NODE_CACHE=false TS_NODE_PROJECT=test/tsconfig.json mocha -r ts-node/register test/*.ts", | ||
"prettier": | ||
@@ -15,0 +15,0 @@ "prettier --no-semi --single-quote --print-width 120 --parser typescript --list-different \"{src,test}/**/*.ts\"", |
@@ -390,9 +390,10 @@ [![build status](https://img.shields.io/travis/gcanti/io-ts/master.svg?style=flat-square)](https://travis-ci.org/gcanti/io-ts) | ||
Due to an upstream [bug](https://github.com/Microsoft/TypeScript/issues/14041), VS Code might display weird types for | ||
nested interfaces | ||
Due to an upstream [bug](https://github.com/Microsoft/TypeScript/issues/14041), VS Code might display `any` for nested | ||
types | ||
```ts | ||
const NestedInterface = t.type({ | ||
foo: t.type({ | ||
bar: t.string | ||
foo: t.string, | ||
bar: t.type({ | ||
baz: t.string | ||
}) | ||
@@ -406,5 +407,4 @@ }) | ||
type NestedInterfaceType = { | ||
foo: t.InterfaceOf<{ | ||
bar: t.StringType; | ||
}>; | ||
foo: string; | ||
bar: any; | ||
} | ||
@@ -415,7 +415,8 @@ | ||
type NestedInterfaceType = { | ||
foo: { | ||
bar: string; | ||
}; | ||
foo: string; | ||
bar: { | ||
baz: string | ||
} | ||
} | ||
*/ | ||
``` |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
106015
1187
420