-
Notifications
You must be signed in to change notification settings - Fork 63
Expand file tree
/
Copy pathutils.js
More file actions
73 lines (65 loc) · 1.95 KB
/
utils.js
File metadata and controls
73 lines (65 loc) · 1.95 KB
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
var fs = require('fs'),
path = require('path');
String.prototype.escapeSpecialChars = function() {
return this.replace(/\n/g, '\\n')
.replace(/\r/g, '\\r')
.replace(/\t/g, '\\t')
.replace(/\f/g, '\\f')
.replace(/\u0008/g, '\\u0008') // \b
.replace(/\v/g, '\\u000b') // \v
.replace(/\0/g, '\\u0000') // \0
.replace(/\\\'/, '\''); // TODO: check why this exists
};
var titleCase = function toTitleCase(str) {
return str.replace(/\w\S*/g, function(txt) {
return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
});
};
var uuid = function uuidGenerator() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = Math.random() * 16 | 0,
v = c === 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16);
});
};
var browserString = function browserString(config) {
var os_details = config.os + ' ' + config.os_version;
if (config.browser) {
return os_details + ', ' + (config.browser === 'ie' ? 'Internet Explorer' : titleCase(config.browser)) + ' ' + config.browser_version;
} else {
return os_details + (config.device ? (', ' + config.device) : '');
}
};
var objectSize = function objectSize(obj) {
var size = 0,
key;
for (key in obj) {
if (obj.hasOwnProperty(key)) {
size++;
}
}
return size;
};
var createTestsFailedError = function createTestsFailedError(config) {
var error = null;
if (config.status && config.exit_with_fail) {
error = new Error('Some tests failed.');
error.name = 'TestsFailedError';
}
return error;
};
var mkdirp = function mkdirp(filepath) {
var dirname = path.dirname(filepath);
if (!fs.existsSync(dirname)) {
mkdirp(dirname);
}
if (!fs.existsSync(filepath)) {
fs.mkdirSync(filepath);
}
};
exports.titleCase = titleCase;
exports.uuid = uuid;
exports.browserString = browserString;
exports.objectSize = objectSize;
exports.createTestsFailedError = createTestsFailedError;
exports.mkdirp = mkdirp;