1 | #Index
|
2 |
|
3 | **Functions**
|
4 |
|
5 | * [all(...predicate)](#all)
|
6 | * [any(...predicate)](#any)
|
7 | * [array(value)](#array)
|
8 | * [arrayLike(value)](#arrayLike)
|
9 | * [arrayOf(predicate, [value], [...additionalArgs])](#arrayOf)
|
10 | * [blank(value)](#blank)
|
11 | * [boolean(value)](#boolean)
|
12 | * [date(value)](#date)
|
13 | * [defined(value)](#defined)
|
14 | * [divisible(divisor, [value])](#divisible)
|
15 | * [divisibleWithRemainder(divisor, remainder, [value])](#divisibleWithRemainder)
|
16 | * [empty(value)](#empty)
|
17 | * [endsWith(suffix, [value])](#endsWith)
|
18 | * [equal(expected, [value])](#equal)
|
19 | * [false(value)](#false)
|
20 | * [falsy(value)](#falsy)
|
21 | * [finite(value)](#finite)
|
22 | * [function(value)](#function)
|
23 | * [greaterThan(expected, [value])](#greaterThan)
|
24 | * [greaterThanOrEqual(expected, [value])](#greaterThanOrEqual)
|
25 | * [hasOwnProperty(property, [object])](#hasOwnProperty)
|
26 | * [hasProperty(property, [object])](#hasProperty)
|
27 | * [in(collection, [value])](#in)
|
28 | * [instanceOf(clazz, [value])](#instanceOf)
|
29 | * [integer(value)](#integer)
|
30 | * [lessThan(expected, [value])](#lessThan)
|
31 | * [lessThanOrEqual(expected, [value])](#lessThanOrEqual)
|
32 | * [matches(regexp, [value])](#matches)
|
33 | * [NaN(value)](#NaN)
|
34 | * [negative(value)](#negative)
|
35 | * [not(predicate, [value], [...additionalArgs])](#not)
|
36 | * [notBlank(value)](#notBlank)
|
37 | * [notEmpty(value)](#notEmpty)
|
38 | * [null(value)](#null)
|
39 | * [number(value)](#number)
|
40 | * [object(value)](#object)
|
41 | * [objectOf(predicate, [object], [...additionalArgs])](#objectOf)
|
42 | * [oneOf(...allowedValue)](#oneOf)
|
43 | * [plainObject(value)](#plainObject)
|
44 | * [positive(value)](#positive)
|
45 | * [primitive(value)](#primitive)
|
46 | * [property(propertyName, predicate, [value], [...additionalArgs])](#property)
|
47 | * [regExp(value)](#regExp)
|
48 | * [startsWith(prefix, [value])](#startsWith)
|
49 | * [strictEqual(expected, [value])](#strictEqual)
|
50 | * [string(value)](#string)
|
51 | * [structure(structure, [value], [...additionalArgs])](#structure)
|
52 | * [true(value)](#true)
|
53 | * [truthy(value)](#truthy)
|
54 | * [undefined(value)](#undefined)
|
55 | * [undefinedOr(predicate, [value])](#undefinedOr)
|
56 |
|
57 | **Typedefs**
|
58 |
|
59 | * [type: Predicate](#Predicate)
|
60 |
|
61 | <a name="all"></a>
|
62 | #all(...predicate)
|
63 | Returns a function that calls predicates and returns true if all of them are satisfied, otherwise returns false
|
64 |
|
65 | **Aliases** _and_
|
66 |
|
67 | **Params**
|
68 |
|
69 | - ...predicate <code>[Predicate](#Predicate)</code>
|
70 |
|
71 | **Throws**
|
72 |
|
73 | * `TypeError` - if not every predicate is a function
|
74 |
|
75 | **Returns**: [Predicate](#Predicate)
|
76 | **Example**
|
77 | ```js
|
78 | var is = require('predicates');
|
79 | var isNumberGreaterThan10 = is.all(is.number, is.greaterThan(10));
|
80 |
|
81 | isNumberGreaterThan10(0); // false
|
82 | isNumberGreaterThan10(11); // true
|
83 | isNumberGreaterThan10('11'); // false
|
84 | ```
|
85 | <a name="any"></a>
|
86 | #any(...predicate)
|
87 | Returns a function that calls predicates in the order until one of them will be satisfied, otherwise returns false.
|
88 |
|
89 | **Aliases** _or_
|
90 |
|
91 | **Params**
|
92 |
|
93 | - ...predicate <code>[Predicate](#Predicate)</code>
|
94 |
|
95 | **Throws**
|
96 |
|
97 | * `TypeError` - if not every predicate is a function
|
98 |
|
99 | **Returns**: [Predicate](#Predicate)
|
100 | **Example**
|
101 | ```js
|
102 | var is = require('predicates');
|
103 |
|
104 | var isStringOrNumber = is.any(is.string, is.number);
|
105 |
|
106 | isStringOrNumber(0); // true
|
107 | isStringOrNumber('string'); // true
|
108 | isStringOrNumber(undefined); // false
|
109 | ```
|
110 | <a name="array"></a>
|
111 | #array(value)
|
112 | Checks whether a value is an array
|
113 |
|
114 | **Aliases** _arr_
|
115 |
|
116 | **Params**
|
117 |
|
118 | - value `*`
|
119 |
|
120 | **Returns**: `Boolean`
|
121 | **Example**
|
122 | ```js
|
123 | var is = require('predicates');
|
124 |
|
125 | is.array([]); // true
|
126 | is.array({}); // false
|
127 | ```
|
128 | <a name="arrayLike"></a>
|
129 | #arrayLike(value)
|
130 | Checks whether a value looks like an array
|
131 | That means:
|
132 | * is an object
|
133 | * has 'length' property
|
134 | * 'length' property is a number greater or equal 0
|
135 |
|
136 | **Aliases** _arrLike_
|
137 |
|
138 | **Params**
|
139 |
|
140 | - value `*`
|
141 |
|
142 | **Returns**: `Boolean`
|
143 | **Example**
|
144 | ```js
|
145 | var is = require('predicates');
|
146 |
|
147 | is.arrayLike(arguments); // true
|
148 | is.arrayLike(document.querySelectorAll('div')); // true
|
149 | is.arrayLike([1, 2, 3]); // true
|
150 | is.arrayLike({}); // false
|
151 | ```
|
152 | <a name="arrayOf"></a>
|
153 | #arrayOf(predicate, [value], [...additionalArgs])
|
154 | Checks whether every element of an array passes the predicate
|
155 |
|
156 | **Aliases** _arrOf_
|
157 |
|
158 | **Params**
|
159 |
|
160 | - predicate <code>[Predicate](#Predicate)</code>
|
161 | - \[value\] `Array`
|
162 | - \[...additionalArgs\] `*` - additional arguments passed to the predicate
|
163 |
|
164 | **Throws**
|
165 |
|
166 | * `TypeError` - if predicate is not a function
|
167 |
|
168 | **Returns**: `Boolean` | [Predicate](#Predicate) - returns bool if at least two arguments provided, otherwise a predicate
|
169 | **Example**
|
170 | ```js
|
171 | var is = require('predicates');
|
172 |
|
173 | var isArrayOfStrings = is.arrayOf(is.string);
|
174 |
|
175 | isArrayOfStrings(['1', '2']); // true
|
176 | // same as
|
177 | is.arrayOf(is.string, ['1', '2']); // true
|
178 |
|
179 | isArrayOfStrings([1, 2]); // false
|
180 | ```
|
181 | <a name="blank"></a>
|
182 | #blank(value)
|
183 | Checks whether a value is empty string or contains only whitespaces
|
184 |
|
185 | **Params**
|
186 |
|
187 | - value `String`
|
188 |
|
189 | **Returns**: `Boolean`
|
190 | **Example**
|
191 | ```js
|
192 | var is = require('predicates');
|
193 |
|
194 | is.blank(''); // true
|
195 | is.blank(' '); // true
|
196 | is.blank('test'); // false
|
197 | ```
|
198 | <a name="boolean"></a>
|
199 | #boolean(value)
|
200 | Checks whether a value is a boolean
|
201 |
|
202 | **Aliases** _bool_
|
203 |
|
204 | **Params**
|
205 |
|
206 | - value `*`
|
207 |
|
208 | **Returns**: `Boolean`
|
209 | **Example**
|
210 | ```js
|
211 | var is = require('predicates');
|
212 |
|
213 | is.boolean(true); // true
|
214 | is.boolean(false);; // true
|
215 | is.boolean(0); // false
|
216 | ```
|
217 | <a name="date"></a>
|
218 | #date(value)
|
219 | Checks whether a value is a Date object
|
220 |
|
221 | **Params**
|
222 |
|
223 | - value `*`
|
224 |
|
225 | **Returns**: `Boolean`
|
226 | **Example**
|
227 | ```js
|
228 | var is = require('predicates');
|
229 |
|
230 | is.date(new Date()); true
|
231 | is.date(1415402574000); // false
|
232 | ```
|
233 | <a name="defined"></a>
|
234 | #defined(value)
|
235 | Checks whether a value is not undefined - in other words, is defined
|
236 |
|
237 | **Params**
|
238 |
|
239 | - value `*`
|
240 |
|
241 | **Returns**: `Boolean`
|
242 | **Example**
|
243 | ```js
|
244 | var is = require('predicates');
|
245 |
|
246 | is.defined(''); // true
|
247 | is.defined(1); // true
|
248 | is.defined(undefined); // false
|
249 | ```
|
250 | <a name="divisible"></a>
|
251 | #divisible(divisor, [value])
|
252 | Checks whether a value is a number and it's divisible by divisor
|
253 |
|
254 | **Aliases** _divisibleBy_, _divBy_
|
255 |
|
256 | **Params**
|
257 |
|
258 | - divisor `Number`
|
259 | - \[value\] `Number`
|
260 |
|
261 | **Throws**
|
262 |
|
263 | * `Error` - if the divisor is 0
|
264 |
|
265 | * `TypeError` - if the divisor is not a finite number
|
266 |
|
267 | **Returns**: `Boolean` | [Predicate](#Predicate) - returns bool if at least 2 arguments provided, otherwise a predicate
|
268 | **Example**
|
269 | ```js
|
270 | var is = require('predicates');
|
271 |
|
272 | is.divisible(7, 14); // true
|
273 | is.divisible(7)(14); // true
|
274 | is.divisible(7, 10); // false
|
275 | ```
|
276 | <a name="divisibleWithRemainder"></a>
|
277 | #divisibleWithRemainder(divisor, remainder, [value])
|
278 | Checks whether a value is a number and it's divisible by divisor with given remainder
|
279 | In other words value % div === remainder
|
280 |
|
281 | **Aliases** _divisibleByWithRemainder_, _divByWithRemainder_
|
282 |
|
283 | **Params**
|
284 |
|
285 | - divisor `Number`
|
286 | - remainder `Number`
|
287 | - \[value\] `Number`
|
288 |
|
289 | **Throws**
|
290 |
|
291 | * `Error` - if less than 2 arguments provided
|
292 |
|
293 | * `Error` - if the divisor is 0
|
294 |
|
295 | * `Error` - if the remainder is greater than the divisor
|
296 |
|
297 | * `TypeError` - if the divisor is not a finite number
|
298 |
|
299 | * `TypeError` - if the remainder is not a finite number
|
300 |
|
301 | **Returns**: `Boolean` | [Predicate](#Predicate) - returns bool if at least 3 arguments provided, otherwise a predicate
|
302 | **Example**
|
303 | ```js
|
304 | var is = require('predicates');
|
305 |
|
306 | is.divisibleWithRemainder(3, 2, 5); // true since 5%3 === 2
|
307 | is.divisibleWithRemainder(3, 2)(5); // true
|
308 | is.divisibleWithRemainder(3, 1, 5); // false since 5%3 !== 1
|
309 |
|
310 | var isEven = is.divisibleWithRemainder(2, 1);
|
311 |
|
312 | isEven(1); // true
|
313 | isEven(2); // false
|
314 | isEven(3); // true
|
315 |
|
316 | *
|
317 | ```
|
318 | <a name="empty"></a>
|
319 | #empty(value)
|
320 | Checks whether a value is empty
|
321 | Value is empty when:
|
322 | * is an array like object with length === 0
|
323 | * is an object without enumerable properties
|
324 | * is an empty string
|
325 |
|
326 | **Params**
|
327 |
|
328 | - value `*`
|
329 |
|
330 | **Returns**: `Boolean`
|
331 | **Example**
|
332 | ```js
|
333 | var is = require('predicates');
|
334 |
|
335 | is.empty(''); // true
|
336 | is.empty([]); // true
|
337 | is.empty({}); // true
|
338 | is.empty([1]); // false
|
339 | is.empty('test'); // false
|
340 | ```
|
341 | <a name="endsWith"></a>
|
342 | #endsWith(suffix, [value])
|
343 | Checks whether a string ends with a given suffix
|
344 |
|
345 | **Params**
|
346 |
|
347 | - suffix `String`
|
348 | - \[value\] `String`
|
349 |
|
350 | **Throws**
|
351 |
|
352 | * `TypeError` - if suffix is not a string
|
353 |
|
354 | * `Error` - if suffix is empty
|
355 |
|
356 | **Returns**: `Boolean` | [Predicate](#Predicate) - returns bool if at least two arguments provided, otherwise a predicate
|
357 | **Example**
|
358 | ```js
|
359 | var is = require('predicates');
|
360 |
|
361 | var isYelling = is.endsWith('!');
|
362 |
|
363 | isYelling('shut up!'); // true
|
364 | // same as
|
365 | is.endsWith('!', 'shut up!'); // true
|
366 | isYelling('be quiet please'); // false
|
367 | ```
|
368 | <a name="equal"></a>
|
369 | #equal(expected, [value])
|
370 | Checks whether values are equal (using == operator)
|
371 |
|
372 | **Aliases** _equalTo_, _eq_
|
373 |
|
374 | **Params**
|
375 |
|
376 | - expected `*`
|
377 | - \[value\] `*`
|
378 |
|
379 | **Returns**: `Boolean` | [Predicate](#Predicate) - bool if at least two arguments provided, otherwise a predicate
|
380 | **Example**
|
381 | ```js
|
382 | var is = require('predicates');
|
383 |
|
384 | var isTimmy = is.equal('Timmy');
|
385 |
|
386 | isTimmy('Timmy'); // true
|
387 | // same as
|
388 | is.equal('Timmy', 'Timmy'); // true
|
389 | is.equal(1, '1'); // true
|
390 | isTimmy('Franko'); // false
|
391 | ```
|
392 | <a name="false"></a>
|
393 | #false(value)
|
394 | Checks whether a value is false a boolean false
|
395 |
|
396 | **Params**
|
397 |
|
398 | - value `*`
|
399 |
|
400 | **Returns**: `Boolean`
|
401 | **Example**
|
402 | ```js
|
403 | var is = require('predicates');
|
404 |
|
405 | is.false(false); // true
|
406 | is.false(0); // false
|
407 | ```
|
408 | <a name="falsy"></a>
|
409 | #falsy(value)
|
410 | Checks whether a value is falsy
|
411 |
|
412 | **Aliases** _falsey_
|
413 |
|
414 | **Params**
|
415 |
|
416 | - value `*`
|
417 |
|
418 | **Returns**: `Boolean`
|
419 | **Example**
|
420 | ```js
|
421 | var is = require('predicates');
|
422 |
|
423 | is.falsy(0); // true
|
424 | is.falsy(false); // true
|
425 | is.falsy(1); // false
|
426 | ```
|
427 | <a name="finite"></a>
|
428 | #finite(value)
|
429 | Checks whether a value is a number and it's finite
|
430 |
|
431 | **Params**
|
432 |
|
433 | - value `*`
|
434 |
|
435 | **Returns**: `Boolean`
|
436 | **Example**
|
437 | ```js
|
438 | var is = require('predicates');
|
439 |
|
440 | is.finite(1); // false
|
441 | is.finite(Infinity); // false
|
442 | ```
|
443 | <a name="function"></a>
|
444 | #function(value)
|
445 | Checks whether a value is a function
|
446 |
|
447 | **Aliases** _func_, _fn_
|
448 |
|
449 | **Params**
|
450 |
|
451 | - value `*`
|
452 |
|
453 | **Returns**: `Boolean`
|
454 | **Example**
|
455 | ```js
|
456 | var is = require('predicates');
|
457 |
|
458 | is.function(function() {}); // true
|
459 | is.function(alert); // true
|
460 | is.function('alert'); // false
|
461 | ```
|
462 | <a name="greaterThan"></a>
|
463 | #greaterThan(expected, [value])
|
464 | Checks whether a value is greater than expected number
|
465 |
|
466 | **Aliases** _greater_, _gt_
|
467 |
|
468 | **Params**
|
469 |
|
470 | - expected `Number`
|
471 | - \[value\] `Number`
|
472 |
|
473 | **Returns**: `Boolean` | [Predicate](#Predicate) - bool if at least two arguments provided, otherwise a predicate
|
474 | **Example**
|
475 | ```js
|
476 | var is = require('predicates');
|
477 |
|
478 | var isGreaterThan0 = is.greaterThan(0);
|
479 |
|
480 | isGreaterThan0(10); // true
|
481 | // same as
|
482 | is.greaterThan(0, 10); // true
|
483 | isGreaterThan0(-1); // false
|
484 | ```
|
485 | <a name="greaterThanOrEqual"></a>
|
486 | #greaterThanOrEqual(expected, [value])
|
487 | Checks whether a value is greater or equal to expected number
|
488 |
|
489 | **Aliases** _greaterOrEqual_, _greaterEq_, _gtEq_
|
490 |
|
491 | **Params**
|
492 |
|
493 | - expected `Number`
|
494 | - \[value\] `Number`
|
495 |
|
496 | **Returns**: `Boolean` | [Predicate](#Predicate) - bool if at least two arguments provided, otherwise a predicate
|
497 | **Example**
|
498 | ```js
|
499 | var is = require('predicates');
|
500 |
|
501 | var isAdultAge = is.greaterThanOrEqual(18);
|
502 |
|
503 | isAdultAge(22); // true
|
504 | // same as
|
505 | is.greaterThanOrEqual(18, 22);
|
506 |
|
507 | isAdultAge(16); // false
|
508 | ```
|
509 | <a name="hasOwnProperty"></a>
|
510 | #hasOwnProperty(property, [object])
|
511 | Checks whether an object has own property
|
512 |
|
513 | **Aliases** _hasOwn_
|
514 |
|
515 | **Params**
|
516 |
|
517 | - property `String`
|
518 | - \[object\] `Object`
|
519 |
|
520 | **Throws**
|
521 |
|
522 | * `TypeError` - if property is not a string
|
523 |
|
524 | **Returns**: `Boolean` | [Predicate](#Predicate) - bool if at least two arguments provided, otherwise a predicate
|
525 | **Example**
|
526 | ```js
|
527 | var is = require('predicates');
|
528 |
|
529 | var isCustomized = is.hasOwnProperty('delay');
|
530 |
|
531 | var Timer = function() {};
|
532 | Timer.prototype.delay = 100;
|
533 |
|
534 | var timer1 = new Timer();
|
535 | var timer2 = new Timer();
|
536 | timer1.delay = 1000;
|
537 |
|
538 | isCustomized(timer1) // true
|
539 | // same as
|
540 | is.hasOwnProperty('delay', timer1); // true
|
541 |
|
542 | isCustomized(timer2); // false
|
543 | ```
|
544 | <a name="hasProperty"></a>
|
545 | #hasProperty(property, [object])
|
546 | Checks whether an object has a given property
|
547 |
|
548 | **Aliases** _has_
|
549 |
|
550 | **Params**
|
551 |
|
552 | - property `String`
|
553 | - \[object\] `Object`
|
554 |
|
555 | **Throws**
|
556 |
|
557 | * `TypeError` - if property is not a string
|
558 |
|
559 | **Returns**: `Boolean` | [Predicate](#Predicate) - bool if at least two arguments provided, otherwise a predicate
|
560 | **Example**
|
561 | ```js
|
562 | var is = require('predicates');
|
563 |
|
564 | var isDuck = is.hasProperty('quack');
|
565 |
|
566 | isDuck({quack: ':)'}); // true
|
567 | // same as
|
568 | is.hasProperty('quack', {quack: ':)'}); // true
|
569 |
|
570 | isDuck({type: 'car'}); // false
|
571 | ```
|
572 | <a name="in"></a>
|
573 | #in(collection, [value])
|
574 | Checks whether a value exists in collection
|
575 | Values are compared using === operator
|
576 |
|
577 | **Params**
|
578 |
|
579 | - collection `Array` - of allowed values
|
580 | - \[value\] `*`
|
581 |
|
582 | **Throws**
|
583 |
|
584 | * `TypeError` - if collection is not an array
|
585 |
|
586 | * `Error` - if collection is empty
|
587 |
|
588 | **Returns**: `Boolean` | [Predicate](#Predicate) - bool if at least two arguments provided, otherwise a predicate
|
589 | **Example**
|
590 | ```js
|
591 | var is = require('predicates');
|
592 |
|
593 | var isImage = is.in(['image/gif', 'image/jpeg']);
|
594 | // same as
|
595 | // var isImage = is.oneOf('image/gif', 'image/jpeg');
|
596 |
|
597 | isImage('image/jpeg'); // true
|
598 | // same as
|
599 | is.in(['image/gif', 'image/jpeg'], 'image/jpeg'); // true
|
600 |
|
601 | isImage('text/html'); // false
|
602 | ```
|
603 | <a name="instanceOf"></a>
|
604 | #instanceOf(clazz, [value])
|
605 | Checks whether a value is an instance of given "class"
|
606 |
|
607 | **Aliases** _instance_
|
608 |
|
609 | **Params**
|
610 |
|
611 | - clazz <code>[function](#function)</code>
|
612 | - \[value\] `*`
|
613 |
|
614 | **Throws**
|
615 |
|
616 | * `TypeError` - if class is not a function
|
617 |
|
618 | **Returns**: `Boolean` | [Predicate](#Predicate) - bool if at least two arguments provided, otherwise a predicate
|
619 | **Example**
|
620 | ```js
|
621 | var is = require('predicates');
|
622 |
|
623 | var Duck = function() {};
|
624 | var Car = function() {};
|
625 |
|
626 | var isDuck = is.instanceOf(Duck);
|
627 |
|
628 | isDuck(new Duck); // true
|
629 | // same as
|
630 | is.instanceOf(Duck, new Duck); // true
|
631 |
|
632 | isDuck(new Car); // false
|
633 | ```
|
634 | <a name="integer"></a>
|
635 | #integer(value)
|
636 | Checks whether a value is an integer
|
637 |
|
638 | **Aliases** _int_
|
639 |
|
640 | **Params**
|
641 |
|
642 | - value `*`
|
643 |
|
644 | **Returns**: `Boolean`
|
645 | **Example**
|
646 | ```js
|
647 | var is = require('predicates');
|
648 |
|
649 | is.integer(10); // true
|
650 | is.integer(10.4); // false
|
651 | ```
|
652 | <a name="lessThan"></a>
|
653 | #lessThan(expected, [value])
|
654 | Checks whether a value is less than expected number
|
655 |
|
656 | **Aliases** _less_, _lt_
|
657 |
|
658 | **Params**
|
659 |
|
660 | - expected `Number`
|
661 | - \[value\] `Number`
|
662 |
|
663 | **Returns**: `Boolean` | [Predicate](#Predicate) - bool if at least two arguments provided, otherwise a predicate
|
664 | **Example**
|
665 | ```js
|
666 | var isChildAge = is.lessThan(18);
|
667 |
|
668 | isChildAge(10); // true
|
669 | // same as
|
670 | is.lessThan(18, 10); // true
|
671 | isChildAge(18); // false
|
672 | isChildAge(22); // false
|
673 | ```
|
674 | <a name="lessThanOrEqual"></a>
|
675 | #lessThanOrEqual(expected, [value])
|
676 | Checks whether a value is less or equal to expected number
|
677 |
|
678 | **Aliases** _lessOrEqual_, _lessEq_, _ltEq_
|
679 |
|
680 | **Params**
|
681 |
|
682 | - expected `Number`
|
683 | - \[value\] `Number`
|
684 |
|
685 | **Returns**: `Boolean` | [Predicate](#Predicate) - bool if at least two arguments provided, otherwise a predicate
|
686 | **Example**
|
687 | ```js
|
688 | var is = require('predicates');
|
689 |
|
690 | var isChildAge = is.lessThanOrEqual(17);
|
691 |
|
692 | isChildAge(10); // true
|
693 | // same as
|
694 | is.lessThanOrEqual(17, 10); // true
|
695 |
|
696 | isChildAge(18); // false
|
697 | ```
|
698 | <a name="matches"></a>
|
699 | #matches(regexp, [value])
|
700 | Checks whether a value matches a regexp
|
701 |
|
702 | **Aliases** _match_
|
703 |
|
704 | **Params**
|
705 |
|
706 | - regexp `RegExp`
|
707 | - \[value\] `String`
|
708 |
|
709 | **Throws**
|
710 |
|
711 | * `TypeError` - if regexp is not a regexp
|
712 |
|
713 | **Returns**: `Boolean` | [Predicate](#Predicate) - bool if at least two arguments provided, otherwise a predicate
|
714 | **Example**
|
715 | ```js
|
716 | var is = require('predicates');
|
717 |
|
718 | var isWindows9x = is.matches(/^Windows 9/);
|
719 |
|
720 | isWindows9x('Windows 9'); // true - :D
|
721 | // same as
|
722 | is.matches(/^Windows 9/, 'Windows 9'); // also true - hue hue
|
723 |
|
724 | isWindows9x('Windows 10); // false
|
725 | ```
|
726 | <a name="NaN"></a>
|
727 | #NaN(value)
|
728 | Checks whether a value is a NaN number
|
729 |
|
730 | **Aliases** _nan_
|
731 |
|
732 | **Params**
|
733 |
|
734 | - value `*`
|
735 |
|
736 | **Returns**: `Boolean`
|
737 | **Example**
|
738 | ```js
|
739 | var is = require('predicates');
|
740 |
|
741 | is.NaN(NaN); // true
|
742 | is.NaN(10); // false
|
743 | ```
|
744 | <a name="negative"></a>
|
745 | #negative(value)
|
746 | Checks whether a value is a negative number
|
747 |
|
748 | **Params**
|
749 |
|
750 | - value `Number`
|
751 |
|
752 | **Returns**: `Boolean`
|
753 | **Example**
|
754 | ```js
|
755 | var is = require('predicates');
|
756 |
|
757 | is.negative(-1); // true
|
758 | is.negative(0); // false
|
759 | ```
|
760 | <a name="not"></a>
|
761 | #not(predicate, [value], [...additionalArgs])
|
762 | Negates result of a predicate
|
763 |
|
764 | **Aliases** _negate_
|
765 |
|
766 | **Params**
|
767 |
|
768 | - predicate <code>[Predicate](#Predicate)</code>
|
769 | - \[value\] `*`
|
770 | - \[...additionalArgs\] `*` - additional arguments passed to the predicate
|
771 |
|
772 | **Returns**: `Boolean` | [Predicate](#Predicate) - returns bool if at least two arguments provided, otherwise a predicate
|
773 | **Example**
|
774 | ```js
|
775 | var is = require('predicates');
|
776 |
|
777 | var isNotEmpty = is.not(is.empty);
|
778 | isNotEmpty([1, 2]);// true
|
779 | // same as
|
780 | is.not(is.empty, [1, 2]); // true
|
781 | isNotEmpty(''); // false
|
782 | ```
|
783 | <a name="notBlank"></a>
|
784 | #notBlank(value)
|
785 | Checks whether a value is a string and contains at least one non-whitespace character
|
786 |
|
787 | **Params**
|
788 |
|
789 | - value `String`
|
790 |
|
791 | **Returns**: `Boolean`
|
792 | **Example**
|
793 | ```js
|
794 | var is = require('predicates');
|
795 |
|
796 | is.notBlank(''); // false
|
797 | is.notBlank(' '); // false
|
798 | is.notBlank('test'); // true
|
799 | is.notBlank({toString: function() { return 'test'; }}); // false - since it's not a string
|
800 | ```
|
801 | <a name="notEmpty"></a>
|
802 | #notEmpty(value)
|
803 | Checks whether value is not empty.
|
804 |
|
805 | See [empty](#empty) for list of conditions that determine when a value is empty
|
806 |
|
807 | **Params**
|
808 |
|
809 | - value `*`
|
810 |
|
811 | **Returns**: `Boolean`
|
812 | **Example**
|
813 | ```js
|
814 | var is = require('predicates');
|
815 |
|
816 | is.notEmpty([1]); // true
|
817 | is.notEmpty('value'); // true
|
818 | is.notEmpty([]); // false
|
819 | is.notEmpty(''); // false
|
820 | ```
|
821 | <a name="null"></a>
|
822 | #null(value)
|
823 | Checks whether a value is null
|
824 |
|
825 | **Params**
|
826 |
|
827 | - value `*`
|
828 |
|
829 | **Returns**: `Boolean`
|
830 | **Example**
|
831 | ```js
|
832 | var is = require('predicates');
|
833 |
|
834 | is.null(null); // true
|
835 | is.null({}); // false
|
836 | ```
|
837 | <a name="number"></a>
|
838 | #number(value)
|
839 | Checks whether a value is a number
|
840 |
|
841 | **Aliases** _num_
|
842 |
|
843 | **Params**
|
844 |
|
845 | - value `*`
|
846 |
|
847 | **Returns**: `Boolean`
|
848 | **Example**
|
849 | ```js
|
850 | var is = require('predicates');
|
851 |
|
852 | is.number(10); // true
|
853 | is.number('10'); // false
|
854 | ```
|
855 | <a name="object"></a>
|
856 | #object(value)
|
857 | Checks whether a value is an object
|
858 |
|
859 | **Aliases** _obj_
|
860 |
|
861 | **Params**
|
862 |
|
863 | - value `*`
|
864 |
|
865 | **Returns**: `Boolean`
|
866 | **Example**
|
867 | ```js
|
868 | var is = require('predicates');
|
869 |
|
870 | is.object({}); // true
|
871 | is.object('object'); // false
|
872 | ```
|
873 | <a name="objectOf"></a>
|
874 | #objectOf(predicate, [object], [...additionalArgs])
|
875 | Checks whether every enumerable property of object satisfies a predicate
|
876 |
|
877 | **Aliases** _objOf_
|
878 |
|
879 | **Params**
|
880 |
|
881 | - predicate <code>[Predicate](#Predicate)</code>
|
882 | - \[object\] `Object`
|
883 | - \[...additionalArgs\] `*` - additional arguments passed to the predicate
|
884 |
|
885 | **Returns**: `Boolean` | [Predicate](#Predicate) - returns bool if at least two arguments provided, otherwise a predicate
|
886 | **Example**
|
887 | ```js
|
888 | var is = require('predicates');
|
889 |
|
890 | var isObjectOfStrings = is.objectOf(is.string);
|
891 |
|
892 | isObjectOfStrings({key: 'value', key1: 'value'}); // true
|
893 | // same as
|
894 | is.objectOf(is.string, {key: 'value', key1: 'value'}); // true
|
895 |
|
896 | isObjectOfStrings({key: 1, key1: 'value'}); // false
|
897 | ```
|
898 | <a name="oneOf"></a>
|
899 | #oneOf(...allowedValue)
|
900 | Returns a function that checks whether a value is equal to one of allowed values
|
901 | Function compares values using === operator
|
902 |
|
903 | **Params**
|
904 |
|
905 | - ...allowedValue `*`
|
906 |
|
907 | **Throws**
|
908 |
|
909 | * `Error` - if 0 or 1 allowed value provided
|
910 |
|
911 | **Returns**: [Predicate](#Predicate)
|
912 | **Example**
|
913 | ```js
|
914 | var is = require('predicates');
|
915 |
|
916 | var isAllowedToAccess = is.oneOf('ROLE_ADMIN', 'ROLE_USER');
|
917 | // same as
|
918 | // var isAllowedToAccess = is.in(['ROLE_ADMIN', 'ROLE_USER']);
|
919 |
|
920 | isAllowedToAccess('ROLE_ADMIN'); // true
|
921 | isAllowedToAccess('ROLE_ANONYMOUS'); // false
|
922 | ```
|
923 | <a name="plainObject"></a>
|
924 | #plainObject(value)
|
925 | Checks whether a value is a plain object.
|
926 | Plain object is an object which prototype is Object.prototype or null
|
927 |
|
928 | **Params**
|
929 |
|
930 | - value `*`
|
931 |
|
932 | **Returns**: `boolean`
|
933 | **Example**
|
934 | ```js
|
935 | var is = require('predicates');
|
936 |
|
937 | is.plainObject({property: 'value'}); // true
|
938 | is.plainObject(new Object); // true
|
939 | is.plainObject(Object.create(null)); // true
|
940 | is.plainObject(new String('test')); // false
|
941 |
|
942 | var Foo = function() {};
|
943 | is.plainObject(new Foo); // false
|
944 | ```
|
945 | <a name="positive"></a>
|
946 | #positive(value)
|
947 | Checks whether a value is a positive number
|
948 |
|
949 | **Params**
|
950 |
|
951 | - value `Number`
|
952 |
|
953 | **Returns**: `Boolean`
|
954 | **Example**
|
955 | ```js
|
956 | var is = require('predicates');
|
957 |
|
958 | is.positive(10); // true
|
959 | is.positive(-1); // false
|
960 | ```
|
961 | <a name="primitive"></a>
|
962 | #primitive(value)
|
963 | Checks whether a value is a primitive.
|
964 |
|
965 | Helpful links:
|
966 | * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures
|
967 | * http://javascriptweblog.wordpress.com/2010/09/27/the-secret-life-of-javascript-primitives/
|
968 |
|
969 | NOTE! A primitive value wrapped by a corresponding object is not a primitive anymore
|
970 | ```js
|
971 | var a = 'test' // this is a primitive
|
972 | a = new String('test'); // and this is not a primitive
|
973 | ```
|
974 |
|
975 | **Params**
|
976 |
|
977 | - value `*`
|
978 |
|
979 | **Returns**: `Boolean`
|
980 | **Example**
|
981 | ```js
|
982 | var is = require('predicates');
|
983 |
|
984 | is.primitive('test'); // true
|
985 | is.primitive(undefined); // true
|
986 | is.primitive(10); // true
|
987 | is.primitive(null); // true
|
988 | is.primitive(false); // true
|
989 |
|
990 | is.primitive(new Number(10)); // false
|
991 | is.primitive(new String('test')); // false
|
992 | is.primitive(new Boolean(true)); // false
|
993 | is.primitive({}); // false
|
994 | ```
|
995 | <a name="property"></a>
|
996 | #property(propertyName, predicate, [value], [...additionalArgs])
|
997 | Checks whether a value of given property of an object satisfies a predicate
|
998 |
|
999 | If you need to check more properties at a time use [structure](#structure).
|
1000 |
|
1001 | NOTE! Provided predicate will be called ALWAYS if a provided value is an object.
|
1002 |
|
1003 | **Aliases** _prop_
|
1004 |
|
1005 | **Params**
|
1006 |
|
1007 | - propertyName `*`
|
1008 | - predicate <code>[Predicate](#Predicate)</code>
|
1009 | - \[value\] `Object`
|
1010 | - \[...additionalArgs\] `*` - additional arguments passed to the predicate
|
1011 |
|
1012 | **Throws**
|
1013 |
|
1014 | * `TypeError` - if predicate is not a function
|
1015 |
|
1016 | * `Error` - if too few arguments provided
|
1017 |
|
1018 | **Returns**: `Boolean` | [Predicate](#Predicate) - boolean if at least 3 arguments provided, otherwise a predicate
|
1019 | **Example**
|
1020 | ```js
|
1021 | var is = require('predicates');
|
1022 |
|
1023 | is.property('name', is.string, {name: 'Tommy'}); // true
|
1024 | is.property('name', is.string)({name: 'Tommy'}); // true
|
1025 | is.property('name', is.string, {name: 2}); // false - since 2 is not a string
|
1026 | is.property('name', is.string, {}); // false - since undefined is not a string
|
1027 | ```
|
1028 | <a name="regExp"></a>
|
1029 | #regExp(value)
|
1030 | Checks whether a value is a regexp
|
1031 |
|
1032 | **Aliases** _regexp_
|
1033 |
|
1034 | **Params**
|
1035 |
|
1036 | - value `*`
|
1037 |
|
1038 | **Returns**: `Boolean`
|
1039 | **Example**
|
1040 | ```js
|
1041 | var is = require('predicates');
|
1042 |
|
1043 | is.regExp(/t/); // true
|
1044 | is.regExp(new RegExp(/t/)); // true
|
1045 | is.regExp('.*'); // false
|
1046 | ```
|
1047 | <a name="startsWith"></a>
|
1048 | #startsWith(prefix, [value])
|
1049 | Checks whether a string starts with a given prefix
|
1050 |
|
1051 | **Params**
|
1052 |
|
1053 | - prefix `String`
|
1054 | - \[value\] `String`
|
1055 |
|
1056 | **Throws**
|
1057 |
|
1058 | * `TypeError` - if prefix is not a string
|
1059 |
|
1060 | * `Error` - if prefix is empty
|
1061 |
|
1062 | **Returns**: `Boolean` | [Predicate](#Predicate) - returns bool if at least two arguments provided, otherwise a predicate
|
1063 | **Example**
|
1064 | ```js
|
1065 | var is = require('predicates');
|
1066 |
|
1067 | var isProfessor = is.startsWith('Prof. ');
|
1068 | isProfessor('Prof. Bend Ovah'); // true
|
1069 | // same as
|
1070 | is.startsWith('Prof. ', 'Prof. Bend Ovah'); // true
|
1071 |
|
1072 | isProfessor('Dr. Here U\' Are'); // false
|
1073 | ```
|
1074 | <a name="strictEqual"></a>
|
1075 | #strictEqual(expected, [value])
|
1076 | Checks whether a value is strictly equal to expected value (uses === operator)
|
1077 |
|
1078 | **Aliases** _strictEqualTo_
|
1079 |
|
1080 | **Params**
|
1081 |
|
1082 | - expected `*`
|
1083 | - \[value\] `*`
|
1084 |
|
1085 | **Returns**: `Boolean` | [Predicate](#Predicate) - bool if at least two arguments provided, otherwise a predicate
|
1086 | **Example**
|
1087 | ```js
|
1088 | var is = require('predicates');
|
1089 |
|
1090 | var mom = {};
|
1091 | var isMyMom = is.strictEqual(mom);
|
1092 |
|
1093 | isMyMom(mom); // true - mom is only one. Remember about it...
|
1094 | // same as
|
1095 | is.strictEqual(mom, mom); // true
|
1096 | isMyMom({}); // false
|
1097 | ```
|
1098 | <a name="string"></a>
|
1099 | #string(value)
|
1100 | Checks whether a value is a function
|
1101 |
|
1102 | **Aliases** _str_
|
1103 |
|
1104 | **Params**
|
1105 |
|
1106 | - value `*`
|
1107 |
|
1108 | **Returns**: `Boolean`
|
1109 | **Example**
|
1110 | ```js
|
1111 | var is = require('predicates');
|
1112 |
|
1113 | is.string('test'); // true
|
1114 | is.string({}); // false
|
1115 | ```
|
1116 | <a name="structure"></a>
|
1117 | #structure(structure, [value], [...additionalArgs])
|
1118 | Checks whether an object satisfies predicates defined in structure
|
1119 |
|
1120 | NOTE: All predicates defined in structure must be satisfied.
|
1121 | If some of the properties are optional use [undefinedOr](#undefinedOr)
|
1122 |
|
1123 | You shouldn't use this function to validate input from the user and expect complex report "what's wrong with this is object".
|
1124 | There are few reasons for that:
|
1125 | * it's just a predicate (that returns only true or false)
|
1126 | * breaks [the design rule](design.md#user-content-defined-and-generated-predicates-will-not-throw-any-errors)
|
1127 |
|
1128 | See examples for inspiration how you can use _structure_
|
1129 |
|
1130 | **Params**
|
1131 |
|
1132 | - structure `Object`
|
1133 | - \[value\] `Object`
|
1134 | - \[...additionalArgs\] `*` - additional arguments passed to the predicates
|
1135 |
|
1136 | **Returns**: `Boolean` | [Predicate](#Predicate) - returns bool if more than 1 argument provided, otherwise a predicate
|
1137 | **Example**
|
1138 | ```js
|
1139 | // simple object matching
|
1140 | var is = require('predicates');
|
1141 |
|
1142 | var schema = {
|
1143 | name: is.string, // only string
|
1144 | phone: is.or(is.string, is.number), // string or number
|
1145 | surname: is.undefinedOr(is.string) // optional
|
1146 | },
|
1147 | isPerson = is.structure(schema);
|
1148 |
|
1149 | var person = {name: 'Tommy', phone: 80129292};
|
1150 | isPerson(person); // true
|
1151 | // same as
|
1152 | is.structure(schema, person); // true
|
1153 | isPerson({name: 'Tommy'});
|
1154 | ```
|
1155 | **Example**
|
1156 | ```js
|
1157 | // filtering
|
1158 | var is = require('predicates');
|
1159 |
|
1160 | var people = [
|
1161 | {name: 'Prof. Bend Ovah', age: 55, sex: 'male'},
|
1162 | {name: 'Dr. Supa Kaki', age: 34, sex: 'female'},
|
1163 | {name: 'Prof. Anti Santy', age: 46, sex: 'male'}
|
1164 | ];
|
1165 |
|
1166 | var professors = people.filter(is.structure({
|
1167 | name: is.startsWith('Prof.')
|
1168 | }));
|
1169 |
|
1170 | // [
|
1171 | // {name: 'Prof. Bend Ovah', age: 55, sex: 'male'},
|
1172 | // {name: 'Prof. Anti Santy', age: 46, sex: 'male'}
|
1173 | // ]
|
1174 | ```
|
1175 | **Example**
|
1176 | ```js
|
1177 | // duck typing
|
1178 |
|
1179 | var isDuck = is.structure({
|
1180 | quack: is.function,
|
1181 | walk: is.function
|
1182 | });
|
1183 |
|
1184 | isDuck({
|
1185 | say: function() { return 'woof! woof!';
|
1186 | }}); // not a duck
|
1187 |
|
1188 | isDuck({
|
1189 | quack: function() { return 'quack!'; },
|
1190 | walk: function() { return 'tup tup tup'; }
|
1191 | }); // yep, it's a duck
|
1192 | ```
|
1193 | <a name="true"></a>
|
1194 | #true(value)
|
1195 | Checks whether a value is a boolean true
|
1196 |
|
1197 | **Params**
|
1198 |
|
1199 | - value `Boolean`
|
1200 |
|
1201 | **Returns**: `Boolean`
|
1202 | **Example**
|
1203 | ```js
|
1204 | var is = require('predicates');
|
1205 |
|
1206 | is.true(true); // true
|
1207 | is.true('true'); // false
|
1208 | ```
|
1209 | <a name="truthy"></a>
|
1210 | #truthy(value)
|
1211 | Checks whether a value is truthy
|
1212 |
|
1213 | **Params**
|
1214 |
|
1215 | - value `*`
|
1216 |
|
1217 | **Returns**: `Boolean`
|
1218 | **Example**
|
1219 | ```js
|
1220 | var is = require('predicates');
|
1221 |
|
1222 | is.truthy(true); // true
|
1223 | is.truthy(1); // true
|
1224 | is.truthy(0); // false
|
1225 | ```
|
1226 | <a name="undefined"></a>
|
1227 | #undefined(value)
|
1228 | Checks whether a value is undefined
|
1229 |
|
1230 | **Params**
|
1231 |
|
1232 | - value `*`
|
1233 |
|
1234 | **Returns**: `Boolean`
|
1235 | **Example**
|
1236 | ```js
|
1237 | var is = require('predicates');
|
1238 |
|
1239 | is.undefined(undefined); // true
|
1240 | is.undefined(0); // false
|
1241 | ```
|
1242 | <a name="undefinedOr"></a>
|
1243 | #undefinedOr(predicate, [value])
|
1244 | Checks whether a value is undefined or satisfies given predicate
|
1245 | Very useful to check optional arguments of function.
|
1246 |
|
1247 | **Params**
|
1248 |
|
1249 | - predicate <code>[Predicate](#Predicate)</code>
|
1250 | - \[value\] `*`
|
1251 |
|
1252 | **Returns**: `Boolean` | [Predicate](#Predicate) - returns bool if more than 1 argument provided, otherwise a predicate
|
1253 | **Example**
|
1254 | ```js
|
1255 | var is = require('predicates');
|
1256 |
|
1257 | var isUndefinedOrString = is.undefinedOr(is.string);
|
1258 |
|
1259 | isUndefinedOrString(undefined); // true
|
1260 | isUndefinedOrString('test'); // true
|
1261 | // same as
|
1262 | is.undefinedOr(is.string, undefined); // true
|
1263 | is.undefinedOr(is.string, 'test'); // true
|
1264 |
|
1265 | isUndefinedOrString({}); // false
|
1266 | ```
|
1267 | <a name="Predicate"></a>
|
1268 | #type: Predicate
|
1269 | A function that returns true or false based on input arguments
|
1270 |
|
1271 | **Params**
|
1272 |
|
1273 | - ...arg `*`
|
1274 |
|
1275 | **Returns**: `Boolean`
|