-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
326 lines (309 loc) · 7.22 KB
/
index.js
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/**
* Isomorphic, functional type-checking for Javascript.
* @module typical
* @typicalname t
* @example
* import t from 'typical'
* const allDefined = array.every(t.isDefined)
*/
/**
* Returns true if input is a number (including infinity). It is a more reasonable alternative to `typeof n` which returns `number` for `NaN`.
*
* @param {*} n - The input to test
* @returns {boolean} `true` if input is a number
* @static
* @example
* > t.isNumber(0)
* true
* > t.isNumber(1)
* true
* > t.isNumber(1.1)
* true
* > t.isNumber(0xff)
* true
* > t.isNumber(0644)
* true
* > t.isNumber(6.2e5)
* true
* > t.isNumber(NaN)
* false
* > t.isNumber(Infinity)
* true
*/
export function isNumber (n) {
return !isNaN(parseFloat(n))
}
/**
* Returns true if input is a finite number. Identical to `isNumber` beside excluding infinity.
*
* @param {*} n - The input to test
* @returns {boolean}
* @static
* @example
* > t.isFiniteNumber(0)
* true
* > t.isFiniteNumber(1)
* true
* > t.isFiniteNumber(1.1)
* true
* > t.isFiniteNumber(0xff)
* true
* > t.isFiniteNumber(0644)
* true
* > t.isFiniteNumber(6.2e5)
* true
* > t.isFiniteNumber(NaN)
* false
* > t.isFiniteNumber(Infinity)
* false
*/
export function isFiniteNumber (n) {
return !isNaN(parseFloat(n)) && isFinite(n)
}
/**
* A plain object is a simple object literal, it is not an instance of a class. Returns true if the input `typeof` is `object` and directly decends from `Object`.
*
* @param {*} input - The input to test
* @returns {boolean}
* @static
* @example
* > t.isPlainObject({ something: 'one' })
* true
* > t.isPlainObject(new Date())
* false
* > t.isPlainObject([ 0, 1 ])
* false
* > t.isPlainObject(/test/)
* false
* > t.isPlainObject(1)
* false
* > t.isPlainObject('one')
* false
* > t.isPlainObject(null)
* false
* > t.isPlainObject((function * () {})())
* false
* > t.isPlainObject(function * () {})
* false
*/
export function isPlainObject (input) {
return input !== null && typeof input === 'object' && input.constructor === Object
}
/**
* An array-like value has all the properties of an array yet is not an array instance. An example is the `arguments` object. Returns `true`` if the input value is an object, not `null`` and has a `length` property set with a numeric value.
*
* @param {*} input - The input to test
* @returns {boolean}
* @static
* @example
* function sum(x, y){
* console.log(t.isArrayLike(arguments))
* // prints `true`
* }
*/
export function isArrayLike (input) {
return isObject(input) && typeof input.length === 'number'
}
/**
* Returns true if the typeof input is `'object'` but not null.
* @param {*} input - The input to test
* @returns {boolean}
* @static
*/
export function isObject (input) {
return typeof input === 'object' && input !== null
}
/**
* Returns true if the input value is defined.
* @param {*} input - The input to test
* @returns {boolean}
* @static
*/
export function isDefined (input) {
return typeof input !== 'undefined'
}
/**
* Returns true if the input value is undefined.
* @param {*} input - The input to test
* @returns {boolean}
* @static
*/
export function isUndefined (input) {
return !isDefined(input)
}
/**
* Returns true if the input value is null.
* @param {*} input - The input to test
* @returns {boolean}
* @static
*/
export function isNull (input) {
return input === null
}
/**
* Returns true if the input value is not one of `undefined`, `null`, or `NaN`.
* @param {*} input - The input to test
* @returns {boolean}
* @static
*/
export function isDefinedValue (input) {
return isDefined(input) && !isNull(input) && !Number.isNaN(input)
}
/**
* Returns true if the input value is an ES2015 `class`.
* @param {*} input - The input to test
* @returns {boolean}
* @static
*/
export function isClass (input) {
if (typeof input === 'function') {
return /^class /.test(Function.prototype.toString.call(input))
} else {
return false
}
}
/**
* Returns true if the input is a string, number, symbol, boolean, null or undefined value.
* @param {*} input - The input to test
* @returns {boolean}
* @static
*/
export function isPrimitive (input) {
if (input === null) return true
switch (typeof input) {
case 'string':
case 'number':
case 'symbol':
case 'undefined':
case 'boolean':
return true
default:
return false
}
}
/**
* Returns true if the input is a Promise.
* @param {*} input - The input to test
* @returns {boolean}
* @static
*/
export function isPromise (input) {
if (input) {
const isPromise = isDefined(Promise) && input instanceof Promise
const isThenable = input.then && typeof input.then === 'function'
return !!(isPromise || isThenable)
} else {
return false
}
}
/**
* Returns true if the input is an iterable (`Map`, `Set`, `Array`, Generator etc.).
* @param {*} input - The input to test
* @returns {boolean}
* @static
* @example
* > t.isIterable('string')
* true
* > t.isIterable(new Map())
* true
* > t.isIterable([])
* true
* > t.isIterable((function * () {})())
* true
* > t.isIterable(Promise.resolve())
* false
* > t.isIterable(Promise)
* false
* > t.isIterable(true)
* false
* > t.isIterable({})
* false
* > t.isIterable(0)
* false
* > t.isIterable(1.1)
* false
* > t.isIterable(NaN)
* false
* > t.isIterable(Infinity)
* false
* > t.isIterable(function () {})
* false
* > t.isIterable(Date)
* false
* > t.isIterable()
* false
* > t.isIterable({ then: function () {} })
* false
*/
export function isIterable (input) {
if (input === null || !isDefined(input)) {
return false
} else {
return (
typeof input[Symbol.iterator] === 'function' ||
typeof input[Symbol.asyncIterator] === 'function'
)
}
}
/**
* Returns true if the input value is a string. The equivalent of `typeof input === 'string'` for use in funcitonal contexts.
* @param {*} input - The input to test
* @returns {boolean}
* @static
*/
export function isString (input) {
return typeof input === 'string'
}
/**
* Returns true if the input value is a function. The equivalent of `typeof input === 'function'` for use in funcitonal contexts.
* @param {*} input - The input to test
* @returns {boolean}
* @static
*/
export function isFunction (input) {
return typeof input === 'function'
}
/**
* Returns true if the input value is an async function or method.
* @param {*} input - The input to test
* @returns {boolean}
* @static
* @example
* > t.isAsyncFunction(function () {})
* false
* > t.isAsyncFunction(new Function())
* false
* > t.isAsyncFunction(() => {})
* false
* > t.isAsyncFunction(async function () {})
* true
* > const AsyncFunction = async function () {}.constructor
* > t.isAsyncFunction(new AsyncFunction())
* true
* > t.isAsyncFunction(async () => {})
* true
* > class Command { async execute () {} }
* > t.isAsyncFunction(new Command().execute)
* true
*/
export function isAsyncFunction (input) {
return (typeof input === 'function' && input.constructor.name === 'AsyncFunction')
}
export default {
isNumber,
isFiniteNumber,
isPlainObject,
isArrayLike,
isObject,
isDefined,
isUndefined,
isNull,
isDefinedValue,
isClass,
isPrimitive,
isPromise,
isIterable,
isString,
isFunction,
isAsyncFunction
}