Comparing version 0.2.4 to 0.3.1
@@ -42,2 +42,3 @@ /** | ||
to: function(v) { | ||
if (_.isUndefined(v)){ return false; } | ||
if(_.isBoolean(v)) return v; | ||
@@ -88,2 +89,6 @@ if(v === 'true') return true; | ||
if (_.isUndefined(v)){ | ||
return ''; | ||
} | ||
if(_.isString(v)) return v; | ||
@@ -142,2 +147,8 @@ | ||
to: function(v) { | ||
if (_.isArray(v)) { | ||
throw new Error('E_runtimeInputTypeCoercionError'); | ||
} | ||
if (!_.isObject(v)){ | ||
throw new Error('E_runtimeInputTypeCoercionError'); | ||
} | ||
return {}; | ||
@@ -182,2 +193,6 @@ }, | ||
if (_.isUndefined(v)){ | ||
return 0; | ||
} | ||
// Check for Infinity | ||
@@ -184,0 +199,0 @@ if(v === Infinity || v === -Infinity) { |
@@ -18,2 +18,6 @@ /** | ||
// * (allow anything) | ||
if (expected === '*'){ | ||
return _.isUndefined(actual) ? '' : actual; | ||
} | ||
// Arrays | ||
@@ -57,7 +61,7 @@ if (_.isArray(expected)) { | ||
// Check `actual` value against expectedType | ||
if (!expectedType.is(coercedValue)){ | ||
if (!expectedType.is(actual)){ | ||
// Invalid expected type. Try to coerce: | ||
try { | ||
coercedValue = expectedType.to(coercedValue); | ||
coercedValue = expectedType.to(actual); | ||
} | ||
@@ -64,0 +68,0 @@ catch (e) { |
{ | ||
"name": "rttc", | ||
"version": "0.2.4", | ||
"version": "0.3.1", | ||
"description": "Runtime type-checking for JavaScript.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -18,14 +18,30 @@ # rttc | ||
#### General | ||
+ `null` is never allowed. | ||
+ `NaN` is never allowed. | ||
+ `Infinity` is never allowed. | ||
+ `-Infinity` is never allowed. | ||
#### Coercion vs. Validation | ||
+ `.validate()` either returns a (potentially "lightly" coerced) version of the value that was accepted, or it throws. The "lightly" coerced value might turn `"3"` into `3`, `"true"` into `true`, `-4.5` into `"-4.5"`, etc. | ||
+ `.coerce()` ALWAYS returns an acceptable version of the value, even if it has to mangle it to get there. | ||
+ `.coerce()` ALWAYS returns an acceptable version of the value, even if it has to mangle it to get there (i.e. by using the "base value" for the expected type.) | ||
#### Base values | ||
+ For "string", base value is `""` | ||
+ For "number", base value is `0` | ||
+ For "boolean", base value is `false` | ||
+ For any "dictionary" (`{}`), base value is `{}`, with whatever keys are expected (recursive) | ||
+ For a generic "array" (`[]`), base value is `[]`, with a single archetypal item matching the expectation (recursive) | ||
<!-- | ||
TODO: | ||
+ For "stream", base value is an empty readable buffer stream (i.e. not in object mode) | ||
+ For "machine", base value is a no-op machine that calls its success exit. | ||
--> | ||
#### Edge cases | ||
+ `undefined` will always be coerced to the base value of the expected type. | ||
+ `null` is never valid. | ||
+ `NaN` is never valid. | ||
+ `Infinity` is never valid. | ||
+ `-Infinity` is never valid. | ||
#### Dictionaries | ||
@@ -136,3 +152,3 @@ | ||
#### `.validate(expected, actual)` | ||
#### rttc.validate(expected, actual) | ||
@@ -195,3 +211,3 @@ ```js | ||
#### `.coerce(expected, actual)` | ||
#### rttc.coerce(expected, actual) | ||
@@ -198,0 +214,0 @@ |
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
73655
2099
365