mirror of https://codeberg.org/pzp/pzp-goals.git
parse() and serialize()
This commit is contained in:
parent
49c4e1b280
commit
e8e88a8bf3
32
lib/index.js
32
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, '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,
|
||||
|
|
|
@ -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' })
|
||||
|
||||
|
|
Loading…
Reference in New Issue