Permalink
Browse files

(fix convert) Allow nullableToValidation to receive a fallback value

  • Loading branch information...
robotlolita committed Mar 23, 2017
1 parent 0b82321 commit da6c2e3091ea070c200c6a8cbb15461981d9185f
Showing with 22 additions and 13 deletions.
  1. +4 −4 src/data/conversions/nullable-to-validation.js
  2. +13 −4 src/data/validation/index.js
  3. +5 −5 test/specs-src/data.validation.js
@@ -16,12 +16,12 @@ const { Success, Failure } = require('folktale/data/validation/validation');
* - "@boris-marinov"
*
* type: |
* forall a:
* (a or None) => Validation None a
* forall a, b:
* (a or None, b) => Validation b a
*/
const nullableToValidation = (a) =>
const nullableToValidation = (a, fallbackValue) =>
a != null ? Success(a)
:/*else*/ Failure(a);
:/*else*/ Failure(fallbackValue);
module.exports = nullableToValidation;
@@ -7,20 +7,29 @@
//
//----------------------------------------------------------------------
const Validation = require('./validation');
const { typeSymbol } = require('folktale/core/adt/data');
/*~
* stability: unstable
* name: module folktale/data/validation
*/
module.exports = {
...require('./validation'),
Success: Validation.Success,
Failure: Validation.Failure,
hasInstance: Validation.hasInstance,
of: Validation.of,
fromJSON: Validation.fromJSON,
[typeSymbol]: Validation[typeSymbol],
collect: require('./collect'),
/*~
* type: |
* forall a: (a or None) => Validation None a
* forall a, b: (a or None, b) => Validation b a
*/
fromNullable(aNullable) {
return require('folktale/data/conversions/nullable-to-validation')(aNullable);
fromNullable(aNullable, fallbackValue) {
return require('folktale/data/conversions/nullable-to-validation')(aNullable, fallbackValue);
},
/*~
@@ -138,13 +138,13 @@ describe('Data.Validation', () => {
});
});
describe('Conversions', () => {
property('Failure#fromNullable', () => {
return _.fromNullable(null).equals(_.Failure(null))
&& _.fromNullable(undefined).equals(_.Failure(undefined));
property('Failure#fromNullable', 'string', (a) => {
return _.fromNullable(null, a).equals(_.Failure(a))
&& _.fromNullable(undefined, a).equals(_.Failure(a));
});
property('Success#fromNullable', 'number | string | bool | dict nat | array nat', (a) => {
return _.fromNullable(a).equals(_.Success(a))
property('Success#fromNullable', 'number | string | bool | dict nat | array nat', 'string', (a, b) => {
return _.fromNullable(a, b).equals(_.Success(a))
});
property('Validation#fromResult', 'json', (a) => {
return _.fromResult(_.Success(a).toResult()).equals(_.Success(a));

0 comments on commit da6c2e3

Please sign in to comment.