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
|
var fs = require('fs');
var path = require('path');
var os = require('os');
function matchObject(item, str) {
return Object.prototype.toString.call(item)
=== '[object ' + str + ']';
}
function checkStat(name, fn) {
try {
return fn(name);
} catch (err) {
if (/^(ENOENT|EPERM|EACCES)$/.test(err.code)) {
if (err.code !== 'ENOENT') {
console.warn('Warning: Cannot access %s', name);
}
return false;
}
throw err;
}
}
var is = {
nil: function(item) {
return item == null;
},
array: function(item) {
return Array.isArray(item);
},
emptyObject: function(item) {
for (var key in item) {
return false;
}
return true;
},
buffer: function(item) {
return Buffer.isBuffer(item);
},
regExp: function(item) {
return matchObject(item, 'RegExp');
},
string: function(item) {
return matchObject(item, 'String');
},
func: function(item) {
return typeof item === 'function';
},
number: function(item) {
return matchObject(item, 'Number');
},
exists: function(name) {
return fs.existsSync(name);
},
file: function(name) {
return checkStat(name, function(n) {
return fs.statSync(n).isFile()
});
},
samePath: function(a, b) {
return path.resolve(a) === path.resolve(b);
},
directory: function(name) {
return checkStat(name, function(n) {
return fs.statSync(n).isDirectory()
});
},
symbolicLink: function(name) {
return checkStat(name, function(n) {
return fs.lstatSync(n).isSymbolicLink();
});
},
windows: function() {
return os.platform() === 'win32';
}
};
module.exports = is;
|