Skip to content

Commit

Permalink
(fix convert) Allow nullableToValidation to receive a fallback value
Browse files Browse the repository at this point in the history
  • Loading branch information
robotlolita committed Mar 23, 2017
1 parent 0b82321 commit da6c2e3
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 13 deletions.
8 changes: 4 additions & 4 deletions src/data/conversions/nullable-to-validation.js
Expand Up @@ -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;
17 changes: 13 additions & 4 deletions src/data/validation/index.js
Expand Up @@ -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);
},

/*~
Expand Down
10 changes: 5 additions & 5 deletions test/specs-src/data.validation.js
Expand Up @@ -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));
Expand Down

0 comments on commit da6c2e3

Please sign in to comment.