Skip to content

Commit

Permalink
fix: Fixes #877 DECR and DECRBY defaults value if key doesn't exist (#…
Browse files Browse the repository at this point in the history
…969)

* fix: default set key-value to 0 if does not exist

https://redis.io/commands/decr

fixes #877

* fix: default set key-value to 0 if does not exist

https://redis.io/commands/decrby

fixes #877
  • Loading branch information
douglascayers authored Sep 30, 2020
1 parent e27ad44 commit 9f740f3
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/commands/decr.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
export function decr(key) {
if (!this.data.has(key)) {
this.data.set(key, '0');
}
const curVal = Number(this.data.get(key));
const nextVal = curVal - 1;
this.data.set(key, nextVal.toString());
Expand Down
3 changes: 3 additions & 0 deletions src/commands/decrby.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
export function decrby(key, decrement = 0) {
if (!this.data.has(key)) {
this.data.set(key, '0');
}
const curVal = Number(this.data.get(key));
const nextVal = curVal - parseInt(decrement, 10);
this.data.set(key, nextVal.toString());
Expand Down

0 comments on commit 9f740f3

Please sign in to comment.