Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion packages/core/src/Id.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ export type Tagged =
| PropertyId
| TheoremId
| { kind: 'trait'; space: number; property: number }
| { kind: 'unknown'; id: number }

export type SpaceId = { kind: 'space'; id: number }
export type PropertyId = { kind: 'property'; id: number }
export type TheoremId = { kind: 'theorem'; id: number }

const pattern = /^(?<prefix>[spti])0*(?<id>\d+)/i
const pattern = /^(?<prefix>[spti])?0*(?<id>\d+)/i

export function traitId({ space, property }: TraitId): string {
return `${space}|${property}`
Expand All @@ -33,6 +34,9 @@ export function tag(input: string): Tagged | null {
}

const id = parseInt(match.groups.id)
if (!match.groups.prefix) {
return { kind: 'unknown', id }
}
switch (match.groups.prefix.toUpperCase()) {
case 'S':
return { kind: 'space', id }
Expand Down
44 changes: 39 additions & 5 deletions packages/core/test/Id.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, expect, it } from 'vitest'
import { format, toInt, trim } from '../src/Id'
import { format, toInt, trim, tag } from '../src/Id'

describe('format', () => {
it('pads the given character', () => {
Expand All @@ -16,25 +16,59 @@ describe('toInt', () => {
expect(toInt('T000100')).toEqual(100)
})

it('does not require a leading prefix', () => {
it('does not require leading zeros', () => {
expect(toInt('P12')).toEqual(12)
})

it('does not match unknown types', () => {
it('does not match nonexistent kinds', () => {
expect(toInt('Z000100')).toEqual(0)
})

it('matches unknown kind', () => {
expect(toInt('000123')).toEqual(123)
})
})

describe('trim', () => {
it('trims down to the numeric content', () => {
expect(trim('T000100')).toEqual('100')
})

it('does not require a leading prefix', () => {
it('does not require leading zeros', () => {
expect(trim('P12')).toEqual('12')
})

it('does not match unknown types', () => {
it('returns self for nonexistent kinds', () => {
expect(trim('Z000100')).toEqual('Z000100')
})

it('matches unknown kind', () => {
expect(trim('000123')).toEqual('123')
})
})

describe('tag', () => {
it('tags spaces', () => {
expect(tag('S000100')?.kind).toEqual('space')
})

it('tags properties', () => {
expect(tag('P000100')?.kind).toEqual('property')
})

it('tags theorems', () => {
expect(tag('T000100')?.kind).toEqual('theorem')
})

it('tags unknowns', () => {
expect(tag('000100')?.kind).toEqual('unknown')
})

it('is null for nonexistent kinds', () => {
expect(tag('X000100')).toEqual(null)
})

it('allows lowercase', () => {
expect(tag('s000100')?.kind).toEqual('space')
})
})
8 changes: 4 additions & 4 deletions packages/core/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ export default defineConfig({
},
test: {
coverage: {
lines: 91.04,
branches: 93.68,
statements: 91.04,
functions: 83.55,
lines: 91.19,
branches: 94.07,
statements: 91.19,
functions: 84.21,
skipFull: true,
thresholdAutoUpdate: true,
exclude: ['src/Formula/Grammar.ts', 'test'],
Expand Down
5 changes: 5 additions & 0 deletions packages/viewer/src/params/id.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import type { ParamMatcher } from '@sveltejs/kit'

export const match: ParamMatcher = param => {
return /^\w?(\d+)(.md)?$/.test(param)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason you want to allow things that don't start with an expected letter prefix here?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I got it ... because you want spaces/1 to Just Work. I think that's fine in this case, but does complicate the case of spaces/1/properties/1 if you want to rewrite it. In that case, you can't just rewrite the given id string, and instead have to deeply understand the position in the routes.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm okay with requiring prefix; at least, supporting non-prefixed IDs when known in context can come later.

}