UNPKG

2.35 kBMarkdownView Raw
1# API design
2
3## Generated predicates might be called with more than one argument
4
5Most of type checking, utility libraries force you use predicates with only one argument.
6_Predicates_ doesn't have such a limitation and additionally preserves the context function call which allows you to create even more powerful logic.
7
8
9```js
10var is = require('predicates');
11
12var isOkToModifyTags = is.all(
13 is.arrayOf(is.string),
14 function(tags, previousTags) {
15 // no need to save them again if they are the same as previous ones
16 return tags.join(',') !== previousTags.join(',');
17 }
18);
19
20Module.prototype.modifyTags = function(entityId, tags) {
21 var previousTags = getTags(entityId);
22 if (isOkToModifyTags(tags, previousTags)) {
23 this.saveTags(entityId, tags);
24 } else {
25 // no need to save them again if they are the same as previous ones
26 }
27}
28```
29
30## Prevents from doing stupid, illogical mistakes
31_Predicates_ checks provided arguments and throws an error if something illogical happened.
32
33```js
34is.startsWith(''); // Error: Prefix cannot be empty
35// since that would be true for all strings
36
37is.in([]); // Error: Collection cannot be empty
38// always false
39```
40
41That's why it's a good practice to create predicates at the beginning of a module definition to quickly catch any mistake.
42
43```js
44// some module
45var is = require('predicates'),
46
47var isImage = is.in([]); // Error: Collection cannot be empty
48
49// You don't need to run the whole application to get the error that your is wrong
50var Module = function(options) {
51 // assert options
52 ...
53};
54
55module.exports = Module;
56```
57
58## Defined and generated predicates will not throw any error
59You don't need to check the arguments provided to predicates to make sure they won't cause an error - _predicates_ does it for you.
60
61```js
62var isDuck = is.hasProperty('quack');
63
64isDuck(undefined); // false - no error
65isDuck(1); // false - no error
66isDuck('duck'); // false - no error
67```
68
69NOTE! This rule applies only for predicates defined in the library. Any user-defined predicate MAY throw errors but _predicates_ will not catch them.
70
71```js
72var assertName = is.all(is.string, function(value) {
73 if (value === 'admin') {
74 throw new Error('Admin is a reserved user name');
75 }
76});
77
78assertName('admin'); // Error: Admin is a reserved user name
79```
\No newline at end of file