Permalink
Browse files

(docs) Test objects with the new deep equality

  • Loading branch information...
robotlolita committed Mar 23, 2017
1 parent 80cc374 commit 373b518f6c44dd8ec3f52fe966152c0ad13baae9
Showing with 27 additions and 27 deletions.
  1. +4 −4 docs/source/en/data/maybe/index.md
  2. +4 −4 docs/source/en/data/result/index.md
  3. +19 −19 docs/source/en/data/validation/index.md
@@ -176,7 +176,7 @@ values::
}
first([{ title: 'Hello' }]).map(render);
// => Maybe.Just(['item', ['title', 'Hello']])
// ==> Maybe.Just(['item', ['title', 'Hello']])
first([]).map(render);
// ==> Maybe.Nothing()
@@ -205,11 +205,11 @@ another Maybe value::
}
first([{ title: 'Hello' }]).map(render);
// => Maybe.Just(['item', ['title', 'Hello']])
// ==> Maybe.Just(['item', ['title', 'Hello']])
first([{ title: 'Hello' }]).map(render)
.map(second);
// => Maybe.Just(Maybe.Just(['title', 'Hello']))
// ==> Maybe.Just(Maybe.Just(['title', 'Hello']))
Ideally we'd like to get back `Maybe.Just(['title', 'Hello'])`, but `.map()`
isn't the method for that. Instead, we can use the `.chain()` method. `.chain()`
@@ -220,7 +220,7 @@ the operation. Like `.map()`, `.chain()` only applies its function argument to
first([{ title: 'Hello' }]).map(render)
.chain(second);
// => Maybe.Just(['title', 'Hello'])
// ==> Maybe.Just(['title', 'Hello'])
first([]).map(render).chain(second);
// ==> Maybe.Nothing()
@@ -267,7 +267,7 @@ Using `Result`, the previous examples would look like this::
type: 'email',
email: 'alissa@somedomain'
});
// => Result.Ok({ name: 'Alissa', type: 'email', phone: undefined, email: 'alissa@somedomain' })
// ==> Result.Ok({ name: 'Alissa', type: 'email', phone: undefined, email: 'alissa@somedomain' })
So, Results give you simpler and more predictable forms of error handling, that
@@ -376,7 +376,7 @@ parse a single digit::
};
digit('012');
// => Result.Ok(['0', '12'])
// ==> Result.Ok(['0', '12'])
digit('a12');
// ==> Result.Error('Expected a digit (0..9), got "a"')
@@ -390,13 +390,13 @@ If we have a fixed number of digits that would look like the following::
const twoDigits = (input) =>
digit(input).chain(([char1, rest]) =>
digit(rest).chain(([char2, rest]) =>
digit(rest).map(([char2, rest]) =>
[char1 + char2, rest]
)
);
twoDigits('012');
// => Result.Ok(['01', '2'])
// ==> Result.Ok(['01', '2'])
twoDigits('a12');
// ==> Result.Error('Expected a digit (0..9), got "a"')
@@ -29,7 +29,7 @@ or a `Failure(value)`, which contains an error.
.map(_ => password);
isPasswordValid('foo');
// ==> Failure(['Password must have more than 6 characters', 'Password must contain a special character.'])
// ==> Failure(['Password must have more than 6 characters.', 'Password must contain a special character.'])
isPasswordValid('rosesarered');
// ==> Failure(['Password must contain a special character.'])
@@ -70,7 +70,7 @@ With branching, things get quickly out of hand, because it's difficult to abstra
name: 'Alissa',
password: 'alis'
});
// ==> ['Password must have more than 6 characters', 'Password must contain a special character']
// ==> ['Password must have at least 6 characters', 'Password must contain a special character']
validateForm({
@@ -85,18 +85,18 @@ You can manage this complexity by designing a special function for verifying if
const validators = {
notEmpty(object, { property }) {
return object[property].trim() ? [`${property} can't be empty']
: /* else */ [];
return !object[property].trim() ? [`${property} can't be empty`]
: /* else */ [];
},
minLength(object, { property, min }) {
const value = object[property]
return value.length < min ? [`${property} should be at least ${min} characters']
const value = object[property];
return value.length < min ? [`${property} must have at least ${min} characters`]
: /* else */ [];
},
regexp(object, { property, regexp, message }) {
return !regex.test(object[property]) ? [message]
return !regexp.test(object[property]) ? [message]
: /* else */ [];
}
};
@@ -122,22 +122,22 @@ You can manage this complexity by designing a special function for verifying if
rule: 'regexp',
property: 'password',
regexp: /\W/,
message: 'Password must contain a special character'
message: 'password must contain a special character'
}
]);
])(data);
}
validateForm2({
name: '',
password: 'roses$are$red'
});
// ==> ['Name is required']
// ==> ['name can\'t be empty']
validateForm2({
name: 'Alissa',
password: 'alis'
});
// ==> ['Password must have more than 6 characters', 'Password must contain a special character']
// ==> ['password must have at least 6 characters', 'password must contain a special character']
validateForm2({
name: 'Alissa',
@@ -154,19 +154,19 @@ Neither of those are very compelling. The Validation structure gives you a tool
const notEmpty = (field, value) =>
value.trim() ? Success(field)
: /* else */ Failure([`${field} can't be empty']);
: /* else */ Failure([`${field} can't be empty`]);
const minLength = (field, min, value) =>
value.length > min ? Success(value)
: /* otherwise */ Failure(['${field} must have more than ${min} characters.']);
: /* otherwise */ Failure([`${field} must have at least ${min} characters`]);
const matches = (field, regexp, value, message = '') =>
regexp.test(value) ? Success(value)
: /* otherwise */ Failure([message || '${field} must match ${regexp}']);
: /* otherwise */ Failure([message || `${field} must match ${regexp}`]);
const isPasswordValid = (password) =>
Success().concat(minLength('password', password))
.concat(matches('password', /\W/))
Success().concat(minLength('password', 6, password))
.concat(matches('password', /\W/, password, 'password must contain a special character'))
.map(_ => password);
const isNameValid = (name) =>
@@ -181,17 +181,17 @@ Neither of those are very compelling. The Validation structure gives you a tool
name: '',
password: 'roses$are$red'
});
// ==> Failure(['name is required'])
// ==> Failure(['name can\'t be empty'])
validateForm3({
name: 'Alissa',
password: 'alis'
});
// ==> Failure(['password must have more than 6 characters', 'password must contain a special character'])
// ==> Failure(['password must have at least 6 characters', 'password must contain a special character'])
validateForm3({
name: 'Alissa',
password: 'roses$are$red'
});
// => Success({ name: 'Alissa', password: 'roses$are$red' })
// ==> Success({ name: 'Alissa', password: 'roses$are$red' })

0 comments on commit 373b518

Please sign in to comment.