forked from AlreadyBored/basic-js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransform-array.js
More file actions
36 lines (32 loc) · 764 Bytes
/
Copy pathtransform-array.js
File metadata and controls
36 lines (32 loc) · 764 Bytes
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
const CustomError = require('../extensions/custom-error');
module.exports = function transform(arr) {
let result = [];
if (!Array.isArray(arr)) {
throw new Error();
}
let cmd = false;
for (let i = 0; i < arr.length; i++) {
switch (arr[i]) {
case '--discard-next':
cmd = true;
i++;
break;
case '--discard-prev':
!cmd && result.pop();
cmd = true;
break;
case '--double-next':
cmd = true;
(i + 1) < arr.length && result.push(arr[i + 1]);
break;
case '--double-prev':
i !== 0 && !cmd && result.push(arr[i - 1]);
cmd = true;
break;
default:
cmd = false;
result.push(arr[i]);
}
}
return result;
};