Skip to content

Commit ca26a9e

Browse files
committed
fix(command): fixed optional step for INCR-like commands
1 parent f5711fa commit ca26a9e

File tree

8 files changed

+450
-313
lines changed

8 files changed

+450
-313
lines changed

CHANGES.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changes Logs
22

3+
## v3.0.5
4+
5+
- fix(command): fixed optional step for `INCR-like` commands.
6+
37
## v3.0.4
48

59
- fix(protocol): ensure writing network and queue in sync, avoiding disorder of execution queue.

eslint.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ module.exports = [
55
...LitertEslintRules.configs.typescript,
66
{
77
plugins: {
8-
'@litert/rules': require('@litert/eslint-plugin-rules'),
8+
'@litert/rules': LitertEslintRules,
99
},
1010
files: [
1111
'src/lib/**/*.ts',

package-lock.json

Lines changed: 268 additions & 272 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@litert/redis",
3-
"version": "3.0.4",
3+
"version": "3.0.5",
44
"description": "A redis protocol implement for Node.js.",
55
"main": "./lib/index.js",
66
"scripts": {
@@ -33,12 +33,12 @@
3333
"types": "./lib/index.d.ts",
3434
"typings": "./lib/index.d.ts",
3535
"devDependencies": {
36-
"@commitlint/cli": "^19.4.1",
37-
"@commitlint/config-conventional": "^19.4.1",
38-
"@litert/eslint-plugin-rules": "^0.2.0",
39-
"@types/node": "^22.5.1",
40-
"husky": "^9.1.5",
41-
"typescript": "^5.6.3"
36+
"@commitlint/cli": "^19.6.1",
37+
"@commitlint/config-conventional": "^19.6.0",
38+
"@litert/eslint-plugin-rules": "^0.2.5",
39+
"@types/node": "^22.10.2",
40+
"husky": "^9.1.7",
41+
"typescript": "^5.7.2"
4242
},
4343
"engines": {
4444
"node": ">=16.0.0"

src/lib/Commands.ts

Lines changed: 35 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
* limitations under the License.
1515
*/
1616

17+
/* eslint-disable max-lines */
18+
1719
import * as C from './Common';
1820
import * as U from './Utils';
1921
import * as E from './Errors';
@@ -77,7 +79,7 @@ export const COMMANDS: Record<keyof C.ICommandAPIs, ICommand> = {
7779
},
7880
process(data: Buffer | string): string {
7981

80-
return data instanceof Buffer ? data.toString() : data;
82+
return data instanceof Buffer ? data.toString() : data as string;
8183
}
8284
},
8385

@@ -146,61 +148,59 @@ export const COMMANDS: Record<keyof C.ICommandAPIs, ICommand> = {
146148
* @see https://redis.io/docs/latest/commands/incr
147149
*/
148150
'incr': {
149-
prepare(key: string, step: number): IPrepareResult {
151+
prepare(key: string, step: number = 1): IPrepareResult {
150152

151153
return {
152154
'cmd': 'INCRBY',
153-
'args': [key, step || 1]
155+
'args': [key, step]
154156
};
155-
}
157+
},
158+
// process: parseInt, // always returning integer
156159
},
157160

158161
/**
159162
* Command: incrByFloat
160163
* @see https://redis.io/docs/latest/commands/incrbyfloat
161164
*/
162165
'incrByFloat': {
163-
prepare(key: string, step: number): IPrepareResult {
166+
prepare(key: string, step: number = 1): IPrepareResult {
164167

165168
return {
166169
'cmd': 'INCRBYFLOAT',
167170
'args': [key, step]
168171
};
169172
},
170-
process(data: Buffer | string): number {
171-
return parseFloat(data as string);
172-
}
173+
process: parseFloat,
173174
},
174175

175176
/**
176177
* Command: decr
177178
* @see https://redis.io/docs/latest/commands/decr
178179
*/
179180
'decr': {
180-
prepare(key: string, step: number): IPrepareResult {
181+
prepare(key: string, step: number = 1): IPrepareResult {
181182

182183
return {
183184
'cmd': 'DECRBY',
184185
'args': [key, step]
185186
};
186-
}
187+
},
188+
// process: parseInt, // always returning integer
187189
},
188190

189191
/**
190192
* Command: incrByFloat
191193
* @see https://redis.io/docs/latest/commands/incrbyfloat
192194
*/
193195
'decrByFloat': {
194-
prepare(key: string, step: number): IPrepareResult {
196+
prepare(key: string, step: number = 1): IPrepareResult {
195197

196198
return {
197199
'cmd': 'INCRBYFLOAT',
198200
'args': [key, -step]
199201
};
200202
},
201-
process(data: string | Buffer): number {
202-
return parseFloat(data as string);
203-
}
203+
process: parseFloat,
204204
},
205205

206206
/**
@@ -987,7 +987,7 @@ export const COMMANDS: Record<keyof C.ICommandAPIs, ICommand> = {
987987
'mSet': {
988988
prepare(kv: Record<string, string | Buffer>): IPrepareResult {
989989

990-
const args: any[] = [];
990+
const args: Array<string | Buffer> = [];
991991

992992
for (const k in kv) {
993993

@@ -1010,7 +1010,7 @@ export const COMMANDS: Record<keyof C.ICommandAPIs, ICommand> = {
10101010
'mSetNX': {
10111011
prepare(kv: Record<string, string | Buffer>): IPrepareResult {
10121012

1013-
const args: any[] = [];
1013+
const args: Array<string | Buffer> = [];
10141014

10151015
for (const k in kv) {
10161016

@@ -1031,37 +1031,49 @@ export const COMMANDS: Record<keyof C.ICommandAPIs, ICommand> = {
10311031
* @see https://redis.io/docs/latest/commands/hincrby
10321032
*/
10331033
'hIncr': {
1034-
prepare: createDefaultPreparer('HINCRBY')
1034+
prepare(key: string, field: string, step: number = 1): IPrepareResult {
1035+
return {
1036+
cmd: 'HINCRBY',
1037+
args: [key, field, step]
1038+
};
1039+
},
1040+
// process: parseInt, // always returning integer
10351041
},
10361042

10371043
/**
10381044
* Command: hIncrByFloat
10391045
* @see https://redis.io/docs/latest/commands/hincrbyfloat
10401046
*/
10411047
'hIncrByFloat': {
1042-
prepare: createDefaultPreparer('HINCRBYFLOAT'),
1043-
process: parseFloat
1048+
prepare(key: string, field: string, step: number = 1): IPrepareResult {
1049+
return {
1050+
cmd: 'HINCRBYFLOAT',
1051+
args: [key, field, step]
1052+
};
1053+
},
1054+
process: parseFloat,
10441055
},
10451056

10461057
/**
10471058
* Command: hIncr
10481059
* @see https://redis.io/docs/latest/commands/hincrby
10491060
*/
10501061
'hDecr': {
1051-
prepare(key: string, field: string, step: number): IPrepareResult {
1062+
prepare(key: string, field: string, step: number = 1): IPrepareResult {
10521063
return {
10531064
cmd: 'HINCRBY',
1054-
args: [key, field, -(step || 1)]
1065+
args: [key, field, -step]
10551066
};
1056-
}
1067+
},
1068+
// process: parseInt, // always returning integer
10571069
},
10581070

10591071
/**
10601072
* Command: hIncrByFloat
10611073
* @see https://redis.io/docs/latest/commands/hincrbyfloat
10621074
*/
10631075
'hDecrByFloat': {
1064-
prepare(key: string, field: string, step: number): IPrepareResult {
1076+
prepare(key: string, field: string, step: number = 1): IPrepareResult {
10651077
return {
10661078
cmd: 'HINCRBYFLOAT',
10671079
args: [key, field, -step]

src/lib/Common.ts

Lines changed: 47 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable max-lines */
12
/**
23
* Copyright 2024 Angus.Fenying <fenying@litert.org>
34
*
@@ -388,27 +389,43 @@ export interface ICommandAPIs {
388389

389390
/**
390391
* Command: incr
392+
*
393+
* @param key The key of the hash.
394+
* @param increment The step to increase. [Default: 1]
395+
*
391396
* @see https://redis.io/docs/latest/commands/incr
392397
*/
393-
incr(key: string, step?: number): Promise<number>;
398+
incr(key: string, increment?: number): Promise<number>;
394399

395400
/**
396401
* Command: incrByFloat
402+
*
403+
* @param key The key of the hash.
404+
* @param increment The step to increase. [Default: 1]
405+
*
397406
* @see https://redis.io/docs/latest/commands/incrbyfloat
398407
*/
399-
incrByFloat(key: string, step: number): Promise<number>;
408+
incrByFloat(key: string, increment?: number): Promise<number>;
400409

401410
/**
402411
* Command: decr
412+
*
413+
* @param key The key of the hash.
414+
* @param decrement The step to decrease. [Default: 1]
415+
*
403416
* @see https://redis.io/docs/latest/commands/decr
404417
*/
405-
decr(key: string, step?: number): Promise<number>;
418+
decr(key: string, decrement?: number): Promise<number>;
406419

407420
/**
408421
* Command: incrByFloat
422+
*
423+
* @param key The key of the hash.
424+
* @param decrement The step to decrease. [Default: 1]
425+
*
409426
* @see https://redis.io/docs/latest/commands/incrbyfloat
410427
*/
411-
decrByFloat(key: string, step: number): Promise<number>;
428+
decrByFloat(key: string, decrement?: number): Promise<number>;
412429

413430
/**
414431
* Command: del
@@ -815,27 +832,47 @@ export interface ICommandAPIs {
815832

816833
/**
817834
* Command: hIncr
835+
*
836+
* @param key The key of the hash.
837+
* @param field The field of the hash.
838+
* @param increment The step to increase. [Default: 1]
839+
*
818840
* @see https://redis.io/docs/latest/commands/hincrby
819841
*/
820-
hIncr(key: string, field: string, step?: number): Promise<number>;
842+
hIncr(key: string, field: string, increment?: number): Promise<number>;
821843

822844
/**
823-
* Command: hIncr
845+
* Command: hIncrByFloat
846+
*
847+
* @param key The key of the hash.
848+
* @param field The field of the hash.
849+
* @param increment The step to increase. [Default: 1]
850+
*
824851
* @see https://redis.io/docs/latest/commands/hincrbyfloat
825852
*/
826-
hIncrByFloat(key: string, field: string, step: number): Promise<number>;
853+
hIncrByFloat(key: string, field: string, increment?: number): Promise<number>;
827854

828855
/**
829856
* Command: hIncr
857+
*
858+
* @param key The key of the hash.
859+
* @param field The field of the hash.
860+
* @param decrement The step to decrease. [Default: 1]
861+
*
830862
* @see https://redis.io/docs/latest/commands/hincrby
831863
*/
832-
hDecr(key: string, field: string, step?: number): Promise<number>;
864+
hDecr(key: string, field: string, decrement?: number): Promise<number>;
833865

834866
/**
835-
* Command: hIncr
867+
* Command: hIncrByFloat
868+
*
869+
* @param key The key of the hash.
870+
* @param field The field of the hash.
871+
* @param decrement The step to decrease. [Default: 1]
872+
*
836873
* @see https://redis.io/docs/latest/commands/hincrbyfloat
837874
*/
838-
hDecrByFloat(key: string, field: string, step: number): Promise<number>;
875+
hDecrByFloat(key: string, field: string, decrement?: number): Promise<number>;
839876

840877
/**
841878
* Command: hKeys

src/lib/ProtocolClient.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable max-lines */
12
/**
23
* Copyright 2024 Angus.Fenying <fenying@litert.org>
34
*

0 commit comments

Comments
 (0)