-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathutil.js
More file actions
109 lines (89 loc) · 2.57 KB
/
Copy pathutil.js
File metadata and controls
109 lines (89 loc) · 2.57 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
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
function determineModificationRange (opts, filesize) {
var minArg = parseFloat(opts.min);
var maxArg = parseFloat(opts.max);
var startArg = parseInt(opts.start, 10);
var stopArg = parseInt(opts.stop, 10);
if (isNaN(minArg)) {
minArg = undefined;
}
if (isNaN(maxArg)) {
maxArg = undefined;
}
if (isNaN(startArg)) {
startArg = undefined;
}
if (isNaN(stopArg)) {
stopArg = undefined;
}
var startByte;
var stopByte;
if (!minArg && !maxArg && !startArg && !stopArg) {
throw new Error('Must specify --min and --max or --start and --stop');
}
if ((minArg || maxArg) && (startArg || stopArg)) {
throw new Error(
'Cannot use both min/max and start/stop entry methods; ' +
'use --min and --max alone or use --start and --stop alone'
);
}
if (startArg || stopArg) {
if (startArg === undefined || stopArg === undefined) {
throw new Error('Must specify both --start and --stop');
}
if (startArg < 0) {
throw new Error('--start cannot be less than 0');
}
if (stopArg > filesize) {
throw new Error('--stop cannot be larger than filesize (' + filesize + ')');
}
return {
start: startArg,
stop: stopArg
};
}
if (minArg || maxArg) {
if (minArg === undefined || maxArg === undefined) {
throw new Error('Must specify both --min and --max');
}
return {
start: Math.floor( filesize * minArg ),
stop: Math.floor( filesize * maxArg )
};
}
}
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
function checkArguments(opts) {
var hasMinMax = opts.min !== undefined || opts.max !== undefined;
var hasStartStop = opts.start !== undefined || opts.stop !== undefined;
if (hasMinMax && hasStartStop) {
throw new Error('min/max and start/stop cannot be used together');
}
if (hasStartStop && opts.stop === undefined) {
throw new Error('stop must be provided');
}
if (hasStartStop && opts.start === undefined) {
throw new Error('start must be provided');
}
if (hasMinMax && opts.max === undefined) {
throw new Error('max must be provided');
}
if (hasMinMax && opts.min === undefined) {
throw new Error('min must be provided');
}
if ((opts.min || 0) < 0) {
throw new Error('min must be >= 0');
}
if ((opts.max || 0) > 1) {
throw new Error('max must be <= 1');
}
if ((opts.start || 0) < 0) {
throw new Error('start must be >= 0');
}
}
module.exports = {
checkArguments: checkArguments,
getRandomInt: getRandomInt,
determineModificationRange: determineModificationRange
};