-
Notifications
You must be signed in to change notification settings - Fork 1
/
helpers.js
61 lines (58 loc) · 1.65 KB
/
helpers.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
// Lifted directly from MDN
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/repeat
var repeat = function(string, count) {
'use strict';
if (string == null) {
throw new TypeError('can\'t convert ' + string + ' to object');
}
var str = '' + string;
count = +count;
if (count != count) {
count = 0;
}
if (count < 0) {
throw new RangeError('repeat count must be non-negative');
}
if (count == Infinity) {
throw new RangeError('repeat count must be less than infinity');
}
count = Math.floor(count);
if (str.length == 0 || count == 0) {
return '';
}
// Ensuring count is a 31-bit integer allows us to heavily optimize the
// main part. But anyway, most current (august 2014) browsers can't handle
// strings 1 << 28 chars or longer, so:
if (str.length * count >= 1 << 28) {
throw new RangeError('repeat count must not overflow maximum string size');
}
var rpt = '';
for (;;) {
if ((count & 1) == 1) {
rpt += str;
}
count >>>= 1;
if (count == 0) {
break;
}
str += str;
}
return rpt;
};
var pretty = function(prog, indent) {
return JSON.stringify(prog, function(key, value) {
if (_.isFunction(value)) {
var name = value.name || "(anonymous)";
return "function " + name;
}
return value;
}, 2);
};
var log = function() {
console.log(">>> " + Array.prototype.slice.call(arguments).map(pretty).join("\n"));
};
module.exports = {
repeat: repeat,
pretty: pretty,
log: log
};