@0001-labs/idv1 2.0.0 → 2.0.2

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 +24 -1
  2. package/package.json +1 -1
  3. package/src/idv1.ts +29 -2
package/README.md CHANGED
@@ -29,7 +29,30 @@ const parsed = parseCode(candidate)
29
29
 
30
30
  ## The idv1 command
31
31
 
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.
32
+ A codebase keeps a `.id` file. It may open with five lines naming the shapes in pattern order (`LLNNNN`, `NNLLLL`, `LLLLNN`, `LLNNXX`, `XXXXXX`); a line left as its pattern keeps no name. A shape name is a label, not a taxonomy reservation. Then each section name stands on its own line, followed by one entry a line, key first. 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.
33
+
34
+ ```text
35
+ User
36
+ List
37
+ Product
38
+ Item
39
+ Event
40
+
41
+ namespaces
42
+ SH Shop
43
+
44
+ taxonomies
45
+ coNNXX Component
46
+ NNsvLL Service
47
+
48
+ ids
49
+ SHop01 Shop @ .
50
+ co12Ca Cart @ src/cart
51
+ 02svSt Stripe
52
+
53
+ facts
54
+ SHop01 rHasPt co12Ca
55
+ ```
33
56
 
34
57
  ```sh
35
58
  npx idv1 directory
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@0001-labs/idv1",
3
- "version": "2.0.0",
3
+ "version": "2.0.2",
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,9 +11,36 @@ 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 = ['namespaces', 'taxonomies', 'ids', 'facts'] as const
15
+ export const patternShape = (pattern: string) => parseCode(pattern.replace(/N/g, '1').replace(/[LXOP]/g, 'a'))?.shape ?? null
16
+
14
17
  export function readDirectory(text: string): Directory {
15
- const value = JSON.parse(text) as Partial<Directory>
16
- return { shapes: value.shapes ?? {}, namespaces: list(value.namespaces), taxonomies: list(value.taxonomies), parts: list(value.parts), facts: list(value.facts) }
18
+ if (text.trimStart().startsWith('{')) {
19
+ const value = JSON.parse(text) as Partial<Directory>
20
+ return { shapes: value.shapes ?? {}, namespaces: list(value.namespaces), taxonomies: list(value.taxonomies), parts: list(value.parts), facts: list(value.facts) }
21
+ }
22
+ const directory: Directory = { shapes: {}, namespaces: [], taxonomies: [], parts: [], facts: [] }
23
+ let section = '', shapeLine = 0
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
+ const [key, ...rest] = line.split(' '), name = rest.join(' ')
27
+ if (!section) { const shape = SHAPES[shapeLine++]; if (shape && line !== shape) directory.shapes![shape] = line }
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
+ }
33
+ return directory
34
+ }
35
+
36
+ export function writeDirectory(directory: Directory): string {
37
+ const shapes = Object.keys(directory.shapes ?? {}).length ? SHAPES.map(shape => directory.shapes?.[shape] ?? shape).join('\n') + '\n\n' : ''
38
+ return shapes + [
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'
17
44
  }
18
45
 
19
46
  export function directoryHealth(directory: Directory, exists: (path: string) => boolean = () => true): string[] {