@milaboratories/pl-model-middle-layer 1.32.2 → 1.34.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/dist/folders/commit.cjs +79 -0
- package/dist/folders/commit.cjs.map +1 -0
- package/dist/folders/commit.d.ts +82 -0
- package/dist/folders/commit.d.ts.map +1 -0
- package/dist/folders/commit.js +78 -0
- package/dist/folders/commit.js.map +1 -0
- package/dist/folders/decode.cjs +79 -0
- package/dist/folders/decode.cjs.map +1 -0
- package/dist/folders/decode.d.ts +57 -0
- package/dist/folders/decode.d.ts.map +1 -0
- package/dist/folders/decode.js +77 -0
- package/dist/folders/decode.js.map +1 -0
- package/dist/folders/document.cjs +49 -0
- package/dist/folders/document.cjs.map +1 -0
- package/dist/folders/document.d.ts +56 -0
- package/dist/folders/document.d.ts.map +1 -0
- package/dist/folders/document.js +45 -0
- package/dist/folders/document.js.map +1 -0
- package/dist/folders/index.cjs +37 -0
- package/dist/folders/index.d.ts +10 -0
- package/dist/folders/index.js +10 -0
- package/dist/folders/inheritance.cjs +23 -0
- package/dist/folders/inheritance.cjs.map +1 -0
- package/dist/folders/inheritance.d.ts +19 -0
- package/dist/folders/inheritance.d.ts.map +1 -0
- package/dist/folders/inheritance.js +23 -0
- package/dist/folders/inheritance.js.map +1 -0
- package/dist/folders/invariants.cjs +115 -0
- package/dist/folders/invariants.cjs.map +1 -0
- package/dist/folders/invariants.d.ts +50 -0
- package/dist/folders/invariants.d.ts.map +1 -0
- package/dist/folders/invariants.js +114 -0
- package/dist/folders/invariants.js.map +1 -0
- package/dist/folders/naming.cjs +54 -0
- package/dist/folders/naming.cjs.map +1 -0
- package/dist/folders/naming.d.ts +32 -0
- package/dist/folders/naming.d.ts.map +1 -0
- package/dist/folders/naming.js +52 -0
- package/dist/folders/naming.js.map +1 -0
- package/dist/folders/planner.cjs +192 -0
- package/dist/folders/planner.cjs.map +1 -0
- package/dist/folders/planner.d.ts +100 -0
- package/dist/folders/planner.d.ts.map +1 -0
- package/dist/folders/planner.js +190 -0
- package/dist/folders/planner.js.map +1 -0
- package/dist/folders/removal.cjs +47 -0
- package/dist/folders/removal.cjs.map +1 -0
- package/dist/folders/removal.d.ts +42 -0
- package/dist/folders/removal.d.ts.map +1 -0
- package/dist/folders/removal.js +45 -0
- package/dist/folders/removal.js.map +1 -0
- package/dist/folders/view.cjs +212 -0
- package/dist/folders/view.cjs.map +1 -0
- package/dist/folders/view.d.ts +98 -0
- package/dist/folders/view.d.ts.map +1 -0
- package/dist/folders/view.js +207 -0
- package/dist/folders/view.js.map +1 -0
- package/dist/index.cjs +43 -2
- package/dist/index.d.ts +12 -2
- package/dist/index.js +12 -1
- package/dist/project.cjs +23 -0
- package/dist/project.cjs.map +1 -0
- package/dist/project.d.ts +13 -0
- package/dist/project.d.ts.map +1 -1
- package/dist/project.js +22 -0
- package/dist/project.js.map +1 -0
- package/package.json +6 -3
- package/src/folders/commit.ts +133 -0
- package/src/folders/decode.test.ts +128 -0
- package/src/folders/decode.ts +142 -0
- package/src/folders/document.ts +96 -0
- package/src/folders/index.ts +9 -0
- package/src/folders/inheritance.test.ts +91 -0
- package/src/folders/inheritance.ts +33 -0
- package/src/folders/invariants.test.ts +166 -0
- package/src/folders/invariants.ts +159 -0
- package/src/folders/naming.test.ts +62 -0
- package/src/folders/naming.ts +59 -0
- package/src/folders/planner.test.ts +450 -0
- package/src/folders/planner.ts +279 -0
- package/src/folders/removal.test.ts +142 -0
- package/src/folders/removal.ts +77 -0
- package/src/folders/test_utils.ts +86 -0
- package/src/folders/view.test.ts +433 -0
- package/src/folders/view.ts +334 -0
- package/src/index.ts +1 -0
- package/src/project.ts +21 -0
|
@@ -0,0 +1,334 @@
|
|
|
1
|
+
import type { ProjectId, TemplateId } from "@milaboratories/pl-model-common";
|
|
2
|
+
import { normalizeDescription } from "../project";
|
|
3
|
+
import type { FoldersDecoded, FoldersDocumentProblem } from "./decode";
|
|
4
|
+
import type { FoldersDocument, FolderId } from "./document";
|
|
5
|
+
import { emptyFoldersDocument } from "./document";
|
|
6
|
+
import type { FoldersItem } from "./planner";
|
|
7
|
+
|
|
8
|
+
/** An item as the folder machinery needs to see it: an identity and the label shown to a user. */
|
|
9
|
+
export interface FoldersItemInput<Id extends string> {
|
|
10
|
+
readonly id: Id;
|
|
11
|
+
readonly name: string;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/** Where an item sits in the tree. */
|
|
15
|
+
export interface FoldersPlacement {
|
|
16
|
+
/** Absent for an item at the top level. */
|
|
17
|
+
readonly folder?: FolderId;
|
|
18
|
+
/** Containing folders, outermost first, including the folder holding the item. */
|
|
19
|
+
readonly ancestors: readonly FolderId[];
|
|
20
|
+
/**
|
|
21
|
+
* Names of those folders, outermost first. Empty for an item at the top level. This is what
|
|
22
|
+
* tells two identically named items apart in a search across the whole tree.
|
|
23
|
+
*/
|
|
24
|
+
readonly path: readonly string[];
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** An item with the folder holding it resolved. */
|
|
28
|
+
export interface FoldersItemView<Id extends string>
|
|
29
|
+
extends FoldersItemInput<Id>, FoldersPlacement {}
|
|
30
|
+
|
|
31
|
+
export type FoldersProjectInput = FoldersItemInput<ProjectId>;
|
|
32
|
+
export type FoldersTemplateInput = FoldersItemInput<TemplateId>;
|
|
33
|
+
export type FoldersProjectView = FoldersItemView<ProjectId>;
|
|
34
|
+
export type FoldersTemplateView = FoldersItemView<TemplateId>;
|
|
35
|
+
|
|
36
|
+
/** A folder with its place in the tree resolved. */
|
|
37
|
+
export interface FolderView {
|
|
38
|
+
readonly id: FolderId;
|
|
39
|
+
readonly name: string;
|
|
40
|
+
/** Absent for a top-level folder. */
|
|
41
|
+
readonly parent?: FolderId;
|
|
42
|
+
/** Containing folders, outermost first, excluding this folder. */
|
|
43
|
+
readonly ancestors: readonly FolderId[];
|
|
44
|
+
/** Names of the ancestors followed by this folder's own name. */
|
|
45
|
+
readonly path: readonly string[];
|
|
46
|
+
/** Free text the user wrote about the folder. Absent when there is none. */
|
|
47
|
+
readonly description?: string;
|
|
48
|
+
/** True when the stored parent was unusable and this folder was lifted to the top level. */
|
|
49
|
+
readonly lifted: boolean;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** The whole tree as the desktop consumes it. */
|
|
53
|
+
export interface FoldersView {
|
|
54
|
+
readonly folders: readonly FolderView[];
|
|
55
|
+
/** Every project handed in, in the order it was handed in. Nothing here is ever dropped. */
|
|
56
|
+
readonly projects: readonly FoldersProjectView[];
|
|
57
|
+
/** Every template handed in, in the order it was handed in. Nothing here is ever dropped. */
|
|
58
|
+
readonly templates: readonly FoldersTemplateView[];
|
|
59
|
+
/** False when folder writes must be refused. */
|
|
60
|
+
readonly writable: boolean;
|
|
61
|
+
/** Present when the stored document could not be used as it stands. */
|
|
62
|
+
readonly problem?: FoldersDocumentProblem;
|
|
63
|
+
/** True when what the view shows differs from what is stored, because the read healed it. */
|
|
64
|
+
readonly healed: boolean;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Resolve a decoded document against the project and template lists into the view the desktop
|
|
69
|
+
* renders.
|
|
70
|
+
*
|
|
71
|
+
* The read heals rather than fails. An item the document does not mention is at the top level;
|
|
72
|
+
* an assignment naming an item that is gone is ignored; a folder whose parent is missing, or
|
|
73
|
+
* which sits in a cycle, is lifted to the top level instead of being hidden. No input can make a
|
|
74
|
+
* project or a template disappear.
|
|
75
|
+
*/
|
|
76
|
+
export function healFolders(
|
|
77
|
+
decoded: FoldersDecoded,
|
|
78
|
+
projects: readonly FoldersProjectInput[],
|
|
79
|
+
templates: readonly FoldersTemplateInput[] = [],
|
|
80
|
+
): FoldersView {
|
|
81
|
+
const view = foldersViewFromDocument(decoded.document, projects, templates);
|
|
82
|
+
return { ...view, writable: decoded.writable, problem: decoded.problem };
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** The same healing read, for a caller that already holds a document. */
|
|
86
|
+
export function foldersViewFromDocument(
|
|
87
|
+
document: FoldersDocument,
|
|
88
|
+
projects: readonly FoldersProjectInput[],
|
|
89
|
+
templates: readonly FoldersTemplateInput[] = [],
|
|
90
|
+
): FoldersView {
|
|
91
|
+
const resolution = resolveFolders(document);
|
|
92
|
+
const { folders, byId } = resolution;
|
|
93
|
+
let healed = resolution.healed;
|
|
94
|
+
|
|
95
|
+
const placedProjects = place(projects, document.assignments, byId);
|
|
96
|
+
const placedTemplates = place(templates, document.templateAssignments, byId);
|
|
97
|
+
if (placedProjects.healed || placedTemplates.healed) healed = true;
|
|
98
|
+
|
|
99
|
+
return {
|
|
100
|
+
folders,
|
|
101
|
+
projects: placedProjects.items,
|
|
102
|
+
templates: placedTemplates.items,
|
|
103
|
+
writable: true,
|
|
104
|
+
healed,
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** Direct children of a folder, or of the top level when no folder is given. */
|
|
109
|
+
export function foldersChildren(
|
|
110
|
+
view: FoldersView,
|
|
111
|
+
parent?: FolderId,
|
|
112
|
+
): {
|
|
113
|
+
readonly folders: readonly FolderView[];
|
|
114
|
+
readonly projects: readonly FoldersProjectView[];
|
|
115
|
+
readonly templates: readonly FoldersTemplateView[];
|
|
116
|
+
} {
|
|
117
|
+
return {
|
|
118
|
+
folders: view.folders.filter((folder) => folder.parent === parent),
|
|
119
|
+
projects: view.projects.filter((project) => project.folder === parent),
|
|
120
|
+
templates: view.templates.filter((template) => template.folder === parent),
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Names already taken directly inside a folder, or at the top level when no folder is given.
|
|
126
|
+
*
|
|
127
|
+
* Each kind has its own namespace: two folders, two projects or two templates beside each other
|
|
128
|
+
* never answer to one name, but a folder, a project and a template may. With `kind` given, only the
|
|
129
|
+
* names of that kind are listed — the list a name for an item of that kind is checked against.
|
|
130
|
+
* Without it, the names of every kind are listed.
|
|
131
|
+
*
|
|
132
|
+
* `exclude` names the ids that must not count against themselves — the items being renamed or
|
|
133
|
+
* moved.
|
|
134
|
+
*/
|
|
135
|
+
export function foldersSiblingNames(
|
|
136
|
+
view: FoldersView,
|
|
137
|
+
parent?: FolderId,
|
|
138
|
+
exclude: Iterable<string> = [],
|
|
139
|
+
options: { readonly kind?: FoldersItem["kind"] } = {},
|
|
140
|
+
): string[] {
|
|
141
|
+
const skipped = new Set<string>(exclude);
|
|
142
|
+
const children = foldersChildren(view, parent);
|
|
143
|
+
const listed: readonly { readonly id: string; readonly name: string }[] =
|
|
144
|
+
options.kind === "folder"
|
|
145
|
+
? children.folders
|
|
146
|
+
: options.kind === "project"
|
|
147
|
+
? children.projects
|
|
148
|
+
: options.kind === "template"
|
|
149
|
+
? children.templates
|
|
150
|
+
: [...children.folders, ...children.projects, ...children.templates];
|
|
151
|
+
return listed.filter((item) => !skipped.has(item.id)).map((item) => item.name);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/** A folder and every folder beneath it, the folder itself first. */
|
|
155
|
+
export function foldersSubtree(view: FoldersView, folder: FolderId): readonly FolderId[] {
|
|
156
|
+
const subtree: FolderId[] = [];
|
|
157
|
+
const queue: FolderId[] = [folder];
|
|
158
|
+
while (queue.length > 0) {
|
|
159
|
+
const current = queue.shift();
|
|
160
|
+
if (current === undefined) break;
|
|
161
|
+
subtree.push(current);
|
|
162
|
+
for (const candidate of view.folders)
|
|
163
|
+
if (candidate.parent === current) queue.push(candidate.id);
|
|
164
|
+
}
|
|
165
|
+
return subtree;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* The document a healed view stands for — what a write must persist so that the next read finds
|
|
170
|
+
* the tree it just rendered.
|
|
171
|
+
*/
|
|
172
|
+
export function foldersDocumentFromView(view: FoldersView): FoldersDocument {
|
|
173
|
+
const document = emptyFoldersDocument();
|
|
174
|
+
return {
|
|
175
|
+
...document,
|
|
176
|
+
folders: view.folders.map((folder) => {
|
|
177
|
+
const description = normalizeDescription(folder.description);
|
|
178
|
+
return {
|
|
179
|
+
id: folder.id,
|
|
180
|
+
name: folder.name,
|
|
181
|
+
...(folder.parent === undefined ? {} : { parent: folder.parent }),
|
|
182
|
+
...(description === undefined ? {} : { description }),
|
|
183
|
+
};
|
|
184
|
+
}),
|
|
185
|
+
assignments: Object.fromEntries(
|
|
186
|
+
view.projects.flatMap((project) =>
|
|
187
|
+
project.folder === undefined ? [] : [[project.id, project.folder] as const],
|
|
188
|
+
),
|
|
189
|
+
),
|
|
190
|
+
templateAssignments: Object.fromEntries(
|
|
191
|
+
view.templates.flatMap((template) =>
|
|
192
|
+
template.folder === undefined ? [] : [[template.id, template.folder] as const],
|
|
193
|
+
),
|
|
194
|
+
),
|
|
195
|
+
};
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
//
|
|
199
|
+
// Internals
|
|
200
|
+
//
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* Resolve where each item of one list sits, and report whether doing so had to heal anything: a
|
|
204
|
+
* duplicate id in the list, an assignment to a folder that is not there, or an assignment naming
|
|
205
|
+
* an item the list no longer holds.
|
|
206
|
+
*/
|
|
207
|
+
function place<Id extends string>(
|
|
208
|
+
items: readonly FoldersItemInput<Id>[],
|
|
209
|
+
assignments: Readonly<Record<string, FolderId>>,
|
|
210
|
+
byId: ReadonlyMap<FolderId, FolderView>,
|
|
211
|
+
): { readonly items: FoldersItemView<Id>[]; readonly healed: boolean } {
|
|
212
|
+
let healed = false;
|
|
213
|
+
const seen = new Set<string>();
|
|
214
|
+
const placed: FoldersItemView<Id>[] = [];
|
|
215
|
+
|
|
216
|
+
for (const item of items) {
|
|
217
|
+
if (seen.has(item.id)) {
|
|
218
|
+
healed = true;
|
|
219
|
+
continue;
|
|
220
|
+
}
|
|
221
|
+
seen.add(item.id);
|
|
222
|
+
|
|
223
|
+
const assigned = assignments[item.id];
|
|
224
|
+
const folder = assigned === undefined ? undefined : byId.get(assigned);
|
|
225
|
+
if (assigned !== undefined && folder === undefined) healed = true;
|
|
226
|
+
|
|
227
|
+
placed.push(
|
|
228
|
+
folder === undefined
|
|
229
|
+
? { id: item.id, name: item.name, ancestors: [], path: [] }
|
|
230
|
+
: {
|
|
231
|
+
id: item.id,
|
|
232
|
+
name: item.name,
|
|
233
|
+
folder: folder.id,
|
|
234
|
+
ancestors: [...folder.ancestors, folder.id],
|
|
235
|
+
path: [...folder.path],
|
|
236
|
+
},
|
|
237
|
+
);
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
for (const id of Object.keys(assignments)) if (!seen.has(id)) healed = true;
|
|
241
|
+
|
|
242
|
+
return { items: placed, healed };
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
interface FolderResolution {
|
|
246
|
+
readonly folders: readonly FolderView[];
|
|
247
|
+
readonly byId: ReadonlyMap<FolderId, FolderView>;
|
|
248
|
+
readonly healed: boolean;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
/**
|
|
252
|
+
* Place every folder in the tree, lifting to the top level the ones whose stored parent cannot be
|
|
253
|
+
* honoured. A lifted folder keeps its own children, which simply sit one or more levels higher.
|
|
254
|
+
*/
|
|
255
|
+
function resolveFolders(document: FoldersDocument): FolderResolution {
|
|
256
|
+
const stored = new Map<FolderId, { name: string; parent?: FolderId; description?: string }>();
|
|
257
|
+
let healed = false;
|
|
258
|
+
for (const folder of document.folders) {
|
|
259
|
+
if (stored.has(folder.id)) {
|
|
260
|
+
healed = true;
|
|
261
|
+
continue;
|
|
262
|
+
}
|
|
263
|
+
stored.set(folder.id, {
|
|
264
|
+
name: folder.name,
|
|
265
|
+
parent: folder.parent,
|
|
266
|
+
description: folder.description,
|
|
267
|
+
});
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
const resolved = new Map<FolderId, FolderView>();
|
|
271
|
+
const visiting = new Set<FolderId>();
|
|
272
|
+
|
|
273
|
+
const resolve = (id: FolderId): FolderView => {
|
|
274
|
+
const existing = resolved.get(id);
|
|
275
|
+
if (existing !== undefined) return existing;
|
|
276
|
+
|
|
277
|
+
const entry = stored.get(id);
|
|
278
|
+
if (entry === undefined) throw new Error(`folder ${id} is not in the document`);
|
|
279
|
+
|
|
280
|
+
if (visiting.has(id)) {
|
|
281
|
+
// A cycle. Cutting it here, at the folder the walk came back to, keeps the rest of the
|
|
282
|
+
// chain attached below this one instead of scattering every folder in the cycle.
|
|
283
|
+
const lifted = topLevel(id, entry.name, true, entry.description);
|
|
284
|
+
resolved.set(id, lifted);
|
|
285
|
+
healed = true;
|
|
286
|
+
return lifted;
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
visiting.add(id);
|
|
290
|
+
try {
|
|
291
|
+
const parentId = entry.parent;
|
|
292
|
+
const parent =
|
|
293
|
+
parentId === undefined || !stored.has(parentId) ? undefined : resolve(parentId);
|
|
294
|
+
|
|
295
|
+
const alreadyResolved = resolved.get(id);
|
|
296
|
+
if (alreadyResolved !== undefined) return alreadyResolved;
|
|
297
|
+
|
|
298
|
+
const view =
|
|
299
|
+
parent === undefined
|
|
300
|
+
? topLevel(id, entry.name, parentId !== undefined, entry.description)
|
|
301
|
+
: {
|
|
302
|
+
id,
|
|
303
|
+
name: entry.name,
|
|
304
|
+
parent: parent.id,
|
|
305
|
+
ancestors: [...parent.ancestors, parent.id],
|
|
306
|
+
path: [...parent.path, entry.name],
|
|
307
|
+
...(entry.description === undefined ? {} : { description: entry.description }),
|
|
308
|
+
lifted: false,
|
|
309
|
+
};
|
|
310
|
+
|
|
311
|
+
if (view.lifted) healed = true;
|
|
312
|
+
resolved.set(id, view);
|
|
313
|
+
return view;
|
|
314
|
+
} finally {
|
|
315
|
+
visiting.delete(id);
|
|
316
|
+
}
|
|
317
|
+
};
|
|
318
|
+
|
|
319
|
+
const folders: FolderView[] = [];
|
|
320
|
+
for (const id of stored.keys()) folders.push(resolve(id));
|
|
321
|
+
|
|
322
|
+
return { folders, byId: resolved, healed };
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
function topLevel(id: FolderId, name: string, lifted: boolean, description?: string): FolderView {
|
|
326
|
+
return {
|
|
327
|
+
id,
|
|
328
|
+
name,
|
|
329
|
+
ancestors: [],
|
|
330
|
+
path: [name],
|
|
331
|
+
...(description === undefined ? {} : { description }),
|
|
332
|
+
lifted,
|
|
333
|
+
};
|
|
334
|
+
}
|
package/src/index.ts
CHANGED
package/src/project.ts
CHANGED
|
@@ -1,4 +1,25 @@
|
|
|
1
1
|
export interface ProjectMeta {
|
|
2
2
|
/** Project name */
|
|
3
3
|
readonly label: string;
|
|
4
|
+
/**
|
|
5
|
+
* Free text the user wrote about the project. Absent when there is none — a project nobody
|
|
6
|
+
* described and one whose description was cleared carry the same metadata.
|
|
7
|
+
*/
|
|
8
|
+
readonly description?: string;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* A description as it is stored: trimmed, and absent when nothing is left. A description of
|
|
13
|
+
* whitespace only is no description, and storing it as an empty string would make an untouched
|
|
14
|
+
* item and a cleared one look different everywhere the value is compared or rendered.
|
|
15
|
+
*/
|
|
16
|
+
export function normalizeDescription(text?: string): string | undefined {
|
|
17
|
+
const trimmed = text?.trim();
|
|
18
|
+
return trimmed === undefined || trimmed === "" ? undefined : trimmed;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** Project metadata with a blank description left out. */
|
|
22
|
+
export function normalizeProjectMeta(meta: ProjectMeta): ProjectMeta {
|
|
23
|
+
const description = normalizeDescription(meta.description);
|
|
24
|
+
return description === undefined ? { label: meta.label } : { label: meta.label, description };
|
|
4
25
|
}
|