@0001-labs/idv1 1.0.6 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +26 -30
- package/package.json +1 -1
- package/src/directory.ts +0 -1
- package/src/identifier.ts +36 -46
- package/src/idv1.ts +16 -13
- package/src/namespace.ts +1 -1
- package/src/taxonomy.ts +3 -3
- package/src/util/blockedWords.ts +1 -1
- package/src/util/capacity.ts +8 -8
- package/src/util/scan.ts +2 -3
package/README.md
CHANGED
|
@@ -1,61 +1,57 @@
|
|
|
1
1
|
# IDv1
|
|
2
2
|
|
|
3
|
-
Six-character IDs
|
|
3
|
+
Six-character IDs with five structural shapes, named by their patterns. Applications choose what those shapes mean.
|
|
4
4
|
|
|
5
5
|
```sh
|
|
6
6
|
npm install @0001-labs/idv1
|
|
7
7
|
```
|
|
8
8
|
|
|
9
9
|
```ts
|
|
10
|
-
import { codeCandidate, parseCode } from '@0001-labs/idv1'
|
|
10
|
+
import { codeCandidate, parseCode, type LLNNXXId } from '@0001-labs/idv1'
|
|
11
11
|
|
|
12
|
-
const candidate = codeCandidate('
|
|
12
|
+
const candidate: LLNNXXId = codeCandidate('LLNNXX')
|
|
13
13
|
const parsed = parseCode(candidate)
|
|
14
14
|
```
|
|
15
15
|
|
|
16
|
-
`parseCode(unknown)` returns
|
|
16
|
+
`parseCode(unknown)` returns `{ shape, id }` with a shape-branded ID, or `null`. `codeShape` and `isCodeOf(shape, value)` use the same parser. The types are `LLNNNNId`, `NNLLLLId`, `LLLLNNId`, `LLNNXXId` and `XXXXXXId`, also available as `Id<Shape>`. Validation establishes syntax, not an assigned record.
|
|
17
17
|
|
|
18
|
-
|
|
18
|
+
| Shape | Structure |
|
|
19
|
+
| --- | --- |
|
|
20
|
+
| `LLNNNN` | Two ASCII letters, four digits |
|
|
21
|
+
| `NNLLLL` | Two digits, four ASCII letters |
|
|
22
|
+
| `LLLLNN` | Four ASCII letters, two digits |
|
|
23
|
+
| `LLNNXX` | Two letters, two digits, two letters or digits; the final pair contains at least one letter |
|
|
24
|
+
| `XXXXXX` | Every remaining exact six-character ASCII base62 string |
|
|
25
|
+
|
|
26
|
+
`codeCandidate(shape)` draws with Web Crypto. `LLNNNN` and `LLLLNN` accept an optional exact two-letter prefix, preserving case. Candidates obey word filters; `LLLLNN` also excludes blocked digit pairs. A bounded draw fails after 1,000 attempts. An injected `random` must return finite numbers in `[0, 1)`. Claim a candidate in its namespace and store it beside the record’s UUID. A code is a short handle; its pool can reset and reissue codes.
|
|
19
27
|
|
|
20
|
-
`ID_CAPACITY`
|
|
28
|
+
`ID_CAPACITY` is keyed by the same patterns and counts structural capacity before generation filters. `TOTAL_ID_CAPACITY` is their sum. Scan encoding keeps its checksum and payload format; `parseCode` reads the shape of a branded scan result. `isScanIdOf(shape, value)` checks one shape.
|
|
21
29
|
|
|
22
30
|
## The idv1 command
|
|
23
31
|
|
|
24
|
-
A codebase
|
|
32
|
+
A codebase keeps a `.id` file with optional `shapes` names keyed by pattern, namespaces, taxonomies, parts and facts. A shape name is a label, not a taxonomy reservation. Taxonomies declare their own pattern and structural shape.
|
|
25
33
|
|
|
26
34
|
```sh
|
|
27
|
-
npx idv1 directory
|
|
28
|
-
npx idv1 status
|
|
29
|
-
npx idv1 health
|
|
30
|
-
npx idv1 resolve co14Ch
|
|
35
|
+
npx idv1 directory
|
|
36
|
+
npx idv1 status
|
|
37
|
+
npx idv1 health
|
|
38
|
+
npx idv1 resolve co14Ch
|
|
31
39
|
```
|
|
32
40
|
|
|
33
|
-
|
|
41
|
+
The same operations are exported: `readDirectory`, `directoryHealth`, `resolveCode`, `taxonomyOf`, `describeDirectory`, `describeStatus` and `runDirectoryCommand`.
|
|
34
42
|
|
|
35
|
-
The package ships TypeScript source. Bundlers and `tsx` read it
|
|
43
|
+
The package ships TypeScript source. Bundlers and `tsx` read it directly. Plain Node 22.18 or later imports `@0001-labs/idv1/register` first, then dynamically imports the package.
|
|
36
44
|
|
|
37
45
|
## Explorer utility
|
|
38
46
|
|
|
39
|
-
Import `createExplorer` and `mountExplorer` from `@0001-labs/idv1/explorer`, and `@0001-labs/idv1/explorer.css` for the browser view. Supply `ExplorerData`: namespace names, shapes, taxonomies, identifiers, nodes and
|
|
40
|
-
|
|
41
|
-
`createExplorer(data)` returns `entries(path)`, `title(path)` and `stepName(path, depth)` for any renderer. `mountExplorer(element, data)` renders that model and returns a cleanup function. Theme it with the `--idv1-explorer-*` CSS variables; its host owns loading and authorization.
|
|
47
|
+
Import `createExplorer` and `mountExplorer` from `@0001-labs/idv1/explorer`, and `@0001-labs/idv1/explorer.css` for the browser view. Supply `ExplorerData`: namespace names, shapes, taxonomies, identifiers, nodes and links. The utility owns drill-down, counts, identifier resolution, taxonomy highlighting, keyboard navigation and URL history.
|
|
42
48
|
|
|
43
|
-
|
|
49
|
+
Optional `collections` carry source records and lazy `load` functions. Records may link to host details with `href`. The utility contains no application records or network requests. `createExplorer(data)` returns `entries(path)`, `title(path)`, `stepName(path, depth)` and `load(path)`. `mountExplorer(element, data)` renders it and returns a cleanup function. Theme it with `--idv1-explorer-*` CSS variables; the host owns loading and authorization.
|
|
44
50
|
|
|
45
|
-
|
|
51
|
+
## Upgrading from 1.x
|
|
46
52
|
|
|
47
|
-
|
|
48
|
-
| --- | --- |
|
|
49
|
-
| Account | `LLNNNN` |
|
|
50
|
-
| List | `NNLLLL` |
|
|
51
|
-
| Project | `LLLLNN`, formerly also accepted as Account |
|
|
52
|
-
| Item | `LLNN` followed by two ASCII letters or digits, with at least one letter |
|
|
53
|
-
| Event | Any remaining string of exactly six ASCII letters or digits |
|
|
54
|
-
|
|
55
|
-
Use the root export and `parseCode` in place of the `advanced` subpath and its classifier. The Product kind and studio-specific account reservations were removed. Keep reservations in the application, choose the intended shape for each former Product use, and review stored codes before changing how your app classifies them.
|
|
53
|
+
Release 2 changes API names, not code grammar, scan payloads or capacity. Replace semantic shape arguments with patterns, `parseCode(...).kind` with `.shape`, and `codeKind` with `codeShape`. Use `codeCandidate(shape)` and `isCodeOf(shape, value)` in place of per-kind wrappers. Regex constants are pattern-named, such as `LLNNXX_CODE`; `blockedLLLLNNCode` replaces the semantic name of its filter. `.id` part and taxonomy `shape` values use patterns. Keep application words in the optional `shapes` map.
|
|
56
54
|
|
|
57
55
|
## Documentation
|
|
58
56
|
|
|
59
|
-
The [
|
|
60
|
-
|
|
61
|
-
[MIT license](LICENSE)
|
|
57
|
+
The [specification](https://idv1.0001.dev/docs/spec) owns the code design. [MIT license](LICENSE).
|
package/package.json
CHANGED
package/src/directory.ts
CHANGED
package/src/identifier.ts
CHANGED
|
@@ -1,44 +1,39 @@
|
|
|
1
|
-
import { blockedCodeLetters,
|
|
1
|
+
import { blockedCodeLetters, blockedLLLLNNCode } from './util/blockedWords'
|
|
2
2
|
|
|
3
3
|
export const LETTERS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
|
|
4
4
|
export const CAPITALS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
|
|
5
5
|
export const DIGITS = '0123456789'
|
|
6
6
|
export const ALPHABET = DIGITS + LETTERS
|
|
7
7
|
|
|
8
|
-
export const
|
|
9
|
-
export const
|
|
10
|
-
export const
|
|
11
|
-
export const
|
|
8
|
+
export const LLNNNN_CODE = /^[A-Za-z]{2}[0-9]{4}(?![\s\S])/
|
|
9
|
+
export const NNLLLL_CODE = /^[0-9]{2}[A-Za-z]{4}(?![\s\S])/
|
|
10
|
+
export const LLLLNN_CODE = /^[A-Za-z]{4}[0-9]{2}(?![\s\S])/
|
|
11
|
+
export const LLNNXX_CODE = /^[A-Za-z]{2}[0-9]{2}(?:[A-Za-z][A-Za-z0-9]|[0-9][A-Za-z])(?![\s\S])/
|
|
12
12
|
export const ANY_CODE = /^[A-Za-z0-9]{6}(?![\s\S])/
|
|
13
|
-
export const
|
|
13
|
+
export const XXXXXX_CODE = new RegExp(`^(?!${[LLNNNN_CODE, NNLLLL_CODE, LLLLNN_CODE, LLNNXX_CODE].map(pattern => pattern.source.slice(1)).join('|')})[A-Za-z0-9]{6}(?![\\s\\S])`)
|
|
14
14
|
|
|
15
|
-
export type
|
|
15
|
+
export type CodeShape = 'LLNNNN' | 'NNLLLL' | 'LLLLNN' | 'LLNNXX' | 'XXXXXX'
|
|
16
16
|
declare const idBrand: unique symbol
|
|
17
|
-
export type Id<K extends
|
|
18
|
-
export type
|
|
19
|
-
export type
|
|
20
|
-
export type
|
|
21
|
-
export type
|
|
22
|
-
export type
|
|
23
|
-
export type ParsedCode = { [K in
|
|
17
|
+
export type Id<K extends CodeShape = CodeShape> = string & { readonly [idBrand]: K }
|
|
18
|
+
export type LLNNNNId = Id<'LLNNNN'>
|
|
19
|
+
export type NNLLLLId = Id<'NNLLLL'>
|
|
20
|
+
export type LLLLNNId = Id<'LLLLNN'>
|
|
21
|
+
export type LLNNXXId = Id<'LLNNXX'>
|
|
22
|
+
export type XXXXXXId = Id<'XXXXXX'>
|
|
23
|
+
export type ParsedCode = { [K in CodeShape]: { shape: K; id: Id<K> } }[CodeShape]
|
|
24
24
|
|
|
25
|
-
export const CODE_PATTERN = {
|
|
25
|
+
export const CODE_PATTERN = { LLNNNN: LLNNNN_CODE, NNLLLL: NNLLLL_CODE, LLLLNN: LLLLNN_CODE, LLNNXX: LLNNXX_CODE, XXXXXX: XXXXXX_CODE } as const
|
|
26
26
|
|
|
27
27
|
export function parseCode(value: unknown): ParsedCode | null {
|
|
28
28
|
if (typeof value !== 'string' || !ANY_CODE.test(value)) return null
|
|
29
|
-
for (const
|
|
30
|
-
if (CODE_PATTERN[
|
|
29
|
+
for (const shape of ['LLNNNN', 'NNLLLL', 'LLLLNN', 'LLNNXX'] as const) {
|
|
30
|
+
if (CODE_PATTERN[shape].test(value)) return { shape, id: value } as ParsedCode
|
|
31
31
|
}
|
|
32
|
-
return {
|
|
32
|
+
return { shape: 'XXXXXX', id: value as XXXXXXId }
|
|
33
33
|
}
|
|
34
34
|
|
|
35
|
-
export const
|
|
36
|
-
export const isCodeOf = <K extends
|
|
37
|
-
export const isAccountCode = (value: unknown): value is AccountId => isCodeOf('account', value)
|
|
38
|
-
export const isListCode = (value: unknown): value is ListId => isCodeOf('list', value)
|
|
39
|
-
export const isProjectCode = (value: unknown): value is ProjectId => isCodeOf('project', value)
|
|
40
|
-
export const isItemCode = (value: unknown): value is ItemId => isCodeOf('item', value)
|
|
41
|
-
export const isEventCode = (value: unknown): value is EventId => isCodeOf('event', value)
|
|
35
|
+
export const codeShape = (value: unknown): CodeShape | null => parseCode(value)?.shape ?? null
|
|
36
|
+
export const isCodeOf = <K extends CodeShape>(shape: K, value: unknown): value is Id<K> => codeShape(value) === shape
|
|
42
37
|
|
|
43
38
|
export type Random = () => number
|
|
44
39
|
|
|
@@ -73,35 +68,30 @@ export function nameLetters(name: string, email = ''): string | null {
|
|
|
73
68
|
return null
|
|
74
69
|
}
|
|
75
70
|
|
|
76
|
-
export function codeCandidate<K extends
|
|
71
|
+
export function codeCandidate<K extends CodeShape>(shape: K, options: { letters?: string | null; random?: Random } = {}): Id<K> {
|
|
77
72
|
const random = options.random ?? secureRandom
|
|
78
73
|
const prefix = options.letters ?? ''
|
|
79
|
-
if (prefix && (!['
|
|
80
|
-
throw new Error('A prefix must be exactly two ASCII letters for
|
|
74
|
+
if (prefix && (!['LLNNNN', 'LLLLNN'].includes(shape) || !/^[A-Za-z]{2}(?![\s\S])/.test(prefix))) {
|
|
75
|
+
throw new Error('A prefix must be exactly two ASCII letters for LLNNNN or LLLLNN.')
|
|
81
76
|
}
|
|
82
77
|
for (let attempt = 0; attempt < 1000; attempt++) {
|
|
83
78
|
let candidate: string
|
|
84
|
-
switch (
|
|
85
|
-
case '
|
|
86
|
-
case '
|
|
87
|
-
case '
|
|
88
|
-
case '
|
|
89
|
-
case '
|
|
79
|
+
switch (shape) {
|
|
80
|
+
case 'LLNNNN': candidate = (prefix || draw(LETTERS, 2, random)) + draw(DIGITS, 4, random); break
|
|
81
|
+
case 'NNLLLL': candidate = draw(DIGITS, 2, random) + draw(LETTERS, 4, random); break
|
|
82
|
+
case 'LLLLNN': candidate = (prefix || draw(LETTERS, 2, random)) + draw(LETTERS, 2, random) + draw(DIGITS, 2, random); break
|
|
83
|
+
case 'LLNNXX': candidate = draw(LETTERS, 2, random) + draw(DIGITS, 2, random) + draw(ALPHABET, 2, random); break
|
|
84
|
+
case 'XXXXXX': candidate = draw(ALPHABET, 6, random); break
|
|
90
85
|
default: throw new Error('Unknown IDv1 shape.')
|
|
91
86
|
}
|
|
92
87
|
const parsed = parseCode(candidate)
|
|
93
|
-
if (parsed?.
|
|
88
|
+
if (parsed?.shape === shape && !(shape === 'LLLLNN' ? blockedLLLLNNCode(candidate) : blockedCodeLetters(candidate))) return parsed.id as Id<K>
|
|
94
89
|
}
|
|
95
|
-
throw new Error(`No ${
|
|
90
|
+
throw new Error(`No ${shape} code could be drawn.`)
|
|
96
91
|
}
|
|
97
92
|
|
|
98
|
-
export const accountCodeCandidate = (letters: string | null = null, random: Random = secureRandom): AccountId => codeCandidate('account', { letters, random })
|
|
99
|
-
export const listCodeCandidate = (random: Random = secureRandom): ListId => codeCandidate('list', { random })
|
|
100
|
-
export const projectCodeCandidate = (letters: string | null = null, random: Random = secureRandom): ProjectId => codeCandidate('project', { letters, random })
|
|
101
|
-
export const itemCodeCandidate = (random: Random = secureRandom): ItemId => codeCandidate('item', { random })
|
|
102
|
-
export const eventCodeCandidate = (random: Random = secureRandom): EventId => codeCandidate('event', { random })
|
|
103
93
|
|
|
104
|
-
export function
|
|
94
|
+
export function caseVariantsLLNNNN(code: string): string[] {
|
|
105
95
|
const [first, second] = [code[0], code[1]].map(letter => [...new Set([letter.toUpperCase(), letter.toLowerCase()])])
|
|
106
96
|
return first.flatMap(a => second.map(b => a + b + code.slice(2)))
|
|
107
97
|
}
|
|
@@ -110,15 +100,15 @@ export const JUNCTION_SEPARATOR = '-'
|
|
|
110
100
|
|
|
111
101
|
export function junction(...codes: string[]): string {
|
|
112
102
|
if (codes.length < 2) throw new Error('A junction joins two codes or more.')
|
|
113
|
-
for (const code of codes) if (!
|
|
103
|
+
for (const code of codes) if (!codeShape(code)) throw new Error(`${code} is no IDv1 code.`)
|
|
114
104
|
return codes.join(JUNCTION_SEPARATOR)
|
|
115
105
|
}
|
|
116
106
|
|
|
117
|
-
export function junctionParts(value: string): { code: string;
|
|
107
|
+
export function junctionParts(value: string): { code: string; shape: CodeShape }[] | null {
|
|
118
108
|
const codes = value.split(JUNCTION_SEPARATOR)
|
|
119
109
|
if (codes.length < 2) return null
|
|
120
|
-
const parts = codes.map(code => ({ code,
|
|
121
|
-
return parts.every((part): part is { code: string;
|
|
110
|
+
const parts = codes.map(code => ({ code, shape: codeShape(code) }))
|
|
111
|
+
return parts.every((part): part is { code: string; shape: CodeShape } => part.shape !== null) ? parts : null
|
|
122
112
|
}
|
|
123
113
|
|
|
124
114
|
export const isJunction = (value: string): boolean => junctionParts(value) !== null
|
package/src/idv1.ts
CHANGED
|
@@ -1,24 +1,24 @@
|
|
|
1
|
-
import { JUNCTION_SEPARATOR, parseCode, type
|
|
2
|
-
import { blockedCodeLetters,
|
|
1
|
+
import { JUNCTION_SEPARATOR, parseCode, type CodeShape } from './identifier'
|
|
2
|
+
import { blockedCodeLetters, blockedLLLLNNCode } from './util/blockedWords'
|
|
3
3
|
import { namespaceHealth, namespaceOf, type Namespace } from './namespace'
|
|
4
4
|
import { taxonomyHealth, taxonomyOf, type Taxonomy } from './taxonomy'
|
|
5
5
|
|
|
6
|
-
export type {
|
|
7
|
-
export
|
|
8
|
-
export type
|
|
9
|
-
export type
|
|
10
|
-
export type Resolved = { id: string; shape: CodeKind | null; namespace?: Namespace; taxonomy?: Taxonomy; part?: Part; facts: string[] }
|
|
6
|
+
export type Part = { id: string; shape: CodeShape; name: string; path: string | null }
|
|
7
|
+
export type ShapeNames = Partial<Record<CodeShape, string>>
|
|
8
|
+
export type Directory = { shapes?: ShapeNames; namespaces: Namespace[]; taxonomies: Taxonomy[]; parts: Part[]; facts: string[] }
|
|
9
|
+
export type Resolved = { id: string; shape: CodeShape | null; namespace?: Namespace; taxonomy?: Taxonomy; part?: Part; facts: string[] }
|
|
11
10
|
|
|
12
|
-
const SHAPES:
|
|
11
|
+
const SHAPES: CodeShape[] = ['LLNNNN', 'NNLLLL', 'LLLLNN', 'LLNNXX', 'XXXXXX']
|
|
13
12
|
const list = <T>(value: unknown): T[] => (Array.isArray(value) ? value : []) as T[]
|
|
14
13
|
|
|
15
14
|
export function readDirectory(text: string): Directory {
|
|
16
15
|
const value = JSON.parse(text) as Partial<Directory>
|
|
17
|
-
return { namespaces: list(value.namespaces), taxonomies: list(value.taxonomies), parts: list(value.parts), facts: list(value.facts) }
|
|
16
|
+
return { shapes: value.shapes ?? {}, namespaces: list(value.namespaces), taxonomies: list(value.taxonomies), parts: list(value.parts), facts: list(value.facts) }
|
|
18
17
|
}
|
|
19
18
|
|
|
20
19
|
export function directoryHealth(directory: Directory, exists: (path: string) => boolean = () => true): string[] {
|
|
21
20
|
const problems: string[] = []
|
|
21
|
+
for (const [shape, name] of Object.entries(directory.shapes ?? {})) if (!SHAPES.includes(shape as CodeShape) || typeof name !== 'string' || !name.trim()) problems.push(`Invalid shape name: ${shape}`)
|
|
22
22
|
const seen = (values: string[], what: string) => values.forEach((value, index) => { if (values.indexOf(value) !== index) problems.push(`${what} ${value} is declared twice`) })
|
|
23
23
|
problems.push(...namespaceHealth(directory.namespaces), ...taxonomyHealth(directory.taxonomies))
|
|
24
24
|
seen(directory.namespaces.map(namespace => namespace.shorthand), 'Namespace')
|
|
@@ -26,9 +26,9 @@ export function directoryHealth(directory: Directory, exists: (path: string) =>
|
|
|
26
26
|
seen(directory.parts.map(part => part.id), 'Id')
|
|
27
27
|
const ids = new Set(directory.parts.map(part => part.id))
|
|
28
28
|
for (const part of directory.parts) {
|
|
29
|
-
const shape = parseCode(part.id)?.
|
|
29
|
+
const shape = parseCode(part.id)?.shape
|
|
30
30
|
if (shape !== part.shape) problems.push(`${part.id} reads as ${shape ?? 'no shape'}, not ${part.shape}`)
|
|
31
|
-
else if (shape === '
|
|
31
|
+
else if (shape === 'LLLLNN' ? blockedLLLLNNCode(part.id) : blockedCodeLetters(part.id)) problems.push(`${part.id} uses blocked letters or digits`)
|
|
32
32
|
if (!part.name) problems.push(`${part.id} has no name`)
|
|
33
33
|
if (part.path && !exists(part.path)) problems.push(`${part.id}: ${part.path} is gone`)
|
|
34
34
|
}
|
|
@@ -44,7 +44,7 @@ export function resolveCode(directory: Directory, value: string): Resolved[] {
|
|
|
44
44
|
const qualified = /^-([A-Z]{2})-(.+)$/.exec(value)
|
|
45
45
|
const ids = (qualified ? qualified[2] : value).split(JUNCTION_SEPARATOR)
|
|
46
46
|
return ids.map(id => {
|
|
47
|
-
const shape = parseCode(id)?.
|
|
47
|
+
const shape = parseCode(id)?.shape ?? null
|
|
48
48
|
const namespace = namespaceOf(directory, id, qualified?.[1])
|
|
49
49
|
const facts = directory.facts.filter(fact => { const [subject, , object] = fact.split(' '); return subject === id || object === id })
|
|
50
50
|
return { id, shape, namespace, taxonomy: shape ? taxonomyOf(directory, id) : undefined, part: directory.parts.find(part => part.id === id), facts }
|
|
@@ -72,7 +72,7 @@ export function describeStatus(directory: Directory, problems: string[]): string
|
|
|
72
72
|
`${count(directory.parts.length, 'part')} · ${count(directory.facts.length, 'fact')} · ${count(directory.namespaces.length, 'namespace')} · ${count(directory.taxonomies.length, 'taxonomy', 'taxonomies')}`,
|
|
73
73
|
problems.length ? `${count(problems.length, 'problem')}: run idv1 health` : 'healthy',
|
|
74
74
|
'',
|
|
75
|
-
...shapes.map(([shape, n]) => ` ${shape.padEnd(9)}${n}`),
|
|
75
|
+
...shapes.map(([shape, n]) => ` ${shape.padEnd(9)}${n} ${directory.shapes?.[shape] ?? shape}`),
|
|
76
76
|
...directory.taxonomies.map(taxonomy => ` ${taxonomy.shorthand.padEnd(9)}${directory.parts.filter(part => taxonomyOf(directory, part.id) === taxonomy).length} ${taxonomy.name}`),
|
|
77
77
|
].join('\n')
|
|
78
78
|
}
|
|
@@ -84,6 +84,9 @@ export function describeDirectory(directory: Directory): string {
|
|
|
84
84
|
const line = (part: Part | undefined, id: string, depth: number) => `${' '.repeat(depth)}${id} ${part?.name ?? '?'}${part && taxonomyOf(directory, id) ? ` (${taxonomyOf(directory, id)!.name})` : ''}${part?.path ? ` ${part.path}` : ''}`
|
|
85
85
|
const tree = (id: string, depth: number, trail: Set<string>): string[] => trail.has(id) ? [] : [line(directory.parts.find(part => part.id === id), id, depth), ...children(id).flatMap(child => tree(child, depth + 1, new Set([...trail, id])))]
|
|
86
86
|
return [
|
|
87
|
+
'Shapes',
|
|
88
|
+
...SHAPES.map(shape => ` ${shape} ${directory.shapes?.[shape] ?? shape}`),
|
|
89
|
+
'',
|
|
87
90
|
'Namespaces',
|
|
88
91
|
...directory.namespaces.map(({ shorthand, name }) => ` ${shorthand} ${name}`),
|
|
89
92
|
'',
|
package/src/namespace.ts
CHANGED
|
@@ -3,7 +3,7 @@ import { parseCode } from './identifier'
|
|
|
3
3
|
export type Namespace = { shorthand: string; name: string }
|
|
4
4
|
|
|
5
5
|
export function namespaceOf({ namespaces }: { namespaces: Namespace[] }, id: string, qualified?: string): Namespace | undefined {
|
|
6
|
-
const shorthand = qualified ?? (parseCode(id)?.
|
|
6
|
+
const shorthand = qualified ?? (parseCode(id)?.shape === 'LLLLNN' ? id.slice(0, 2) : undefined)
|
|
7
7
|
return namespaces.find(namespace => namespace.shorthand === shorthand) ?? (namespaces.length === 1 ? namespaces[0] : undefined)
|
|
8
8
|
}
|
|
9
9
|
|
package/src/taxonomy.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import { CODE_PATTERN, parseCode, type
|
|
1
|
+
import { CODE_PATTERN, parseCode, type CodeShape } from './identifier'
|
|
2
2
|
|
|
3
|
-
export type Taxonomy = { shorthand: string; name: string; shape:
|
|
3
|
+
export type Taxonomy = { shorthand: string; name: string; shape: CodeShape; pattern: string }
|
|
4
4
|
|
|
5
5
|
export function taxonomyOf(directory: { taxonomies: Taxonomy[] }, id: string): Taxonomy | undefined {
|
|
6
|
-
const shape = parseCode(id)?.
|
|
6
|
+
const shape = parseCode(id)?.shape
|
|
7
7
|
return directory.taxonomies.find(taxonomy => {
|
|
8
8
|
const at = taxonomy.pattern.replace(/\s/g, '').indexOf(taxonomy.shorthand)
|
|
9
9
|
return taxonomy.shape === shape && at >= 0 && id.slice(at, at + 2) === taxonomy.shorthand
|
package/src/util/blockedWords.ts
CHANGED
|
@@ -19,6 +19,6 @@ export function blockedCodeLetters(letters: string): boolean {
|
|
|
19
19
|
return BLOCKED_CODE_WORDS.some(word => small.includes(word))
|
|
20
20
|
}
|
|
21
21
|
|
|
22
|
-
export function
|
|
22
|
+
export function blockedLLLLNNCode(code: string): boolean {
|
|
23
23
|
return blockedCodeLetters(code.slice(0, 4)) || BLOCKED_CODE_DIGITS.includes(code.slice(4))
|
|
24
24
|
}
|
package/src/util/capacity.ts
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
const LETTERS = 52
|
|
2
2
|
const DIGITS = 10
|
|
3
3
|
export const TOTAL_ID_CAPACITY = (LETTERS + DIGITS) ** 6
|
|
4
|
-
const
|
|
5
|
-
const
|
|
6
|
-
const
|
|
7
|
-
const
|
|
4
|
+
const LLNNNN = LETTERS ** 2 * DIGITS ** 4
|
|
5
|
+
const NNLLLL = DIGITS ** 2 * LETTERS ** 4
|
|
6
|
+
const LLLLNN = LETTERS ** 4 * DIGITS ** 2
|
|
7
|
+
const LLNNXX = LETTERS ** 2 * DIGITS ** 2 * ((LETTERS + DIGITS) ** 2 - DIGITS ** 2)
|
|
8
8
|
|
|
9
|
-
export const ID_CAPACITY = {
|
|
10
|
-
export type
|
|
9
|
+
export const ID_CAPACITY = { LLNNNN, NNLLLL, LLLLNN, LLNNXX, XXXXXX: TOTAL_ID_CAPACITY - LLNNNN - NNLLLL - LLLLNN - LLNNXX } as const
|
|
10
|
+
export type IdCapacityShape = keyof typeof ID_CAPACITY
|
|
11
11
|
|
|
12
|
-
export function idUsage(
|
|
13
|
-
const capacity = ID_CAPACITY[
|
|
12
|
+
export function idUsage(shape: IdCapacityShape, used: number): { used: number; capacity: number; left: number; percent: number } {
|
|
13
|
+
const capacity = ID_CAPACITY[shape]
|
|
14
14
|
if (!Number.isSafeInteger(used) || used < 0) throw new RangeError('Usage must be a nonnegative safe integer.')
|
|
15
15
|
return { used, capacity, left: Math.max(0, capacity - used), percent: (used / capacity) * 100 }
|
|
16
16
|
}
|
package/src/util/scan.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ALPHABET,
|
|
1
|
+
import { ALPHABET, isCodeOf, parseCode, type CodeShape, type Id } from '../identifier'
|
|
2
2
|
|
|
3
3
|
const alphabet = ALPHABET
|
|
4
4
|
export const scanColumns = 16
|
|
@@ -93,8 +93,7 @@ export function decodeScan(bits: readonly boolean[]): Id | null {
|
|
|
93
93
|
return parseCode(id)?.id ?? null
|
|
94
94
|
}
|
|
95
95
|
|
|
96
|
-
export const
|
|
97
|
-
export const isUserScanId = (id: string): boolean => isAccountCode(id)
|
|
96
|
+
export const isScanIdOf = (shape: CodeShape, id: string): boolean => isCodeOf(shape, id)
|
|
98
97
|
export const isSupportedScanId = (id: string): boolean => parseCode(id) !== null
|
|
99
98
|
|
|
100
99
|
export type ScanRect = { x: number; y: number; width: number; height: number }
|