File size: 4,026 Bytes
bc20498
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*global HTMLElement DocumentTouch */

import window from './window';

let navigator = window ? window.navigator : null;
let document = window ? window.document : null;

let typeofstr = typeof '';
let typeofobj = typeof {};
let typeoffn = typeof function(){};
let typeofhtmlele = typeof HTMLElement;

let instanceStr = function( obj ){
  return obj && obj.instanceString && fn( obj.instanceString ) ? obj.instanceString() : null;
};

export const defined = obj =>
  obj != null; // not undefined or null

export const string = obj =>
  obj != null && typeof obj == typeofstr;

export const fn = obj =>
  obj != null && typeof obj === typeoffn;

export const array = obj =>
  !(elementOrCollection(obj)) && (Array.isArray ? Array.isArray( obj ) : obj != null && obj instanceof Array);

export const plainObject = obj =>
  obj != null && typeof obj === typeofobj && !array( obj ) && obj.constructor === Object;

export const object = obj =>
  obj != null && typeof obj === typeofobj;

export const number = obj =>
  obj != null && typeof obj === typeof 1 && !isNaN( obj );

export const integer = obj =>
  number( obj ) && Math.floor( obj ) === obj;

export const bool = obj =>
  obj != null && typeof obj === typeof true;

export const htmlElement = obj => {
  if( 'undefined' === typeofhtmlele ){
    return undefined;
  } else {
    return null != obj && obj instanceof HTMLElement;
  }
};

export const elementOrCollection = obj =>
  element( obj ) || collection( obj );

export const element = obj =>
  instanceStr( obj ) === 'collection' && obj._private.single;

export const collection = obj =>
  instanceStr( obj ) === 'collection' && !obj._private.single;

export const core = obj =>
  instanceStr( obj ) === 'core';

export const style = obj =>
  instanceStr( obj ) === 'style';

export const stylesheet = obj =>
  instanceStr( obj ) === 'stylesheet';

export const event = obj =>
  instanceStr( obj ) === 'event';

export const thread = obj =>
  instanceStr( obj ) === 'thread';

export const fabric = obj =>
  instanceStr( obj ) === 'fabric';

export const emptyString = obj => {
  if( obj === undefined || obj === null ){ // null is empty
    return true;
  } else if( obj === '' || obj.match( /^\s+$/ ) ){
    return true; // empty string is empty
  }

  return false; // otherwise, we don't know what we've got
};

export const nonemptyString = obj => {
  if( obj && string( obj ) && obj !== '' && !obj.match( /^\s+$/ ) ){
    return true;
  }

  return false;
};

export const domElement = obj => {
  if( typeof HTMLElement === 'undefined' ){
    return false; // we're not in a browser so it doesn't matter
  } else {
    return obj instanceof HTMLElement;
  }
};

export const boundingBox = obj =>
  plainObject( obj ) &&
    number( obj.x1 ) && number( obj.x2 ) &&
    number( obj.y1 ) && number( obj.y2 )
  ;

export const promise = obj =>
  object( obj ) && fn( obj.then );

export const touch = () =>
  window && ( ('ontouchstart' in window) || window.DocumentTouch && document instanceof DocumentTouch );

export const gecko = () =>
  window && ( typeof InstallTrigger !== 'undefined' || ('MozAppearance' in document.documentElement.style) );

export const webkit = () =>
  window && ( typeof webkitURL !== 'undefined' || ('WebkitAppearance' in document.documentElement.style) );

export const chromium = () =>
  window && ( typeof chrome !== 'undefined' );

export const khtml = () =>
  navigator && navigator.vendor.match( /kde/i ); // probably a better way to detect this...

export const khtmlEtc = () =>
  khtml() || webkit() || chromium();

export const ms = () =>
  navigator && navigator.userAgent.match( /msie|trident|edge/i ); // probably a better way to detect this...

export const windows = () =>
  navigator && navigator.appVersion.match( /Win/i );

export const mac = () =>
  navigator && navigator.appVersion.match( /Mac/i );

export const linux = () =>
  navigator && navigator.appVersion.match( /Linux/i );

export const unix = () =>
  navigator && navigator.appVersion.match( /X11/i );