1 | # API design
|
2 |
|
3 | ## Generated predicates might be called with more than one argument
|
4 |
|
5 | Most 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
|
10 | var is = require('predicates');
|
11 |
|
12 | var 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 |
|
20 | Module.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
|
34 | is.startsWith(''); // Error: Prefix cannot be empty
|
35 | // since that would be true for all strings
|
36 |
|
37 | is.in([]); // Error: Collection cannot be empty
|
38 | // always false
|
39 | ```
|
40 |
|
41 | That'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
|
45 | var is = require('predicates'),
|
46 |
|
47 | var 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
|
50 | var Module = function(options) {
|
51 | // assert options
|
52 | ...
|
53 | };
|
54 |
|
55 | module.exports = Module;
|
56 | ```
|
57 |
|
58 | ## Defined and generated predicates will not throw any error
|
59 | You 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
|
62 | var isDuck = is.hasProperty('quack');
|
63 |
|
64 | isDuck(undefined); // false - no error
|
65 | isDuck(1); // false - no error
|
66 | isDuck('duck'); // false - no error
|
67 | ```
|
68 |
|
69 | NOTE! 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
|
72 | var assertName = is.all(is.string, function(value) {
|
73 | if (value === 'admin') {
|
74 | throw new Error('Admin is a reserved user name');
|
75 | }
|
76 | });
|
77 |
|
78 | assertName('admin'); // Error: Admin is a reserved user name
|
79 | ``` |
\ | No newline at end of file |