Skip to content

Commit

Permalink
add getrange command (stipsan#165)
Browse files Browse the repository at this point in the history
* added badge to compat table for easy overview

* fix messed up render

* add title

* add getrange command
  • Loading branch information
stipsan authored Oct 1, 2016
1 parent e9f2890 commit 65b54cd
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 10 deletions.
12 changes: 6 additions & 6 deletions compat.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
## Supported commands ![Commands Coverage: 29%](https://img.shields.io/badge/coverage-29%25-orange.svg)

### Supported commands
| redis | ioredis | ioredis-mock |
|-------|:-------:|:------------:|
|[append](http://redis.io/commands/APPEND)|:white_check_mark:|:white_check_mark:|
|[auth](http://redis.io/commands/AUTH)|:white_check_mark:|:x:|
|[bgrewriteaof](http://redis.io/commands/BGREWRITEAOF)|:white_check_mark:|:x:|
|[bgsave](http://redis.io/commands/BGSAVE)|:white_check_mark:|:x:|
|[auth](http://redis.io/commands/AUTH)|:white_check_mark:|:white_check_mark:|
|[bgrewriteaof](http://redis.io/commands/BGREWRITEAOF)|:white_check_mark:|:white_check_mark:|
|[bgsave](http://redis.io/commands/BGSAVE)|:white_check_mark:|:white_check_mark:|
|[bitcount](http://redis.io/commands/BITCOUNT)|:white_check_mark:|:x:|
|[bitfield](http://redis.io/commands/BITFIELD)|:white_check_mark:|:x:|
|[bitop](http://redis.io/commands/BITOP)|:white_check_mark:|:x:|
Expand Down Expand Up @@ -62,7 +62,7 @@
|[incrbyfloat](http://redis.io/commands/INCRBYFLOAT)|:white_check_mark:|:x:|
|[info](http://redis.io/commands/INFO)|:white_check_mark:|:x:|
|[keys](http://redis.io/commands/KEYS)|:white_check_mark:|:white_check_mark:|
|[lastsave](http://redis.io/commands/LASTSAVE)|:white_check_mark:|:x:|
|[lastsave](http://redis.io/commands/LASTSAVE)|:white_check_mark:|:white_check_mark:|
|[lindex](http://redis.io/commands/LINDEX)|:white_check_mark:|:x:|
|[linsert](http://redis.io/commands/LINSERT)|:white_check_mark:|:x:|
|[llen](http://redis.io/commands/LLEN)|:white_check_mark:|:white_check_mark:|
Expand Down Expand Up @@ -107,7 +107,7 @@
|[rpush](http://redis.io/commands/RPUSH)|:white_check_mark:|:white_check_mark:|
|[rpushx](http://redis.io/commands/RPUSHX)|:white_check_mark:|:x:|
|[sadd](http://redis.io/commands/SADD)|:white_check_mark:|:white_check_mark:|
|[save](http://redis.io/commands/SAVE)|:white_check_mark:|:x:|
|[save](http://redis.io/commands/SAVE)|:white_check_mark:|:white_check_mark:|
|[scan](http://redis.io/commands/SCAN)|:white_check_mark:|:x:|
|[scard](http://redis.io/commands/SCARD)|:white_check_mark:|:x:|
|[script](http://redis.io/commands/SCRIPT)|:white_check_mark:|:x:|
Expand Down
34 changes: 30 additions & 4 deletions scripts/update-compat.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,44 @@ const blacklist = [
];
const filteredCommands = commands.list.filter(command => blacklist.indexOf(command) === -1);

let tableMd = `
### Supported commands
let supportedCommands = 0;
let tableRows = `
| redis | ioredis | ioredis-mock |
|-------|:-------:|:------------:|`;
filteredCommands.forEach((command) => {
const redisCol = `[${command}](http://redis.io/commands/${command.toUpperCase()})`;
const ioredisCol = command in redis.prototype ? ':white_check_mark:' : ':x:';
const ioredisMockCol = command in mockedRedis ? ':white_check_mark:' : ':x:';
tableMd += `
const supportedCommand = command in mockedRedis;
const ioredisMockCol = supportedCommand ? ':white_check_mark:' : ':x:';
if (supportedCommand) {
supportedCommands += 1;
}
tableRows += `
|${redisCol}|${ioredisCol}|${ioredisMockCol}|`;
});

const percentage = Math.floor((supportedCommands / filteredCommands.length) * 100);

let color = 'red';
if (percentage >= 25) {
color = 'orange';
}
if (percentage >= 55) {
color = 'yellow';
}
if (percentage >= 70) {
color = 'yellowgreen';
}
if (percentage >= 85) {
color = 'green';
}
if (percentage === 100) {
color = 'brightgreen';
}

const tableMd = `## Supported commands ![Commands Coverage: ${percentage}%](https://img.shields.io/badge/coverage-${percentage}%25-${color}.svg)
${tableRows}`;

fs.writeFile(path.resolve(__dirname, '..', 'compat.md'), tableMd, (err) => {
if (err) throw err;
});
9 changes: 9 additions & 0 deletions src/commands/getrange.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export function getrange(key, start, end) {
const val = this.data.get(key);

if (end === -1) {
return val.slice(start);
}

return val.slice(start, end + 1);
}
1 change: 1 addition & 0 deletions src/commands/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export * from './del';
export * from './exists';
export * from './expire';
export * from './get';
export * from './getrange';
export * from './getset';
export * from './hdel';
export * from './hexists';
Expand Down
27 changes: 27 additions & 0 deletions test/commands/getrange.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import expect from 'expect';

import MockRedis from '../../src';

describe('getrange', () => {
const redis = new MockRedis({
data: {
foo: 'This is a string',
},
});

it('should return "This"', () =>
redis.getrange('foo', 0, 3).then(result => expect(result).toBe('This'))
);

it('should return "ing"', () =>
redis.getrange('foo', -3, -1).then(result => expect(result).toBe('ing'))
);

it('should return "This is a string"', () =>
redis.getrange('foo', 0, -1).then(result => expect(result).toBe('This is a string'))
);

it('should return "string"', () =>
redis.getrange('foo', 10, 100).then(result => expect(result).toBe('string'))
);
});

0 comments on commit 65b54cd

Please sign in to comment.