parse() and serialize()

This commit is contained in:
Andre Staltz 2023-12-21 14:00:26 +02:00
parent 49c4e1b280
commit e8e88a8bf3
No known key found for this signature in database
GPG Key ID: 9EDE23EA7E8A4890
2 changed files with 53 additions and 2 deletions

View File

@ -187,6 +187,36 @@ function initGoals(peer, config) {
}
}
/**
* @public
* @param {GoalDSL} goalDSL
* @returns {Goal}
*/
function parse(goalDSL) {
return new GoalImpl('?', goalDSL)
}
/**
* @param {Pick<Goal, 'type' | 'count'>} 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
@ -292,6 +322,8 @@ function initGoals(peer, config) {
}
return {
parse,
serialize,
set,
get,
getMsgPurpose,

View File

@ -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' })