Skip to content

Commit 33ac07f

Browse files
committed
feat(command): added count param of RPOP and LPOP
1 parent ea5fdd4 commit 33ac07f

File tree

4 files changed

+115
-6
lines changed

4 files changed

+115
-6
lines changed

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## v1.2.4
44

55
- feat(command): added command `HRANDFILED` supports.
6+
- feat(command): added `count` param of command `R/LPOP`.
67

78
## v1.2.3
89

src/examples/debug.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,12 @@ function sleep(ms: number): Promise<void> {
5858
console.log(await cli.hRandFieldWithValues('aaaa', 2));
5959
console.log(await cli.hRandFieldWithValues$('aaaa', 2));
6060

61+
await cli.rPush('list-test', ['1', '2', '3', '4', '5']);
62+
console.log(await cli.rPop('list-test'));
63+
console.log(await cli.rPop('list-test', 4));
64+
console.log(await cli.rPop('list-test'));
65+
console.log(await cli.rPop('list-test', 4));
66+
6167
console.log('PING ->', await cli.ping(''));
6268
console.log('GET ->', await cli.set('a', '123'));
6369
console.log('INCR ->', await cli.incr('a', 23));

src/lib/Commands.ts

Lines changed: 84 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1688,16 +1688,55 @@ export const COMMANDS: Record<keyof C.ICommandAPIs, ICommand> = {
16881688
* @see https://redis.io/commands/lPop
16891689
*/
16901690
'lPop': {
1691-
prepare: createDefaultPreparer('LPOP'),
1692-
process: U.buffer2String
1691+
prepare(key: string, count: number = 1): IPrepareResult {
1692+
1693+
return {
1694+
args: [key, count],
1695+
cmd: 'LPOP'
1696+
};
1697+
},
1698+
process(data: any, args: any[]): string | null | string[] {
1699+
1700+
if (data === null) {
1701+
1702+
return args[1] !== '1' ? [] : null;
1703+
}
1704+
1705+
if (data.length === 1) {
1706+
1707+
return data[0][1].toString();
1708+
}
1709+
1710+
return U.list2StringList(data);
1711+
}
16931712
},
16941713

16951714
/**
16961715
* Command: lPop
16971716
* @see https://redis.io/commands/lPop
16981717
*/
16991718
'lPop$': {
1700-
prepare: createDefaultPreparer('LPOP')
1719+
prepare(key: string, count: number = 1): IPrepareResult {
1720+
1721+
return {
1722+
args: [key, count],
1723+
cmd: 'LPOP'
1724+
};
1725+
},
1726+
process(data: any, args: any[]): Buffer | null | Buffer[] {
1727+
1728+
if (data === null) {
1729+
1730+
return null;
1731+
}
1732+
1733+
if (data.length === 1) {
1734+
1735+
return args[1] !== '1' ? [] : null;
1736+
}
1737+
1738+
return U.list2BufferList(data);
1739+
}
17011740
},
17021741

17031742
/**
@@ -1777,16 +1816,55 @@ export const COMMANDS: Record<keyof C.ICommandAPIs, ICommand> = {
17771816
* @see https://redis.io/commands/rPop
17781817
*/
17791818
'rPop': {
1780-
prepare: createDefaultPreparer('RPOP'),
1781-
process: U.buffer2String
1819+
prepare(key: string, count: number = 1): IPrepareResult {
1820+
1821+
return {
1822+
args: [key, count],
1823+
cmd: 'RPOP'
1824+
};
1825+
},
1826+
process(data: any, args: any[]): string | null | string[] {
1827+
1828+
if (data === null) {
1829+
1830+
return args[1] !== '1' ? [] : null;
1831+
}
1832+
1833+
if (data.length === 1) {
1834+
1835+
return data[0][1].toString();
1836+
}
1837+
1838+
return U.list2StringList(data);
1839+
}
17821840
},
17831841

17841842
/**
17851843
* Command: rPop
17861844
* @see https://redis.io/commands/rPop
17871845
*/
17881846
'rPop$': {
1789-
prepare: createDefaultPreparer('RPOP')
1847+
prepare(key: string, count: number = 1): IPrepareResult {
1848+
1849+
return {
1850+
args: [key, count],
1851+
cmd: 'RPOP'
1852+
};
1853+
},
1854+
process(data: any, args: any[]): Buffer | null | Buffer[] {
1855+
1856+
if (data === null) {
1857+
1858+
return null;
1859+
}
1860+
1861+
if (data.length === 1) {
1862+
1863+
return args[1] !== '1' ? [] : null;
1864+
}
1865+
1866+
return U.list2BufferList(data);
1867+
}
17901868
},
17911869

17921870
/**

src/lib/Common.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -838,12 +838,24 @@ export interface ICommandAPIs {
838838
*/
839839
lPop(key: string): Promise<string | null>;
840840

841+
/**
842+
* Command: lPop
843+
* @see https://redis.io/commands/lPop
844+
*/
845+
lPop(key: string, count: number): Promise<string[]>;
846+
841847
/**
842848
* Command: lPop
843849
* @see https://redis.io/commands/lPop
844850
*/
845851
lPop$(key: string): Promise<Buffer | null>;
846852

853+
/**
854+
* Command: lPop
855+
* @see https://redis.io/commands/lPop
856+
*/
857+
lPop$(key: string, count: number): Promise<Buffer[]>;
858+
847859
/**
848860
* Command: lPush
849861
* @see https://redis.io/commands/lPush
@@ -892,12 +904,24 @@ export interface ICommandAPIs {
892904
*/
893905
rPop(key: string): Promise<string | null>;
894906

907+
/**
908+
* Command: rPop
909+
* @see https://redis.io/commands/rPop
910+
*/
911+
rPop(key: string, count: number): Promise<string[]>;
912+
895913
/**
896914
* Command: rPop
897915
* @see https://redis.io/commands/rPop
898916
*/
899917
rPop$(key: string): Promise<Buffer | null>;
900918

919+
/**
920+
* Command: rPop
921+
* @see https://redis.io/commands/rPop
922+
*/
923+
rPop$(key: string, count: number): Promise<Buffer[]>;
924+
901925
/**
902926
* Command: rPopLPush
903927
* @see https://redis.io/commands/rPopLPush

0 commit comments

Comments
 (0)