diff --git a/lib/index.js b/lib/index.js index ba3e590..e5a849a 100644 --- a/lib/index.js +++ b/lib/index.js @@ -187,6 +187,36 @@ function initGoals(peer, config) { } } + /** + * @public + * @param {GoalDSL} goalDSL + * @returns {Goal} + */ + function parse(goalDSL) { + return new GoalImpl('?', goalDSL) + } + + /** + * @param {Pick} goal + * @returns {GoalDSL} + */ + function serialize(goal) { + switch (goal.type) { + case 'newest': + return `newest-${goal.count}` + case 'all': + return 'all' + case 'set': + return 'set' + case 'dict': + return 'dict' + case 'none': + return 'none' + default: + throw new Error(`Unrecognized goal type: ${goal.type}`) + } + } + /** * @public * @param {string} tangleID @@ -268,14 +298,14 @@ function initGoals(peer, config) { assertDictPlugin(peer) const span = peer.dict.getGhostSpan() if (peer.dict.isGhostable(msgID, tangle.id)) { - return ['ghost', {tangleID: tangle.id, span }] + return ['ghost', { tangleID: tangle.id, span }] } } if (goalType === 'set') { assertSetPlugin(peer) const span = peer.set.getGhostSpan() if (peer.set.isGhostable(msgID, tangle.id)) { - return ['ghost', {tangleID: tangle.id, span }] + return ['ghost', { tangleID: tangle.id, span }] } } } @@ -292,6 +322,8 @@ function initGoals(peer, config) { } return { + parse, + serialize, set, get, getMsgPurpose, diff --git a/test/goals.test.js b/test/goals.test.js index d895bb0..218054b 100644 --- a/test/goals.test.js +++ b/test/goals.test.js @@ -4,6 +4,25 @@ const { isMapIterator } = require('node:util/types') const p = require('node:util').promisify const { createPeer } = require('./util') +test('parse() and serialize()', async (t) => { + const peer = createPeer({ name: 'alice' }) + await peer.db.loaded() + + const all = peer.goals.parse('all') + assert.equal(all.type, 'all') + assert.equal(all.count, Infinity) + + const newest = peer.goals.parse('newest-123') + assert.equal(newest.type, 'newest') + assert.equal(newest.count, 123) + + assert.equal(peer.goals.serialize(all), 'all') + assert.equal(peer.goals.serialize(newest), 'newest-123') + + await p(setTimeout)(200) // necessary wait, otherwise peer.close fails + await p(peer.close)(true) +}) + test('set, getByID, list, watch', async (t) => { const alice = createPeer({ name: 'alice' })