@milaboratories/pl-model-middle-layer 1.33.0 → 1.34.1

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 (41) hide show
  1. package/dist/folders/commit.d.ts +1 -1
  2. package/dist/folders/index.d.ts +1 -1
  3. package/dist/folders/inheritance.cjs +4 -4
  4. package/dist/folders/inheritance.cjs.map +1 -1
  5. package/dist/folders/inheritance.d.ts +4 -4
  6. package/dist/folders/inheritance.js +4 -4
  7. package/dist/folders/inheritance.js.map +1 -1
  8. package/dist/folders/invariants.cjs +9 -9
  9. package/dist/folders/invariants.cjs.map +1 -1
  10. package/dist/folders/invariants.d.ts +5 -3
  11. package/dist/folders/invariants.d.ts.map +1 -1
  12. package/dist/folders/invariants.js +9 -9
  13. package/dist/folders/invariants.js.map +1 -1
  14. package/dist/folders/naming.cjs +5 -4
  15. package/dist/folders/naming.cjs.map +1 -1
  16. package/dist/folders/naming.d.ts +5 -4
  17. package/dist/folders/naming.d.ts.map +1 -1
  18. package/dist/folders/naming.js +5 -4
  19. package/dist/folders/naming.js.map +1 -1
  20. package/dist/folders/planner.cjs +8 -3
  21. package/dist/folders/planner.cjs.map +1 -1
  22. package/dist/folders/planner.d.ts.map +1 -1
  23. package/dist/folders/planner.js +8 -3
  24. package/dist/folders/planner.js.map +1 -1
  25. package/dist/folders/view.cjs +7 -5
  26. package/dist/folders/view.cjs.map +1 -1
  27. package/dist/folders/view.d.ts +8 -3
  28. package/dist/folders/view.d.ts.map +1 -1
  29. package/dist/folders/view.js +7 -5
  30. package/dist/folders/view.js.map +1 -1
  31. package/dist/index.d.ts +1 -1
  32. package/package.json +4 -4
  33. package/src/folders/inheritance.test.ts +11 -6
  34. package/src/folders/inheritance.ts +7 -6
  35. package/src/folders/invariants.test.ts +15 -6
  36. package/src/folders/invariants.ts +15 -11
  37. package/src/folders/naming.ts +5 -4
  38. package/src/folders/planner.test.ts +20 -6
  39. package/src/folders/planner.ts +10 -7
  40. package/src/folders/view.test.ts +17 -6
  41. package/src/folders/view.ts +15 -5
@@ -1,6 +1,6 @@
1
1
  import { FoldersDocument } from "./document.js";
2
- import { FoldersView } from "./view.js";
3
2
  import { FoldersLabelRename, FoldersMoveIssue, FoldersMovePlan, FoldersMovePlanResult } from "./planner.js";
3
+ import { FoldersView } from "./view.js";
4
4
  import { FoldersRemoval, FoldersRemovalIssue, FoldersRemovalPlanResult } from "./removal.js";
5
5
  import { ProjectId, TemplateId } from "@milaboratories/pl-model-common";
6
6
  //#region src/folders/commit.d.ts
@@ -1,9 +1,9 @@
1
1
  import { FOLDERS_SCHEMA_VERSION, FolderEntry, FolderId, FoldersDocument, asFolderId, emptyFoldersDocument } from "./document.js";
2
2
  import { FoldersDecoded, FoldersDocumentProblem, FoldersStoredDocument, decodeFoldersDocument, decodeStoredFoldersDocument, encodeFoldersDocument } from "./decode.js";
3
+ import { FoldersItem, FoldersLabelRename, FoldersLeafItem, FoldersMoveApplication, FoldersMoveEntry, FoldersMoveIssue, FoldersMovePlan, FoldersMovePlanResult, applyFoldersMove, foldersMovePlansEqual, planFoldersMove } from "./planner.js";
3
4
  import { FolderView, FoldersItemInput, FoldersItemView, FoldersPlacement, FoldersProjectInput, FoldersProjectView, FoldersTemplateInput, FoldersTemplateView, FoldersView, foldersChildren, foldersDocumentFromView, foldersSiblingNames, foldersSubtree, foldersViewFromDocument, healFolders } from "./view.js";
4
5
  import { FoldersViolation, formatFoldersViolation, validateFoldersDocument } from "./invariants.js";
5
6
  import { foldersNameBlank, foldersNameTaken, foldersUniqueName } from "./naming.js";
6
- import { FoldersItem, FoldersLabelRename, FoldersLeafItem, FoldersMoveApplication, FoldersMoveEntry, FoldersMoveIssue, FoldersMovePlan, FoldersMovePlanResult, applyFoldersMove, foldersMovePlansEqual, planFoldersMove } from "./planner.js";
7
7
  import { FoldersRemoval, FoldersRemovalIssue, FoldersRemovalPlanResult, applyFoldersRemoval, foldersRemovalsEqual, planFoldersRemoval } from "./removal.js";
8
8
  import { FoldersEdit, FoldersMoveOutcome, FoldersRemovalOutcome, commitFoldersMove, commitFoldersRemoval } from "./commit.js";
9
9
  import { inheritedFolder } from "./inheritance.js";
@@ -8,14 +8,14 @@ const require_naming = require("./naming.cjs");
8
8
  * top level reads as the app having lost them. `undefined` means leave it at the top level:
9
9
  *
10
10
  * - the source is itself at the top level, or is no longer in the tree at all;
11
- * - the name is already taken where the source sits. The creating caller chose that name, so
12
- * suffixing it here would disagree with what it reported, and refusing the whole creation over
13
- * a placement would be the worse trade.
11
+ * - an item of the created one's kind already carries the name where the source sits. The
12
+ * creating caller chose that name, so suffixing it here would disagree with what it reported,
13
+ * and refusing the whole creation over a placement would be the worse trade.
14
14
  */
15
15
  function inheritedFolder(view, source, created, name) {
16
16
  const folder = view.projects.find((candidate) => candidate.id === source)?.folder;
17
17
  if (folder === void 0) return void 0;
18
- return require_naming.foldersNameTaken(name, require_view.foldersSiblingNames(view, folder, [created.id])) ? void 0 : folder;
18
+ return require_naming.foldersNameTaken(name, require_view.foldersSiblingNames(view, folder, [created.id], { kind: created.kind })) ? void 0 : folder;
19
19
  }
20
20
  //#endregion
21
21
  exports.inheritedFolder = inheritedFolder;
@@ -1 +1 @@
1
- {"version":3,"file":"inheritance.cjs","names":["foldersNameTaken","foldersSiblingNames"],"sources":["../../src/folders/inheritance.ts"],"sourcesContent":["import type { ProjectId } from \"@milaboratories/pl-model-common\";\nimport type { FolderId } from \"./document\";\nimport { foldersNameTaken } from \"./naming\";\nimport type { FoldersLeafItem } from \"./planner\";\nimport type { FoldersView } from \"./view\";\nimport { foldersSiblingNames } from \"./view\";\n\n/**\n * The folder a newly created item should land in, given the project it was made from.\n *\n * A copy of a project, and a template taken from one, belong beside their source; landing at the\n * top level reads as the app having lost them. `undefined` means leave it at the top level:\n *\n * - the source is itself at the top level, or is no longer in the tree at all;\n * - the name is already taken where the source sits. The creating caller chose that name, so\n * suffixing it here would disagree with what it reported, and refusing the whole creation over\n * a placement would be the worse trade.\n */\nexport function inheritedFolder(\n view: FoldersView,\n source: ProjectId,\n created: FoldersLeafItem,\n name: string,\n): FolderId | undefined {\n const folder = view.projects.find((candidate) => candidate.id === source)?.folder;\n if (folder === undefined) return undefined;\n\n // Scoped to the destination, because the rule is: names are unique within a parent, never\n // globally. The item being created is left out in case the view already lists it.\n const siblings = foldersSiblingNames(view, folder, [created.id]);\n return foldersNameTaken(name, siblings) ? undefined : folder;\n}\n"],"mappings":";;;;;;;;;;;;;;AAkBA,SAAgB,gBACd,MACA,QACA,SACA,MACsB;CACtB,MAAM,SAAS,KAAK,SAAS,MAAM,cAAc,UAAU,OAAO,MAAM,CAAC,EAAE;CAC3E,IAAI,WAAW,KAAA,GAAW,OAAO,KAAA;CAKjC,OAAOA,eAAAA,iBAAiB,MADPC,aAAAA,oBAAoB,MAAM,QAAQ,CAAC,QAAQ,EAAE,CACzB,CAAC,IAAI,KAAA,IAAY;AACxD"}
1
+ {"version":3,"file":"inheritance.cjs","names":["foldersNameTaken","foldersSiblingNames"],"sources":["../../src/folders/inheritance.ts"],"sourcesContent":["import type { ProjectId } from \"@milaboratories/pl-model-common\";\nimport type { FolderId } from \"./document\";\nimport { foldersNameTaken } from \"./naming\";\nimport type { FoldersLeafItem } from \"./planner\";\nimport type { FoldersView } from \"./view\";\nimport { foldersSiblingNames } from \"./view\";\n\n/**\n * The folder a newly created item should land in, given the project it was made from.\n *\n * A copy of a project, and a template taken from one, belong beside their source; landing at the\n * top level reads as the app having lost them. `undefined` means leave it at the top level:\n *\n * - the source is itself at the top level, or is no longer in the tree at all;\n * - an item of the created one's kind already carries the name where the source sits. The\n * creating caller chose that name, so suffixing it here would disagree with what it reported,\n * and refusing the whole creation over a placement would be the worse trade.\n */\nexport function inheritedFolder(\n view: FoldersView,\n source: ProjectId,\n created: FoldersLeafItem,\n name: string,\n): FolderId | undefined {\n const folder = view.projects.find((candidate) => candidate.id === source)?.folder;\n if (folder === undefined) return undefined;\n\n // Scoped to the destination and to the created item's kind, because the rule is: names are\n // unique among one kind within a parent, never globally. The item being created is left out in\n // case the view already lists it.\n const siblings = foldersSiblingNames(view, folder, [created.id], { kind: created.kind });\n return foldersNameTaken(name, siblings) ? undefined : folder;\n}\n"],"mappings":";;;;;;;;;;;;;;AAkBA,SAAgB,gBACd,MACA,QACA,SACA,MACsB;CACtB,MAAM,SAAS,KAAK,SAAS,MAAM,cAAc,UAAU,OAAO,MAAM,CAAC,EAAE;CAC3E,IAAI,WAAW,KAAA,GAAW,OAAO,KAAA;CAMjC,OAAOA,eAAAA,iBAAiB,MADPC,aAAAA,oBAAoB,MAAM,QAAQ,CAAC,QAAQ,EAAE,GAAG,EAAE,MAAM,QAAQ,KAAK,CACjD,CAAC,IAAI,KAAA,IAAY;AACxD"}
@@ -1,6 +1,6 @@
1
1
  import { FolderId } from "./document.js";
2
- import { FoldersView } from "./view.js";
3
2
  import { FoldersLeafItem } from "./planner.js";
3
+ import { FoldersView } from "./view.js";
4
4
  import { ProjectId } from "@milaboratories/pl-model-common";
5
5
  //#region src/folders/inheritance.d.ts
6
6
  /**
@@ -10,9 +10,9 @@ import { ProjectId } from "@milaboratories/pl-model-common";
10
10
  * top level reads as the app having lost them. `undefined` means leave it at the top level:
11
11
  *
12
12
  * - the source is itself at the top level, or is no longer in the tree at all;
13
- * - the name is already taken where the source sits. The creating caller chose that name, so
14
- * suffixing it here would disagree with what it reported, and refusing the whole creation over
15
- * a placement would be the worse trade.
13
+ * - an item of the created one's kind already carries the name where the source sits. The
14
+ * creating caller chose that name, so suffixing it here would disagree with what it reported,
15
+ * and refusing the whole creation over a placement would be the worse trade.
16
16
  */
17
17
  export declare function inheritedFolder(view: FoldersView, source: ProjectId, created: FoldersLeafItem, name: string): FolderId | undefined;
18
18
  //#endregion
@@ -8,14 +8,14 @@ import { foldersNameTaken } from "./naming.js";
8
8
  * top level reads as the app having lost them. `undefined` means leave it at the top level:
9
9
  *
10
10
  * - the source is itself at the top level, or is no longer in the tree at all;
11
- * - the name is already taken where the source sits. The creating caller chose that name, so
12
- * suffixing it here would disagree with what it reported, and refusing the whole creation over
13
- * a placement would be the worse trade.
11
+ * - an item of the created one's kind already carries the name where the source sits. The
12
+ * creating caller chose that name, so suffixing it here would disagree with what it reported,
13
+ * and refusing the whole creation over a placement would be the worse trade.
14
14
  */
15
15
  function inheritedFolder(view, source, created, name) {
16
16
  const folder = view.projects.find((candidate) => candidate.id === source)?.folder;
17
17
  if (folder === void 0) return void 0;
18
- return foldersNameTaken(name, foldersSiblingNames(view, folder, [created.id])) ? void 0 : folder;
18
+ return foldersNameTaken(name, foldersSiblingNames(view, folder, [created.id], { kind: created.kind })) ? void 0 : folder;
19
19
  }
20
20
  //#endregion
21
21
  export { inheritedFolder };
@@ -1 +1 @@
1
- {"version":3,"file":"inheritance.js","names":[],"sources":["../../src/folders/inheritance.ts"],"sourcesContent":["import type { ProjectId } from \"@milaboratories/pl-model-common\";\nimport type { FolderId } from \"./document\";\nimport { foldersNameTaken } from \"./naming\";\nimport type { FoldersLeafItem } from \"./planner\";\nimport type { FoldersView } from \"./view\";\nimport { foldersSiblingNames } from \"./view\";\n\n/**\n * The folder a newly created item should land in, given the project it was made from.\n *\n * A copy of a project, and a template taken from one, belong beside their source; landing at the\n * top level reads as the app having lost them. `undefined` means leave it at the top level:\n *\n * - the source is itself at the top level, or is no longer in the tree at all;\n * - the name is already taken where the source sits. The creating caller chose that name, so\n * suffixing it here would disagree with what it reported, and refusing the whole creation over\n * a placement would be the worse trade.\n */\nexport function inheritedFolder(\n view: FoldersView,\n source: ProjectId,\n created: FoldersLeafItem,\n name: string,\n): FolderId | undefined {\n const folder = view.projects.find((candidate) => candidate.id === source)?.folder;\n if (folder === undefined) return undefined;\n\n // Scoped to the destination, because the rule is: names are unique within a parent, never\n // globally. The item being created is left out in case the view already lists it.\n const siblings = foldersSiblingNames(view, folder, [created.id]);\n return foldersNameTaken(name, siblings) ? undefined : folder;\n}\n"],"mappings":";;;;;;;;;;;;;;AAkBA,SAAgB,gBACd,MACA,QACA,SACA,MACsB;CACtB,MAAM,SAAS,KAAK,SAAS,MAAM,cAAc,UAAU,OAAO,MAAM,CAAC,EAAE;CAC3E,IAAI,WAAW,KAAA,GAAW,OAAO,KAAA;CAKjC,OAAO,iBAAiB,MADP,oBAAoB,MAAM,QAAQ,CAAC,QAAQ,EAAE,CACzB,CAAC,IAAI,KAAA,IAAY;AACxD"}
1
+ {"version":3,"file":"inheritance.js","names":[],"sources":["../../src/folders/inheritance.ts"],"sourcesContent":["import type { ProjectId } from \"@milaboratories/pl-model-common\";\nimport type { FolderId } from \"./document\";\nimport { foldersNameTaken } from \"./naming\";\nimport type { FoldersLeafItem } from \"./planner\";\nimport type { FoldersView } from \"./view\";\nimport { foldersSiblingNames } from \"./view\";\n\n/**\n * The folder a newly created item should land in, given the project it was made from.\n *\n * A copy of a project, and a template taken from one, belong beside their source; landing at the\n * top level reads as the app having lost them. `undefined` means leave it at the top level:\n *\n * - the source is itself at the top level, or is no longer in the tree at all;\n * - an item of the created one's kind already carries the name where the source sits. The\n * creating caller chose that name, so suffixing it here would disagree with what it reported,\n * and refusing the whole creation over a placement would be the worse trade.\n */\nexport function inheritedFolder(\n view: FoldersView,\n source: ProjectId,\n created: FoldersLeafItem,\n name: string,\n): FolderId | undefined {\n const folder = view.projects.find((candidate) => candidate.id === source)?.folder;\n if (folder === undefined) return undefined;\n\n // Scoped to the destination and to the created item's kind, because the rule is: names are\n // unique among one kind within a parent, never globally. The item being created is left out in\n // case the view already lists it.\n const siblings = foldersSiblingNames(view, folder, [created.id], { kind: created.kind });\n return foldersNameTaken(name, siblings) ? undefined : folder;\n}\n"],"mappings":";;;;;;;;;;;;;;AAkBA,SAAgB,gBACd,MACA,QACA,SACA,MACsB;CACtB,MAAM,SAAS,KAAK,SAAS,MAAM,cAAc,UAAU,OAAO,MAAM,CAAC,EAAE;CAC3E,IAAI,WAAW,KAAA,GAAW,OAAO,KAAA;CAMjC,OAAO,iBAAiB,MADP,oBAAoB,MAAM,QAAQ,CAAC,QAAQ,EAAE,GAAG,EAAE,MAAM,QAAQ,KAAK,CACjD,CAAC,IAAI,KAAA,IAAY;AACxD"}
@@ -8,9 +8,8 @@ let _milaboratories_pl_model_common = require("@milaboratories/pl-model-common")
8
8
  * write that would persist any of them is refused, because a document no reader can render as
9
9
  * stored is a document someone has to repair by hand.
10
10
  *
11
- * Project and template names are optional. Without them the namespace folders, projects and
12
- * templates share can only be checked between folders, so a write path that can supply them
13
- * should. The template list also tells an assignment for a template that is gone — which a read
11
+ * Project and template names are optional. Without them only folder names can be checked for
12
+ * duplicates, so a write path that can supply them should. The template list also tells an assignment for a template that is gone — which a read
14
13
  * prunes — from one naming a folder that does not exist, which is a broken document.
15
14
  */
16
15
  function validateFoldersDocument(document, projects, templates) {
@@ -48,22 +47,23 @@ function validateFoldersDocument(document, projects, templates) {
48
47
  });
49
48
  }
50
49
  const namesByParent = /* @__PURE__ */ new Map();
51
- const claim = (parent, name) => {
52
- const key = parent ?? "";
50
+ const claim = (item, parent, name) => {
51
+ const key = `${item}:${parent ?? ""}`;
53
52
  const names = namesByParent.get(key) ?? [];
54
53
  namesByParent.set(key, names);
55
54
  if (require_naming.foldersNameTaken(name, names)) violations.push({
56
55
  kind: "duplicate-name",
56
+ item,
57
57
  parent,
58
58
  name
59
59
  });
60
60
  names.push(name);
61
61
  };
62
- for (const folder of byId.values()) claim(folder.parent, folder.name);
62
+ for (const folder of byId.values()) claim("folder", folder.parent, folder.name);
63
63
  const knownProjects = new Set((projects ?? []).map((project) => project.id));
64
64
  for (const project of projects ?? []) {
65
65
  const parent = document.assignments[project.id];
66
- if (parent === void 0 || byId.has(parent)) claim(parent, project.name);
66
+ if (parent === void 0 || byId.has(parent)) claim("project", parent, project.name);
67
67
  }
68
68
  for (const [project, folder] of Object.entries(document.assignments)) if (!byId.has(folder) && (projects === void 0 || knownProjects.has(project))) violations.push({
69
69
  kind: "unknown-assignment-folder",
@@ -72,7 +72,7 @@ function validateFoldersDocument(document, projects, templates) {
72
72
  });
73
73
  for (const template of templates ?? []) {
74
74
  const parent = document.templateAssignments[template.id];
75
- if (parent === void 0 || byId.has(parent)) claim(parent, template.name);
75
+ if (parent === void 0 || byId.has(parent)) claim("template", parent, template.name);
76
76
  }
77
77
  const knownTemplates = new Set((templates ?? []).map((template) => template.id));
78
78
  for (const [template, folder] of Object.entries(document.templateAssignments)) if (!byId.has(folder) && (templates === void 0 || knownTemplates.has(template))) violations.push({
@@ -89,7 +89,7 @@ function formatFoldersViolation(violation) {
89
89
  case "empty-folder-name": return `folder ${violation.folder} has an empty name`;
90
90
  case "missing-parent": return `folder ${violation.folder} names a parent that does not exist (${violation.parent})`;
91
91
  case "cycle": return `folder ${violation.folder} sits in a cycle of parents`;
92
- case "duplicate-name": return violation.parent === void 0 ? `the name "${violation.name}" is used more than once at the top level` : `the name "${violation.name}" is used more than once inside folder ${violation.parent}`;
92
+ case "duplicate-name": return violation.parent === void 0 ? `the ${violation.item} name "${violation.name}" is used more than once at the top level` : `the ${violation.item} name "${violation.name}" is used more than once inside folder ${violation.parent}`;
93
93
  case "unknown-assignment-folder": return `project ${violation.project} is assigned to a folder that does not exist (${violation.folder})`;
94
94
  case "unknown-template-assignment-folder": return `template ${violation.template} is assigned to a folder that does not exist (${violation.folder})`;
95
95
  }
@@ -1 +1 @@
1
- {"version":3,"file":"invariants.cjs","names":["foldersNameBlank","foldersNameTaken"],"sources":["../../src/folders/invariants.ts"],"sourcesContent":["import type { ProjectId, TemplateId } from \"@milaboratories/pl-model-common\";\nimport { asProjectId, asTemplateId } from \"@milaboratories/pl-model-common\";\nimport type { FoldersDocument, FolderId } from \"./document\";\nimport { foldersNameBlank, foldersNameTaken } from \"./naming\";\nimport type { FoldersProjectInput, FoldersTemplateInput } from \"./view\";\n\n/** One way a document breaks the rules the write path enforces. */\nexport type FoldersViolation =\n | { readonly kind: \"duplicate-folder-id\"; readonly folder: FolderId }\n | { readonly kind: \"empty-folder-name\"; readonly folder: FolderId }\n | {\n readonly kind: \"missing-parent\";\n readonly folder: FolderId;\n readonly parent: FolderId;\n }\n | { readonly kind: \"cycle\"; readonly folder: FolderId }\n | {\n readonly kind: \"duplicate-name\";\n readonly parent?: FolderId;\n readonly name: string;\n }\n | {\n readonly kind: \"unknown-assignment-folder\";\n readonly project: ProjectId;\n readonly folder: FolderId;\n }\n | {\n readonly kind: \"unknown-template-assignment-folder\";\n readonly template: TemplateId;\n readonly folder: FolderId;\n };\n\n/**\n * Everything wrong with a document, in one pass.\n *\n * This is the write path's check: a read heals the same problems instead of reporting them, but a\n * write that would persist any of them is refused, because a document no reader can render as\n * stored is a document someone has to repair by hand.\n *\n * Project and template names are optional. Without them the namespace folders, projects and\n * templates share can only be checked between folders, so a write path that can supply them\n * should. The template list also tells an assignment for a template that is gone — which a read\n * prunes — from one naming a folder that does not exist, which is a broken document.\n */\nexport function validateFoldersDocument(\n document: FoldersDocument,\n projects?: readonly FoldersProjectInput[],\n templates?: readonly FoldersTemplateInput[],\n): readonly FoldersViolation[] {\n const violations: FoldersViolation[] = [];\n\n const byId = new Map<FolderId, { name: string; parent?: FolderId }>();\n for (const folder of document.folders) {\n if (byId.has(folder.id)) {\n violations.push({ kind: \"duplicate-folder-id\", folder: folder.id });\n continue;\n }\n if (foldersNameBlank(folder.name))\n violations.push({ kind: \"empty-folder-name\", folder: folder.id });\n byId.set(folder.id, { name: folder.name, parent: folder.parent });\n }\n\n for (const [id, folder] of byId) {\n if (folder.parent !== undefined && !byId.has(folder.parent)) {\n violations.push({ kind: \"missing-parent\", folder: id, parent: folder.parent });\n continue;\n }\n\n if (onCycle(id, byId)) violations.push({ kind: \"cycle\", folder: id });\n }\n\n // The comparison is the naming rule's, not a second one written here: a document is invalid in\n // exactly the cases the rule would have refused, or the write path and the checker would drift.\n const namesByParent = new Map<string, string[]>();\n const claim = (parent: FolderId | undefined, name: string) => {\n const key = parent ?? \"\";\n const names = namesByParent.get(key) ?? [];\n namesByParent.set(key, names);\n if (foldersNameTaken(name, names)) violations.push({ kind: \"duplicate-name\", parent, name });\n names.push(name);\n };\n\n for (const folder of byId.values()) claim(folder.parent, folder.name);\n\n const knownProjects = new Set<string>((projects ?? []).map((project) => project.id));\n for (const project of projects ?? []) {\n const parent = document.assignments[project.id];\n if (parent === undefined || byId.has(parent)) claim(parent, project.name);\n }\n\n for (const [project, folder] of Object.entries(document.assignments))\n if (!byId.has(folder) && (projects === undefined || knownProjects.has(project)))\n violations.push({\n kind: \"unknown-assignment-folder\",\n project: asProjectId(project),\n folder,\n });\n\n for (const template of templates ?? []) {\n const parent = document.templateAssignments[template.id];\n if (parent === undefined || byId.has(parent)) claim(parent, template.name);\n }\n\n const knownTemplates = new Set<string>((templates ?? []).map((template) => template.id));\n for (const [template, folder] of Object.entries(document.templateAssignments))\n if (!byId.has(folder) && (templates === undefined || knownTemplates.has(template)))\n violations.push({\n kind: \"unknown-template-assignment-folder\",\n template: asTemplateId(template),\n folder,\n });\n\n return violations;\n}\n\n/** A one-line description of a violation, for an error a user or a log will read. */\nexport function formatFoldersViolation(violation: FoldersViolation): string {\n switch (violation.kind) {\n case \"duplicate-folder-id\":\n return `folder id ${violation.folder} appears more than once`;\n case \"empty-folder-name\":\n return `folder ${violation.folder} has an empty name`;\n case \"missing-parent\":\n return `folder ${violation.folder} names a parent that does not exist (${violation.parent})`;\n case \"cycle\":\n return `folder ${violation.folder} sits in a cycle of parents`;\n case \"duplicate-name\":\n return violation.parent === undefined\n ? `the name \"${violation.name}\" is used more than once at the top level`\n : `the name \"${violation.name}\" is used more than once inside folder ${violation.parent}`;\n case \"unknown-assignment-folder\":\n return `project ${violation.project} is assigned to a folder that does not exist (${violation.folder})`;\n case \"unknown-template-assignment-folder\":\n return `template ${violation.template} is assigned to a folder that does not exist (${violation.folder})`;\n }\n}\n\n//\n// Internals\n//\n\n/**\n * True when following parents from this folder leads back to it. A folder that merely hangs below\n * a cycle is not on it.\n */\nfunction onCycle(id: FolderId, byId: ReadonlyMap<FolderId, { parent?: FolderId }>): boolean {\n const seen = new Set<FolderId>();\n let current = byId.get(id)?.parent;\n while (current !== undefined && !seen.has(current)) {\n if (current === id) return true;\n seen.add(current);\n current = byId.get(current)?.parent;\n }\n return false;\n}\n"],"mappings":";;;;;;;;;;;;;;;AA4CA,SAAgB,wBACd,UACA,UACA,WAC6B;CAC7B,MAAM,aAAiC,CAAC;CAExC,MAAM,uBAAO,IAAI,IAAmD;CACpE,KAAK,MAAM,UAAU,SAAS,SAAS;EACrC,IAAI,KAAK,IAAI,OAAO,EAAE,GAAG;GACvB,WAAW,KAAK;IAAE,MAAM;IAAuB,QAAQ,OAAO;GAAG,CAAC;GAClE;EACF;EACA,IAAIA,eAAAA,iBAAiB,OAAO,IAAI,GAC9B,WAAW,KAAK;GAAE,MAAM;GAAqB,QAAQ,OAAO;EAAG,CAAC;EAClE,KAAK,IAAI,OAAO,IAAI;GAAE,MAAM,OAAO;GAAM,QAAQ,OAAO;EAAO,CAAC;CAClE;CAEA,KAAK,MAAM,CAAC,IAAI,WAAW,MAAM;EAC/B,IAAI,OAAO,WAAW,KAAA,KAAa,CAAC,KAAK,IAAI,OAAO,MAAM,GAAG;GAC3D,WAAW,KAAK;IAAE,MAAM;IAAkB,QAAQ;IAAI,QAAQ,OAAO;GAAO,CAAC;GAC7E;EACF;EAEA,IAAI,QAAQ,IAAI,IAAI,GAAG,WAAW,KAAK;GAAE,MAAM;GAAS,QAAQ;EAAG,CAAC;CACtE;CAIA,MAAM,gCAAgB,IAAI,IAAsB;CAChD,MAAM,SAAS,QAA8B,SAAiB;EAC5D,MAAM,MAAM,UAAU;EACtB,MAAM,QAAQ,cAAc,IAAI,GAAG,KAAK,CAAC;EACzC,cAAc,IAAI,KAAK,KAAK;EAC5B,IAAIC,eAAAA,iBAAiB,MAAM,KAAK,GAAG,WAAW,KAAK;GAAE,MAAM;GAAkB;GAAQ;EAAK,CAAC;EAC3F,MAAM,KAAK,IAAI;CACjB;CAEA,KAAK,MAAM,UAAU,KAAK,OAAO,GAAG,MAAM,OAAO,QAA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,OAAO,UAAU,UAAU,OAAO;EACpC,KAAK,kBACH,OAAO,UAAU,WAAW,KAAA,IACxB,aAAa,UAAU,KAAK,6CAC5B,aAAa,UAAU,KAAK,yCAAyC,UAAU;EACrF,KAAK,6BACH,OAAO,WAAW,UAAU,QAAQ,gDAAgD,UAAU,OAAO;EACvG,KAAK,sCACH,OAAO,YAAY,UAAU,SAAS,gDAAgD,UAAU,OAAO;CAC3G;AACF;;;;;AAUA,SAAS,QAAQ,IAAc,MAA6D;CAC1F,MAAM,uBAAO,IAAI,IAAc;CAC/B,IAAI,UAAU,KAAK,IAAI,EAAE,CAAC,EAAE;CAC5B,OAAO,YAAY,KAAA,KAAa,CAAC,KAAK,IAAI,OAAO,GAAG;EAClD,IAAI,YAAY,IAAI,OAAO;EAC3B,KAAK,IAAI,OAAO;EAChB,UAAU,KAAK,IAAI,OAAO,CAAC,EAAE;CAC/B;CACA,OAAO;AACT"}
1
+ {"version":3,"file":"invariants.cjs","names":["foldersNameBlank","foldersNameTaken"],"sources":["../../src/folders/invariants.ts"],"sourcesContent":["import type { ProjectId, TemplateId } from \"@milaboratories/pl-model-common\";\nimport { asProjectId, asTemplateId } from \"@milaboratories/pl-model-common\";\nimport type { FoldersDocument, FolderId } from \"./document\";\nimport { foldersNameBlank, foldersNameTaken } from \"./naming\";\nimport type { FoldersItem } from \"./planner\";\nimport type { FoldersProjectInput, FoldersTemplateInput } from \"./view\";\n\n/** One way a document breaks the rules the write path enforces. */\nexport type FoldersViolation =\n | { readonly kind: \"duplicate-folder-id\"; readonly folder: FolderId }\n | { readonly kind: \"empty-folder-name\"; readonly folder: FolderId }\n | {\n readonly kind: \"missing-parent\";\n readonly folder: FolderId;\n readonly parent: FolderId;\n }\n | { readonly kind: \"cycle\"; readonly folder: FolderId }\n | {\n readonly kind: \"duplicate-name\";\n /** Which kind of item the name is used twice among. */\n readonly item: FoldersItem[\"kind\"];\n readonly parent?: FolderId;\n readonly name: string;\n }\n | {\n readonly kind: \"unknown-assignment-folder\";\n readonly project: ProjectId;\n readonly folder: FolderId;\n }\n | {\n readonly kind: \"unknown-template-assignment-folder\";\n readonly template: TemplateId;\n readonly folder: FolderId;\n };\n\n/**\n * Everything wrong with a document, in one pass.\n *\n * This is the write path's check: a read heals the same problems instead of reporting them, but a\n * write that would persist any of them is refused, because a document no reader can render as\n * stored is a document someone has to repair by hand.\n *\n * Project and template names are optional. Without them only folder names can be checked for\n * duplicates, so a write path that can supply them should. The template list also tells an assignment for a template that is gone — which a read\n * prunes — from one naming a folder that does not exist, which is a broken document.\n */\nexport function validateFoldersDocument(\n document: FoldersDocument,\n projects?: readonly FoldersProjectInput[],\n templates?: readonly FoldersTemplateInput[],\n): readonly FoldersViolation[] {\n const violations: FoldersViolation[] = [];\n\n const byId = new Map<FolderId, { name: string; parent?: FolderId }>();\n for (const folder of document.folders) {\n if (byId.has(folder.id)) {\n violations.push({ kind: \"duplicate-folder-id\", folder: folder.id });\n continue;\n }\n if (foldersNameBlank(folder.name))\n violations.push({ kind: \"empty-folder-name\", folder: folder.id });\n byId.set(folder.id, { name: folder.name, parent: folder.parent });\n }\n\n for (const [id, folder] of byId) {\n if (folder.parent !== undefined && !byId.has(folder.parent)) {\n violations.push({ kind: \"missing-parent\", folder: id, parent: folder.parent });\n continue;\n }\n\n if (onCycle(id, byId)) violations.push({ kind: \"cycle\", folder: id });\n }\n\n // The comparison is the naming rule's, not a second one written here: a document is invalid in\n // exactly the cases the rule would have refused, or the write path and the checker would drift.\n // Each kind of item has a namespace of its own inside a parent.\n const namesByParent = new Map<string, string[]>();\n const claim = (item: FoldersItem[\"kind\"], parent: FolderId | undefined, name: string) => {\n const key = `${item}:${parent ?? \"\"}`;\n const names = namesByParent.get(key) ?? [];\n namesByParent.set(key, names);\n if (foldersNameTaken(name, names))\n violations.push({ kind: \"duplicate-name\", item, parent, name });\n names.push(name);\n };\n\n for (const folder of byId.values()) claim(\"folder\", folder.parent, folder.name);\n\n const knownProjects = new Set<string>((projects ?? []).map((project) => project.id));\n for (const project of projects ?? []) {\n const parent = document.assignments[project.id];\n if (parent === undefined || byId.has(parent)) claim(\"project\", parent, project.name);\n }\n\n for (const [project, folder] of Object.entries(document.assignments))\n if (!byId.has(folder) && (projects === undefined || knownProjects.has(project)))\n violations.push({\n kind: \"unknown-assignment-folder\",\n project: asProjectId(project),\n folder,\n });\n\n for (const template of templates ?? []) {\n const parent = document.templateAssignments[template.id];\n if (parent === undefined || byId.has(parent)) claim(\"template\", parent, template.name);\n }\n\n const knownTemplates = new Set<string>((templates ?? []).map((template) => template.id));\n for (const [template, folder] of Object.entries(document.templateAssignments))\n if (!byId.has(folder) && (templates === undefined || knownTemplates.has(template)))\n violations.push({\n kind: \"unknown-template-assignment-folder\",\n template: asTemplateId(template),\n folder,\n });\n\n return violations;\n}\n\n/** A one-line description of a violation, for an error a user or a log will read. */\nexport function formatFoldersViolation(violation: FoldersViolation): string {\n switch (violation.kind) {\n case \"duplicate-folder-id\":\n return `folder id ${violation.folder} appears more than once`;\n case \"empty-folder-name\":\n return `folder ${violation.folder} has an empty name`;\n case \"missing-parent\":\n return `folder ${violation.folder} names a parent that does not exist (${violation.parent})`;\n case \"cycle\":\n return `folder ${violation.folder} sits in a cycle of parents`;\n case \"duplicate-name\":\n return violation.parent === undefined\n ? `the ${violation.item} name \"${violation.name}\" is used more than once at the top level`\n : `the ${violation.item} name \"${violation.name}\" is used more than once inside folder ${violation.parent}`;\n case \"unknown-assignment-folder\":\n return `project ${violation.project} is assigned to a folder that does not exist (${violation.folder})`;\n case \"unknown-template-assignment-folder\":\n return `template ${violation.template} is assigned to a folder that does not exist (${violation.folder})`;\n }\n}\n\n//\n// Internals\n//\n\n/**\n * True when following parents from this folder leads back to it. A folder that merely hangs below\n * a cycle is not on it.\n */\nfunction onCycle(id: FolderId, byId: ReadonlyMap<FolderId, { parent?: FolderId }>): boolean {\n const seen = new Set<FolderId>();\n let current = byId.get(id)?.parent;\n while (current !== undefined && !seen.has(current)) {\n if (current === id) return true;\n se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ACpC,KAAK,kBACH,OAAO,UAAU,WAAW,KAAA,IACxB,OAAO,UAAU,KAAK,SAAS,UAAU,KAAK,6CAC9C,OAAO,UAAU,KAAK,SAAS,UAAU,KAAK,yCAAyC,UAAU;EACvG,KAAK,6BACH,OAAO,WAAW,UAAU,QAAQ,gDAAgD,UAAU,OAAO;EACvG,KAAK,sCACH,OAAO,YAAY,UAAU,SAAS,gDAAgD,UAAU,OAAO;CAC3G;AACF;;;;;AAUA,SAAS,QAAQ,IAAc,MAA6D;CAC1F,MAAM,uBAAO,IAAI,IAAc;CAC/B,IAAI,UAAU,KAAK,IAAI,EAAE,CAAC,EAAE;CAC5B,OAAO,YAAY,KAAA,KAAa,CAAC,KAAK,IAAI,OAAO,GAAG;EAClD,IAAI,YAAY,IAAI,OAAO;EAC3B,KAAK,IAAI,OAAO;EAChB,UAAU,KAAK,IAAI,OAAO,CAAC,EAAE;CAC/B;CACA,OAAO;AACT"}
@@ -1,4 +1,5 @@
1
1
  import { FolderId, FoldersDocument } from "./document.js";
2
+ import { FoldersItem } from "./planner.js";
2
3
  import { FoldersProjectInput, FoldersTemplateInput } from "./view.js";
3
4
  import { ProjectId, TemplateId } from "@milaboratories/pl-model-common";
4
5
  //#region src/folders/invariants.d.ts
@@ -18,6 +19,8 @@ export type FoldersViolation = {
18
19
  readonly folder: FolderId;
19
20
  } | {
20
21
  readonly kind: "duplicate-name";
22
+ /** Which kind of item the name is used twice among. */
23
+ readonly item: FoldersItem["kind"];
21
24
  readonly parent?: FolderId;
22
25
  readonly name: string;
23
26
  } | {
@@ -36,9 +39,8 @@ export type FoldersViolation = {
36
39
  * write that would persist any of them is refused, because a document no reader can render as
37
40
  * stored is a document someone has to repair by hand.
38
41
  *
39
- * Project and template names are optional. Without them the namespace folders, projects and
40
- * templates share can only be checked between folders, so a write path that can supply them
41
- * should. The template list also tells an assignment for a template that is gone — which a read
42
+ * Project and template names are optional. Without them only folder names can be checked for
43
+ * duplicates, so a write path that can supply them should. The template list also tells an assignment for a template that is gone — which a read
42
44
  * prunes — from one naming a folder that does not exist, which is a broken document.
43
45
  */
44
46
  export declare function validateFoldersDocument(document: FoldersDocument, projects?: readonly FoldersProjectInput[], templates?: readonly FoldersTemplateInput[]): readonly FoldersViolation[];
@@ -1 +1 @@
1
- {"version":3,"file":"invariants.d.ts","names":[],"sources":["../../src/folders/invariants.ts"],"mappings":";;;;;YAOY;WACG;WAAsC,QAAQ;;WAC9C;WAAoC,QAAQ;;WAE5C;WACA,QAAQ;WACR,QAAQ;;WAER;WAAwB,QAAQ;;WAEhC;WACA,SAAS;WACT;;WAGA;WACA,SAAS;WACT,QAAQ;;WAGR;WACA,UAAU;WACV,QAAQ;;;;;;;;;;;;;;wBAeP,wBACd,UAAU,iBACV,oBAAoB,uBACpB,qBAAqB,kCACX;;wBAoEI,uBAAuB,WAAW"}
1
+ {"version":3,"file":"invariants.d.ts","names":[],"sources":["../../src/folders/invariants.ts"],"mappings":";;;;;;YAQY;WACG;WAAsC,QAAQ;;WAC9C;WAAoC,QAAQ;;WAE5C;WACA,QAAQ;WACR,QAAQ;;WAER;WAAwB,QAAQ;;WAEhC;;WAEA,MAAM;WACN,SAAS;WACT;;WAGA;WACA,SAAS;WACT,QAAQ;;WAGR;WACA,UAAU;WACV,QAAQ;;;;;;;;;;;;;wBAcP,wBACd,UAAU,iBACV,oBAAoB,uBACpB,qBAAqB,kCACX;;wBAsEI,uBAAuB,WAAW"}
@@ -8,9 +8,8 @@ import { asProjectId, asTemplateId } from "@milaboratories/pl-model-common";
8
8
  * write that would persist any of them is refused, because a document no reader can render as
9
9
  * stored is a document someone has to repair by hand.
10
10
  *
11
- * Project and template names are optional. Without them the namespace folders, projects and
12
- * templates share can only be checked between folders, so a write path that can supply them
13
- * should. The template list also tells an assignment for a template that is gone — which a read
11
+ * Project and template names are optional. Without them only folder names can be checked for
12
+ * duplicates, so a write path that can supply them should. The template list also tells an assignment for a template that is gone — which a read
14
13
  * prunes — from one naming a folder that does not exist, which is a broken document.
15
14
  */
16
15
  function validateFoldersDocument(document, projects, templates) {
@@ -48,22 +47,23 @@ function validateFoldersDocument(document, projects, templates) {
48
47
  });
49
48
  }
50
49
  const namesByParent = /* @__PURE__ */ new Map();
51
- const claim = (parent, name) => {
52
- const key = parent ?? "";
50
+ const claim = (item, parent, name) => {
51
+ const key = `${item}:${parent ?? ""}`;
53
52
  const names = namesByParent.get(key) ?? [];
54
53
  namesByParent.set(key, names);
55
54
  if (foldersNameTaken(name, names)) violations.push({
56
55
  kind: "duplicate-name",
56
+ item,
57
57
  parent,
58
58
  name
59
59
  });
60
60
  names.push(name);
61
61
  };
62
- for (const folder of byId.values()) claim(folder.parent, folder.name);
62
+ for (const folder of byId.values()) claim("folder", folder.parent, folder.name);
63
63
  const knownProjects = new Set((projects ?? []).map((project) => project.id));
64
64
  for (const project of projects ?? []) {
65
65
  const parent = document.assignments[project.id];
66
- if (parent === void 0 || byId.has(parent)) claim(parent, project.name);
66
+ if (parent === void 0 || byId.has(parent)) claim("project", parent, project.name);
67
67
  }
68
68
  for (const [project, folder] of Object.entries(document.assignments)) if (!byId.has(folder) && (projects === void 0 || knownProjects.has(project))) violations.push({
69
69
  kind: "unknown-assignment-folder",
@@ -72,7 +72,7 @@ function validateFoldersDocument(document, projects, templates) {
72
72
  });
73
73
  for (const template of templates ?? []) {
74
74
  const parent = document.templateAssignments[template.id];
75
- if (parent === void 0 || byId.has(parent)) claim(parent, template.name);
75
+ if (parent === void 0 || byId.has(parent)) claim("template", parent, template.name);
76
76
  }
77
77
  const knownTemplates = new Set((templates ?? []).map((template) => template.id));
78
78
  for (const [template, folder] of Object.entries(document.templateAssignments)) if (!byId.has(folder) && (templates === void 0 || knownTemplates.has(template))) violations.push({
@@ -89,7 +89,7 @@ function formatFoldersViolation(violation) {
89
89
  case "empty-folder-name": return `folder ${violation.folder} has an empty name`;
90
90
  case "missing-parent": return `folder ${violation.folder} names a parent that does not exist (${violation.parent})`;
91
91
  case "cycle": return `folder ${violation.folder} sits in a cycle of parents`;
92
- case "duplicate-name": return violation.parent === void 0 ? `the name "${violation.name}" is used more than once at the top level` : `the name "${violation.name}" is used more than once inside folder ${violation.parent}`;
92
+ case "duplicate-name": return violation.parent === void 0 ? `the ${violation.item} name "${violation.name}" is used more than once at the top level` : `the ${violation.item} name "${violation.name}" is used more than once inside folder ${violation.parent}`;
93
93
  case "unknown-assignment-folder": return `project ${violation.project} is assigned to a folder that does not exist (${violation.folder})`;
94
94
  case "unknown-template-assignment-folder": return `template ${violation.template} is assigned to a folder that does not exist (${violation.folder})`;
95
95
  }
@@ -1 +1 @@
1
- {"version":3,"file":"invariants.js","names":[],"sources":["../../src/folders/invariants.ts"],"sourcesContent":["import type { ProjectId, TemplateId } from \"@milaboratories/pl-model-common\";\nimport { asProjectId, asTemplateId } from \"@milaboratories/pl-model-common\";\nimport type { FoldersDocument, FolderId } from \"./document\";\nimport { foldersNameBlank, foldersNameTaken } from \"./naming\";\nimport type { FoldersProjectInput, FoldersTemplateInput } from \"./view\";\n\n/** One way a document breaks the rules the write path enforces. */\nexport type FoldersViolation =\n | { readonly kind: \"duplicate-folder-id\"; readonly folder: FolderId }\n | { readonly kind: \"empty-folder-name\"; readonly folder: FolderId }\n | {\n readonly kind: \"missing-parent\";\n readonly folder: FolderId;\n readonly parent: FolderId;\n }\n | { readonly kind: \"cycle\"; readonly folder: FolderId }\n | {\n readonly kind: \"duplicate-name\";\n readonly parent?: FolderId;\n readonly name: string;\n }\n | {\n readonly kind: \"unknown-assignment-folder\";\n readonly project: ProjectId;\n readonly folder: FolderId;\n }\n | {\n readonly kind: \"unknown-template-assignment-folder\";\n readonly template: TemplateId;\n readonly folder: FolderId;\n };\n\n/**\n * Everything wrong with a document, in one pass.\n *\n * This is the write path's check: a read heals the same problems instead of reporting them, but a\n * write that would persist any of them is refused, because a document no reader can render as\n * stored is a document someone has to repair by hand.\n *\n * Project and template names are optional. Without them the namespace folders, projects and\n * templates share can only be checked between folders, so a write path that can supply them\n * should. The template list also tells an assignment for a template that is gone — which a read\n * prunes — from one naming a folder that does not exist, which is a broken document.\n */\nexport function validateFoldersDocument(\n document: FoldersDocument,\n projects?: readonly FoldersProjectInput[],\n templates?: readonly FoldersTemplateInput[],\n): readonly FoldersViolation[] {\n const violations: FoldersViolation[] = [];\n\n const byId = new Map<FolderId, { name: string; parent?: FolderId }>();\n for (const folder of document.folders) {\n if (byId.has(folder.id)) {\n violations.push({ kind: \"duplicate-folder-id\", folder: folder.id });\n continue;\n }\n if (foldersNameBlank(folder.name))\n violations.push({ kind: \"empty-folder-name\", folder: folder.id });\n byId.set(folder.id, { name: folder.name, parent: folder.parent });\n }\n\n for (const [id, folder] of byId) {\n if (folder.parent !== undefined && !byId.has(folder.parent)) {\n violations.push({ kind: \"missing-parent\", folder: id, parent: folder.parent });\n continue;\n }\n\n if (onCycle(id, byId)) violations.push({ kind: \"cycle\", folder: id });\n }\n\n // The comparison is the naming rule's, not a second one written here: a document is invalid in\n // exactly the cases the rule would have refused, or the write path and the checker would drift.\n const namesByParent = new Map<string, string[]>();\n const claim = (parent: FolderId | undefined, name: string) => {\n const key = parent ?? \"\";\n const names = namesByParent.get(key) ?? [];\n namesByParent.set(key, names);\n if (foldersNameTaken(name, names)) violations.push({ kind: \"duplicate-name\", parent, name });\n names.push(name);\n };\n\n for (const folder of byId.values()) claim(folder.parent, folder.name);\n\n const knownProjects = new Set<string>((projects ?? []).map((project) => project.id));\n for (const project of projects ?? []) {\n const parent = document.assignments[project.id];\n if (parent === undefined || byId.has(parent)) claim(parent, project.name);\n }\n\n for (const [project, folder] of Object.entries(document.assignments))\n if (!byId.has(folder) && (projects === undefined || knownProjects.has(project)))\n violations.push({\n kind: \"unknown-assignment-folder\",\n project: asProjectId(project),\n folder,\n });\n\n for (const template of templates ?? []) {\n const parent = document.templateAssignments[template.id];\n if (parent === undefined || byId.has(parent)) claim(parent, template.name);\n }\n\n const knownTemplates = new Set<string>((templates ?? []).map((template) => template.id));\n for (const [template, folder] of Object.entries(document.templateAssignments))\n if (!byId.has(folder) && (templates === undefined || knownTemplates.has(template)))\n violations.push({\n kind: \"unknown-template-assignment-folder\",\n template: asTemplateId(template),\n folder,\n });\n\n return violations;\n}\n\n/** A one-line description of a violation, for an error a user or a log will read. */\nexport function formatFoldersViolation(violation: FoldersViolation): string {\n switch (violation.kind) {\n case \"duplicate-folder-id\":\n return `folder id ${violation.folder} appears more than once`;\n case \"empty-folder-name\":\n return `folder ${violation.folder} has an empty name`;\n case \"missing-parent\":\n return `folder ${violation.folder} names a parent that does not exist (${violation.parent})`;\n case \"cycle\":\n return `folder ${violation.folder} sits in a cycle of parents`;\n case \"duplicate-name\":\n return violation.parent === undefined\n ? `the name \"${violation.name}\" is used more than once at the top level`\n : `the name \"${violation.name}\" is used more than once inside folder ${violation.parent}`;\n case \"unknown-assignment-folder\":\n return `project ${violation.project} is assigned to a folder that does not exist (${violation.folder})`;\n case \"unknown-template-assignment-folder\":\n return `template ${violation.template} is assigned to a folder that does not exist (${violation.folder})`;\n }\n}\n\n//\n// Internals\n//\n\n/**\n * True when following parents from this folder leads back to it. A folder that merely hangs below\n * a cycle is not on it.\n */\nfunction onCycle(id: FolderId, byId: ReadonlyMap<FolderId, { parent?: FolderId }>): boolean {\n const seen = new Set<FolderId>();\n let current = byId.get(id)?.parent;\n while (current !== undefined && !seen.has(current)) {\n if (current === id) return true;\n seen.add(current);\n current = byId.get(current)?.parent;\n }\n return false;\n}\n"],"mappings":";;;;;;;;;;;;;;;AA4CA,SAAgB,wBACd,UACA,UACA,WAC6B;CAC7B,MAAM,aAAiC,CAAC;CAExC,MAAM,uBAAO,IAAI,IAAmD;CACpE,KAAK,MAAM,UAAU,SAAS,SAAS;EACrC,IAAI,KAAK,IAAI,OAAO,EAAE,GAAG;GACvB,WAAW,KAAK;IAAE,MAAM;IAAuB,QAAQ,OAAO;GAAG,CAAC;GAClE;EACF;EACA,IAAI,iBAAiB,OAAO,IAAI,GAC9B,WAAW,KAAK;GAAE,MAAM;GAAqB,QA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,KAAK,IAAI,OAAO,CAAC,EAAE;CAC/B;CACA,OAAO;AACT"}
1
+ {"version":3,"file":"invariants.js","names":[],"sources":["../../src/folders/invariants.ts"],"sourcesContent":["import type { ProjectId, TemplateId } from \"@milaboratories/pl-model-common\";\nimport { asProjectId, asTemplateId } from \"@milaboratories/pl-model-common\";\nimport type { FoldersDocument, FolderId } from \"./document\";\nimport { foldersNameBlank, foldersNameTaken } from \"./naming\";\nimport type { FoldersItem } from \"./planner\";\nimport type { FoldersProjectInput, FoldersTemplateInput } from \"./view\";\n\n/** One way a document breaks the rules the write path enforces. */\nexport type FoldersViolation =\n | { readonly kind: \"duplicate-folder-id\"; readonly folder: FolderId }\n | { readonly kind: \"empty-folder-name\"; readonly folder: FolderId }\n | {\n readonly kind: \"missing-parent\";\n readonly folder: FolderId;\n readonly parent: FolderId;\n }\n | { readonly kind: \"cycle\"; readonly folder: FolderId }\n | {\n readonly kind: \"duplicate-name\";\n /** Which kind of item the name is used twice among. */\n readonly item: FoldersItem[\"kind\"];\n readonly parent?: FolderId;\n readonly name: string;\n }\n | {\n readonly kind: \"unknown-assignment-folder\";\n readonly project: ProjectId;\n readonly folder: FolderId;\n }\n | {\n readonly kind: \"unknown-template-assignment-folder\";\n readonly template: TemplateId;\n readonly folder: FolderId;\n };\n\n/**\n * Everything wrong with a document, in one pass.\n *\n * This is the write path's check: a read heals the same problems instead of reporting them, but a\n * write that would persist any of them is refused, because a document no reader can render as\n * stored is a document someone has to repair by hand.\n *\n * Project and template names are optional. Without them only folder names can be checked for\n * duplicates, so a write path that can supply them should. The template list also tells an assignment for a template that is gone — which a read\n * prunes — from one naming a folder that does not exist, which is a broken document.\n */\nexport function validateFoldersDocument(\n document: FoldersDocument,\n projects?: readonly FoldersProjectInput[],\n templates?: readonly FoldersTemplateInput[],\n): readonly FoldersViolation[] {\n const violations: FoldersViolation[] = [];\n\n const byId = new Map<FolderId, { name: string; parent?: FolderId }>();\n for (const folder of document.folders) {\n if (byId.has(folder.id)) {\n violations.push({ kind: \"duplicate-folder-id\", folder: folder.id });\n continue;\n }\n if (foldersNameBlank(folder.name))\n violations.push({ kind: \"empty-folder-name\", folder: folder.id });\n byId.set(folder.id, { name: folder.name, parent: folder.parent });\n }\n\n for (const [id, folder] of byId) {\n if (folder.parent !== undefined && !byId.has(folder.parent)) {\n violations.push({ kind: \"missing-parent\", folder: id, parent: folder.parent });\n continue;\n }\n\n if (onCycle(id, byId)) violations.push({ kind: \"cycle\", folder: id });\n }\n\n // The comparison is the naming rule's, not a second one written here: a document is invalid in\n // exactly the cases the rule would have refused, or the write path and the checker would drift.\n // Each kind of item has a namespace of its own inside a parent.\n const namesByParent = new Map<string, string[]>();\n const claim = (item: FoldersItem[\"kind\"], parent: FolderId | undefined, name: string) => {\n const key = `${item}:${parent ?? \"\"}`;\n const names = namesByParent.get(key) ?? [];\n namesByParent.set(key, names);\n if (foldersNameTaken(name, names))\n violations.push({ kind: \"duplicate-name\", item, parent, name });\n names.push(name);\n };\n\n for (const folder of byId.values()) claim(\"folder\", folder.parent, folder.name);\n\n const knownProjects = new Set<string>((projects ?? []).map((project) => project.id));\n for (const project of projects ?? []) {\n const parent = document.assignments[project.id];\n if (parent === undefined || byId.has(parent)) claim(\"project\", parent, project.name);\n }\n\n for (const [project, folder] of Object.entries(document.assignments))\n if (!byId.has(folder) && (projects === undefined || knownProjects.has(project)))\n violations.push({\n kind: \"unknown-assignment-folder\",\n project: asProjectId(project),\n folder,\n });\n\n for (const template of templates ?? []) {\n const parent = document.templateAssignments[template.id];\n if (parent === undefined || byId.has(parent)) claim(\"template\", parent, template.name);\n }\n\n const knownTemplates = new Set<string>((templates ?? []).map((template) => template.id));\n for (const [template, folder] of Object.entries(document.templateAssignments))\n if (!byId.has(folder) && (templates === undefined || knownTemplates.has(template)))\n violations.push({\n kind: \"unknown-template-assignment-folder\",\n template: asTemplateId(template),\n folder,\n });\n\n return violations;\n}\n\n/** A one-line description of a violation, for an error a user or a log will read. */\nexport function formatFoldersViolation(violation: FoldersViolation): string {\n switch (violation.kind) {\n case \"duplicate-folder-id\":\n return `folder id ${violation.folder} appears more than once`;\n case \"empty-folder-name\":\n return `folder ${violation.folder} has an empty name`;\n case \"missing-parent\":\n return `folder ${violation.folder} names a parent that does not exist (${violation.parent})`;\n case \"cycle\":\n return `folder ${violation.folder} sits in a cycle of parents`;\n case \"duplicate-name\":\n return violation.parent === undefined\n ? `the ${violation.item} name \"${violation.name}\" is used more than once at the top level`\n : `the ${violation.item} name \"${violation.name}\" is used more than once inside folder ${violation.parent}`;\n case \"unknown-assignment-folder\":\n return `project ${violation.project} is assigned to a folder that does not exist (${violation.folder})`;\n case \"unknown-template-assignment-folder\":\n return `template ${violation.template} is assigned to a folder that does not exist (${violation.folder})`;\n }\n}\n\n//\n// Internals\n//\n\n/**\n * True when following parents from this folder leads back to it. A folder that merely hangs below\n * a cycle is not on it.\n */\nfunction onCycle(id: FolderId, byId: ReadonlyMap<FolderId, { parent?: FolderId }>): boolean {\n const seen = new Set<FolderId>();\n let current = byId.get(id)?.parent;\n while (current !== undefined && !seen.has(current)) {\n if (current === id) return true;\n seen.add(current);\n current = byId.get(current)?.parent;\n }\n return false;\n}\n"],"mappings":";;;;;;;;;;;;;;AA8CA,SAAgB,wBACd,UACA,UACA,WAC6B;CAC7B,MAAM,aAAiC,CAAC;CAExC,MAAM,uBAAO,IAAI,IAAmD;CACpE,KAAK,MAAM,UAAU,SAAS,SAAS;EACrC,IAAI,KAAK,IAAI,OAAO,EAAE,GAAG;GACvB,WAAW,KAAK;IAAE,MAAM;IAAuB,QAAQ,OAAO;GAAG,CAAC;GAClE;EACF;EACA,IAAI,iBAAiB,OAAO,IAAI,GAC9B,WAAW,KAAK;GAAE,MAAM;GAAqB,QAAQ,OAAO;EAAG,CAAC;EAClE,KAAK,IAAI,OAAO,IAAI;GAAE,MAAM,OAAO;GAAM,QAAQ,OAAO;EAAO,CAAC;CAClE;CAEA,KAAK,MAAM,CAAC,IAAI,WAAW,MAAM;EAC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@@
3
3
  * The one naming rule for folders, projects and templates.
4
4
  *
5
5
  * Scope is always the children of one parent — a folder, or the top level, which is simply the
6
- * folder with no parent. Folders, projects and templates share that one namespace, so no two of
7
- * them inside the same parent can carry the same name. Comparison ignores case and surrounding
8
- * whitespace: `Samples`, `samples` and ` Samples ` are the same thing to the person looking at
9
- * them.
6
+ * folder with no parent — and one kind of item. No two folders, no two projects and no two
7
+ * templates inside the same parent can carry the same name, while a folder, a project and a
8
+ * template beside each other may: the list shows each kind under its own heading, so they are
9
+ * never mistaken for one another. Comparison ignores case and surrounding whitespace: `Samples`,
10
+ * `samples` and ` Samples ` are the same thing to the person looking at them.
10
11
  *
11
12
  * The rule auto-renames only where a machine chose the name — a move and a duplicate. Where a
12
13
  * human typed the name, a caller uses {@link foldersNameTaken} and rejects the name with
@@ -1 +1 @@
1
- {"version":3,"file":"naming.cjs","names":[],"sources":["../../src/folders/naming.ts"],"sourcesContent":["/**\n * The one naming rule for folders, projects and templates.\n *\n * Scope is always the children of one parent — a folder, or the top level, which is simply the\n * folder with no parent. Folders, projects and templates share that one namespace, so no two of\n * them inside the same parent can carry the same name. Comparison ignores case and surrounding\n * whitespace: `Samples`, `samples` and ` Samples ` are the same thing to the person looking at\n * them.\n *\n * The rule auto-renames only where a machine chose the name — a move and a duplicate. Where a\n * human typed the name, a caller uses {@link foldersNameTaken} and rejects the name with\n * an error instead: a text field that quietly disagrees with what was typed is worse than one\n * that says no.\n *\n * Names already stored are never rewritten. Accounts predating folders may hold collisions,\n * because nothing enforced uniqueness before.\n */\n\n/** True when a sibling already carries this name, compared as the rule above compares names. */\nexport function foldersNameTaken(name: string, siblingNames: Iterable<string>): boolean {\n const wanted = normalizeName(name);\n for (const sibling of siblingNames) if (normalizeName(sibling) === wanted) return true;\n return false;\n}\n\n/** True when a name has nothing in it but whitespace; a folder cannot be called that. */\nexport function foldersNameBlank(name: string): boolean {\n return name.trim() === \"\";\n}\n\n/**\n * The name to use for an item landing beside `siblingNames`.\n *\n * A free name is returned untouched. A taken one gains an `X (Copy)`, `X (Copy 2)` … suffix, and\n * an existing copy suffix is replaced rather than stacked, so repeated copies stay readable.\n */\nexport function foldersUniqueName(desiredName: string, siblingNames: Iterable<string>): string {\n const taken = new Set<string>();\n for (const sibling of siblingNames) taken.add(normalizeName(sibling));\n\n if (!taken.has(normalizeName(desiredName))) return desiredName;\n\n const base = desiredName.replace(COPY_SUFFIX, \"\");\n let candidate = `${base} (Copy)`;\n for (let index = 2; taken.has(normalizeName(candidate)); index++)\n candidate = `${base} (Copy ${index})`;\n return candidate;\n}\n\n//\n// Internals\n//\n\nconst COPY_SUFFIX = / \\(Copy(?: \\d+)?\\)$/;\n\nfunction normalizeName(name: string): string {\n return name.trim().toLowerCase();\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;AAmBA,SAAgB,iBAAiB,MAAc,cAAyC;CACtF,MAAM,SAAS,cAAc,IAAI;CACjC,KAAK,MAAM,WAAW,cAAc,IAAI,cAAc,OAAO,MAAM,QAAQ,OAAO;CAClF,OAAO;AACT;;AAGA,SAAgB,iBAAiB,MAAuB;CACtD,OAAO,KAAK,KAAK,MAAM;AACzB;;;;;;;AAQA,SAAgB,kBAAkB,aAAqB,cAAwC;CAC7F,MAAM,wBAAQ,IAAI,IAAY;CAC9B,KAAK,MAAM,WAAW,cAAc,MAAM,IAAI,cAAc,OAAO,CAAC;CAEpE,IAAI,CAAC,MAAM,IAAI,cAAc,WAAW,CAAC,GAAG,OAAO;CAEnD,MAAM,OAAO,YAAY,QAAQ,aAAa,EAAE;CAChD,IAAI,YAAY,GAAG,KAAK;CACxB,KAAK,IAAI,QAAQ,GAAG,MAAM,IAAI,cAAc,SAAS,CAAC,GAAG,SACvD,YAAY,GAAG,KAAK,SAAS,MAAM;CACrC,OAAO;AACT;AAMA,MAAM,cAAc;AAEpB,SAAS,cAAc,MAAsB;CAC3C,OAAO,KAAK,KAAK,CAAC,CAAC,YAAY;AACjC"}
1
+ {"version":3,"file":"naming.cjs","names":[],"sources":["../../src/folders/naming.ts"],"sourcesContent":["/**\n * The one naming rule for folders, projects and templates.\n *\n * Scope is always the children of one parent — a folder, or the top level, which is simply the\n * folder with no parent — and one kind of item. No two folders, no two projects and no two\n * templates inside the same parent can carry the same name, while a folder, a project and a\n * template beside each other may: the list shows each kind under its own heading, so they are\n * never mistaken for one another. Comparison ignores case and surrounding whitespace: `Samples`,\n * `samples` and ` Samples ` are the same thing to the person looking at them.\n *\n * The rule auto-renames only where a machine chose the name — a move and a duplicate. Where a\n * human typed the name, a caller uses {@link foldersNameTaken} and rejects the name with\n * an error instead: a text field that quietly disagrees with what was typed is worse than one\n * that says no.\n *\n * Names already stored are never rewritten. Accounts predating folders may hold collisions,\n * because nothing enforced uniqueness before.\n */\n\n/** True when a sibling already carries this name, compared as the rule above compares names. */\nexport function foldersNameTaken(name: string, siblingNames: Iterable<string>): boolean {\n const wanted = normalizeName(name);\n for (const sibling of siblingNames) if (normalizeName(sibling) === wanted) return true;\n return false;\n}\n\n/** True when a name has nothing in it but whitespace; a folder cannot be called that. */\nexport function foldersNameBlank(name: string): boolean {\n return name.trim() === \"\";\n}\n\n/**\n * The name to use for an item landing beside `siblingNames`.\n *\n * A free name is returned untouched. A taken one gains an `X (Copy)`, `X (Copy 2)` … suffix, and\n * an existing copy suffix is replaced rather than stacked, so repeated copies stay readable.\n */\nexport function foldersUniqueName(desiredName: string, siblingNames: Iterable<string>): string {\n const taken = new Set<string>();\n for (const sibling of siblingNames) taken.add(normalizeName(sibling));\n\n if (!taken.has(normalizeName(desiredName))) return desiredName;\n\n const base = desiredName.replace(COPY_SUFFIX, \"\");\n let candidate = `${base} (Copy)`;\n for (let index = 2; taken.has(normalizeName(candidate)); index++)\n candidate = `${base} (Copy ${index})`;\n return candidate;\n}\n\n//\n// Internals\n//\n\nconst COPY_SUFFIX = / \\(Copy(?: \\d+)?\\)$/;\n\nfunction normalizeName(name: string): string {\n return name.trim().toLowerCase();\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAoBA,SAAgB,iBAAiB,MAAc,cAAyC;CACtF,MAAM,SAAS,cAAc,IAAI;CACjC,KAAK,MAAM,WAAW,cAAc,IAAI,cAAc,OAAO,MAAM,QAAQ,OAAO;CAClF,OAAO;AACT;;AAGA,SAAgB,iBAAiB,MAAuB;CACtD,OAAO,KAAK,KAAK,MAAM;AACzB;;;;;;;AAQA,SAAgB,kBAAkB,aAAqB,cAAwC;CAC7F,MAAM,wBAAQ,IAAI,IAAY;CAC9B,KAAK,MAAM,WAAW,cAAc,MAAM,IAAI,cAAc,OAAO,CAAC;CAEpE,IAAI,CAAC,MAAM,IAAI,cAAc,WAAW,CAAC,GAAG,OAAO;CAEnD,MAAM,OAAO,YAAY,QAAQ,aAAa,EAAE;CAChD,IAAI,YAAY,GAAG,KAAK;CACxB,KAAK,IAAI,QAAQ,GAAG,MAAM,IAAI,cAAc,SAAS,CAAC,GAAG,SACvD,YAAY,GAAG,KAAK,SAAS,MAAM;CACrC,OAAO;AACT;AAMA,MAAM,cAAc;AAEpB,SAAS,cAAc,MAAsB;CAC3C,OAAO,KAAK,KAAK,CAAC,CAAC,YAAY;AACjC"}
@@ -3,10 +3,11 @@
3
3
  * The one naming rule for folders, projects and templates.
4
4
  *
5
5
  * Scope is always the children of one parent — a folder, or the top level, which is simply the
6
- * folder with no parent. Folders, projects and templates share that one namespace, so no two of
7
- * them inside the same parent can carry the same name. Comparison ignores case and surrounding
8
- * whitespace: `Samples`, `samples` and ` Samples ` are the same thing to the person looking at
9
- * them.
6
+ * folder with no parent — and one kind of item. No two folders, no two projects and no two
7
+ * templates inside the same parent can carry the same name, while a folder, a project and a
8
+ * template beside each other may: the list shows each kind under its own heading, so they are
9
+ * never mistaken for one another. Comparison ignores case and surrounding whitespace: `Samples`,
10
+ * `samples` and ` Samples ` are the same thing to the person looking at them.
10
11
  *
11
12
  * The rule auto-renames only where a machine chose the name — a move and a duplicate. Where a
12
13
  * human typed the name, a caller uses {@link foldersNameTaken} and rejects the name with
@@ -1 +1 @@
1
- {"version":3,"file":"naming.d.ts","names":[],"sources":["../../src/folders/naming.ts"],"mappings":";;;;;;;;;;;;;;;;;;;wBAmBgB,iBAAiB,cAAc,cAAc;;wBAO7C,iBAAiB;;;;;;;wBAUjB,kBAAkB,qBAAqB,cAAc"}
1
+ {"version":3,"file":"naming.d.ts","names":[],"sources":["../../src/folders/naming.ts"],"mappings":";;;;;;;;;;;;;;;;;;;;wBAoBgB,iBAAiB,cAAc,cAAc;;wBAO7C,iBAAiB;;;;;;;wBAUjB,kBAAkB,qBAAqB,cAAc"}
@@ -3,10 +3,11 @@
3
3
  * The one naming rule for folders, projects and templates.
4
4
  *
5
5
  * Scope is always the children of one parent — a folder, or the top level, which is simply the
6
- * folder with no parent. Folders, projects and templates share that one namespace, so no two of
7
- * them inside the same parent can carry the same name. Comparison ignores case and surrounding
8
- * whitespace: `Samples`, `samples` and ` Samples ` are the same thing to the person looking at
9
- * them.
6
+ * folder with no parent — and one kind of item. No two folders, no two projects and no two
7
+ * templates inside the same parent can carry the same name, while a folder, a project and a
8
+ * template beside each other may: the list shows each kind under its own heading, so they are
9
+ * never mistaken for one another. Comparison ignores case and surrounding whitespace: `Samples`,
10
+ * `samples` and ` Samples ` are the same thing to the person looking at them.
10
11
  *
11
12
  * The rule auto-renames only where a machine chose the name — a move and a duplicate. Where a
12
13
  * human typed the name, a caller uses {@link foldersNameTaken} and rejects the name with
@@ -1 +1 @@
1
- {"version":3,"file":"naming.js","names":[],"sources":["../../src/folders/naming.ts"],"sourcesContent":["/**\n * The one naming rule for folders, projects and templates.\n *\n * Scope is always the children of one parent — a folder, or the top level, which is simply the\n * folder with no parent. Folders, projects and templates share that one namespace, so no two of\n * them inside the same parent can carry the same name. Comparison ignores case and surrounding\n * whitespace: `Samples`, `samples` and ` Samples ` are the same thing to the person looking at\n * them.\n *\n * The rule auto-renames only where a machine chose the name — a move and a duplicate. Where a\n * human typed the name, a caller uses {@link foldersNameTaken} and rejects the name with\n * an error instead: a text field that quietly disagrees with what was typed is worse than one\n * that says no.\n *\n * Names already stored are never rewritten. Accounts predating folders may hold collisions,\n * because nothing enforced uniqueness before.\n */\n\n/** True when a sibling already carries this name, compared as the rule above compares names. */\nexport function foldersNameTaken(name: string, siblingNames: Iterable<string>): boolean {\n const wanted = normalizeName(name);\n for (const sibling of siblingNames) if (normalizeName(sibling) === wanted) return true;\n return false;\n}\n\n/** True when a name has nothing in it but whitespace; a folder cannot be called that. */\nexport function foldersNameBlank(name: string): boolean {\n return name.trim() === \"\";\n}\n\n/**\n * The name to use for an item landing beside `siblingNames`.\n *\n * A free name is returned untouched. A taken one gains an `X (Copy)`, `X (Copy 2)` … suffix, and\n * an existing copy suffix is replaced rather than stacked, so repeated copies stay readable.\n */\nexport function foldersUniqueName(desiredName: string, siblingNames: Iterable<string>): string {\n const taken = new Set<string>();\n for (const sibling of siblingNames) taken.add(normalizeName(sibling));\n\n if (!taken.has(normalizeName(desiredName))) return desiredName;\n\n const base = desiredName.replace(COPY_SUFFIX, \"\");\n let candidate = `${base} (Copy)`;\n for (let index = 2; taken.has(normalizeName(candidate)); index++)\n candidate = `${base} (Copy ${index})`;\n return candidate;\n}\n\n//\n// Internals\n//\n\nconst COPY_SUFFIX = / \\(Copy(?: \\d+)?\\)$/;\n\nfunction normalizeName(name: string): string {\n return name.trim().toLowerCase();\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;AAmBA,SAAgB,iBAAiB,MAAc,cAAyC;CACtF,MAAM,SAAS,cAAc,IAAI;CACjC,KAAK,MAAM,WAAW,cAAc,IAAI,cAAc,OAAO,MAAM,QAAQ,OAAO;CAClF,OAAO;AACT;;AAGA,SAAgB,iBAAiB,MAAuB;CACtD,OAAO,KAAK,KAAK,MAAM;AACzB;;;;;;;AAQA,SAAgB,kBAAkB,aAAqB,cAAwC;CAC7F,MAAM,wBAAQ,IAAI,IAAY;CAC9B,KAAK,MAAM,WAAW,cAAc,MAAM,IAAI,cAAc,OAAO,CAAC;CAEpE,IAAI,CAAC,MAAM,IAAI,cAAc,WAAW,CAAC,GAAG,OAAO;CAEnD,MAAM,OAAO,YAAY,QAAQ,aAAa,EAAE;CAChD,IAAI,YAAY,GAAG,KAAK;CACxB,KAAK,IAAI,QAAQ,GAAG,MAAM,IAAI,cAAc,SAAS,CAAC,GAAG,SACvD,YAAY,GAAG,KAAK,SAAS,MAAM;CACrC,OAAO;AACT;AAMA,MAAM,cAAc;AAEpB,SAAS,cAAc,MAAsB;CAC3C,OAAO,KAAK,KAAK,CAAC,CAAC,YAAY;AACjC"}
1
+ {"version":3,"file":"naming.js","names":[],"sources":["../../src/folders/naming.ts"],"sourcesContent":["/**\n * The one naming rule for folders, projects and templates.\n *\n * Scope is always the children of one parent — a folder, or the top level, which is simply the\n * folder with no parent — and one kind of item. No two folders, no two projects and no two\n * templates inside the same parent can carry the same name, while a folder, a project and a\n * template beside each other may: the list shows each kind under its own heading, so they are\n * never mistaken for one another. Comparison ignores case and surrounding whitespace: `Samples`,\n * `samples` and ` Samples ` are the same thing to the person looking at them.\n *\n * The rule auto-renames only where a machine chose the name — a move and a duplicate. Where a\n * human typed the name, a caller uses {@link foldersNameTaken} and rejects the name with\n * an error instead: a text field that quietly disagrees with what was typed is worse than one\n * that says no.\n *\n * Names already stored are never rewritten. Accounts predating folders may hold collisions,\n * because nothing enforced uniqueness before.\n */\n\n/** True when a sibling already carries this name, compared as the rule above compares names. */\nexport function foldersNameTaken(name: string, siblingNames: Iterable<string>): boolean {\n const wanted = normalizeName(name);\n for (const sibling of siblingNames) if (normalizeName(sibling) === wanted) return true;\n return false;\n}\n\n/** True when a name has nothing in it but whitespace; a folder cannot be called that. */\nexport function foldersNameBlank(name: string): boolean {\n return name.trim() === \"\";\n}\n\n/**\n * The name to use for an item landing beside `siblingNames`.\n *\n * A free name is returned untouched. A taken one gains an `X (Copy)`, `X (Copy 2)` … suffix, and\n * an existing copy suffix is replaced rather than stacked, so repeated copies stay readable.\n */\nexport function foldersUniqueName(desiredName: string, siblingNames: Iterable<string>): string {\n const taken = new Set<string>();\n for (const sibling of siblingNames) taken.add(normalizeName(sibling));\n\n if (!taken.has(normalizeName(desiredName))) return desiredName;\n\n const base = desiredName.replace(COPY_SUFFIX, \"\");\n let candidate = `${base} (Copy)`;\n for (let index = 2; taken.has(normalizeName(candidate)); index++)\n candidate = `${base} (Copy ${index})`;\n return candidate;\n}\n\n//\n// Internals\n//\n\nconst COPY_SUFFIX = / \\(Copy(?: \\d+)?\\)$/;\n\nfunction normalizeName(name: string): string {\n return name.trim().toLowerCase();\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAoBA,SAAgB,iBAAiB,MAAc,cAAyC;CACtF,MAAM,SAAS,cAAc,IAAI;CACjC,KAAK,MAAM,WAAW,cAAc,IAAI,cAAc,OAAO,MAAM,QAAQ,OAAO;CAClF,OAAO;AACT;;AAGA,SAAgB,iBAAiB,MAAuB;CACtD,OAAO,KAAK,KAAK,MAAM;AACzB;;;;;;;AAQA,SAAgB,kBAAkB,aAAqB,cAAwC;CAC7F,MAAM,wBAAQ,IAAI,IAAY;CAC9B,KAAK,MAAM,WAAW,cAAc,MAAM,IAAI,cAAc,OAAO,CAAC;CAEpE,IAAI,CAAC,MAAM,IAAI,cAAc,WAAW,CAAC,GAAG,OAAO;CAEnD,MAAM,OAAO,YAAY,QAAQ,aAAa,EAAE;CAChD,IAAI,YAAY,GAAG,KAAK;CACxB,KAAK,IAAI,QAAQ,GAAG,MAAM,IAAI,cAAc,SAAS,CAAC,GAAG,SACvD,YAAY,GAAG,KAAK,SAAS,MAAM;CACrC,OAAO;AACT;AAMA,MAAM,cAAc;AAEpB,SAAS,cAAc,MAAsB;CAC3C,OAAO,KAAK,KAAK,CAAC,CAAC,YAAY;AACjC"}
@@ -51,11 +51,16 @@ function planFoldersMove(view, items, destination) {
51
51
  ok: false,
52
52
  issues
53
53
  };
54
- const taken = require_view.foldersSiblingNames(view, destination, moved.map(({ item }) => item.id));
54
+ const movedIds = moved.map(({ item }) => item.id);
55
+ const taken = {
56
+ folder: require_view.foldersSiblingNames(view, destination, movedIds, { kind: "folder" }),
57
+ project: require_view.foldersSiblingNames(view, destination, movedIds, { kind: "project" }),
58
+ template: require_view.foldersSiblingNames(view, destination, movedIds, { kind: "template" })
59
+ };
55
60
  const entries = [];
56
61
  for (const { item, name, sourceFolder } of moved.sort(byCanonicalOrder)) {
57
- const resulting = require_naming.foldersUniqueName(name, taken);
58
- taken.push(resulting);
62
+ const resulting = require_naming.foldersUniqueName(name, taken[item.kind]);
63
+ taken[item.kind].push(resulting);
59
64
  entries.push({
60
65
  item,
61
66
  currentName: name,
@@ -1 +1 @@
1
- {"version":3,"file":"planner.cjs","names":["foldersSubtree","foldersSiblingNames","foldersUniqueName","foldersDocumentFromView"],"sources":["../../src/folders/planner.ts"],"sourcesContent":["import type { ProjectId, TemplateId } from \"@milaboratories/pl-model-common\";\nimport type { FoldersDocument, FolderId } from \"./document\";\nimport { foldersUniqueName } from \"./naming\";\nimport type { FoldersView } from \"./view\";\nimport { foldersDocumentFromView, foldersSiblingNames, foldersSubtree } from \"./view\";\n\n/** A folder, a project or a stored template, as something a move acts on. */\nexport type FoldersItem =\n | { readonly kind: \"folder\"; readonly id: FolderId }\n | { readonly kind: \"project\"; readonly id: ProjectId }\n | { readonly kind: \"template\"; readonly id: TemplateId };\n\n/** A tree item that is not a folder: a project or a stored template. */\nexport type FoldersLeafItem = Exclude<FoldersItem, { readonly kind: \"folder\" }>;\n\n/** What one item is called after the move, and what it was called before. */\nexport interface FoldersMoveEntry {\n readonly item: FoldersItem;\n readonly currentName: string;\n /** The name the item carries once the plan is applied. */\n readonly name: string;\n /** True when the destination already held that name and the item is being renamed. */\n readonly renamed: boolean;\n /** Folder the item is coming from; absent when it is at the top level. */\n readonly sourceFolder?: FolderId;\n}\n\n/**\n * Every item of a move and its resulting name.\n *\n * A plan is computed for the preview and recomputed inside the write, and the write commits only\n * when the two are identical, so a plan confirmed against state that has since changed is refused\n * rather than applied. Entries are in a canonical order for exactly that comparison.\n */\nexport interface FoldersMovePlan {\n /** Folder everything lands in; absent for the top level. */\n readonly destination?: FolderId;\n readonly entries: readonly FoldersMoveEntry[];\n}\n\n/** One reason a move cannot be planned. */\nexport type FoldersMoveIssue =\n | { readonly kind: \"unknown-destination\"; readonly folder: FolderId }\n | { readonly kind: \"unknown-item\"; readonly item: FoldersItem }\n | {\n readonly kind: \"into-own-descendant\";\n readonly folder: FolderId;\n readonly destination: FolderId;\n };\n\n/** A plan, or everything standing in the way of one. */\nexport type FoldersMovePlanResult =\n | { readonly ok: true; readonly plan: FoldersMovePlan }\n | { readonly ok: false; readonly issues: readonly FoldersMoveIssue[] };\n\n/**\n * Plan a move of any mix of folders, projects and templates into one destination.\n *\n * The only function that decides what a moved item ends up called. The preview and the write both\n * call it, over the same view, so the two cannot drift apart.\n *\n * A folder moves with everything inside it. When the selection holds a folder together with\n * something inside it, that item travels with the folder and is not planned on its own: moving it\n * separately as well would pull it out of the subtree it belongs to.\n */\nexport function planFoldersMove(\n view: FoldersView,\n items: readonly FoldersItem[],\n destination?: FolderId,\n): FoldersMovePlanResult {\n const issues: FoldersMoveIssue[] = [];\n\n const destinationKnown =\n destination === undefined || view.folders.some((folder) => folder.id === destination);\n if (destination !== undefined && !destinationKnown)\n issues.push({ kind: \"unknown-destination\", folder: destination });\n\n const located: Located[] = [];\n const seen = new Set<string>();\n for (const item of items) {\n const key = `${item.kind}:${item.id}`;\n if (seen.has(key)) continue;\n seen.add(key);\n\n const known = locate(view, item);\n if (known === undefined) {\n issues.push({ kind: \"unknown-item\", item });\n continue;\n }\n located.push({ item, ...known });\n }\n\n const carried = new Set<FolderId>(\n located.flatMap(({ item }) => (item.kind === \"folder\" ? foldersSubtree(view, item.id) : [])),\n );\n const moved = located.filter(\n ({ sourceFolder }) => sourceFolder === undefined || !carried.has(sourceFolder),\n );\n\n if (destination !== undefined && destinationKnown)\n for (const { item } of moved) {\n if (item.kind !== \"folder\") continue;\n if (foldersSubtree(view, item.id).includes(destination))\n issues.push({ kind: \"into-own-descendant\", folder: item.id, destination });\n }\n\n if (issues.length > 0) return { ok: false, issues };\n\n const taken = foldersSiblingNames(\n view,\n destination,\n moved.map(({ item }) => item.id),\n );\n\n const entries: FoldersMoveEntry[] = [];\n for (const { item, name, sourceFolder } of moved.sort(byCanonicalOrder)) {\n const resulting = foldersUniqueName(name, taken);\n taken.push(resulting);\n entries.push({\n item,\n currentName: name,\n name: resulting,\n renamed: resulting !== name,\n ...(sourceFolder === undefined ? {} : { sourceFolder }),\n });\n }\n\n return {\n ok: true,\n plan: { ...(destination === undefined ? {} : { destination }), entries },\n };\n}\n\n/** True when two plans say exactly the same thing. */\nexport function foldersMovePlansEqual(a: FoldersMovePlan, b: FoldersMovePlan): boolean {\n if (a.destination !== b.destination) return false;\n if (a.entries.length !== b.entries.length) return false;\n return a.entries.every((entry, index) => {\n const other = b.entries[index];\n if (other === undefined) return false;\n return (\n entry.item.kind === other.item.kind &&\n entry.item.id === other.item.id &&\n entry.currentName === other.currentName &&\n entry.name === other.name &&\n entry.renamed === other.renamed &&\n entry.sourceFolder === other.sourceFolder\n );\n });\n}\n\n/**\n * A project or a template the move gives a new name. Their names live in their own metadata, not\n * in the folder document, so the write path has to carry these out separately; a folder's name is\n * in the document and is renamed there.\n */\nexport interface FoldersLabelRename {\n readonly item: FoldersLeafItem;\n readonly name: string;\n}\n\n/** What applying a plan changes. */\nexport interface FoldersMoveApplication {\n /** The document to persist. */\n readonly document: FoldersDocument;\n /** Projects and templates whose label the move changes. */\n readonly labelRenames: readonly FoldersLabelRename[];\n}\n\n/**\n * The document a plan produces, plus the project and template label renames it implies.\n *\n * Applying a plan is arithmetic on an already validated plan; whether that plan may be committed\n * is the write path's decision.\n */\nexport function applyFoldersMove(\n view: FoldersView,\n movePlan: FoldersMovePlan,\n): FoldersMoveApplication {\n const folderParents = new Map<FolderId, FolderId | undefined>();\n const folderNames = new Map<FolderId, string>();\n const projectFolders = new Map<ProjectId, FolderId | undefined>();\n const templateFolders = new Map<TemplateId, FolderId | undefined>();\n const labelRenames: FoldersLabelRename[] = [];\n\n for (const entry of movePlan.entries) {\n switch (entry.item.kind) {\n case \"folder\":\n folderParents.set(entry.item.id, movePlan.destination);\n folderNames.set(entry.item.id, entry.name);\n break;\n case \"project\":\n projectFolders.set(entry.item.id, movePlan.destination);\n if (entry.renamed) labelRenames.push({ item: entry.item, name: entry.name });\n break;\n case \"template\":\n templateFolders.set(entry.item.id, movePlan.destination);\n if (entry.renamed) labelRenames.push({ item: entry.item, name: entry.name });\n break;\n }\n }\n\n const folders = view.folders.map((folder) => ({\n ...folder,\n parent: folderParents.has(folder.id) ? folderParents.get(folder.id) : folder.parent,\n name: folderNames.get(folder.id) ?? folder.name,\n }));\n\n const projects = view.projects.map((project) => ({\n ...project,\n folder: projectFolders.has(project.id) ? projectFolders.get(project.id) : project.folder,\n }));\n\n const templates = view.templates.map((template) => ({\n ...template,\n folder: templateFolders.has(template.id) ? templateFolders.get(template.id) : template.folder,\n }));\n\n const document = foldersDocumentFromView({ ...view, folders, projects, templates });\n return { document, labelRenames };\n}\n\n//\n// Internals\n//\n\n/** An item of the selection, with what the view says about it. */\ninterface Located {\n readonly item: FoldersItem;\n readonly name: string;\n /** Folder holding the item; absent when it is at the top level. */\n readonly sourceFolder?: FolderId;\n}\n\n/** The name and containing folder of an item the view holds, whichever kind it is. */\nfunction locate(\n view: FoldersView,\n item: FoldersItem,\n): { readonly name: string; readonly sourceFolder?: FolderId } | undefined {\n switch (item.kind) {\n case \"folder\": {\n const folder = view.folders.find((candidate) => candidate.id === item.id);\n return folder === undefined ? undefined : { name: folder.name, sourceFolder: folder.parent };\n }\n case \"project\": {\n const project = view.projects.find((candidate) => candidate.id === item.id);\n return project === undefined\n ? undefined\n : { name: project.name, sourceFolder: project.folder };\n }\n case \"template\": {\n const template = view.templates.find((candidate) => candidate.id === item.id);\n return template === undefined\n ? undefined\n : { name: template.name, sourceFolder: template.folder };\n }\n }\n}\n\n/**\n * Folders, then projects, then templates; within a kind by name, then by id. The order of the\n * items a caller hands in must not change the plan, or a preview and its write could differ over\n * nothing.\n */\nfunction byCanonicalOrder(a: Located, b: Located): number {\n if (a.item.kind !== b.item.kind) return KIND_ORDER[a.item.kind] - KIND_ORDER[b.item.kind];\n const byName = a.name.toLowerCase().localeCompare(b.name.toLowerCase());\n if (byName !== 0) return byName;\n return String(a.item.id).localeCompare(String(b.item.id));\n}\n\nconst KIND_ORDER: Readonly<Record<FoldersItem[\"kind\"], number>> = {\n folder: 0,\n project: 1,\n template: 2,\n};\n"],"mappings":";;;;;;;;;;;;;AAiEA,SAAgB,gBACd,MACA,OACA,aACuB;CACvB,MAAM,SAA6B,CAAC;CAEpC,MAAM,mBACJ,gBAAgB,KAAA,KAAa,KAAK,QAAQ,MAAM,WAAW,OAAO,OAAO,WAAW;CACtF,IAAI,gBAAgB,KAAA,KAAa,CAAC,kBAChC,OAAO,KAAK;EAAE,MAAM;EAAuB,QAAQ;CAAY,CAAC;CAElE,MAAM,UAAqB,CAAC;CAC5B,MAAM,uBAAO,IAAI,IAAY;CAC7B,KAAK,MAAM,QAAQ,OAAO;EACxB,MAAM,MAAM,GAAG,KAAK,KAAK,GAAG,KAAK;EACjC,IAAI,KAAK,IAAI,GAAG,GAAG;EACnB,KAAK,IAAI,GAAG;EAEZ,MAAM,QAAQ,OAAO,MAAM,IAAI;EAC/B,IAAI,UAAU,KAAA,GAAW;GACvB,OAAO,KAAK;IAAE,MAAM;IAAgB;GAAK,CAAC;GAC1C;EACF;EACA,QAAQ,KAAK;GAAE;GAAM,GAAG;EAAM,CAAC;CACjC;CAEA,MAAM,UAAU,IAAI,IAClB,QAAQ,SAAS,EAAE,WAAY,KAAK,SAAS,WAAWA,aAAAA,eAAe,MAAM,KAAK,EAAE,IAAI,CAAC,CAAE,CAC7F;CACA,MAAM,QAAQ,QAAQ,QACnB,EAAE,mBAAmB,iBAAiB,KAAA,KAAa,CAAC,QAAQ,IAAI,YAAY,CAC/E;C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QC,aAAAA,wBAAwB;GAAE,GAAG;GAAM;GAAS;GAAU;EAAU,CACjE;EAAG;CAAa;AAClC;;AAeA,SAAS,OACP,MACA,MACyE;CACzE,QAAQ,KAAK,MAAb;EACE,KAAK,UAAU;GACb,MAAM,SAAS,KAAK,QAAQ,MAAM,cAAc,UAAU,OAAO,KAAK,EAAE;GACxE,OAAO,WAAW,KAAA,IAAY,KAAA,IAAY;IAAE,MAAM,OAAO;IAAM,cAAc,OAAO;GAAO;EAC7F;EACA,KAAK,WAAW;GACd,MAAM,UAAU,KAAK,SAAS,MAAM,cAAc,UAAU,OAAO,KAAK,EAAE;GAC1E,OAAO,YAAY,KAAA,IACf,KAAA,IACA;IAAE,MAAM,QAAQ;IAAM,cAAc,QAAQ;GAAO;EACzD;EACA,KAAK,YAAY;GACf,MAAM,WAAW,KAAK,UAAU,MAAM,cAAc,UAAU,OAAO,KAAK,EAAE;GAC5E,OAAO,aAAa,KAAA,IAChB,KAAA,IACA;IAAE,MAAM,SAAS;IAAM,cAAc,SAAS;GAAO;EAC3D;CACF;AACF;;;;;;AAOA,SAAS,iBAAiB,GAAY,GAAoB;CACxD,IAAI,EAAE,KAAK,SAAS,EAAE,KAAK,MAAM,OAAO,WAAW,EAAE,KAAK,QAAQ,WAAW,EAAE,KAAK;CACpF,MAAM,SAAS,EAAE,KAAK,YAAY,CAAC,CAAC,cAAc,EAAE,KAAK,YAAY,CAAC;CACtE,IAAI,WAAW,GAAG,OAAO;CACzB,OAAO,OAAO,EAAE,KAAK,EAAE,CAAC,CAAC,cAAc,OAAO,EAAE,KAAK,EAAE,CAAC;AAC1D;AAEA,MAAM,aAA4D;CAChE,QAAQ;CACR,SAAS;CACT,UAAU;AACZ"}
1
+ {"version":3,"file":"planner.cjs","names":["foldersSubtree","foldersSiblingNames","foldersUniqueName","foldersDocumentFromView"],"sources":["../../src/folders/planner.ts"],"sourcesContent":["import type { ProjectId, TemplateId } from \"@milaboratories/pl-model-common\";\nimport type { FoldersDocument, FolderId } from \"./document\";\nimport { foldersUniqueName } from \"./naming\";\nimport type { FoldersView } from \"./view\";\nimport { foldersDocumentFromView, foldersSiblingNames, foldersSubtree } from \"./view\";\n\n/** A folder, a project or a stored template, as something a move acts on. */\nexport type FoldersItem =\n | { readonly kind: \"folder\"; readonly id: FolderId }\n | { readonly kind: \"project\"; readonly id: ProjectId }\n | { readonly kind: \"template\"; readonly id: TemplateId };\n\n/** A tree item that is not a folder: a project or a stored template. */\nexport type FoldersLeafItem = Exclude<FoldersItem, { readonly kind: \"folder\" }>;\n\n/** What one item is called after the move, and what it was called before. */\nexport interface FoldersMoveEntry {\n readonly item: FoldersItem;\n readonly currentName: string;\n /** The name the item carries once the plan is applied. */\n readonly name: string;\n /** True when the destination already held that name and the item is being renamed. */\n readonly renamed: boolean;\n /** Folder the item is coming from; absent when it is at the top level. */\n readonly sourceFolder?: FolderId;\n}\n\n/**\n * Every item of a move and its resulting name.\n *\n * A plan is computed for the preview and recomputed inside the write, and the write commits only\n * when the two are identical, so a plan confirmed against state that has since changed is refused\n * rather than applied. Entries are in a canonical order for exactly that comparison.\n */\nexport interface FoldersMovePlan {\n /** Folder everything lands in; absent for the top level. */\n readonly destination?: FolderId;\n readonly entries: readonly FoldersMoveEntry[];\n}\n\n/** One reason a move cannot be planned. */\nexport type FoldersMoveIssue =\n | { readonly kind: \"unknown-destination\"; readonly folder: FolderId }\n | { readonly kind: \"unknown-item\"; readonly item: FoldersItem }\n | {\n readonly kind: \"into-own-descendant\";\n readonly folder: FolderId;\n readonly destination: FolderId;\n };\n\n/** A plan, or everything standing in the way of one. */\nexport type FoldersMovePlanResult =\n | { readonly ok: true; readonly plan: FoldersMovePlan }\n | { readonly ok: false; readonly issues: readonly FoldersMoveIssue[] };\n\n/**\n * Plan a move of any mix of folders, projects and templates into one destination.\n *\n * The only function that decides what a moved item ends up called. The preview and the write both\n * call it, over the same view, so the two cannot drift apart.\n *\n * A folder moves with everything inside it. When the selection holds a folder together with\n * something inside it, that item travels with the folder and is not planned on its own: moving it\n * separately as well would pull it out of the subtree it belongs to.\n */\nexport function planFoldersMove(\n view: FoldersView,\n items: readonly FoldersItem[],\n destination?: FolderId,\n): FoldersMovePlanResult {\n const issues: FoldersMoveIssue[] = [];\n\n const destinationKnown =\n destination === undefined || view.folders.some((folder) => folder.id === destination);\n if (destination !== undefined && !destinationKnown)\n issues.push({ kind: \"unknown-destination\", folder: destination });\n\n const located: Located[] = [];\n const seen = new Set<string>();\n for (const item of items) {\n const key = `${item.kind}:${item.id}`;\n if (seen.has(key)) continue;\n seen.add(key);\n\n const known = locate(view, item);\n if (known === undefined) {\n issues.push({ kind: \"unknown-item\", item });\n continue;\n }\n located.push({ item, ...known });\n }\n\n const carried = new Set<FolderId>(\n located.flatMap(({ item }) => (item.kind === \"folder\" ? foldersSubtree(view, item.id) : [])),\n );\n const moved = located.filter(\n ({ sourceFolder }) => sourceFolder === undefined || !carried.has(sourceFolder),\n );\n\n if (destination !== undefined && destinationKnown)\n for (const { item } of moved) {\n if (item.kind !== \"folder\") continue;\n if (foldersSubtree(view, item.id).includes(destination))\n issues.push({ kind: \"into-own-descendant\", folder: item.id, destination });\n }\n\n if (issues.length > 0) return { ok: false, issues };\n\n // Each kind is named against its own kind only: a moved folder never becomes \"X (Copy)\" for a\n // project called X in the destination.\n const movedIds = moved.map(({ item }) => item.id);\n const taken: Record<FoldersItem[\"kind\"], string[]> = {\n folder: foldersSiblingNames(view, destination, movedIds, { kind: \"folder\" }),\n project: foldersSiblingNames(view, destination, movedIds, { kind: \"project\" }),\n template: foldersSiblingNames(view, destination, movedIds, { kind: \"template\" }),\n };\n\n const entries: FoldersMoveEntry[] = [];\n for (const { item, name, sourceFolder } of moved.sort(byCanonicalOrder)) {\n const resulting = foldersUniqueName(name, taken[item.kind]);\n taken[item.kind].push(resulting);\n entries.push({\n item,\n currentName: name,\n name: resulting,\n renamed: resulting !== name,\n ...(sourceFolder === undefined ? {} : { sourceFolder }),\n });\n }\n\n return {\n ok: true,\n plan: { ...(destination === undefined ? {} : { destination }), entries },\n };\n}\n\n/** True when two plans say exactly the same thing. */\nexport function foldersMovePlansEqual(a: FoldersMovePlan, b: FoldersMovePlan): boolean {\n if (a.destination !== b.destination) return false;\n if (a.entries.length !== b.entries.length) return false;\n return a.entries.every((entry, index) => {\n const other = b.entries[index];\n if (other === undefined) return false;\n return (\n entry.item.kind === other.item.kind &&\n entry.item.id === other.item.id &&\n entry.currentName === other.currentName &&\n entry.name === other.name &&\n entry.renamed === other.renamed &&\n entry.sourceFolder === other.sourceFolder\n );\n });\n}\n\n/**\n * A project or a template the move gives a new name. Their names live in their own metadata, not\n * in the folder document, so the write path has to carry these out separately; a folder's name is\n * in the document and is renamed there.\n */\nexport interface FoldersLabelRename {\n readonly item: FoldersLeafItem;\n readonly name: string;\n}\n\n/** What applying a plan changes. */\nexport interface FoldersMoveApplication {\n /** The document to persist. */\n readonly document: FoldersDocument;\n /** Projects and templates whose label the move changes. */\n readonly labelRenames: readonly FoldersLabelRename[];\n}\n\n/**\n * The document a plan produces, plus the project and template label renames it implies.\n *\n * Applying a plan is arithmetic on an already validated plan; whether that plan may be committed\n * is the write path's decision.\n */\nexport function applyFoldersMove(\n view: FoldersView,\n movePlan: FoldersMovePlan,\n): FoldersMoveApplication {\n const folderParents = new Map<FolderId, FolderId | undefined>();\n const folderNames = new Map<FolderId, string>();\n const projectFolders = new Map<ProjectId, FolderId | undefined>();\n const templateFolders = new Map<TemplateId, FolderId | undefined>();\n const labelRenames: FoldersLabelRename[] = [];\n\n for (const entry of movePlan.entries) {\n switch (entry.item.kind) {\n case \"folder\":\n folderParents.set(entry.item.id, movePlan.destination);\n folderNames.set(entry.item.id, entry.name);\n break;\n case \"project\":\n projectFolders.set(entry.item.id, movePlan.destination);\n if (entry.renamed) labelRenames.push({ item: entry.item, name: entry.name });\n break;\n case \"template\":\n templateFolders.set(entry.item.id, movePlan.destination);\n if (entry.renamed) labelRenames.push({ item: entry.item, name: entry.name });\n break;\n }\n }\n\n const folders = view.folders.map((folder) => ({\n ...folder,\n parent: folderParents.has(folder.id) ? folderParents.get(folder.id) : folder.parent,\n name: folderNames.get(folder.id) ?? folder.name,\n }));\n\n const projects = view.projects.map((project) => ({\n ...project,\n folder: projectFolders.has(project.id) ? projectFolders.get(project.id) : project.folder,\n }));\n\n const templates = view.templates.map((template) => ({\n ...template,\n folder: templateFolders.has(template.id) ? templateFolders.get(template.id) : template.folder,\n }));\n\n const document = foldersDocumentFromView({ ...view, folders, projects, templates });\n return { document, labelRenames };\n}\n\n//\n// Internals\n//\n\n/** An item of the selection, with what the view says about it. */\ninterface Located {\n readonly item: FoldersItem;\n readonly name: string;\n /** Folder holding the item; absent when it is at the top level. */\n readonly sourceFolder?: FolderId;\n}\n\n/** The name and containing folder of an item the view holds, whichever kind it is. */\nfunction locate(\n view: FoldersView,\n item: FoldersItem,\n): { readonly name: string; readonly sourceFolder?: FolderId } | undefined {\n switch (item.kind) {\n case \"folder\": {\n const folder = view.folders.find((candidate) => candidate.id === item.id);\n return folder === undefined ? undefined : { name: folder.name, sourceFolder: folder.parent };\n }\n case \"project\": {\n const project = view.projects.find((candidate) => candidate.id === item.id);\n return project === undefined\n ? undefined\n : { name: project.name, sourceFolder: project.folder };\n }\n case \"template\": {\n const template = view.templates.find((candidate) => candidate.id === item.id);\n return template === undefined\n ? undefined\n : { name: template.name, sourceFolder: template.folder };\n }\n }\n}\n\n/**\n * Folders, then projects, then templates; within a kind by name, then by id. The order of the\n * items a caller hands in must not change the plan, or a preview and its write could differ over\n * nothing.\n */\nfunction byCanonicalOrder(a: Located, b: Located): number {\n if (a.item.kind !== b.item.kind) return KIND_ORDER[a.item.kind] - KIND_ORDER[b.item.kind];\n const byName = a.name.toLowerCase().localeCompare(b.name.toLowerCase());\n if (byName !== 0) return byName;\n return String(a.item.id).localeCompare(String(b.item.id));\n}\n\nconst KIND_ORDER: Readonly<Record<FoldersItem[\"kind\"], number>> = {\n folder: 0,\n project: 1,\n template: 2,\n};\n"],"m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B;WACG;WAAsC,QAAQ;;WAC9C;WAA+B,MAAM;;WAErC;WACA,QAAQ;WACR,aAAa;;;YAIhB;WACG;WAAmB,MAAM;;WACzB;WAAoB,iBAAiB;;;;;;;;;;;;wBAYpC,gBACd,MAAM,aACN,gBAAgB,eAChB,cAAc,WACb;;wBAiEa,sBAAsB,GAAG,iBAAiB,GAAG;;;;;;iBAsB5C;WACN,MAAM;WACN;;;iBAIM;;WAEN,UAAU;;WAEV,uBAAuB;;;;;;;;wBASlB,iBACd,MAAM,aACN,UAAU,kBACT"}
1
+ {"version":3,"file":"planner.d.ts","names":[],"sources":["../../src/folders/planner.ts"],"mappings":";;;;;YAOY;WACG;WAAyB,IAAI;;WAC7B;WAA0B,IAAI;;WAC9B;WAA2B,IAAI;;;YAGlC,kBAAkB,QAAQ;WAAwB;;;iBAG7C;WACN,MAAM;WACN;;WAEA;;WAEA;;WAEA,eAAe;;;;;;;;;iBAUT;;WAEN,cAAc;WACd,kBAAkB;;;YAIjB;WACG;WAAsC,QAAQ;;WAC9C;WAA+B,MAAM;;WAErC;WACA,QAAQ;WACR,aAAa;;;YAIhB;WACG;WAAmB,MAAM;;WACzB;WAAoB,iBAAiB;;;;;;;;;;;;wBAYpC,gBACd,MAAM,aACN,gBAAgB,eAChB,cAAc,WACb;;wBAoEa,sBAAsB,GAAG,iBAAiB,GAAG;;;;;;iBAsB5C;WACN,MAAM;WACN;;;iBAIM;;WAEN,UAAU;;WAEV,uBAAuB;;;;;;;;wBASlB,iBACd,MAAM,aACN,UAAU,kBACT"}
@@ -51,11 +51,16 @@ function planFoldersMove(view, items, destination) {
51
51
  ok: false,
52
52
  issues
53
53
  };
54
- const taken = foldersSiblingNames(view, destination, moved.map(({ item }) => item.id));
54
+ const movedIds = moved.map(({ item }) => item.id);
55
+ const taken = {
56
+ folder: foldersSiblingNames(view, destination, movedIds, { kind: "folder" }),
57
+ project: foldersSiblingNames(view, destination, movedIds, { kind: "project" }),
58
+ template: foldersSiblingNames(view, destination, movedIds, { kind: "template" })
59
+ };
55
60
  const entries = [];
56
61
  for (const { item, name, sourceFolder } of moved.sort(byCanonicalOrder)) {
57
- const resulting = foldersUniqueName(name, taken);
58
- taken.push(resulting);
62
+ const resulting = foldersUniqueName(name, taken[item.kind]);
63
+ taken[item.kind].push(resulting);
59
64
  entries.push({
60
65
  item,
61
66
  currentName: name,