-
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.
- Loading branch information
Showing
5 changed files
with
54 additions
and
11 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,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 |
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,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') | ||
}) | ||
}) | ||
}) |