-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: zrangebylex and zrevrangebylex (#1269)
- Loading branch information
Showing
7 changed files
with
368 additions
and
20 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
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 |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { convertStringToBuffer } from '../commands-utils/convertStringToBuffer' | ||
import { | ||
RANGE_LEX, | ||
zrangeBaseCommand, | ||
} from './zrange-command.common' | ||
|
||
export function zrangebylex(...args) { | ||
return zrangeBaseCommand.call(this, args, 0, false, RANGE_LEX); | ||
} | ||
|
||
export function zrangebylexBuffer(...args) { | ||
const val = zrangebylex.apply(this, args) | ||
return convertStringToBuffer(val) | ||
} |
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 |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { convertStringToBuffer } from '../commands-utils/convertStringToBuffer' | ||
import { | ||
DIRECTION_REVERSE, | ||
RANGE_LEX, | ||
zrangeBaseCommand, | ||
} from './zrange-command.common' | ||
|
||
export function zrevrangebylex(...args) { | ||
return zrangeBaseCommand.call(this, args, 0, false, RANGE_LEX, DIRECTION_REVERSE); | ||
} | ||
|
||
export function zrevrangebylexBuffer(...args) { | ||
const val = zrevrangebylex.apply(this, args) | ||
return convertStringToBuffer(val) | ||
} |
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 |
---|---|---|
@@ -0,0 +1,111 @@ | ||
import Redis from 'ioredis' | ||
|
||
describe('zrangebylex', () => { | ||
const data = { | ||
foo: new Map([ | ||
['a', { score: 2, value: 'a' }], | ||
['b', { score: 2, value: 'b' }], | ||
['c', { score: 2, value: 'c' }], | ||
['d', { score: 2, value: 'd' }], | ||
['e', { score: 2, value: 'e' }], | ||
]), | ||
} | ||
it('should return using inclusive compare', () => { | ||
const redis = new Redis({ data }) | ||
|
||
return redis | ||
.zrangebylex('foo', '[b', '[d') | ||
.then(res => expect(res).toEqual(['b', 'c', 'd'])) | ||
}) | ||
|
||
it('should return using exclusive compare', () => { | ||
const redis = new Redis({ data }) | ||
|
||
return redis | ||
.zrangebylex('foo', '(b', '(d') | ||
.then(res => expect(res).toEqual(['c'])) | ||
}) | ||
|
||
it('should accept - string', () => { | ||
const redis = new Redis({ data }) | ||
|
||
return redis | ||
.zrangebylex('foo', '-', '(c') | ||
.then(res => expect(res).toEqual(['a', 'b'])) | ||
}) | ||
it('should accept + string', () => { | ||
const redis = new Redis({ data }) | ||
|
||
return redis | ||
.zrangebylex('foo', '(c', '+') | ||
.then(res => expect(res).toEqual(['d', 'e'])) | ||
}) | ||
|
||
it('should accept -+ strings', () => { | ||
const redis = new Redis({ data }) | ||
|
||
return redis | ||
.zrangebylex('foo', '-', '+') | ||
.then(res => | ||
expect(res).toEqual(['a', 'b', 'c', 'd', 'e']) | ||
) | ||
}) | ||
it('should return empty array if out-of-range', () => { | ||
const redis = new Redis({ data }) | ||
|
||
return redis | ||
.zrangebylex('foo', '(f', '[z') | ||
.then(res => expect(res).toEqual([])) | ||
}) | ||
|
||
it('should return empty array if key not found', () => { | ||
const redis = new Redis({ data }) | ||
|
||
return redis | ||
.zrangebylex('boo', '-', '+') | ||
.then(res => expect(res).toEqual([])) | ||
}) | ||
|
||
it('should return empty array if the key contains something other than a list', () => { | ||
const redis = new Redis({ | ||
data: { | ||
foo: 'not a list', | ||
}, | ||
}) | ||
|
||
return redis | ||
.zrangebylex('foo', '-', '+') | ||
.then(res => expect(res).toEqual([])) | ||
}) | ||
|
||
it('should handle offset and limit (0,1)', () => { | ||
const redis = new Redis({ data }) | ||
return redis | ||
.zrangebylex('foo', '[a', '[c', 'LIMIT', 0, 1) | ||
.then(res => expect(res).toEqual(['a'])) | ||
}) | ||
it('should handle offset and limit (1,2)', () => { | ||
const redis = new Redis({ data }) | ||
return redis | ||
.zrangebylex('foo', '[a', '[c', 'LIMIT', 1, 2) | ||
.then(res => expect(res).toEqual(['b', 'c'])) | ||
}) | ||
it('should handle LIMIT of -1', () => { | ||
const redis = new Redis({ data }) | ||
return redis | ||
.zrangebylex('foo', '-', '+', 'LIMIT', 1, -1) | ||
.then(res => expect(res).toEqual(['b', 'c', 'd'])) | ||
}) | ||
it('should handle LIMIT of -2', () => { | ||
const redis = new Redis({ data }) | ||
return redis | ||
.zrangebylex('foo', '-', '+', 'LIMIT', 1, -2) | ||
.then(res => expect(res).toEqual(['b', 'c'])) | ||
}) | ||
it('should handle LIMIT of 0', () => { | ||
const redis = new Redis({ data }) | ||
return redis | ||
.zrangebylex('foo', '-', '+', 'LIMIT', 1, 0) | ||
.then(res => expect(res).toEqual([])) | ||
}) | ||
}) |
Oops, something went wrong.