@yaks/inspect 0.0.0-stage → 0.2.36

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 (133) hide show
  1. package/Comp.js +469 -0
  2. package/Comp.js.map +1 -0
  3. package/Comp.ts +471 -0
  4. package/Effect.js +136 -0
  5. package/Effect.js.map +1 -0
  6. package/Effect.ts +142 -0
  7. package/Entity.js +112 -0
  8. package/Entity.js.map +1 -0
  9. package/Entity.ts +137 -0
  10. package/Facts.js +143 -0
  11. package/Facts.js.map +1 -0
  12. package/Facts.ts +150 -0
  13. package/Head.js +196 -0
  14. package/Head.js.map +1 -0
  15. package/Head.ts +195 -0
  16. package/Held.js +144 -0
  17. package/Held.js.map +1 -0
  18. package/Held.ts +172 -0
  19. package/History.js +157 -0
  20. package/History.js.map +1 -0
  21. package/History.ts +215 -0
  22. package/Home.js +482 -0
  23. package/Home.js.map +1 -0
  24. package/Home.ts +550 -0
  25. package/LICENSE +202 -0
  26. package/Links.js +197 -0
  27. package/Links.js.map +1 -0
  28. package/Links.ts +212 -0
  29. package/Package.js +195 -0
  30. package/Package.js.map +1 -0
  31. package/Package.ts +191 -0
  32. package/Past.js +163 -0
  33. package/Past.js.map +1 -0
  34. package/Past.ts +181 -0
  35. package/Prop.js +225 -0
  36. package/Prop.js.map +1 -0
  37. package/Prop.ts +208 -0
  38. package/Query.js +207 -0
  39. package/Query.js.map +1 -0
  40. package/Query.ts +229 -0
  41. package/README.md +141 -2
  42. package/Words.js +43 -0
  43. package/Words.js.map +1 -0
  44. package/Words.ts +70 -0
  45. package/_dist/Entity.d.ts +7 -0
  46. package/_dist/Entity.d.ts.map +1 -0
  47. package/_dist/Facts.d.ts +7 -0
  48. package/_dist/Facts.d.ts.map +1 -0
  49. package/_dist/Head.d.ts +22 -0
  50. package/_dist/Head.d.ts.map +1 -0
  51. package/_dist/Held.d.ts +37 -0
  52. package/_dist/Held.d.ts.map +1 -0
  53. package/_dist/Links.d.ts +9 -0
  54. package/_dist/Links.d.ts.map +1 -0
  55. package/_dist/Past.d.ts +65 -0
  56. package/_dist/Past.d.ts.map +1 -0
  57. package/_dist/Words.d.ts +27 -0
  58. package/_dist/Words.d.ts.map +1 -0
  59. package/_dist/census.d.ts +10 -0
  60. package/_dist/census.d.ts.map +1 -0
  61. package/_dist/door.d.ts +28 -0
  62. package/_dist/door.d.ts.map +1 -0
  63. package/_dist/front.d.ts +4 -0
  64. package/_dist/front.d.ts.map +1 -0
  65. package/_dist/host.d.ts +94 -0
  66. package/_dist/host.d.ts.map +1 -0
  67. package/_dist/mod.d.ts +24 -0
  68. package/_dist/mod.d.ts.map +1 -0
  69. package/_dist/notes.d.ts +38 -0
  70. package/_dist/notes.d.ts.map +1 -0
  71. package/_dist/read.d.ts +59 -0
  72. package/_dist/read.d.ts.map +1 -0
  73. package/_dist/reading.d.ts +21 -0
  74. package/_dist/reading.d.ts.map +1 -0
  75. package/_dist/registry.d.ts +12 -0
  76. package/_dist/registry.d.ts.map +1 -0
  77. package/_dist/schema.d.ts +4 -0
  78. package/_dist/schema.d.ts.map +1 -0
  79. package/_dist/state.d.ts +22 -0
  80. package/_dist/state.d.ts.map +1 -0
  81. package/_dist/value.d.ts +20 -0
  82. package/_dist/value.d.ts.map +1 -0
  83. package/_dist/views.d.ts +3 -0
  84. package/_dist/views.d.ts.map +1 -0
  85. package/census.js +50 -0
  86. package/census.js.map +1 -0
  87. package/census.ts +76 -0
  88. package/deno.json +1 -0
  89. package/door.js +54 -0
  90. package/door.js.map +1 -0
  91. package/door.ts +59 -0
  92. package/front.js +12 -0
  93. package/front.js.map +1 -0
  94. package/front.json +76 -0
  95. package/front.ts +13 -0
  96. package/grid.js +249 -0
  97. package/grid.js.map +1 -0
  98. package/grid.ts +395 -0
  99. package/host.js +20 -0
  100. package/host.js.map +1 -0
  101. package/host.ts +134 -0
  102. package/mod.js +20 -0
  103. package/mod.js.map +1 -0
  104. package/mod.ts +45 -0
  105. package/notes.js +184 -0
  106. package/notes.js.map +1 -0
  107. package/notes.ts +288 -0
  108. package/package.json +35 -4
  109. package/read.js +300 -0
  110. package/read.js.map +1 -0
  111. package/read.ts +386 -0
  112. package/reading.js +64 -0
  113. package/reading.js.map +1 -0
  114. package/reading.ts +73 -0
  115. package/registry.js +89 -0
  116. package/registry.js.map +1 -0
  117. package/registry.ts +89 -0
  118. package/rows.js +12 -0
  119. package/rows.js.map +1 -0
  120. package/rows.ts +26 -0
  121. package/schema.js +47 -0
  122. package/schema.js.map +1 -0
  123. package/schema.ts +70 -0
  124. package/state.js +85 -0
  125. package/state.js.map +1 -0
  126. package/state.ts +128 -0
  127. package/terminal-links.ts +16 -0
  128. package/value.js +67 -0
  129. package/value.js.map +1 -0
  130. package/value.ts +79 -0
  131. package/views.js +4 -0
  132. package/views.js.map +1 -0
  133. package/views.ts +6 -0
package/notes.ts ADDED
@@ -0,0 +1,288 @@
1
+ /**
2
+ * Notes: what a person says about a part, left under the heading they saw it
3
+ * under, and picked up as work. Every heading on a page offers a `note`
4
+ * press, which opens one line under it; what is sent is a comment on the
5
+ * page's entity (`comment{target}`) that is also an open task (`task{}`), so
6
+ * it shows under that heading again, and is on every list of open work until
7
+ * someone takes it: `yak task list`, or `yak task list .comment.target=<eid>`
8
+ * for what was said about one part.
9
+ *
10
+ * A comment says nothing of where on its target it was left, so the heading
11
+ * rides in the note's title, after the thing it is about:
12
+ * `task · Properties: status should be an enum`. A note whose title names no
13
+ * heading on the page shows under the page's own.
14
+ *
15
+ * `Part` is a titled section of a page with its note press and its notes.
16
+ *
17
+ * @module
18
+ */
19
+
20
+ import { groupBy } from '@yaks/fp'
21
+ /* @ts-types="npm:@types/react@19.3.0" */ import {
22
+ createElement as h,
23
+ type JSX,
24
+ type ReactNode,
25
+ useLayoutEffect,
26
+ useRef,
27
+ } from 'react'
28
+ import { Button, Composer, Notes, Section } from '@yaks/ui'
29
+ import { editKeys } from '@yaks/ux/edit-keys'
30
+ import type { Bundle, Io } from './host.ts'
31
+ import { comp, count, line, named, str } from './read.js'
32
+ import { rows } from './rows.js'
33
+ import { edited, key, me, put, refusal, write } from './state.js'
34
+
35
+ /** How many notes a page reads. */
36
+ export let NOTES = 100
37
+
38
+ /** The notes left on the entity `eid`, as a query: the comments on it that
39
+ * are tasks. A plain comment is not a note; the page draws it among its
40
+ * links, or a package's page among its comments. */
41
+ export let notesOf = (eid: string): string =>
42
+ `.comment&.task&.comment.target=${eid}&.limit=${NOTES}&*`
43
+
44
+ /**
45
+ * A note's title: the thing it is about, the heading it was left under, and
46
+ * the first line of what it says.
47
+ *
48
+ * ```ts
49
+ * import { titled } from './notes.ts'
50
+ * titled('task', 'Properties', 'status should be an enum\nit is text')
51
+ * // 'task · Properties: status should be an enum'
52
+ * titled('T-9', '', 'wrong owner') // 'T-9: wrong owner'
53
+ * ```
54
+ */
55
+ export let titled = (subject: string, heading: string, text: string): string =>
56
+ `${heading ? `${subject} · ${heading}` : subject}: ${line(text, 80)}`
57
+
58
+ /**
59
+ * A note on the entity `eid`, left under `heading`, as the batch that
60
+ * leaves it: a comment on it that is an open task.
61
+ *
62
+ * ```ts
63
+ * import { note } from './notes.ts'
64
+ * note('c1', 'task', 'Properties', 'status should be an enum')
65
+ * // [{
66
+ * // entity: { eid: '$note' },
67
+ * // doc: { title: 'task · Properties: status should be an enum', body: 'status should be an enum' },
68
+ * // comment: { target: 'c1' },
69
+ * // task: {},
70
+ * // }]
71
+ * ```
72
+ */
73
+ export let note = (
74
+ eid: string,
75
+ subject: string,
76
+ heading: string,
77
+ text: string,
78
+ ): Bundle[] => [{
79
+ entity: { eid: '$note' },
80
+ doc: { title: titled(subject, heading, text), body: text },
81
+ comment: { target: eid },
82
+ task: {},
83
+ }]
84
+
85
+ /**
86
+ * The notes under each heading of a page: by the heading a note's title
87
+ * names, or under the page's own (`''`) when it names none of them.
88
+ *
89
+ * ```ts
90
+ * import { under } from './notes.ts'
91
+ * let n = (title: string) => ({ entity: { eid: title }, doc: { title } })
92
+ * let by = under([n('task · Properties: a'), n('task: b'), n('task · Gone: c')], 'task', ['Properties'])
93
+ * by.get('Properties')!.length // 1
94
+ * by.get('')!.length // 2
95
+ * ```
96
+ */
97
+ export let under = (
98
+ notes: Bundle[],
99
+ subject: string,
100
+ headings: string[],
101
+ ): Map<string, Bundle[]> =>
102
+ groupBy(
103
+ notes,
104
+ (b) =>
105
+ headings.find((h) =>
106
+ h && str(b, 'doc', 'title').startsWith(`${subject} · ${h}: `)
107
+ ) ?? '',
108
+ )
109
+
110
+ /** What a page calls the thing its notes are about: a component, a property
111
+ * or a package by its name, anything else by its id. */
112
+ export let about = (io: Io, e: Bundle): string =>
113
+ e._comp || e._prop || e._namespace
114
+ ? str(e, 'doc', 'title') || io.id(e)
115
+ : io.id(e)
116
+
117
+ /** The entities `eids` names, asked for so each is called by its name: a
118
+ * hook, answered while the component asking is mounted. */
119
+ export let useNamed = (io: Io, eids: (string | undefined)[]): void => {
120
+ let ids = [...new Set(eids.filter((e): e is string => !!e))].slice(0, 60)
121
+ io.ask(ids.length ? { named: `.entity.eid=${ids.join(',')}` } : {})
122
+ }
123
+
124
+ /** The component sets `rows` are made of, asked for so each row's kind (and
125
+ * its id) is known when it came carrying one component: a hook. */
126
+ export let useSets = (io: Io, rows: Bundle[]): void => {
127
+ let sets = [
128
+ ...new Set(rows.map((b) => comp(b, 'entity').archetype).filter(named)),
129
+ ].slice(0, 60)
130
+ io.ask(
131
+ sets.length
132
+ ? {
133
+ sets: `.archetype&.entity.eid=${
134
+ sets.join(',')
135
+ }&.fields=archetype.tables`,
136
+ }
137
+ : {},
138
+ )
139
+ }
140
+
141
+ /** The notes left on `eid`, and their authors named: a hook. A graph that
142
+ * has no tasks holds no notes. */
143
+ export let useNotes = (io: Io, eid: string): Bundle[] => {
144
+ let got = io.ask(io.vocab.comp('task') ? { notes: notesOf(eid) } : {})
145
+ let notes = rows(got.notes)
146
+ useNamed(io, notes.map((b) => str(b, 'created', 'by')))
147
+ return notes
148
+ }
149
+
150
+ // Whether a note's task is seen to: it wears its `completed` or `cancelled`
151
+ // mark.
152
+ let done = (b: Bundle) => !!b.completed || !!b.cancelled
153
+
154
+ export let Note = ({ e: b, io }: import('./host.ts').Props) => {
155
+ let c = comp(b, 'created')
156
+ return h(
157
+ Notes.Item,
158
+ { key: b.entity.eid, mod: done(b) && 'done' },
159
+ h(Notes.Text, {}, str(b, 'doc', 'body') || str(b, 'doc', 'title')),
160
+ h(
161
+ Notes.Who,
162
+ {},
163
+ typeof c.by == 'string'
164
+ ? (io.get(c.by)
165
+ ? io.show(io.get(c.by)!, 'Inspect.Reference.Inline')
166
+ : io.name(c.by))
167
+ : '',
168
+ ),
169
+ h(
170
+ Notes.When,
171
+ {},
172
+ h(
173
+ 'a',
174
+ { href: io.link(b.entity.eid) },
175
+ c.at ? io.when(String(c.at)) : io.id(b),
176
+ ),
177
+ ),
178
+ )
179
+ }
180
+
181
+ /** What a heading's note line is drawn with. */
182
+ type Said = {
183
+ io: Io
184
+ eid: string
185
+ subject: string
186
+ heading: string
187
+ notes?: Bundle[]
188
+ }
189
+
190
+ // The line a note is typed on: what is typed stays in the element until it
191
+ // is sent, and Escape closes it.
192
+ let Line = ({ io, eid, subject, heading }: Said) => {
193
+ let field = useRef<HTMLInputElement>(null)
194
+ useLayoutEffect(() => field.current?.focus?.(), [])
195
+ let close = () => io.set(put({ note: null }))
196
+ let send = (ev: Event & { currentTarget: HTMLFormElement }) => {
197
+ ev.preventDefault()
198
+ let text = String(field.current?.value ?? '').trim()
199
+ if (!text) return
200
+ close()
201
+ write(io, key(eid, heading), note(eid, subject, heading, text))
202
+ }
203
+ return h(
204
+ Composer,
205
+ { as: 'form', onSubmit: send },
206
+ h(Composer.Field, {
207
+ elRef: field,
208
+ name: 'note',
209
+ 'aria-label': `note on ${heading || subject}`,
210
+ placeholder: 'a note: what is wrong here, or could be better…',
211
+ onKeyDown: editKeys({ cancel: close }),
212
+ }),
213
+ h(Composer.Send, { type: 'submit' }, 'note'),
214
+ )
215
+ }
216
+
217
+ /** The notes under a heading, and its note line while it is open. */
218
+ export let Said = (p: Said): JSX.Element | null => {
219
+ let open = me(p.io).note == key(p.eid, p.heading)
220
+ let notes = p.notes ?? []
221
+ return notes.length || open
222
+ ? h(
223
+ Notes,
224
+ {},
225
+ notes.map((b) => p.io.show(b, 'Inspect.Note')),
226
+ open ? h(Line, p) : null,
227
+ )
228
+ : null
229
+ }
230
+
231
+ /** The press that opens a heading's note line, and closes it: the head's on
232
+ * any page where the controls take input, a part's while its page is
233
+ * edited. */
234
+ export let NoteButton = (
235
+ { io, eid, heading, subject }: Omit<Said, 'notes'>,
236
+ ): JSX.Element | null => {
237
+ let at = key(eid, heading)
238
+ let open = me(io).note == at
239
+ return io.edits && (!heading || open || edited(io, eid))
240
+ ? h(Button, {
241
+ type: 'button',
242
+ mod: 'quiet',
243
+ 'aria-label': `note on ${heading || subject}`,
244
+ 'aria-pressed': open,
245
+ onClick: () => io.set(put({ note: open ? null : at })),
246
+ }, 'note')
247
+ : null
248
+ }
249
+
250
+ /** What a part of a page is drawn with. */
251
+ export type PartProps = Omit<Said, 'notes'> & {
252
+ /** its notes, by heading (`under`) */
253
+ notes?: Map<string, Bundle[]>
254
+ /** what its title says, when it is more than its heading */
255
+ title?: ReactNode
256
+ /** how many things it holds */
257
+ count?: number
258
+ /** said small beside its title */
259
+ note?: ReactNode
260
+ /** what else its title line offers */
261
+ act?: ReactNode
262
+ /** what it is about, under its title */
263
+ sub?: ReactNode
264
+ children?: ReactNode
265
+ }
266
+
267
+ /** A titled part of a page: its heading and what it holds, with its note
268
+ * press and the notes left under it. */
269
+ export let Part = (p: PartProps): JSX.Element => {
270
+ let said = refusal(p.io, key(p.eid, p.heading))
271
+ return h(
272
+ Section,
273
+ { 'data-section': p.heading },
274
+ h(
275
+ Section.Title,
276
+ {},
277
+ p.title ?? p.heading,
278
+ p.count != null ? h(Section.Count, {}, count(p.count)) : null,
279
+ p.note ? h(Section.Note, {}, p.note) : null,
280
+ said ? h(Section.Note, { mod: 'refused' }, said) : null,
281
+ p.act,
282
+ h(NoteButton, p),
283
+ ),
284
+ p.sub ? h(Section.Sub, {}, p.sub) : null,
285
+ h(Said, { ...p, notes: p.notes?.get(p.heading) }),
286
+ p.children,
287
+ )
288
+ }
package/package.json CHANGED
@@ -1,6 +1,37 @@
1
1
  {
2
2
  "name": "@yaks/inspect",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.2.36",
4
+ "homepage": "https://jsr.io/@yaks/inspect",
5
+ "type": "module",
6
+ "exports": {
7
+ ".": {
8
+ "types": "./_dist/mod.d.ts",
9
+ "default": "./mod.js"
10
+ },
11
+ "./views": {
12
+ "types": "./_dist/views.d.ts",
13
+ "default": "./views.js"
14
+ },
15
+ "./front": {
16
+ "types": "./_dist/front.d.ts",
17
+ "default": "./front.js"
18
+ }
19
+ },
20
+ "license": "Apache-2.0",
21
+ "dependencies": {
22
+ "@yaks/edge": "0.2.36",
23
+ "@yaks/fp": "0.2.36",
24
+ "@yaks/graph": "0.2.36",
25
+ "@yaks/markdown": "0.2.36",
26
+ "@yaks/query": "0.2.36",
27
+ "@yaks/render": "0.2.36",
28
+ "@yaks/ui": "0.2.36",
29
+ "@yaks/ux": "0.2.36",
30
+ "@yaks/vocab": "0.2.36",
31
+ "@preact/signals-core": "1.14.5",
32
+ "@types/react": "19.3.0",
33
+ "react": "19.3.0"
34
+ },
35
+ "description": "Inspect views: graph entities, vocabulary components, packages, properties, query rows and the map, drawn through the host’s shared renderer registry.",
36
+ "repository": "https://github.com/yak-sh/yak.git"
37
+ }
package/read.js ADDED
@@ -0,0 +1,300 @@
1
+ /**
2
+ * What the inspector reads off a bundle or an answer: pure functions, the
3
+ * vocabulary of every view. How a value is shaped, which hue a name wears, the
4
+ * components an entity carries, the tables an archetype names, how many
5
+ * entities carry each component, and what a client may write.
6
+ *
7
+ * @module
8
+ */ import { nameIn } from "@yaks/vocab";
9
+ let EID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/;
10
+ let HEX = /^[0-9a-f]{32,40}$/;
11
+ let AT = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}/;
12
+ /**
13
+ * The hue a component's name wears (@yaks/ui `Chip-0` … `Chip-5`). Provenance
14
+ * rides nearly every entity, so it keeps the warm end; every other name
15
+ * hashes into the other four, the same on every page.
16
+ *
17
+ * ```ts
18
+ * import { tone } from './read.ts'
19
+ * tone('created') // '4'
20
+ * tone('task') == tone('task') // true
21
+ * ```
22
+ */ export let tone = (name)=>{
23
+ if (name == 'created') return '4';
24
+ if (name == 'updated') return '5';
25
+ let hash = 2166136261;
26
+ for (let ch of name)hash = Math.imul(hash ^ ch.charCodeAt(0), 16777619);
27
+ return String((hash >>> 0) % 4);
28
+ };
29
+ /**
30
+ * A stored value's shape (@yaks/ui `Value`): nothing, a number, a flag, an
31
+ * id, a moment, structure, or text.
32
+ *
33
+ * ```ts
34
+ * import { shape } from './read.ts'
35
+ * shape(null) // 'nil'
36
+ * shape('') // 'nil'
37
+ * shape(3) // 'num'
38
+ * shape('2026-09-29T19:12:21Z') // 'time'
39
+ * shape({ a: 1 }) // 'json'
40
+ * ```
41
+ */ export let shape = (v)=>v == null || v === '' ? 'nil' : typeof v == 'number' ? 'num' : typeof v == 'boolean' ? 'bool' : typeof v == 'object' ? 'json' : EID.test(String(v)) || HEX.test(String(v)) ? 'id' : AT.test(String(v)) ? 'time' : 'text';
42
+ /**
43
+ * A stored value, as text: absence says which absence it is.
44
+ *
45
+ * ```ts
46
+ * import { face } from './read.ts'
47
+ * face(null) // 'null'
48
+ * face('') // '""'
49
+ * face({ a: 1 }) // '{"a":1}'
50
+ * ```
51
+ */ export let face = (v)=>v === '' ? '""' : v == null ? 'null' : typeof v == 'object' ? JSON.stringify(v) : String(v);
52
+ /**
53
+ * A stored value as its leaves, each with the keys that lead to it: an object
54
+ * opens into its keys, a list of structures into its places, and a scalar or
55
+ * a list of scalars is a leaf. So a structure reads as rows, never as one
56
+ * line of JSON or `[object Object]`.
57
+ *
58
+ * ```ts
59
+ * import { leaves } from './read.ts'
60
+ * leaves(3) // [[[], 3]]
61
+ * leaves({ a: { b: 1 }, c: [1, 2] }) // [[['a', 'b'], 1], [['c'], [1, 2]]]
62
+ * leaves([{ a: 1 }]) // [[['0', 'a'], 1]]
63
+ * leaves({}) // [[[], {}]]
64
+ * ```
65
+ */ export let leaves = (v, at = [])=>{
66
+ let open = !!v && typeof v == 'object' && !(Array.isArray(v) && v.every((x)=>x == null || typeof x != 'object'));
67
+ let kids = open ? Object.entries(v) : [];
68
+ return kids.length ? kids.flatMap(([k, x])=>leaves(x, [
69
+ ...at,
70
+ k
71
+ ])) : [
72
+ [
73
+ at,
74
+ v
75
+ ]
76
+ ];
77
+ };
78
+ /** Whether a client may write this component, or this property of it,
79
+ * here: never where the host's controls take no input (a terminal), nor a
80
+ * component that is not on the wire or is computed, nor a property that is
81
+ * stamped or computed. */ export let writable = (io, name, prop)=>{
82
+ let c = io.vocab.comp(name);
83
+ if (!io.edits || !c?.wire || c.computed) return false;
84
+ let p = prop == null ? undefined : io.vocab.prop(name, prop);
85
+ return prop == null || !!p && !p.stamped && !p.computed;
86
+ };
87
+ /** Whether a value names an entity by its eid. */ export let named = (v)=>typeof v == 'string' && (EID.test(v) || HEX.test(v));
88
+ /**
89
+ * The components a bundle carries, by name, in the order it carries them:
90
+ * everything but its `entity` spine and its `$` requests.
91
+ *
92
+ * ```ts
93
+ * import { comps } from './read.ts'
94
+ * comps({ entity: { eid: 'a' }, doc: { title: 'x' }, $num: true })
95
+ * // [['doc', { title: 'x' }]]
96
+ * ```
97
+ */ export let comps = (b)=>Object.entries(b).filter(([k, v])=>k != 'entity' && !k.startsWith('$') && !!v && typeof v == 'object');
98
+ /** One component of a bundle, or an empty one. */ export let comp = (b, name)=>b?.[name] ?? {};
99
+ /** A string property of a component, or ''. */ export let str = (b, name, prop)=>{
100
+ let v = comp(b, name)[prop];
101
+ return typeof v == 'string' ? v : '';
102
+ };
103
+ /**
104
+ * The components an archetype holds (@yaks/archetype), as `vocab` names them,
105
+ * from the descriptor it stores: their identities, or their labels for words
106
+ * that have none.
107
+ *
108
+ * ```ts
109
+ * import { tables } from './read.ts'
110
+ * tables({ entity: { eid: 'a' }, archetype: { tables: '["doc","task"]' } })
111
+ * // ['doc', 'task']
112
+ * ```
113
+ */ export let tables = (b, vocab)=>{
114
+ try {
115
+ let t = JSON.parse(str(b, 'archetype', 'tables') || '[]');
116
+ if (!Array.isArray(t)) return [];
117
+ return t.map((id)=>vocab ? nameIn(vocab, String(id)) : String(id));
118
+ } catch {
119
+ return [];
120
+ }
121
+ };
122
+ /**
123
+ * How many entities carry each component: each archetype's count of entities
124
+ * (a `.tally=entity.archetype`), added to every table it names.
125
+ *
126
+ * ```ts
127
+ * import { census } from './read.ts'
128
+ * let sets = [
129
+ * { entity: { eid: 'x' }, archetype: { tables: '["doc","task"]' } },
130
+ * { entity: { eid: 'y' }, archetype: { tables: '["doc"]' } },
131
+ * ]
132
+ * census(sets, { x: 3, y: 2 }) // { doc: 5, task: 3 }
133
+ * ```
134
+ */ export let census = (sets, tally, vocab)=>{
135
+ let out = {};
136
+ for (let s of sets){
137
+ let n = tally[s.entity.eid] ?? 0;
138
+ for (let t of tables(s, vocab))out[t] = (out[t] ?? 0) + n;
139
+ }
140
+ return out;
141
+ };
142
+ /** The first line of a text, cut to `n` characters. */ export let line = (text, n = 120)=>{
143
+ let first = String(text ?? '').split('\n').map((l)=>l.trim()).find(Boolean) ?? '';
144
+ return first.length > n ? first.slice(0, n - 1) + '…' : first;
145
+ };
146
+ /**
147
+ * The text an entity holds for reading: a doc's body, else what a message
148
+ * says (`content.body`, a transcript entry's).
149
+ *
150
+ * ```ts
151
+ * import { bodyOf } from './read.ts'
152
+ * bodyOf({ entity: { eid: 'e' }, content: { body: 'hi' } }) // 'hi'
153
+ * ```
154
+ */ export let bodyOf = (b)=>str(b, 'doc', 'body') || str(b, 'content', 'body');
155
+ /**
156
+ * Some words, cut where they run past `n` characters.
157
+ *
158
+ * ```ts
159
+ * import { cut } from './read.ts'
160
+ * cut('abcdef', 4) // 'abc…'
161
+ * cut('abc', 4) // 'abc'
162
+ * ```
163
+ */ export let cut = (text, n)=>text.length > n ? text.slice(0, n - 1).trimEnd() + '…' : text;
164
+ /**
165
+ * What an entity is called, in words: its title; else the `label` its kind
166
+ * gives it (a model's "GPT-6.2 Sol"), or the `name` (a topic's, a
167
+ * component's); else its id where it is numbered
168
+ * (`S-12`); else the first line of what it says (a transcript entry); else
169
+ * its kind and its handle (`process #1320a8ee61`). `id` is the id a person
170
+ * reads, and `kind` the kind it shows as.
171
+ *
172
+ * ```ts
173
+ * import { called } from './read.ts'
174
+ * let e = (b: object) => ({ entity: { eid: 'e1' }, ...b })
175
+ * called(e({ doc: { title: 'Fix it' } }), 'T-9', 'task') // 'Fix it'
176
+ * called(e({ topic: { name: 'naming' } }), '#e1', 'topic') // 'naming'
177
+ * called(e({ model: { name: 'gpt-6.2-sol', label: 'GPT-6.2 Sol' } }), 'O-50', 'model') // 'GPT-6.2 Sol'
178
+ * called(e({ content: { body: '\nuse grams' } }), '#e1', 'entry') // 'use grams'
179
+ * called(e({ process: { pid: 1 } }), '#e1', 'process') // 'process #e1'
180
+ * called(e({ file: { path: 'a/b/c.jsonl' } }), '#e1', 'file') // 'c.jsonl'
181
+ * ```
182
+ */ export let called = (b, id, kind)=>str(b, 'doc', 'title') || str(b, kind, 'label') || str(b, kind, 'name') || str(b, kind, 'path').split('/').at(-1) || (b.entity.num != null ? id : '') || line(bodyOf(b), 80) || (kind && kind != 'entity' ? `${kind} ${id}` : id);
183
+ /** Where an entity came from, said once in a quiet line rather than stated
184
+ * as a fact: who made and changed it and when, what built it, where it was
185
+ * imported from, when it was checked. */ export let PROVENANCE = [
186
+ 'created',
187
+ 'updated',
188
+ 'built',
189
+ 'lines',
190
+ 'verified'
191
+ ];
192
+ /** What a page says in its head and its body rather than as facts. */ let SAID = new Set([
193
+ 'entity',
194
+ 'doc',
195
+ 'content',
196
+ ...PROVENANCE
197
+ ]);
198
+ /**
199
+ * The components a page states as facts, in the order they mean: the kinds
200
+ * it is, most specific first (the vocabulary's kind order); then what else it
201
+ * carries, by name; then the marks of what happened to it. What the head and
202
+ * the body say (its title, its text, where it came from) is left out, and so
203
+ * is anything in `skip`.
204
+ *
205
+ * ```ts
206
+ * import { loadVocab } from '@yaks/vocab'
207
+ * import { facts } from './read.ts'
208
+ * let c = (o = {}) => ({ component: true, properties: {}, ...o })
209
+ * let at = { type: 'string', stamped: true }
210
+ * let vocab = loadVocab([{ $defs: {
211
+ * doc: c(), task: c({ kind: true }), filed: c(), claim: c(),
212
+ * completed: c({ properties: { at, by: at } }),
213
+ * created: c({ properties: { at, by: at } }),
214
+ * } }])
215
+ * facts(vocab, {
216
+ * entity: { eid: 'e' }, completed: {}, created: {}, filed: {}, doc: {},
217
+ * task: {}, claim: {},
218
+ * }) // ['task', 'claim', 'filed', 'completed']
219
+ * ```
220
+ */ export let facts = (vocab, b, skip = [])=>{
221
+ let has = comps(b).map(([n])=>n).filter((n)=>!SAID.has(n) && !skip.includes(n));
222
+ let kinds = vocab.kinds.filter((k)=>has.includes(k));
223
+ let rest = has.filter((n)=>!kinds.includes(n)).toSorted();
224
+ let marks = rest.filter((n)=>vocab.comp(n)?.mark);
225
+ return [
226
+ ...kinds,
227
+ ...rest.filter((n)=>!marks.includes(n)),
228
+ ...marks
229
+ ];
230
+ };
231
+ /**
232
+ * A long hash, as far as a reader needs it: its first ten characters.
233
+ *
234
+ * ```ts
235
+ * import { brief } from './read.ts'
236
+ * brief('f07941748e70ecf0a7272d9a05dba89f0b98cb1cde9709b12a6e1e711ef3621b')
237
+ * // 'f07941748e…'
238
+ * brief('open') // 'open'
239
+ * ```
240
+ */ export let brief = (v)=>/^[0-9a-f]{41,}$/.test(v) ? v.slice(0, 10) + '…' : v;
241
+ /**
242
+ * The relation an edge's entity states: the component beside `edge` that the
243
+ * vocabulary marks `edge` (@yaks/edge). `relations` is those components'
244
+ * names.
245
+ *
246
+ * ```ts
247
+ * import { relation } from './read.ts'
248
+ * relation(
249
+ * { entity: { eid: 'e' }, edge: { from: 'a', to: 'b' }, requires: {} },
250
+ * ['requires', 'contains'],
251
+ * ) // 'requires'
252
+ * ```
253
+ */ export let relation = (b, relations)=>comps(b).map(([name])=>name).find((n)=>relations.includes(n));
254
+ // Counts need only decimal grouping. Keep Intl's rounding and non-finite
255
+ // behavior for other numbers without paying its constructor during module load.
256
+ let grouped;
257
+ /** A number, grouped for reading: 12345 reads 12,345. */ export let count = (n)=>Number.isSafeInteger(n) && !Object.is(n, -0) ? String(n).replace(/\B(?=(\d{3})+(?!\d))/g, ',') : (grouped ??= new Intl.NumberFormat('en-US')).format(n);
258
+ /**
259
+ * A property's type as it reads, off its `_prop` row.
260
+ *
261
+ * ```ts
262
+ * import { typed } from './read.ts'
263
+ * typed({ type: 'string', enum: ['open', 'done'] }) // 'string enum'
264
+ * typed({ type: 'string', ref: 'session' }) // 'string → session'
265
+ * ```
266
+ */ export let typed = (prop)=>[
267
+ [
268
+ prop.type
269
+ ].flat().filter(Boolean).join('|'),
270
+ prop.format,
271
+ prop.enum ? 'enum' : null,
272
+ prop.ref ? `→ ${prop.ref}` : null
273
+ ].filter(Boolean).join(' ');
274
+ /**
275
+ * What is special about a property, off its `_prop` row: who owns it,
276
+ * whether it is stored, how it is found.
277
+ *
278
+ * ```ts
279
+ * import { flags } from './read.ts'
280
+ * flags({ stamped: true, search: true, unique: null }) // ['stamped', 'search']
281
+ * ```
282
+ */ export let flags = (prop)=>[
283
+ 'stamped',
284
+ 'computed',
285
+ 'required',
286
+ 'search',
287
+ 'unique',
288
+ 'identity'
289
+ ].filter((k)=>prop[k] === true);
290
+ /**
291
+ * Some values' distinct members, in the order first met, blanks left out.
292
+ *
293
+ * ```ts
294
+ * import { unique } from './read.ts'
295
+ * unique(['a', '', 'b', 'a', undefined]) // ['a', 'b']
296
+ * ```
297
+ */ export let unique = (xs)=>[
298
+ ...new Set(xs.filter((x)=>!!x))
299
+ ];
300
+ //# sourceMappingURL=read.js.map
package/read.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"read.js","sources":["./read.ts"],"names":[],"mappings":"AAAA;;;;;;;CAOC,GAED,SAAS,MAAM,2BAA4C;AAM3D,IAAI,MAAM;AACV,IAAI,MAAM;AACV,IAAI,KAAK;AAET;;;;;;;;;;CAUC,GACD,OAAO,IAAI,OAAO,CAAC;EACjB,IAAI,QAAQ,WAAW,OAAO;EAC9B,IAAI,QAAQ,WAAW,OAAO;EAC9B,IAAI,OAAO;EACX,KAAK,IAAI,MAAM,KAAM,OAAO,KAAK,IAAI,CAAC,OAAO,GAAG,UAAU,CAAC,IAAI;EAC/D,OAAO,OAAO,CAAC,SAAS,CAAC,IAAI;AAC/B,EAAC;AAED;;;;;;;;;;;;CAYC,GACD,OAAO,IAAI,QAAQ,CAAC,IAClB,KAAK,QAAQ,MAAM,KACf,QACA,OAAO,KAAK,WACZ,QACA,OAAO,KAAK,YACZ,SACA,OAAO,KAAK,WACZ,SACA,IAAI,IAAI,CAAC,OAAO,OAAO,IAAI,IAAI,CAAC,OAAO,MACvC,OACA,GAAG,IAAI,CAAC,OAAO,MACf,SACA,OAAM;AAEZ;;;;;;;;;CASC,GACD,OAAO,IAAI,OAAO,CAAC,IACjB,MAAM,KACF,OACA,KAAK,OACL,SACA,OAAO,KAAK,WACZ,KAAK,SAAS,CAAC,KACf,OAAO,GAAE;AAEf;;;;;;;;;;;;;CAaC,GACD,OAAO,IAAI,SAAS,CAAC,GAAY,KAAe,EAAE;EAChD,IAAI,OAAO,CAAC,CAAC,KAAK,OAAO,KAAK,YAC5B,CAAC,CAAC,MAAM,OAAO,CAAC,MAAM,EAAE,KAAK,CAAC,CAAC,IAAM,KAAK,QAAQ,OAAO,KAAK,SAAS;EACzE,IAAI,OAAO,OAAO,OAAO,OAAO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