UNPKG

4.03 kBJavaScriptView Raw
1/*global HTMLElement DocumentTouch */
2
3import window from './window';
4
5let navigator = window ? window.navigator : null;
6let document = window ? window.document : null;
7
8let typeofstr = typeof '';
9let typeofobj = typeof {};
10let typeoffn = typeof function(){};
11let typeofhtmlele = typeof HTMLElement;
12
13let instanceStr = function( obj ){
14 return obj && obj.instanceString && fn( obj.instanceString ) ? obj.instanceString() : null;
15};
16
17export const defined = obj =>
18 obj != null; // not undefined or null
19
20export const string = obj =>
21 obj != null && typeof obj == typeofstr;
22
23export const fn = obj =>
24 obj != null && typeof obj === typeoffn;
25
26export const array = obj =>
27 !(elementOrCollection(obj)) && (Array.isArray ? Array.isArray( obj ) : obj != null && obj instanceof Array);
28
29export const plainObject = obj =>
30 obj != null && typeof obj === typeofobj && !array( obj ) && obj.constructor === Object;
31
32export const object = obj =>
33 obj != null && typeof obj === typeofobj;
34
35export const number = obj =>
36 obj != null && typeof obj === typeof 1 && !isNaN( obj );
37
38export const integer = obj =>
39 number( obj ) && Math.floor( obj ) === obj;
40
41export const bool = obj =>
42 obj != null && typeof obj === typeof true;
43
44export const htmlElement = obj => {
45 if( 'undefined' === typeofhtmlele ){
46 return undefined;
47 } else {
48 return null != obj && obj instanceof HTMLElement;
49 }
50};
51
52export const elementOrCollection = obj =>
53 element( obj ) || collection( obj );
54
55export const element = obj =>
56 instanceStr( obj ) === 'collection' && obj._private.single;
57
58export const collection = obj =>
59 instanceStr( obj ) === 'collection' && !obj._private.single;
60
61export const core = obj =>
62 instanceStr( obj ) === 'core';
63
64export const style = obj =>
65 instanceStr( obj ) === 'style';
66
67export const stylesheet = obj =>
68 instanceStr( obj ) === 'stylesheet';
69
70export const event = obj =>
71 instanceStr( obj ) === 'event';
72
73export const thread = obj =>
74 instanceStr( obj ) === 'thread';
75
76export const fabric = obj =>
77 instanceStr( obj ) === 'fabric';
78
79export const emptyString = obj => {
80 if( obj === undefined || obj === null ){ // null is empty
81 return true;
82 } else if( obj === '' || obj.match( /^\s+$/ ) ){
83 return true; // empty string is empty
84 }
85
86 return false; // otherwise, we don't know what we've got
87};
88
89export const nonemptyString = obj => {
90 if( obj && string( obj ) && obj !== '' && !obj.match( /^\s+$/ ) ){
91 return true;
92 }
93
94 return false;
95};
96
97export const domElement = obj => {
98 if( typeof HTMLElement === 'undefined' ){
99 return false; // we're not in a browser so it doesn't matter
100 } else {
101 return obj instanceof HTMLElement;
102 }
103};
104
105export const boundingBox = obj =>
106 plainObject( obj ) &&
107 number( obj.x1 ) && number( obj.x2 ) &&
108 number( obj.y1 ) && number( obj.y2 )
109 ;
110
111export const promise = obj =>
112 object( obj ) && fn( obj.then );
113
114export const touch = () =>
115 window && ( ('ontouchstart' in window) || window.DocumentTouch && document instanceof DocumentTouch );
116
117export const gecko = () =>
118 window && ( typeof InstallTrigger !== 'undefined' || ('MozAppearance' in document.documentElement.style) );
119
120export const webkit = () =>
121 window && ( typeof webkitURL !== 'undefined' || ('WebkitAppearance' in document.documentElement.style) );
122
123export const chromium = () =>
124 window && ( typeof chrome !== 'undefined' );
125
126export const khtml = () =>
127 navigator && navigator.vendor.match( /kde/i ); // probably a better way to detect this...
128
129export const khtmlEtc = () =>
130 khtml() || webkit() || chromium();
131
132export const ms = () =>
133 navigator && navigator.userAgent.match( /msie|trident|edge/i ); // probably a better way to detect this...
134
135export const windows = () =>
136 navigator && navigator.appVersion.match( /Win/i );
137
138export const mac = () =>
139 navigator && navigator.appVersion.match( /Mac/i );
140
141export const linux = () =>
142 navigator && navigator.appVersion.match( /Linux/i );
143
144export const unix = () =>
145 navigator && navigator.appVersion.match( /X11/i );