-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.js
105 lines (75 loc) · 2.17 KB
/
util.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
/*
util.js
*/
define(['underscore'], function(_) {
function fmt(s) {
var args = Array.prototype.slice.call(arguments, 1);
return s.replace(/{(\d+)}/g, function(match, number) {
return typeof args[number] != 'undefined' ? args[number] : match;
});
}
return {
/*
Inspection functions
*/
log: function() {
console.log.apply(console, arguments);
},
dir: function(obj) {
for (var k in obj) {
if (obj.__proto__ && !obj.hasOwnProperty(k))
continue;
console.log(fmt('{0}: {1}', k, obj[k]));
}
},
inspect: function(obj, name) {
function str(x) {return typeof x === 'function' ? '[function]' : x}
if (name)
console.log('*' + name + '*\n')
console.log('Prototype members:');
for (var k in obj.__proto__)
console.log(' ' + k + ': ' + str(obj.__proto__[k]));
console.log('\nInstance members:');
for (var k in obj) {
if (obj.hasOwnProperty(k))
console.log(' ' + k + ': ' + str(obj[k]));
}
console.log('\n');
},
/*
misc
*/
even: function(n) {
return n % 2 == 0;
},
odd: function(n) {
return n % 2 == 1;
},
assert: function(cond, error) {
if (!cond) throw error;
},
assert_many: function(conds, error) {
for (var i=0; i < conds.length; i++)
if (!conds[i]) throw error;
},
assert_false: function(cond, error) {
if (cond) throw error;
},
/*
Collection functions
*/
slice: function(arr) {
return Array.prototype.slice.apply(arr, _.rest(arguments));
},
/*
String functions
*/
fmt: fmt,
is_lower: function(s) {
return s == s.toLowerCase();
},
is_upper: function(s) {
return s == s.toUpperCase();
}
}
});