Skip to content

Commit

Permalink
Feat: Support objects and maps in hmset (stipsan#337)
Browse files Browse the repository at this point in the history
* ignore prettier dotfiles

* Update deps

* Fix runkit example link

* Chore: Update feature compat table

* hmset now supports object and map

* Chore: Update feature compat table

* Updated changelog

* Chore: Update feature compat table

* Chore: Update feature compat table
  • Loading branch information
stipsan authored Nov 15, 2017
1 parent 5c0000e commit 87cbdd4
Show file tree
Hide file tree
Showing 10 changed files with 86 additions and 42 deletions.
2 changes: 2 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ scripts
.codeclimate.yml
.eslintignore
.eslintrc
.prettierignore
.prettierrc
appveyor.yml
compat.md
example.js
Expand Down
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ and this project adheres to

## [Unreleased]

### Features

* Add support for passing objects and maps to hmset

## [3.2.0] - 2017-11-16

### Features
Expand Down Expand Up @@ -438,7 +442,8 @@ changes.
* multi
* exec

[unreleased]: https://github.com/stipsan/ioredis-mock/compare/v3.1.3...HEAD
[unreleased]: https://github.com/stipsan/ioredis-mock/compare/v3.2.0...HEAD
[3.2.0]: https://github.com/stipsan/ioredis-mock/compare/v3.1.3...v3.2.0
[3.1.3]: https://github.com/stipsan/ioredis-mock/compare/v3.1.2...v3.1.3
[3.1.2]: https://github.com/stipsan/ioredis-mock/compare/v3.1.1...v3.1.2
[3.1.1]: https://github.com/stipsan/ioredis-mock/compare/v3.1.0...v3.1.1
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# ioredis-mock · [![CircleCI Status](https://img.shields.io/circleci/project/github/stipsan/ioredis-mock.svg?style=flat-square)](https://circleci.com/gh/stipsan/ioredis-mock) [![AppVeyor branch](https://img.shields.io/appveyor/ci/stipsan/ioredis-mock/master.svg?style=flat-square&label=win)](https://ci.appveyor.com/project/stipsan/ioredis-mock) [![npm](https://img.shields.io/npm/dm/ioredis-mock.svg?style=flat-square)](https://npm-stat.com/charts.html?package=ioredis-mock) [![npm version](https://img.shields.io/npm/v/ioredis-mock.svg?style=flat-square)](https://www.npmjs.com/package/ioredis-mock) [![Redis Compatibility: 50%](https://img.shields.io/badge/redis-50%25-yellow.svg?style=flat-square)](compat.md) [![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg?style=flat-square)](https://github.com/semantic-release/semantic-release)
# ioredis-mock · [![CircleCI Status](https://img.shields.io/circleci/project/github/stipsan/ioredis-mock.svg?style=flat-square)](https://circleci.com/gh/stipsan/ioredis-mock) [![AppVeyor branch](https://img.shields.io/appveyor/ci/stipsan/ioredis-mock/master.svg?style=flat-square&label=win)](https://ci.appveyor.com/project/stipsan/ioredis-mock) [![npm](https://img.shields.io/npm/dm/ioredis-mock.svg?style=flat-square)](https://npm-stat.com/charts.html?package=ioredis-mock) [![npm version](https://img.shields.io/npm/v/ioredis-mock.svg?style=flat-square)](https://www.npmjs.com/package/ioredis-mock) [![Redis Compatibility: 53%](https://img.shields.io/badge/redis-53%25-yellow.svg?style=flat-square)](compat.md) [![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg?style=flat-square)](https://github.com/semantic-release/semantic-release)

This library emulates [ioredis](https://github.com/luin/ioredis) by performing
all operations in-memory. The best way to do integration testing against redis
Expand Down
10 changes: 5 additions & 5 deletions compat.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
## Supported commands ![Commands Coverage: 50%](https://img.shields.io/badge/coverage-50%25-yellow.svg)
## Supported commands ![Commands Coverage: 53%](https://img.shields.io/badge/coverage-53%25-yellow.svg)

| redis | ioredis | ioredis-mock |
| --------------------------------------------------------------- | :----------------: | :----------------: |
Expand Down Expand Up @@ -150,21 +150,21 @@
| [unwatch](http://redis.io/commands/UNWATCH) | :white_check_mark: | :x: |
| [wait](http://redis.io/commands/WAIT) | :white_check_mark: | :x: |
| [watch](http://redis.io/commands/WATCH) | :white_check_mark: | :x: |
| [zadd](http://redis.io/commands/ZADD) | :white_check_mark: | :x: |
| [zadd](http://redis.io/commands/ZADD) | :white_check_mark: | :white_check_mark: |
| [zcard](http://redis.io/commands/ZCARD) | :white_check_mark: | :x: |
| [zcount](http://redis.io/commands/ZCOUNT) | :white_check_mark: | :x: |
| [zincrby](http://redis.io/commands/ZINCRBY) | :white_check_mark: | :x: |
| [zinterstore](http://redis.io/commands/ZINTERSTORE) | :white_check_mark: | :x: |
| [zlexcount](http://redis.io/commands/ZLEXCOUNT) | :white_check_mark: | :x: |
| [zrange](http://redis.io/commands/ZRANGE) | :white_check_mark: | :x: |
| [zrange](http://redis.io/commands/ZRANGE) | :white_check_mark: | :white_check_mark: |
| [zrangebylex](http://redis.io/commands/ZRANGEBYLEX) | :white_check_mark: | :x: |
| [zrangebyscore](http://redis.io/commands/ZRANGEBYSCORE) | :white_check_mark: | :x: |
| [zrank](http://redis.io/commands/ZRANK) | :white_check_mark: | :x: |
| [zrem](http://redis.io/commands/ZREM) | :white_check_mark: | :x: |
| [zremrangebylex](http://redis.io/commands/ZREMRANGEBYLEX) | :white_check_mark: | :x: |
| [zremrangebyrank](http://redis.io/commands/ZREMRANGEBYRANK) | :white_check_mark: | :x: |
| [zremrangebyrank](http://redis.io/commands/ZREMRANGEBYRANK) | :white_check_mark: | :white_check_mark: |
| [zremrangebyscore](http://redis.io/commands/ZREMRANGEBYSCORE) | :white_check_mark: | :x: |
| [zrevrange](http://redis.io/commands/ZREVRANGE) | :white_check_mark: | :x: |
| [zrevrange](http://redis.io/commands/ZREVRANGE) | :white_check_mark: | :white_check_mark: |
| [zrevrangebylex](http://redis.io/commands/ZREVRANGEBYLEX) | :white_check_mark: | :x: |
| [zrevrangebyscore](http://redis.io/commands/ZREVRANGEBYSCORE) | :white_check_mark: | :x: |
| [zrevrank](http://redis.io/commands/ZREVRANK) | :white_check_mark: | :x: |
Expand Down
14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,12 @@
},
"dependencies": {
"array-from": "^2.1.1",
"bluebird": "3.5.0",
"bluebird": "^3.5.1",
"es6-map": "^0.1.5",
"es6-set": "^0.1.4",
"lodash": "4.17.4",
"minimatch": "3.0.3",
"object-assign": "4.1.1",
"redis-commands": "1.3.1"
"es6-set": "^0.1.5",
"lodash": "^4.17.4",
"minimatch": "^3.0.4",
"object-assign": "^4.1.1"
},
"peerDependencies": {
"ioredis": "2.x || 3.x"
Expand All @@ -64,6 +63,7 @@
"mocha": "3.2.0",
"prettier": "^1.8.2",
"prettier-package-json": "^1.4.0",
"redis-commands": "1.3.1",
"release-relief": "^1.0.1",
"rimraf": "2.6.1",
"semver": "^5.4.1"
Expand Down Expand Up @@ -115,5 +115,5 @@
"generateNotes": "semantic-release-tamia/generateNotes",
"verifyRelease": "semantic-release-tamia/verifyRelease"
},
"tonicExampleFilename": "example.js"
"runkitExampleFilename": "example.js"
}
14 changes: 9 additions & 5 deletions src/command.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Promise from 'bluebird';

export default function command(emulate) {
export default function command(emulate, commandName) {
return (...args) => {
const lastArgIndex = args.length - 1;
let callback = args[lastArgIndex];
Expand All @@ -11,10 +11,14 @@ export default function command(emulate) {
args.length = lastArgIndex;
}

// transform non-buffer arguments to strings to simulate real ioredis behavior
const stringArgs = args.map(
arg => (arg instanceof Buffer ? arg : arg.toString())
);
// Stopgap until we got proper argument transformers implemented
const stringArgs =
commandName === 'hmset'
? args
: args.map(
// transform non-buffer arguments to strings to simulate real ioredis behavior
arg => (arg instanceof Buffer ? arg : arg.toString())
);

return new Promise(resolve => resolve(emulate(...stringArgs))).asCallback(
callback
Expand Down
17 changes: 15 additions & 2 deletions src/commands/hmset.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,23 @@
export function hmset(key, ...hmsetData) {
import Map from 'es6-map';
import utils from 'ioredis/lib/utils';

export function hmset(key, ...args) {
if (!this.data.has(key)) {
this.data.set(key, {});
}

const hash = this.data.get(key);
let hmsetData = [];
if (args.length === 1) {
if (args[0] instanceof Map) {
hmsetData = utils.convertMapToArray(args[0]);
} else if (typeof args[0] === 'object' && args[0] !== null) {
hmsetData = utils.convertObjectToArray(args[0]);
}
} else {
hmsetData = args;
}

const hash = this.data.get(key);
for (let i = 0; i < hmsetData.length; i += 2) {
hash[hmsetData[i]] = hmsetData[i + 1];
}
Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class RedisMock extends EventEmitter {
this.data = createData(this.expires, data);

Object.keys(commands).forEach(command => {
this[command] = createCommand(commands[command].bind(this));
this[command] = createCommand(commands[command].bind(this), command);
});

process.nextTick(() => {
Expand Down
26 changes: 21 additions & 5 deletions test/commands/hmset.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import expect from 'expect';
import Map from 'es6-map';

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

Expand All @@ -12,13 +13,28 @@ describe('hmset', () => {
.then(() => expect(redis.data.get('user:1')).toEqual(hash));
});

it(
'should let you set multiple hash map keys and values with an object' /* , () => {
it('should let you set multiple hash map keys and values with an object', () => {
const redis = new MockRedis();
const hash = { id: '1', email: 'bruce@wayne.enterprises' };
return redis.hmset('user:1', hash)
return redis
.hmset('user:1', hash)
.then(status => expect(status).toBe('OK'))
.then(() => expect(redis.data.get('user:1')).toEqual(hash));
}*/
);
});

it('should let you set multiple hash map keys and values with a Map', () => {
const redis = new MockRedis();
return redis
.hmset(
'user:1',
new Map([['id', '1'], ['email', 'bruce@wayne.enterprises']])
)
.then(status => expect(status).toBe('OK'))
.then(() =>
expect(redis.data.get('user:1')).toEqual({
id: '1',
email: 'bruce@wayne.enterprises',
})
);
});
});
34 changes: 19 additions & 15 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -961,10 +961,14 @@ block-stream@*:
dependencies:
inherits "~2.0.0"

bluebird@3.5.0, bluebird@^3.3.4:
bluebird@^3.3.4:
version "3.5.0"
resolved "https://registry.yarnpkg.com/bluebird/-/bluebird-3.5.0.tgz#791420d7f551eea2897453a8a77653f96606d67c"

bluebird@^3.5.1:
version "3.5.1"
resolved "https://registry.yarnpkg.com/bluebird/-/bluebird-3.5.1.tgz#d9551f9de98f1fcda1e683d17ee91a0602ee2eb9"

boom@2.x.x:
version "2.10.1"
resolved "https://registry.yarnpkg.com/boom/-/boom-2.10.1.tgz#39c8918ceff5799f83f9492a848f625add0c766f"
Expand Down Expand Up @@ -1617,17 +1621,7 @@ es6-map@^0.1.5:
es6-symbol "~3.1.1"
event-emitter "~0.3.5"

es6-set@^0.1.4, es6-set@~0.1.3:
version "0.1.4"
resolved "https://registry.yarnpkg.com/es6-set/-/es6-set-0.1.4.tgz#9516b6761c2964b92ff479456233a247dc707ce8"
dependencies:
d "~0.1.1"
es5-ext "~0.10.11"
es6-iterator "2"
es6-symbol "3"
event-emitter "~0.3.4"

es6-set@~0.1.5:
es6-set@^0.1.5, es6-set@~0.1.5:
version "0.1.5"
resolved "https://registry.yarnpkg.com/es6-set/-/es6-set-0.1.5.tgz#d2b3ec5d4d800ced818db538d28974db0a73ccb1"
dependencies:
Expand All @@ -1637,6 +1631,16 @@ es6-set@~0.1.5:
es6-symbol "3.1.1"
event-emitter "~0.3.5"

es6-set@~0.1.3:
version "0.1.4"
resolved "https://registry.yarnpkg.com/es6-set/-/es6-set-0.1.4.tgz#9516b6761c2964b92ff479456233a247dc707ce8"
dependencies:
d "~0.1.1"
es5-ext "~0.10.11"
es6-iterator "2"
es6-symbol "3"
event-emitter "~0.3.4"

es6-symbol@3, es6-symbol@~3.1, es6-symbol@~3.1.0:
version "3.1.0"
resolved "https://registry.yarnpkg.com/es6-symbol/-/es6-symbol-3.1.0.tgz#94481c655e7a7cad82eba832d97d5433496d7ffa"
Expand Down Expand Up @@ -3213,7 +3217,7 @@ lodash.values@^4.3.0:
version "4.3.0"
resolved "https://registry.yarnpkg.com/lodash.values/-/lodash.values-4.3.0.tgz#a3a6c2b0ebecc5c2cba1c17e6e620fe81b53d347"

lodash@4.17.4, lodash@^4.0.0, lodash@^4.14.0, lodash@^4.17.4, lodash@^4.2.0, lodash@^4.2.1, lodash@^4.3.0:
lodash@^4.0.0, lodash@^4.14.0, lodash@^4.17.4, lodash@^4.2.0, lodash@^4.2.1, lodash@^4.3.0:
version "4.17.4"
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.4.tgz#78203a4d1c328ae1d86dca6460e369b57f4055ae"

Expand Down Expand Up @@ -3329,7 +3333,7 @@ mime@^1.2.11:
version "1.4.1"
resolved "https://registry.yarnpkg.com/mime/-/mime-1.4.1.tgz#121f9ebc49e3766f311a76e1fa1c8003c4b03aa6"

"minimatch@2 || 3", minimatch@3.0.3, minimatch@^3.0.0, minimatch@^3.0.2, minimatch@^3.0.3:
"minimatch@2 || 3", minimatch@^3.0.0, minimatch@^3.0.2, minimatch@^3.0.3:
version "3.0.3"
resolved "https://registry.yarnpkg.com/minimatch/-/minimatch-3.0.3.tgz#2a4e4090b96b2db06a9d7df01055a62a77c9b774"
dependencies:
Expand Down Expand Up @@ -3543,7 +3547,7 @@ oauth-sign@~0.8.1, oauth-sign@~0.8.2:
version "0.8.2"
resolved "https://registry.yarnpkg.com/oauth-sign/-/oauth-sign-0.8.2.tgz#46a6ab7f0aead8deae9ec0565780b7d4efeb9d43"

object-assign@4.1.1, object-assign@^4.0.1, object-assign@^4.1.0:
object-assign@^4.0.1, object-assign@^4.1.0, object-assign@^4.1.1:
version "4.1.1"
resolved "https://registry.yarnpkg.com/object-assign/-/object-assign-4.1.1.tgz#2109adc7965887cfc05cbbd442cac8bfbb360863"

Expand Down

0 comments on commit 87cbdd4

Please sign in to comment.