Skip to content

Commit

Permalink
Make sure that mapOf also accepts Map type
Browse files Browse the repository at this point in the history
  • Loading branch information
Maurits Rijk committed Apr 12, 2017
1 parent 7ff2b9d commit 0998d8c
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 4 deletions.
2 changes: 0 additions & 2 deletions TODO
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,4 @@ Instrumentation hints:

Other TODO:

- add documentation

- Use speculaas to test speculaas!
1 change: 1 addition & 0 deletions docs/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* [Getting Started](docs/getting-started.md)
* [API Reference](docs/api-reference.md)
* [alt](docs/api/alt.md)
* [and](docs/api/and.md)
* [amp](docs/api/amp.md)
* [cat](docs/api/cat.md)
* [collOf](docs/api/collOf.md)
Expand Down
16 changes: 16 additions & 0 deletions docs/api/and.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
and
=====

Usage: ```and(...)```

Returns a spec that returns the conformed value. Succesive conformed values propagate
through rest of predicates

[Source](https://github.com/mrijk/speculaas/blob/master/lib/and.js)

Example:

```js
const s = require('speculaas');
const {isBoolean, isString} = require('lodash');
```
23 changes: 23 additions & 0 deletions docs/api/mapOf.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
mapOf
=====

Usage: ```mapOf(kpred, vpred, opts)```

Returns a spec for a map whose keys satisfy kpred and vals satisfy vpred.

[Source](https://github.com/mrijk/speculaas/blob/master/lib/mapOf.js)

Example:

```js
const s = require('speculaas');
const {isInteger, isString} = require('lodash');

s.def('::scores', s.mapOf(isString, isInteger));

s.isValid('::scores', {'Sally': 1000, 'Joe': 500});
// true

s.isValid('::scores', {'Sally': 1000, 'Joe': '500'});
// false
```
13 changes: 12 additions & 1 deletion lib/mapOf.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ function mapOf(kpred, vpred, options = {}) {
const {conformKeys = false} = options;

return {
conform: value => checkCount(value, options) && _.every(value, (v, k) => vpred(v) && kpred(k)),
conform: value => {
const val = getValue(value);
return checkCount(val, options) && _.every(val, (v, k) => vpred(v) && kpred(k));
},
gen: () => {
const count = options.count || 10;
const keyGen = gen(kpred);
Expand All @@ -21,4 +24,12 @@ function mapOf(kpred, vpred, options = {}) {
};
}

function getValue(value) {
if (_.isMap(value)) {
return _.fromPairs([...value]);
} else {
return value;
}
}

module.exports = mapOf;
2 changes: 1 addition & 1 deletion test/mapOf.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ describe('Test the mapOf function', () => {
expect(s.isValid('::scores2to3', {'Sally': 1000, 'Joe': 1000, 'Susan': 1000, 'Mike': 1000})).to.be.false;
});

xit('should accept an object of type Map as input', () => {
it('should accept an object of type Map as input', () => {
const map = new Map(_.toPairs({'Sally': '1000', 'Joe': 500}));
expect(s.isValid('::scores', map)).to.be.false;
});
Expand Down

0 comments on commit 0998d8c

Please sign in to comment.