-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathUtils.js
110 lines (94 loc) · 2.72 KB
/
Utils.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
/**
* File: Utils.js
* Author: Lyn Turbak
* Created: 2020/06/18
*
* Helpful utilities for Venbrace parser
*/
function className(obj) {
if (obj && obj.constructor) {
return obj.constructor.name;
} else {
return 'noClassName';
}
}
// Returns zip two arrays, making an array of two-element arrays.
// Length of returned array is length of shorter of two array:
function zip (arr1, arr2) {
if (arr1.length <= arr2) {
return arr1.map( (arr1Elt, index) => [arr1Elt, arr2[index]] );
} else {
return arr2.map( (arr2Elt, index) => [arr1[index], arr2Elt] );
}
}
function spaces(n) {
// Acts like Python ' ' * n
return multiplyString(' ', n);
}
function newlines(n) {
// Acts like Python '\n' * n
return multiplyString('\n', n);
}
function dashes(n) {
// Acts like Python '-' * n
return multiplyString('-', n);
}
function multiplyString(str, n) {
// Acts like python str * n
// console.log("multiplyString('" + str + "', " + n + ")");
if (isInt(n)) {
if (n >= 0) {
return Array(n).fill(str).join('');
} else {
throw 'Utils.multiplyString given negative integer: ' + n.toString();
}
} else {
throw 'Utils.multiplyString given noninteger: ' + n.toString();
}
}
// https://stackoverflow.com/questions/14636536/how-to-check-if-a-variable-is-an-integer-in-javascript
function isInt(value) {
return !isNaN(value) && (function(x) { return (x | 0) === x; })(parseFloat(value))
}
function displayTimeTaken(msg, obj, thunk) {
var start = Date.now();
var result = thunk.call(obj);
var stop = Date.now();
console.log(msg + ((stop - start)/ 1000).toString() + ' seconds');
return result;
}
function capitalizeFirstLetter(str) {
if (str.length == 0) {
return str;
} else {
return str.charAt(0).toUpperCase() + str.slice(1);
}
}
function uncapitalizeFirstLetter(str) {
if (str.length == 0) {
return str;
} else {
return str.charAt(0).toLowerCase() + str.slice(1);
}
}
// Mutate arr1 by pushing all elements of arr2
// See https://stackoverflow.com/questions/4156101/copy-array-items-into-another-array
function pushArray(arr1, arr2) {
arr1.push.apply(arr1, arr2); // mutate arr1 by add all elements of arr2
return arr1; // Return modified arr1 for kicks.
}
function concatArrays(arrayOfArrays) {
return [].concat.apply([], arrayOfArrays);
}
exports.className = className;
exports.zip = zip;
exports.spaces = spaces;
exports.newlines = newlines;
exports.dashes = dashes;
exports.multiplyString = dashes;
exports.isInt = isInt;
exports.displayTimeTaken = displayTimeTaken;
exports.capitalizeFirstLetter = capitalizeFirstLetter;
exports.uncapitalizeFirstLetter = uncapitalizeFirstLetter;
exports.pushArray = pushArray;
exports.concatArrays = concatArrays;