@0001-labs/idv1 2.0.1 → 2.0.3

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.
Files changed (3) hide show
  1. package/README.md +6 -7
  2. package/package.json +1 -1
  3. package/src/idv1.ts +19 -17
package/README.md CHANGED
@@ -29,25 +29,24 @@ const parsed = parseCode(candidate)
29
29
 
30
30
  ## The idv1 command
31
31
 
32
- A codebase keeps a `.id` file: a section name on its own line, then one entry a line, key first. A shape name is a label, not a taxonomy reservation. A taxonomy is written as its pattern, which carries its pair and its shape; an id's shape is read from the id. `readDirectory` still reads the JSON form; `writeDirectory` writes this one.
32
+ A codebase keeps a `.id` file of plain lines, one entry a line, read by the shape of its first word. The first five lines name the shapes in pattern order (`LLNNNN`, `NNLLLL`, `LLLLNN`, `LLNNXX`, `XXXXXX`); a line left as its pattern keeps no name, and a shape name is a label, not a taxonomy reservation. After them, two capitals are a namespace; a six-character pattern holding one lowercase pair is a taxonomy, and carries its shape; three codes with an `r` relation between them are a fact; anything else is an id and its name, with ` @ path` when it lives in a folder. Blank lines and `#` comments are free. `readDirectory` still reads the JSON form; `writeDirectory` writes this one.
33
33
 
34
34
  ```text
35
- shapes
36
- LLNNNN User
35
+ User
36
+ List
37
+ Product
38
+ Item
39
+ Event
37
40
 
38
- namespaces
39
41
  SH Shop
40
42
 
41
- taxonomies
42
43
  coNNXX Component
43
44
  NNsvLL Service
44
45
 
45
- ids
46
46
  SHop01 Shop @ .
47
47
  co12Ca Cart @ src/cart
48
48
  02svSt Stripe
49
49
 
50
- facts
51
50
  SHop01 rHasPt co12Ca
52
51
  ```
53
52
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@0001-labs/idv1",
3
- "version": "2.0.1",
3
+ "version": "2.0.3",
4
4
  "description": "Six-character IDs for agents: five shapes, taxonomy and namespaces, and a CLI that reads a codebase's .id file.",
5
5
  "license": "MIT",
6
6
  "repository": {
package/src/idv1.ts CHANGED
@@ -11,8 +11,11 @@ export type Resolved = { id: string; shape: CodeShape | null; namespace?: Namesp
11
11
  const SHAPES: CodeShape[] = ['LLNNNN', 'NNLLLL', 'LLLLNN', 'LLNNXX', 'XXXXXX']
12
12
  const list = <T>(value: unknown): T[] => (Array.isArray(value) ? value : []) as T[]
13
13
 
14
- const SECTIONS = ['shapes', 'namespaces', 'taxonomies', 'ids', 'facts'] as const
15
14
  export const patternShape = (pattern: string) => parseCode(pattern.replace(/N/g, '1').replace(/[LXOP]/g, 'a'))?.shape ?? null
15
+ const NAMESPACE_KEY = /^[A-Z]{2}$/
16
+ const TAXONOMY_KEY = /^(?=[NLXOP]*[a-z]{2}[NLXOP]*$).{6}$/
17
+ const RELATION = /^r[A-Z][A-Za-z]{4}$/
18
+ const FACT = /^[A-Za-z0-9]{6} r[A-Z][A-Za-z]{4} [A-Za-z0-9]{6}$/
16
19
 
17
20
  export function readDirectory(text: string): Directory {
18
21
  if (text.trimStart().startsWith('{')) {
@@ -20,27 +23,25 @@ export function readDirectory(text: string): Directory {
20
23
  return { shapes: value.shapes ?? {}, namespaces: list(value.namespaces), taxonomies: list(value.taxonomies), parts: list(value.parts), facts: list(value.facts) }
21
24
  }
22
25
  const directory: Directory = { shapes: {}, namespaces: [], taxonomies: [], parts: [], facts: [] }
23
- let section = ''
24
- for (const line of text.split('\n').map(line => line.trim()).filter(line => line && !line.startsWith('#'))) {
25
- if ((SECTIONS as readonly string[]).includes(line)) { section = line; continue }
26
+ text.split('\n').map(line => line.trim()).filter(line => line && !line.startsWith('#')).forEach((line, index) => {
26
27
  const [key, ...rest] = line.split(' '), name = rest.join(' ')
27
- if (section === 'shapes') directory.shapes![key as CodeShape] = name
28
- else if (section === 'namespaces') directory.namespaces.push({ shorthand: key, name })
29
- else if (section === 'taxonomies') directory.taxonomies.push({ shorthand: key.replace(/[^a-z]/g, ''), name, shape: patternShape(key)!, pattern: key })
30
- else if (section === 'ids') { const [label, path] = name.split(' @ '); directory.parts.push({ id: key, shape: parseCode(key)?.shape!, name: label, path: path ?? null }) }
31
- else if (section === 'facts') directory.facts.push(line)
32
- }
28
+ if (index < SHAPES.length) { if (line !== SHAPES[index]) directory.shapes![SHAPES[index]] = line }
29
+ else if (NAMESPACE_KEY.test(key)) directory.namespaces.push({ shorthand: key, name })
30
+ else if (TAXONOMY_KEY.test(key)) directory.taxonomies.push({ shorthand: key.replace(/[^a-z]/g, ''), name, shape: patternShape(key)!, pattern: key })
31
+ else if (FACT.test(line)) directory.facts.push(line)
32
+ else { const [label, path] = name.split(' @ '); directory.parts.push({ id: key, shape: parseCode(key)?.shape!, name: label, path: path ?? null }) }
33
+ })
33
34
  return directory
34
35
  }
35
36
 
36
37
  export function writeDirectory(directory: Directory): string {
37
38
  return [
38
- ['shapes', Object.entries(directory.shapes ?? {}).map(([shape, name]) => `${shape} ${name}`)],
39
- ['namespaces', directory.namespaces.map(({ shorthand, name }) => `${shorthand} ${name}`)],
40
- ['taxonomies', directory.taxonomies.map(({ pattern, name }) => `${pattern.replace(/\s/g, '')} ${name}`)],
41
- ['ids', directory.parts.map(({ id, name, path }) => `${id} ${name}${path ? ` @ ${path}` : ''}`)],
42
- ['facts', directory.facts],
43
- ].filter(([, lines]) => lines.length).map(([section, lines]) => [section, ...lines].join('\n')).join('\n\n') + '\n'
39
+ SHAPES.map(shape => directory.shapes?.[shape] ?? shape),
40
+ directory.namespaces.map(({ shorthand, name }) => `${shorthand} ${name}`),
41
+ directory.taxonomies.map(({ pattern, name }) => `${pattern.replace(/\s/g, '')} ${name}`),
42
+ directory.parts.map(({ id, name, path }) => `${id} ${name}${path ? ` @ ${path}` : ''}`),
43
+ directory.facts,
44
+ ].filter(lines => lines.length).map(lines => lines.join('\n')).join('\n\n') + '\n'
44
45
  }
45
46
 
46
47
  export function directoryHealth(directory: Directory, exists: (path: string) => boolean = () => true): string[] {
@@ -57,11 +58,12 @@ export function directoryHealth(directory: Directory, exists: (path: string) =>
57
58
  if (shape !== part.shape) problems.push(`${part.id} reads as ${shape ?? 'no shape'}, not ${part.shape}`)
58
59
  else if (shape === 'LLLLNN' ? blockedLLLLNNCode(part.id) : blockedCodeLetters(part.id)) problems.push(`${part.id} uses blocked letters or digits`)
59
60
  if (!part.name) problems.push(`${part.id} has no name`)
61
+ if (TAXONOMY_KEY.test(part.id) || FACT.test(`${part.id} ${part.name}`)) problems.push(`${part.id} would read back as a taxonomy or a fact`)
60
62
  if (part.path && !exists(part.path)) problems.push(`${part.id}: ${part.path} is gone`)
61
63
  }
62
64
  for (const fact of directory.facts) {
63
65
  const [subject, relation, object, extra] = fact.split(' ')
64
- if (extra !== undefined || !/^r[A-Za-z]{5}$/.test(relation ?? '')) problems.push(`Not a fact: ${fact}`)
66
+ if (extra !== undefined || !RELATION.test(relation ?? '')) problems.push(`Not a fact: ${fact}`)
65
67
  else for (const id of [subject, object]) if (!ids.has(id)) problems.push(`Unknown id ${id} in: ${fact}`)
66
68
  }
67
69
  return problems