UNPKG

27.6 kBMarkdownView Raw
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)
63Returns 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
78var is = require('predicates');
79var isNumberGreaterThan10 = is.all(is.number, is.greaterThan(10));
80
81isNumberGreaterThan10(0); // false
82isNumberGreaterThan10(11); // true
83isNumberGreaterThan10('11'); // false
84```
85<a name="any"></a>
86#any(...predicate)
87Returns 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
102var is = require('predicates');
103
104var isStringOrNumber = is.any(is.string, is.number);
105
106isStringOrNumber(0); // true
107isStringOrNumber('string'); // true
108isStringOrNumber(undefined); // false
109```
110<a name="array"></a>
111#array(value)
112Checks whether a value is an array
113
114**Aliases** _arr_
115
116**Params**
117
118- value `*`
119
120**Returns**: `Boolean`
121**Example**
122```js
123var is = require('predicates');
124
125is.array([]); // true
126is.array({}); // false
127```
128<a name="arrayLike"></a>
129#arrayLike(value)
130Checks whether a value looks like an array
131That 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
145var is = require('predicates');
146
147is.arrayLike(arguments); // true
148is.arrayLike(document.querySelectorAll('div')); // true
149is.arrayLike([1, 2, 3]); // true
150is.arrayLike({}); // false
151```
152<a name="arrayOf"></a>
153#arrayOf(predicate, [value], [...additionalArgs])
154Checks 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
171var is = require('predicates');
172
173var isArrayOfStrings = is.arrayOf(is.string);
174
175isArrayOfStrings(['1', '2']); // true
176// same as
177is.arrayOf(is.string, ['1', '2']); // true
178
179isArrayOfStrings([1, 2]); // false
180```
181<a name="blank"></a>
182#blank(value)
183Checks 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
192var is = require('predicates');
193
194is.blank(''); // true
195is.blank(' '); // true
196is.blank('test'); // false
197```
198<a name="boolean"></a>
199#boolean(value)
200Checks whether a value is a boolean
201
202**Aliases** _bool_
203
204**Params**
205
206- value `*`
207
208**Returns**: `Boolean`
209**Example**
210```js
211var is = require('predicates');
212
213is.boolean(true); // true
214is.boolean(false);; // true
215is.boolean(0); // false
216```
217<a name="date"></a>
218#date(value)
219Checks whether a value is a Date object
220
221**Params**
222
223- value `*`
224
225**Returns**: `Boolean`
226**Example**
227```js
228var is = require('predicates');
229
230is.date(new Date()); true
231is.date(1415402574000); // false
232```
233<a name="defined"></a>
234#defined(value)
235Checks 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
244var is = require('predicates');
245
246is.defined(''); // true
247is.defined(1); // true
248is.defined(undefined); // false
249```
250<a name="divisible"></a>
251#divisible(divisor, [value])
252Checks 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
270var is = require('predicates');
271
272is.divisible(7, 14); // true
273is.divisible(7)(14); // true
274is.divisible(7, 10); // false
275```
276<a name="divisibleWithRemainder"></a>
277#divisibleWithRemainder(divisor, remainder, [value])
278Checks whether a value is a number and it's divisible by divisor with given remainder
279In 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
304var is = require('predicates');
305
306is.divisibleWithRemainder(3, 2, 5); // true since 5%3 === 2
307is.divisibleWithRemainder(3, 2)(5); // true
308is.divisibleWithRemainder(3, 1, 5); // false since 5%3 !== 1
309
310var isEven = is.divisibleWithRemainder(2, 1);
311
312isEven(1); // true
313isEven(2); // false
314isEven(3); // true
315
316*
317```
318<a name="empty"></a>
319#empty(value)
320Checks whether a value is empty
321Value 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
333var is = require('predicates');
334
335is.empty(''); // true
336is.empty([]); // true
337is.empty({}); // true
338is.empty([1]); // false
339is.empty('test'); // false
340```
341<a name="endsWith"></a>
342#endsWith(suffix, [value])
343Checks 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
359var is = require('predicates');
360
361var isYelling = is.endsWith('!');
362
363isYelling('shut up!'); // true
364// same as
365is.endsWith('!', 'shut up!'); // true
366isYelling('be quiet please'); // false
367```
368<a name="equal"></a>
369#equal(expected, [value])
370Checks 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
382var is = require('predicates');
383
384var isTimmy = is.equal('Timmy');
385
386isTimmy('Timmy'); // true
387// same as
388is.equal('Timmy', 'Timmy'); // true
389is.equal(1, '1'); // true
390isTimmy('Franko'); // false
391```
392<a name="false"></a>
393#false(value)
394Checks whether a value is false a boolean false
395
396**Params**
397
398- value `*`
399
400**Returns**: `Boolean`
401**Example**
402```js
403var is = require('predicates');
404
405is.false(false); // true
406is.false(0); // false
407```
408<a name="falsy"></a>
409#falsy(value)
410Checks whether a value is falsy
411
412**Aliases** _falsey_
413
414**Params**
415
416- value `*`
417
418**Returns**: `Boolean`
419**Example**
420```js
421var is = require('predicates');
422
423is.falsy(0); // true
424is.falsy(false); // true
425is.falsy(1); // false
426```
427<a name="finite"></a>
428#finite(value)
429Checks whether a value is a number and it's finite
430
431**Params**
432
433- value `*`
434
435**Returns**: `Boolean`
436**Example**
437```js
438var is = require('predicates');
439
440is.finite(1); // false
441is.finite(Infinity); // false
442```
443<a name="function"></a>
444#function(value)
445Checks 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
456var is = require('predicates');
457
458is.function(function() {}); // true
459is.function(alert); // true
460is.function('alert'); // false
461```
462<a name="greaterThan"></a>
463#greaterThan(expected, [value])
464Checks 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
476var is = require('predicates');
477
478var isGreaterThan0 = is.greaterThan(0);
479
480isGreaterThan0(10); // true
481// same as
482is.greaterThan(0, 10); // true
483isGreaterThan0(-1); // false
484```
485<a name="greaterThanOrEqual"></a>
486#greaterThanOrEqual(expected, [value])
487Checks 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
499var is = require('predicates');
500
501var isAdultAge = is.greaterThanOrEqual(18);
502
503isAdultAge(22); // true
504// same as
505is.greaterThanOrEqual(18, 22);
506
507isAdultAge(16); // false
508```
509<a name="hasOwnProperty"></a>
510#hasOwnProperty(property, [object])
511Checks 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
527var is = require('predicates');
528
529var isCustomized = is.hasOwnProperty('delay');
530
531var Timer = function() {};
532Timer.prototype.delay = 100;
533
534var timer1 = new Timer();
535var timer2 = new Timer();
536timer1.delay = 1000;
537
538isCustomized(timer1) // true
539// same as
540is.hasOwnProperty('delay', timer1); // true
541
542isCustomized(timer2); // false
543```
544<a name="hasProperty"></a>
545#hasProperty(property, [object])
546Checks 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
562var is = require('predicates');
563
564var isDuck = is.hasProperty('quack');
565
566isDuck({quack: ':)'}); // true
567// same as
568is.hasProperty('quack', {quack: ':)'}); // true
569
570isDuck({type: 'car'}); // false
571```
572<a name="in"></a>
573#in(collection, [value])
574Checks whether a value exists in collection
575Values 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
591var is = require('predicates');
592
593var isImage = is.in(['image/gif', 'image/jpeg']);
594// same as
595// var isImage = is.oneOf('image/gif', 'image/jpeg');
596
597isImage('image/jpeg'); // true
598// same as
599is.in(['image/gif', 'image/jpeg'], 'image/jpeg'); // true
600
601isImage('text/html'); // false
602```
603<a name="instanceOf"></a>
604#instanceOf(clazz, [value])
605Checks 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
621var is = require('predicates');
622
623var Duck = function() {};
624var Car = function() {};
625
626var isDuck = is.instanceOf(Duck);
627
628isDuck(new Duck); // true
629// same as
630is.instanceOf(Duck, new Duck); // true
631
632isDuck(new Car); // false
633```
634<a name="integer"></a>
635#integer(value)
636Checks whether a value is an integer
637
638**Aliases** _int_
639
640**Params**
641
642- value `*`
643
644**Returns**: `Boolean`
645**Example**
646```js
647var is = require('predicates');
648
649is.integer(10); // true
650is.integer(10.4); // false
651```
652<a name="lessThan"></a>
653#lessThan(expected, [value])
654Checks 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
666var isChildAge = is.lessThan(18);
667
668isChildAge(10); // true
669// same as
670is.lessThan(18, 10); // true
671isChildAge(18); // false
672isChildAge(22); // false
673```
674<a name="lessThanOrEqual"></a>
675#lessThanOrEqual(expected, [value])
676Checks 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
688var is = require('predicates');
689
690var isChildAge = is.lessThanOrEqual(17);
691
692isChildAge(10); // true
693// same as
694is.lessThanOrEqual(17, 10); // true
695
696isChildAge(18); // false
697```
698<a name="matches"></a>
699#matches(regexp, [value])
700Checks 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
716var is = require('predicates');
717
718var isWindows9x = is.matches(/^Windows 9/);
719
720isWindows9x('Windows 9'); // true - :D
721// same as
722is.matches(/^Windows 9/, 'Windows 9'); // also true - hue hue
723
724isWindows9x('Windows 10); // false
725```
726<a name="NaN"></a>
727#NaN(value)
728Checks 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
739var is = require('predicates');
740
741is.NaN(NaN); // true
742is.NaN(10); // false
743```
744<a name="negative"></a>
745#negative(value)
746Checks whether a value is a negative number
747
748**Params**
749
750- value `Number`
751
752**Returns**: `Boolean`
753**Example**
754```js
755var is = require('predicates');
756
757is.negative(-1); // true
758is.negative(0); // false
759```
760<a name="not"></a>
761#not(predicate, [value], [...additionalArgs])
762Negates 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
775var is = require('predicates');
776
777var isNotEmpty = is.not(is.empty);
778isNotEmpty([1, 2]);// true
779// same as
780is.not(is.empty, [1, 2]); // true
781isNotEmpty(''); // false
782```
783<a name="notBlank"></a>
784#notBlank(value)
785Checks 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
794var is = require('predicates');
795
796is.notBlank(''); // false
797is.notBlank(' '); // false
798is.notBlank('test'); // true
799is.notBlank({toString: function() { return 'test'; }}); // false - since it's not a string
800```
801<a name="notEmpty"></a>
802#notEmpty(value)
803Checks whether value is not empty.
804
805See [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
814var is = require('predicates');
815
816is.notEmpty([1]); // true
817is.notEmpty('value'); // true
818is.notEmpty([]); // false
819is.notEmpty(''); // false
820```
821<a name="null"></a>
822#null(value)
823Checks whether a value is null
824
825**Params**
826
827- value `*`
828
829**Returns**: `Boolean`
830**Example**
831```js
832var is = require('predicates');
833
834is.null(null); // true
835is.null({}); // false
836```
837<a name="number"></a>
838#number(value)
839Checks whether a value is a number
840
841**Aliases** _num_
842
843**Params**
844
845- value `*`
846
847**Returns**: `Boolean`
848**Example**
849```js
850var is = require('predicates');
851
852is.number(10); // true
853is.number('10'); // false
854```
855<a name="object"></a>
856#object(value)
857Checks whether a value is an object
858
859**Aliases** _obj_
860
861**Params**
862
863- value `*`
864
865**Returns**: `Boolean`
866**Example**
867```js
868var is = require('predicates');
869
870is.object({}); // true
871is.object('object'); // false
872```
873<a name="objectOf"></a>
874#objectOf(predicate, [object], [...additionalArgs])
875Checks 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
888var is = require('predicates');
889
890var isObjectOfStrings = is.objectOf(is.string);
891
892isObjectOfStrings({key: 'value', key1: 'value'}); // true
893// same as
894is.objectOf(is.string, {key: 'value', key1: 'value'}); // true
895
896isObjectOfStrings({key: 1, key1: 'value'}); // false
897```
898<a name="oneOf"></a>
899#oneOf(...allowedValue)
900Returns a function that checks whether a value is equal to one of allowed values
901Function 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
914var is = require('predicates');
915
916var isAllowedToAccess = is.oneOf('ROLE_ADMIN', 'ROLE_USER');
917// same as
918// var isAllowedToAccess = is.in(['ROLE_ADMIN', 'ROLE_USER']);
919
920isAllowedToAccess('ROLE_ADMIN'); // true
921isAllowedToAccess('ROLE_ANONYMOUS'); // false
922```
923<a name="plainObject"></a>
924#plainObject(value)
925Checks whether a value is a plain object.
926Plain 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
935var is = require('predicates');
936
937is.plainObject({property: 'value'}); // true
938is.plainObject(new Object); // true
939is.plainObject(Object.create(null)); // true
940is.plainObject(new String('test')); // false
941
942var Foo = function() {};
943is.plainObject(new Foo); // false
944```
945<a name="positive"></a>
946#positive(value)
947Checks whether a value is a positive number
948
949**Params**
950
951- value `Number`
952
953**Returns**: `Boolean`
954**Example**
955```js
956var is = require('predicates');
957
958is.positive(10); // true
959is.positive(-1); // false
960```
961<a name="primitive"></a>
962#primitive(value)
963Checks whether a value is a primitive.
964
965Helpful 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
969NOTE! A primitive value wrapped by a corresponding object is not a primitive anymore
970```js
971var a = 'test' // this is a primitive
972a = new String('test'); // and this is not a primitive
973```
974
975**Params**
976
977- value `*`
978
979**Returns**: `Boolean`
980**Example**
981```js
982var is = require('predicates');
983
984is.primitive('test'); // true
985is.primitive(undefined); // true
986is.primitive(10); // true
987is.primitive(null); // true
988is.primitive(false); // true
989
990is.primitive(new Number(10)); // false
991is.primitive(new String('test')); // false
992is.primitive(new Boolean(true)); // false
993is.primitive({}); // false
994```
995<a name="property"></a>
996#property(propertyName, predicate, [value], [...additionalArgs])
997Checks whether a value of given property of an object satisfies a predicate
998
999If you need to check more properties at a time use [structure](#structure).
1000
1001NOTE! 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
1021var is = require('predicates');
1022
1023is.property('name', is.string, {name: 'Tommy'}); // true
1024is.property('name', is.string)({name: 'Tommy'}); // true
1025is.property('name', is.string, {name: 2}); // false - since 2 is not a string
1026is.property('name', is.string, {}); // false - since undefined is not a string
1027```
1028<a name="regExp"></a>
1029#regExp(value)
1030Checks whether a value is a regexp
1031
1032**Aliases** _regexp_
1033
1034**Params**
1035
1036- value `*`
1037
1038**Returns**: `Boolean`
1039**Example**
1040```js
1041var is = require('predicates');
1042
1043is.regExp(/t/); // true
1044is.regExp(new RegExp(/t/)); // true
1045is.regExp('.*'); // false
1046```
1047<a name="startsWith"></a>
1048#startsWith(prefix, [value])
1049Checks 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
1065var is = require('predicates');
1066
1067var isProfessor = is.startsWith('Prof. ');
1068isProfessor('Prof. Bend Ovah'); // true
1069// same as
1070is.startsWith('Prof. ', 'Prof. Bend Ovah'); // true
1071
1072isProfessor('Dr. Here U\' Are'); // false
1073```
1074<a name="strictEqual"></a>
1075#strictEqual(expected, [value])
1076Checks 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
1088var is = require('predicates');
1089
1090var mom = {};
1091var isMyMom = is.strictEqual(mom);
1092
1093isMyMom(mom); // true - mom is only one. Remember about it...
1094// same as
1095is.strictEqual(mom, mom); // true
1096isMyMom({}); // false
1097```
1098<a name="string"></a>
1099#string(value)
1100Checks whether a value is a function
1101
1102**Aliases** _str_
1103
1104**Params**
1105
1106- value `*`
1107
1108**Returns**: `Boolean`
1109**Example**
1110```js
1111var is = require('predicates');
1112
1113is.string('test'); // true
1114is.string({}); // false
1115```
1116<a name="structure"></a>
1117#structure(structure, [value], [...additionalArgs])
1118Checks whether an object satisfies predicates defined in structure
1119
1120NOTE: All predicates defined in structure must be satisfied.
1121If some of the properties are optional use [undefinedOr](#undefinedOr)
1122
1123You shouldn't use this function to validate input from the user and expect complex report "what's wrong with this is object".
1124There 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
1128See 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
1140var is = require('predicates');
1141
1142var 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
1149var person = {name: 'Tommy', phone: 80129292};
1150isPerson(person); // true
1151// same as
1152is.structure(schema, person); // true
1153isPerson({name: 'Tommy'});
1154```
1155**Example**
1156```js
1157// filtering
1158var is = require('predicates');
1159
1160var 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
1166var 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
1179var isDuck = is.structure({
1180 quack: is.function,
1181 walk: is.function
1182});
1183
1184isDuck({
1185 say: function() { return 'woof! woof!';
1186}}); // not a duck
1187
1188isDuck({
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)
1195Checks whether a value is a boolean true
1196
1197**Params**
1198
1199- value `Boolean`
1200
1201**Returns**: `Boolean`
1202**Example**
1203```js
1204var is = require('predicates');
1205
1206is.true(true); // true
1207is.true('true'); // false
1208```
1209<a name="truthy"></a>
1210#truthy(value)
1211Checks whether a value is truthy
1212
1213**Params**
1214
1215- value `*`
1216
1217**Returns**: `Boolean`
1218**Example**
1219```js
1220var is = require('predicates');
1221
1222is.truthy(true); // true
1223is.truthy(1); // true
1224is.truthy(0); // false
1225```
1226<a name="undefined"></a>
1227#undefined(value)
1228Checks whether a value is undefined
1229
1230**Params**
1231
1232- value `*`
1233
1234**Returns**: `Boolean`
1235**Example**
1236```js
1237var is = require('predicates');
1238
1239is.undefined(undefined); // true
1240is.undefined(0); // false
1241```
1242<a name="undefinedOr"></a>
1243#undefinedOr(predicate, [value])
1244Checks whether a value is undefined or satisfies given predicate
1245Very 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
1255var is = require('predicates');
1256
1257var isUndefinedOrString = is.undefinedOr(is.string);
1258
1259isUndefinedOrString(undefined); // true
1260isUndefinedOrString('test'); // true
1261// same as
1262is.undefinedOr(is.string, undefined); // true
1263is.undefinedOr(is.string, 'test'); // true
1264
1265isUndefinedOrString({}); // false
1266```
1267<a name="Predicate"></a>
1268#type: Predicate
1269A function that returns true or false based on input arguments
1270
1271**Params**
1272
1273- ...arg `*`
1274
1275**Returns**: `Boolean`