Commit 9db352a29a6d5bac4344c081e9228bd03aaeb97c
rename chooseOnePollType, finish poll.async.get ?
mix irving committed on 3/7/2018, 2:09:43 AMParent: c732390a0378ece4cdd624206b83d7b2c46069e8
Files changed
README.md | changed |
package-lock.json | changed |
poll/async/get.js | changed |
poll/schema/details/chooseOne.js | changed |
poll/sync/chooseOne.js | changed |
poll/sync/isPoll.js | changed |
position/schema/details/chooseOne.js | changed |
position/sync/chooseOne.js | changed |
test/poll/sync/chooseOne.test.js | changed |
test/poll/sync/isPoll.test.js | changed |
test/position/sync/isPosition.test.js | changed |
types.js | changed |
README.md | ||
---|---|---|
@@ -57,11 +57,17 @@ | ||
57 | 57 | ### Methods |
58 | 58 | |
59 | 59 | #### `scuttle.poll.sync.isPoll(msg) => Boolean` |
60 | 60 | |
61 | -takes a msg or the contents of a msg | |
61 | +Takes a msg or the contents of a msg | |
62 | 62 | |
63 | +You can also check for subtypes of poll e.g. | |
63 | 64 | |
65 | +```js | |
66 | +isPoll.chooseOne(msg) | |
67 | +// => Boolean | |
68 | +``` | |
69 | + | |
64 | 70 | ### `scuttle.poll.async.get(key, cb)` |
65 | 71 | |
66 | 72 | fetches all the messages associated with a particular poll, and returns a delightfully accessible object: |
67 | 73 |
package-lock.json | ||
---|---|---|
@@ -307,13 +307,8 @@ | ||
307 | 307 | "version": "4.5.0", |
308 | 308 | "resolved": "https://registry.npmjs.org/lodash.clonedeep/-/lodash.clonedeep-4.5.0.tgz", |
309 | 309 | "integrity": "sha1-4j8/nE+Pvd6HJSnBBxhXoIblzO8=" |
310 | 310 | }, |
311 | - "lodash.merge": { | |
312 | - "version": "4.6.1", | |
313 | - "resolved": "https://registry.npmjs.org/lodash.merge/-/lodash.merge-4.6.1.tgz", | |
314 | - "integrity": "sha512-AOYza4+Hf5z1/0Hztxpm2/xiPZgi/cjMqdnKTUWTBSKchJlxXXuUSxCCl8rJlf4g6yww/j6mA8nC8Hw/EZWxKQ==" | |
315 | - }, | |
316 | 311 | "minimatch": { |
317 | 312 | "version": "3.0.4", |
318 | 313 | "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.0.4.tgz", |
319 | 314 | "integrity": "sha512-yJHVQEhyqPLUTgt9B83PXu6W3rx4MvvHvSUvToogpwoGDOUQ+yDrR0HRot+yOCdCO7u4hX3pWft6kWBBcqh0UA==", |
poll/async/get.js | ||
---|---|---|
@@ -1,7 +1,10 @@ | ||
1 | 1 | const pull = require('pull-stream') |
2 | 2 | const sort = require('ssb-sort') |
3 | 3 | const isPoll = require('../../isPoll') |
4 | +const isPosition = require('../../isPosition') | |
5 | +const { ERROR_POSITION_TYPE } = require('../../types') | |
6 | +const results = require('../../position/sync/chooseOneResults') | |
4 | 7 | |
5 | 8 | module.exports = function (server) { |
6 | 9 | return function get (key, cb) { |
7 | 10 | server.get(key, (err, value) => { |
@@ -32,22 +35,40 @@ | ||
32 | 35 | }) |
33 | 36 | } |
34 | 37 | } |
35 | 38 | |
36 | -function decoratedPoll (poll, msgs) { | |
37 | - msgs = sort(msgs) | |
38 | - // TODO add missingContext warnings | |
39 | +function decoratedPoll (rawPoll, msgs) { | |
40 | + const { title, body } = rawPoll.value.content | |
39 | 41 | |
40 | - const pollType = poll.pollDetails.type | |
41 | - // const pollType = poll.details.type | |
42 | + const poll = Object.assign({}, rawPoll, { | |
43 | + title, | |
44 | + body, | |
42 | 45 | |
43 | - const positions = msgs.filter(isPoll[pollType]) | |
46 | + positions: [], | |
47 | + results: {}, | |
48 | + errors: [] | |
49 | + }) | |
44 | 50 | |
45 | - return Object.assign({}, poll, { | |
46 | - title: poll.content.title, | |
47 | - body: poll.content.body, | |
51 | + // filter position message into 'positions' and 'errors' | |
52 | + // TODO schema checks right position shape, but needs to add e.g. acceptible position ranges based on poll | |
53 | + // TODO add missingContext warnings | |
54 | + msgs = sort(msgs) | |
55 | + const type = poll.value.content.pollDetails.type | |
56 | + msgs.forEach(msg => { | |
57 | + if (isPosition[type](msg)) { | |
58 | + poll.positions.push(msg) | |
59 | + return | |
60 | + } | |
48 | 61 | |
49 | - // positions: msgs.filter, | |
50 | - results: {}, // TODO add reduction of positions | |
51 | - errors: {} | |
62 | + if (isPosition(msg)) { | |
63 | + poll.errors.push({ | |
64 | + type: ERROR_POSITION_TYPE, | |
65 | + message: `Position responses need to be off the ${type} type for this poll`, | |
66 | + position: msg | |
67 | + }) | |
68 | + } | |
52 | 69 | }) |
70 | + | |
71 | + poll.results = results(poll.positions) | |
72 | + | |
73 | + return poll | |
53 | 74 | } |
poll/schema/details/chooseOne.js | ||
---|---|---|
@@ -1,6 +1,6 @@ | ||
1 | -const { chooseOnePollType } = require('../../../types') | |
2 | -const typeStringPattern = `^${chooseOnePollType}$` | |
1 | +const { CHOOSE_ONE } = require('../../../types') | |
2 | +const typeStringPattern = `^${CHOOSE_ONE}$` | |
3 | 3 | |
4 | 4 | var schema = { |
5 | 5 | type: 'object', |
6 | 6 | required: ['type', 'choices'], |
poll/sync/chooseOne.js | ||
---|---|---|
@@ -1,12 +1,12 @@ | ||
1 | 1 | const Poll = require('./poll') |
2 | -const { chooseOnePollType } = require('../../types') | |
2 | +const { CHOOSE_ONE } = require('../../types') | |
3 | 3 | |
4 | 4 | function ChooseOne ({ choices, title, closesAt, body, channel, recps, mentions }) { |
5 | 5 | return Poll({ |
6 | 6 | pollDetails: { |
7 | 7 | choices, |
8 | - type: chooseOnePollType | |
8 | + type: CHOOSE_ONE | |
9 | 9 | }, |
10 | 10 | title, |
11 | 11 | closesAt, |
12 | 12 | body, |
poll/sync/isPoll.js | ||
---|---|---|
@@ -2,15 +2,23 @@ | ||
2 | 2 | const schema = require('../schema/poll') |
3 | 3 | const validator = Validator(schema, {verbose: true}) |
4 | 4 | const getMsgContent = require('../../lib/getMsgContent') |
5 | 5 | |
6 | +const isChooseOnePoll = require('./isChooseOnePoll')() | |
7 | + | |
6 | 8 | // server is not used here. Closure pattern is just for consistency of use with other functions. |
7 | 9 | module.exports = function (server) { |
8 | - return function isPoll (obj) { | |
10 | + function isPoll (obj) { | |
9 | 11 | const result = validator(getMsgContent(obj)) |
10 | 12 | |
11 | 13 | // exposes error messages provided by is-my-json-valid |
12 | 14 | isPoll.errors = validator.errors |
13 | 15 | |
14 | 16 | return result |
15 | 17 | } |
18 | + | |
19 | + isPoll.chooseOne = isChooseOnePoll | |
20 | + // isPoll.dot = isDotPoll | |
21 | + // isPoll.score = isScorePoll | |
22 | + | |
23 | + return isPoll | |
16 | 24 | } |
position/schema/details/chooseOne.js | ||
---|---|---|
@@ -1,6 +1,6 @@ | ||
1 | -const { chooseOnePositionType } = require('../../../types') | |
2 | -const typeStringPattern = `^${chooseOnePositionType}$` | |
1 | +const { CHOOSE_ONE } = require('../../../types') | |
2 | +const typeStringPattern = `^${CHOOSE_ONE}$` | |
3 | 3 | |
4 | 4 | var schema = { |
5 | 5 | type: 'object', |
6 | 6 | required: ['type', 'choice'], |
position/sync/chooseOne.js | ||
---|---|---|
@@ -1,12 +1,12 @@ | ||
1 | 1 | const Postion = require('./position') |
2 | -const { chooseOnePositionType } = require('../../types') | |
2 | +const { CHOOSE_ONE } = require('../../types') | |
3 | 3 | |
4 | 4 | function ChooseOne ({ poll, choice, reason, channel, recps, mentions }) { |
5 | 5 | return Postion({ |
6 | 6 | poll, |
7 | 7 | positionDetails: { |
8 | - type: chooseOnePositionType, | |
8 | + type: CHOOSE_ONE, | |
9 | 9 | choice |
10 | 10 | }, |
11 | 11 | reason, |
12 | 12 | channel, |
test/poll/sync/chooseOne.test.js | ||
---|---|---|
@@ -11,9 +11,10 @@ | ||
11 | 11 | choices: [1, 2, 'three'], |
12 | 12 | title: 'how many food', |
13 | 13 | closesAt: Date.now() |
14 | 14 | }) |
15 | - t.true(isPoll(validPoll), 'simple') | |
15 | + t.true(isPoll(validPoll), 'simple (passes isPoll)') | |
16 | + t.true(isPoll.chooseOne(validPoll), 'simple (passes isPoll.chooseOne)') | |
16 | 17 | |
17 | 18 | var fullPollMsg = { |
18 | 19 | key: '%somekey', |
19 | 20 | value: { |
test/poll/sync/isPoll.test.js | ||
---|---|---|
@@ -1,7 +1,8 @@ | ||
1 | 1 | const test = require('tape') |
2 | 2 | const ChooseOne = require('../../../poll/sync/chooseOne') |
3 | 3 | const isPoll = require('../../../isPoll') |
4 | +const { CHOOSE_ONE } = require('../../../types') | |
4 | 5 | |
5 | 6 | // this is for testing the attributes that are required for all polls |
6 | 7 | |
7 | 8 | test('Poll - common requirements', function (t) { |
@@ -28,9 +29,9 @@ | ||
28 | 29 | |
29 | 30 | var fullyFeatured = { |
30 | 31 | type: 'poll', |
31 | 32 | pollDetails: { |
32 | - type: 'chooseOnePoll', // TODO change to chooseOne? | |
33 | + type: CHOOSE_ONE, | |
33 | 34 | title: 'how many dogs?', |
34 | 35 | choices: [1, 2, 3] |
35 | 36 | }, |
36 | 37 | title: 'how many food', |
test/position/sync/isPosition.test.js | ||
---|---|---|
@@ -1,6 +1,7 @@ | ||
1 | 1 | const test = require('tape') |
2 | 2 | const isPosition = require('../../../isPosition') |
3 | +const { CHOOSE_ONE } = require('../../../types') | |
3 | 4 | |
4 | 5 | // this is for testing the attributes that are required for all polls |
5 | 6 | test('position - common requirements', function (t) { |
6 | 7 | var missingDetails = { |
@@ -13,9 +14,9 @@ | ||
13 | 14 | |
14 | 15 | var missingPoll = { |
15 | 16 | type: 'position', |
16 | 17 | positionDetails: { |
17 | - type: 'chooseOnePosition', | |
18 | + type: CHOOSE_ONE, | |
18 | 19 | choice: 0 |
19 | 20 | } |
20 | 21 | } |
21 | 22 | t.false(isPosition(missingPoll), 'needs poll') |
types.js | ||
---|---|---|
@@ -1,7 +1,6 @@ | ||
1 | 1 | module.exports = { |
2 | - chooseOnePollType: 'chooseOnePoll', | |
3 | - chooseOnePositionType: 'chooseOnePosition' | |
2 | + CHOOSE_ONE: 'chooseOne' | |
4 | 3 | } |
5 | 4 | |
6 | 5 | // Question: do these need to be different, could we just have 'chooseOne', |
7 | 6 | // because we already have: |
@@ -17,8 +16,8 @@ | ||
17 | 16 | // I wonder if a position could just be ... but this would mean we would have to have the parent poll, and then based o nthe type there, run a isOneChoicePostion validator... which I wthink could be good? Maybe I don't understandthe complexity of the other types of positions. |
18 | 17 | // { type: 'position', poll, choice } |
19 | 18 | // |
20 | 19 | // |
21 | -// I not that there's also a validation thing where | |
20 | +// I not that there's also a validation thing where | |
22 | 21 | // { type: 'position', poll, choice: 9000 } |
23 | 22 | // |
24 | 23 | // is probably invalid, and that the choice should be an integer bound by the parent poll choices. Can probably think about that case later, but making out reducer we're gonna have to put some filtering logic somewhere. Could programatically generate a position schema based on a poll.... _WOAH there EASY MIX_ |
Built with git-ssb-web