Skip to content

Commit

Permalink
feat: bring low-level filter
Browse files Browse the repository at this point in the history
  • Loading branch information
antongolub committed Sep 22, 2023
1 parent 1c2a8e1 commit 65e5060
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 9 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ const tower = createTower({
committerName: 'Foo Bar', // Username and email to sign annotaged git tags
committerEmail: '[email protected]', // Defaults to Semrel Extra Bot <[email protected]>
format: v => v + '', // Opt value formatter. Defaults to JSON.stringify
parse: v => v // Opt parser. Defaults to JSON.parse
parse: v => v, // Opt parser. Defaults to JSON.parse
filter: v => v % 2 // Opt low level filter (applied before parse). Defaults to () => true
})

const id: string = 'some@tag'
Expand Down
17 changes: 11 additions & 6 deletions src/main/ts/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {readTags, deleteTag, pushTags} from './git'
import {TTagEntry, TTower, TTowerFactory, TTowerOpts} from './interface'
import {TFilter, TTagEntry, TTower, TTowerFactory, TTowerOpts} from './interface'

export const createTower: TTowerFactory = (opts: TTowerOpts): TTower => ({
async create(tag, data){
Expand All @@ -8,14 +8,18 @@ export const createTower: TTowerFactory = (opts: TTowerOpts): TTower => ({
},
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
async read(id?: string) {
async read(id?: string | TFilter) {
const parser = opts.parse || parse
const entries: TTagEntry[] = (await readTags(opts)).map(({tag: id, body}) => ({id, data: parser(body)}))
if (id === undefined) {
return entries
const predicate = typeof id === 'function' ? id : (opts.filter || filter)
const entries: TTagEntry[] = (await readTags(opts))
.filter(predicate)
.map(({tag: id, body}) => ({id, data: parser(body)}))

if (typeof id === 'string') {
return entries.find(({id: _id}) => _id === id) || null
}

return entries.find(({id: _id}) => _id === id) || null
return entries
},
async delete(tag) {
await deleteTag({...opts, tag})
Expand All @@ -26,6 +30,7 @@ export const createTower: TTowerFactory = (opts: TTowerOpts): TTower => ({
}
})

const filter = () => true
const format = JSON.stringify
const parse = (value: any): TTagEntry['data'] => {
try {
Expand Down
5 changes: 4 additions & 1 deletion src/main/ts/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ export type TTagEntry = {
data: any
}

export type TFilter = (v: {tag: string, body: string}) => boolean

export type TTower = {
create(id: string, data: TTagEntry['data']): Promise<void>
read(id: string): Promise<TTagEntry | null>
read(): Promise<TTagEntry[]>
read(fn?: TFilter): Promise<TTagEntry[]>
update(id: string, data: TTagEntry['data']): Promise<void>
delete(id: string): Promise<void>
}
Expand All @@ -22,6 +24,7 @@ export type TTowerOpts = {
committerEmail?: string
parse?: (v: string) => any
format?: (v: any) => string
filter?: TFilter
}

export type TAnnotatedTag = {
Expand Down
6 changes: 5 additions & 1 deletion src/test/ts/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ describe('tagTower', () => {
assert.equal(await tower.read(id), null)
})

it('supports custom parse/format', async () => {
it('supports custom parse, format & filter', async () => {
const cwd = path.resolve(temp, 'custom-format')
await fs.mkdir(cwd, { recursive: true })
await exec('git', ['init', '--bare'], {cwd})
Expand All @@ -55,6 +55,10 @@ describe('tagTower', () => {
})

await tower.create('foo', 'bar')
await tower.create('baz', 'qux')
assert.equal((await tower.read('foo'))?.data, '++bar++')

const filtered = await tower.read(({tag}) => tag === 'foo')
assert.deepEqual(filtered, [{id: 'foo', data: '++bar++'}])
})
})

0 comments on commit 65e5060

Please sign in to comment.