Skip to content

Commit

Permalink
feat: add setrange command (#1134)
Browse files Browse the repository at this point in the history
  • Loading branch information
stipsan authored Jan 31, 2022
1 parent 92b5e45 commit 466b478
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 11 deletions.
2 changes: 1 addition & 1 deletion compat.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@
| [setbit] | :white_check_mark: | :white_check_mark: |
| [setex] | :white_check_mark: | :white_check_mark: |
| [setnx] | :white_check_mark: | :white_check_mark: |
| [setrange] | :white_check_mark: | :x: |
| [setrange] | :white_check_mark: | :white_check_mark: |
| [shutdown] | :white_check_mark: | :x: |
| [sinter] | :white_check_mark: | :white_check_mark: |
| [sinterstore] | :white_check_mark: | :white_check_mark: |
Expand Down
1 change: 1 addition & 0 deletions src/commands/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ export * from './set'
export * from './setbit'
export * from './setex'
export * from './setnx'
export * from './setrange'
export * from './sinter'
export * from './sinterstore'
export * from './sismember'
Expand Down
26 changes: 26 additions & 0 deletions src/commands/setrange.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
export function setrange(key, offset, value) {
const oldVal = this.data.get(key) || ''
const start = parseInt(offset, 10)
const end = parseInt(start + value.length, 10)

let newVal = ''
if (start > 0) {
newVal = oldVal.slice(0, start)
}

newVal += value

if (end < oldVal.length) {
newVal += oldVal.slice(end, oldVal.length + 1)
}

if (newVal.length < end) {
newVal = newVal.padStart(end, String.fromCharCode(0))
}

this.data.set(key, newVal)

return newVal.length
}

export const setrangeBuffer = setrange
12 changes: 2 additions & 10 deletions test/integration/commands/setbit.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,7 @@ import { runTwinSuite } from '../../../test-utils'
runTwinSuite('setbit', command => {
describe(command, () => {
it('should return old bit value of key', async () => {
const redis = new Redis({
data: {
foo: '@',
},
})
const redis = new Redis()
await redis.set('foo', '@')

return redis[command]('foo', 1, 0)
Expand Down Expand Up @@ -38,11 +34,7 @@ runTwinSuite('setbit', command => {
})

it('should override bit value of key', async () => {
const redis = new Redis({
data: {
foo: 'bar',
},
})
const redis = new Redis()
await redis.set('foo', 'bar')

return redis[command]('foo', 3, 1)
Expand Down
24 changes: 24 additions & 0 deletions test/integration/commands/setrange.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import Redis from 'ioredis'

// eslint-disable-next-line import/no-relative-parent-imports
import { runTwinSuite } from '../../../test-utils'

runTwinSuite('setrange', command => {
describe(command, () => {
const redis = new Redis()

afterAll(() => {
redis.disconnect()
})

it('should return "Hello Redis"', async () => {
await redis.set('key1', 'Hello World')
expect(await redis[command]('key1', 6, 'Redis')).toBe(11)
})

it('should return zero padding', async () => {
expect(await redis[command]('key2', 6, 'Redis')).toBe(11)
expect(await redis.get('key2')).toBe('\x00\x00\x00\x00\x00\x00Redis')
})
})
})

0 comments on commit 466b478

Please sign in to comment.