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
34 lines (31 loc) · 793 Bytes
/
Copy pathtransform-array.js
File metadata and controls
34 lines (31 loc) · 793 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
const CustomError = require("../extensions/custom-error");
module.exports = transform = (arr) => {
if(!Array.isArray(arr))
throw new Error('Not implemented');
const newArr = [];
for(let i = 0; i<arr.length;i++){
switch (arr[i]) {
case '--discard-next':
if(i!==arr.length-1)
newArr.push('discard')
i+=1;
break;
case '--discard-prev':
if(i>=0)
newArr.pop();
break;
case '--double-next':
if(i!==arr.length-1)
newArr.push(arr[i+1])
break;
case '--double-prev':
if(i!==0&&newArr[newArr.length-1]!=='discard')
newArr.push(arr[i-1])
break;
default:
newArr.push(arr[i])
break;
}
}
return newArr.filter (el=>el!=='discard')
}