-
Notifications
You must be signed in to change notification settings - Fork 124
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
110 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,50 @@ | ||
export function lmove(listKey1, listKey2, position1, position2) { | ||
|
||
if (this.data.has(listKey1) && !(this.data.get(listKey1) instanceof Array)) { | ||
throw new Error(`Key ${listKey1} does not contain a list`) | ||
throw new Error( | ||
'WRONGTYPE Operation against a key holding the wrong kind of value' | ||
) | ||
} | ||
if (this.data.has(listKey2) && !(this.data.get(listKey2) instanceof Array)) { | ||
throw new Error(`Key ${listKey2} does not contain a list`) | ||
throw new Error( | ||
'WRONGTYPE Operation against a key holding the wrong kind of value' | ||
) | ||
} | ||
|
||
if (position1 !== "LEFT" && position1 !== "RIGHT") { | ||
throw new Error("Position1 argument must be 'LEFT' or 'RIGHT'"); | ||
if (position1 !== 'LEFT' && position1 !== 'RIGHT') { | ||
throw new Error("Position1 argument must be 'LEFT' or 'RIGHT'") | ||
} | ||
if (position2 !== "LEFT" && position2 !== "RIGHT") { | ||
throw new Error("Position2 argument must be 'LEFT' or 'RIGHT'"); | ||
if (position2 !== 'LEFT' && position2 !== 'RIGHT') { | ||
throw new Error("Position2 argument must be 'LEFT' or 'RIGHT'") | ||
} | ||
|
||
const list1 = this.data.get(listKey1) || [] | ||
let list2 = list1; // Operate on the same list | ||
let list2 = list1 // Operate on the same list | ||
if (listKey1 !== listKey2) { | ||
// Operate on two different lists | ||
list2 = this.data.get(listKey2) || [] | ||
} | ||
|
||
if (list1.length === 0) { | ||
return null; | ||
return null | ||
} | ||
|
||
let value; | ||
if (position1 === "LEFT") { | ||
value = list1.shift(); | ||
let value | ||
if (position1 === 'LEFT') { | ||
value = list1.shift() | ||
} else { | ||
value = list1.pop(); | ||
value = list1.pop() | ||
} | ||
|
||
if (position2 === "LEFT") { | ||
list2.unshift(value); | ||
if (position2 === 'LEFT') { | ||
list2.unshift(value) | ||
} else { | ||
list2.push(value); | ||
list2.push(value) | ||
} | ||
|
||
this.data.set(listKey1, list1); | ||
this.data.set(listKey1, list1) | ||
if (listKey2 !== listKey1) { | ||
this.data.set(listKey2, list2); | ||
this.data.set(listKey2, list2) | ||
} | ||
|
||
return value | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters