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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (87) hide show
  1. package/dist/folders/commit.cjs +79 -0
  2. package/dist/folders/commit.cjs.map +1 -0
  3. package/dist/folders/commit.d.ts +82 -0
  4. package/dist/folders/commit.d.ts.map +1 -0
  5. package/dist/folders/commit.js +78 -0
  6. package/dist/folders/commit.js.map +1 -0
  7. package/dist/folders/decode.cjs +79 -0
  8. package/dist/folders/decode.cjs.map +1 -0
  9. package/dist/folders/decode.d.ts +57 -0
  10. package/dist/folders/decode.d.ts.map +1 -0
  11. package/dist/folders/decode.js +77 -0
  12. package/dist/folders/decode.js.map +1 -0
  13. package/dist/folders/document.cjs +49 -0
  14. package/dist/folders/document.cjs.map +1 -0
  15. package/dist/folders/document.d.ts +56 -0
  16. package/dist/folders/document.d.ts.map +1 -0
  17. package/dist/folders/document.js +45 -0
  18. package/dist/folders/document.js.map +1 -0
  19. package/dist/folders/index.cjs +37 -0
  20. package/dist/folders/index.d.ts +10 -0
  21. package/dist/folders/index.js +10 -0
  22. package/dist/folders/inheritance.cjs +23 -0
  23. package/dist/folders/inheritance.cjs.map +1 -0
  24. package/dist/folders/inheritance.d.ts +19 -0
  25. package/dist/folders/inheritance.d.ts.map +1 -0
  26. package/dist/folders/inheritance.js +23 -0
  27. package/dist/folders/inheritance.js.map +1 -0
  28. package/dist/folders/invariants.cjs +115 -0
  29. package/dist/folders/invariants.cjs.map +1 -0
  30. package/dist/folders/invariants.d.ts +48 -0
  31. package/dist/folders/invariants.d.ts.map +1 -0
  32. package/dist/folders/invariants.js +114 -0
  33. package/dist/folders/invariants.js.map +1 -0
  34. package/dist/folders/naming.cjs +53 -0
  35. package/dist/folders/naming.cjs.map +1 -0
  36. package/dist/folders/naming.d.ts +31 -0
  37. package/dist/folders/naming.d.ts.map +1 -0
  38. package/dist/folders/naming.js +51 -0
  39. package/dist/folders/naming.js.map +1 -0
  40. package/dist/folders/planner.cjs +187 -0
  41. package/dist/folders/planner.cjs.map +1 -0
  42. package/dist/folders/planner.d.ts +100 -0
  43. package/dist/folders/planner.d.ts.map +1 -0
  44. package/dist/folders/planner.js +185 -0
  45. package/dist/folders/planner.js.map +1 -0
  46. package/dist/folders/removal.cjs +47 -0
  47. package/dist/folders/removal.cjs.map +1 -0
  48. package/dist/folders/removal.d.ts +42 -0
  49. package/dist/folders/removal.d.ts.map +1 -0
  50. package/dist/folders/removal.js +45 -0
  51. package/dist/folders/removal.js.map +1 -0
  52. package/dist/folders/view.cjs +210 -0
  53. package/dist/folders/view.cjs.map +1 -0
  54. package/dist/folders/view.d.ts +93 -0
  55. package/dist/folders/view.d.ts.map +1 -0
  56. package/dist/folders/view.js +205 -0
  57. package/dist/folders/view.js.map +1 -0
  58. package/dist/index.cjs +43 -2
  59. package/dist/index.d.ts +12 -2
  60. package/dist/index.js +12 -1
  61. package/dist/project.cjs +23 -0
  62. package/dist/project.cjs.map +1 -0
  63. package/dist/project.d.ts +13 -0
  64. package/dist/project.d.ts.map +1 -1
  65. package/dist/project.js +22 -0
  66. package/dist/project.js.map +1 -0
  67. package/package.json +7 -4
  68. package/src/folders/commit.ts +133 -0
  69. package/src/folders/decode.test.ts +128 -0
  70. package/src/folders/decode.ts +142 -0
  71. package/src/folders/document.ts +96 -0
  72. package/src/folders/index.ts +9 -0
  73. package/src/folders/inheritance.test.ts +86 -0
  74. package/src/folders/inheritance.ts +32 -0
  75. package/src/folders/invariants.test.ts +157 -0
  76. package/src/folders/invariants.ts +155 -0
  77. package/src/folders/naming.test.ts +62 -0
  78. package/src/folders/naming.ts +58 -0
  79. package/src/folders/planner.test.ts +436 -0
  80. package/src/folders/planner.ts +276 -0
  81. package/src/folders/removal.test.ts +142 -0
  82. package/src/folders/removal.ts +77 -0
  83. package/src/folders/test_utils.ts +86 -0
  84. package/src/folders/view.test.ts +422 -0
  85. package/src/folders/view.ts +324 -0
  86. package/src/index.ts +1 -0
  87. package/src/project.ts +21 -0
@@ -0,0 +1,45 @@
1
+ import { z } from "zod";
2
+ //#region src/folders/document.ts
3
+ /**
4
+ * Schema version this build writes and fully understands.
5
+ *
6
+ * A document carrying a higher version is not readable here, and folder writes are refused rather
7
+ * than allowed to truncate whatever the newer build stored.
8
+ */
9
+ const FOLDERS_SCHEMA_VERSION = 1;
10
+ /** Brands a string already known to be a folder id, such as one handed back by a caller. */
11
+ function asFolderId(id) {
12
+ return id;
13
+ }
14
+ const FolderEntry = z.object({
15
+ id: folderId(),
16
+ name: z.string().min(1),
17
+ parent: folderId().optional(),
18
+ description: z.string().optional()
19
+ }).strict();
20
+ const FoldersDocument = z.object({
21
+ schemaVersion: z.literal(1),
22
+ folders: z.array(FolderEntry),
23
+ assignments: z.record(z.string().min(1), folderId()),
24
+ templateAssignments: z.record(z.string().min(1), folderId())
25
+ }).strict();
26
+ /** The document of a user who has never made a folder. */
27
+ function emptyFoldersDocument() {
28
+ return {
29
+ schemaVersion: 1,
30
+ folders: [],
31
+ assignments: {},
32
+ templateAssignments: {}
33
+ };
34
+ }
35
+ /**
36
+ * Folder ids are plain strings on the wire; the brand exists so that a project id and a folder id
37
+ * cannot be swapped by accident in the code consuming them.
38
+ */
39
+ function folderId() {
40
+ return z.string().min(1).transform(asFolderId);
41
+ }
42
+ //#endregion
43
+ export { FOLDERS_SCHEMA_VERSION, FolderEntry, FoldersDocument, asFolderId, emptyFoldersDocument };
44
+
45
+ //# sourceMappingURL=document.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"document.js","names":[],"sources":["../../src/folders/document.ts"],"sourcesContent":["import type { Branded } from \"@milaboratories/pl-model-common\";\nimport { z } from \"zod\";\n\n/**\n * Schema version this build writes and fully understands.\n *\n * A document carrying a higher version is not readable here, and folder writes are refused rather\n * than allowed to truncate whatever the newer build stored.\n */\nexport const FOLDERS_SCHEMA_VERSION = 1;\n\n/** Identifier of a folder, unique within one user's folder document. */\nexport type FolderId = Branded<string, \"FolderId\">;\n\n/** Brands a string already known to be a folder id, such as one handed back by a caller. */\nexport function asFolderId(id: string): FolderId {\n return id as FolderId;\n}\n\n/**\n * A folder as it is persisted: a name, a reference to the folder that contains it, and what the\n * user wrote about it.\n *\n * A folder nobody described carries no description at all rather than an empty one, so clearing\n * a description leaves the same document a folder that never had one has.\n */\nexport interface FolderEntry {\n readonly id: FolderId;\n readonly name: string;\n /** Absent for a top-level folder. */\n readonly parent?: FolderId;\n /** Free text the user wrote about the folder. Absent when there is none. */\n readonly description?: string;\n}\n\nexport const FolderEntry: z.ZodType<FolderEntry, z.ZodTypeDef, unknown> = z\n .object({\n id: folderId(),\n name: z.string().min(1),\n parent: folderId().optional(),\n description: z.string().optional(),\n })\n .strict();\n\n/**\n * The whole folder tree of one user, as one persisted document.\n *\n * Projects and templates live in the same tree, so one folder holds both, and each has its own\n * assignment map. They are separate maps rather than one because the two ids are looked up in\n * two different lists: merged, an id present in neither list could not be told apart from a\n * project that was deleted, and the dead-entry pruning a read performs would have nothing to go\n * on.\n *\n * Each map points from the item to the folder holding it, not the other way round: an item lives\n * in at most one folder, so one entry per item can never contradict itself, and an item the map\n * does not mention needs no entry at all — it is at the top level.\n */\nexport interface FoldersDocument {\n readonly schemaVersion: typeof FOLDERS_SCHEMA_VERSION;\n readonly folders: readonly FolderEntry[];\n /** Project id to the folder holding it. Projects at the top level are absent. */\n readonly assignments: Readonly<Record<string, FolderId>>;\n /** Template id to the folder holding it. Templates at the top level are absent. */\n readonly templateAssignments: Readonly<Record<string, FolderId>>;\n}\n\nexport const FoldersDocument: z.ZodType<FoldersDocument, z.ZodTypeDef, unknown> = z\n .object({\n schemaVersion: z.literal(FOLDERS_SCHEMA_VERSION),\n folders: z.array(FolderEntry),\n assignments: z.record(z.string().min(1), folderId()),\n templateAssignments: z.record(z.string().min(1), folderId()),\n })\n .strict();\n\n/** The document of a user who has never made a folder. */\nexport function emptyFoldersDocument(): FoldersDocument {\n return {\n schemaVersion: FOLDERS_SCHEMA_VERSION,\n folders: [],\n assignments: {},\n templateAssignments: {},\n };\n}\n\n//\n// Internals\n//\n\n/**\n * Folder ids are plain strings on the wire; the brand exists so that a project id and a folder id\n * cannot be swapped by accident in the code consuming them.\n */\nfunction folderId() {\n return z.string().min(1).transform(asFolderId);\n}\n"],"mappings":";;;;;;;;AASA,MAAa,yBAAyB;;AAMtC,SAAgB,WAAW,IAAsB;CAC/C,OAAO;AACT;AAkBA,MAAa,cAA6D,EACvE,OAAO;CACN,IAAI,SAAS;CACb,MAAM,EAAE,OAAO,CAAC,CAAC,IAAI,CAAC;CACtB,QAAQ,SAAS,CAAC,CAAC,SAAS;CAC5B,aAAa,EAAE,OAAO,CAAC,CAAC,SAAS;AACnC,CAAC,CAAC,CACD,OAAO;AAwBV,MAAa,kBAAqE,EAC/E,OAAO;CACN,eAAe,EAAE,QAAA,CAA8B;CAC/C,SAAS,EAAE,MAAM,WAAW;CAC5B,aAAa,EAAE,OAAO,EAAE,OAAO,CAAC,CAAC,IAAI,CAAC,GAAG,SAAS,CAAC;CACnD,qBAAqB,EAAE,OAAO,EAAE,OAAO,CAAC,CAAC,IAAI,CAAC,GAAG,SAAS,CAAC;AAC7D,CAAC,CAAC,CACD,OAAO;;AAGV,SAAgB,uBAAwC;CACtD,OAAO;EACL,eAAA;EACA,SAAS,CAAC;EACV,aAAa,CAAC;EACd,qBAAqB,CAAC;CACxB;AACF;;;;;AAUA,SAAS,WAAW;CAClB,OAAO,EAAE,OAAO,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,UAAU,UAAU;AAC/C"}
@@ -0,0 +1,37 @@
1
+ const require_document = require("./document.cjs");
2
+ const require_decode = require("./decode.cjs");
3
+ const require_view = require("./view.cjs");
4
+ const require_naming = require("./naming.cjs");
5
+ const require_invariants = require("./invariants.cjs");
6
+ const require_planner = require("./planner.cjs");
7
+ const require_removal = require("./removal.cjs");
8
+ const require_commit = require("./commit.cjs");
9
+ const require_inheritance = require("./inheritance.cjs");
10
+ exports.FOLDERS_SCHEMA_VERSION = require_document.FOLDERS_SCHEMA_VERSION;
11
+ exports.FolderEntry = require_document.FolderEntry;
12
+ exports.FoldersDocument = require_document.FoldersDocument;
13
+ exports.applyFoldersMove = require_planner.applyFoldersMove;
14
+ exports.applyFoldersRemoval = require_removal.applyFoldersRemoval;
15
+ exports.asFolderId = require_document.asFolderId;
16
+ exports.commitFoldersMove = require_commit.commitFoldersMove;
17
+ exports.commitFoldersRemoval = require_commit.commitFoldersRemoval;
18
+ exports.decodeFoldersDocument = require_decode.decodeFoldersDocument;
19
+ exports.decodeStoredFoldersDocument = require_decode.decodeStoredFoldersDocument;
20
+ exports.emptyFoldersDocument = require_document.emptyFoldersDocument;
21
+ exports.encodeFoldersDocument = require_decode.encodeFoldersDocument;
22
+ exports.foldersChildren = require_view.foldersChildren;
23
+ exports.foldersDocumentFromView = require_view.foldersDocumentFromView;
24
+ exports.foldersMovePlansEqual = require_planner.foldersMovePlansEqual;
25
+ exports.foldersNameBlank = require_naming.foldersNameBlank;
26
+ exports.foldersNameTaken = require_naming.foldersNameTaken;
27
+ exports.foldersRemovalsEqual = require_removal.foldersRemovalsEqual;
28
+ exports.foldersSiblingNames = require_view.foldersSiblingNames;
29
+ exports.foldersSubtree = require_view.foldersSubtree;
30
+ exports.foldersUniqueName = require_naming.foldersUniqueName;
31
+ exports.foldersViewFromDocument = require_view.foldersViewFromDocument;
32
+ exports.formatFoldersViolation = require_invariants.formatFoldersViolation;
33
+ exports.healFolders = require_view.healFolders;
34
+ exports.inheritedFolder = require_inheritance.inheritedFolder;
35
+ exports.planFoldersMove = require_planner.planFoldersMove;
36
+ exports.planFoldersRemoval = require_removal.planFoldersRemoval;
37
+ exports.validateFoldersDocument = require_invariants.validateFoldersDocument;
@@ -0,0 +1,10 @@
1
+ import { FOLDERS_SCHEMA_VERSION, FolderEntry, FolderId, FoldersDocument, asFolderId, emptyFoldersDocument } from "./document.js";
2
+ import { FoldersDecoded, FoldersDocumentProblem, FoldersStoredDocument, decodeFoldersDocument, decodeStoredFoldersDocument, encodeFoldersDocument } from "./decode.js";
3
+ import { FolderView, FoldersItemInput, FoldersItemView, FoldersPlacement, FoldersProjectInput, FoldersProjectView, FoldersTemplateInput, FoldersTemplateView, FoldersView, foldersChildren, foldersDocumentFromView, foldersSiblingNames, foldersSubtree, foldersViewFromDocument, healFolders } from "./view.js";
4
+ import { FoldersViolation, formatFoldersViolation, validateFoldersDocument } from "./invariants.js";
5
+ import { foldersNameBlank, foldersNameTaken, foldersUniqueName } from "./naming.js";
6
+ import { FoldersItem, FoldersLabelRename, FoldersLeafItem, FoldersMoveApplication, FoldersMoveEntry, FoldersMoveIssue, FoldersMovePlan, FoldersMovePlanResult, applyFoldersMove, foldersMovePlansEqual, planFoldersMove } from "./planner.js";
7
+ import { FoldersRemoval, FoldersRemovalIssue, FoldersRemovalPlanResult, applyFoldersRemoval, foldersRemovalsEqual, planFoldersRemoval } from "./removal.js";
8
+ import { FoldersEdit, FoldersMoveOutcome, FoldersRemovalOutcome, commitFoldersMove, commitFoldersRemoval } from "./commit.js";
9
+ import { inheritedFolder } from "./inheritance.js";
10
+ export { FOLDERS_SCHEMA_VERSION, FolderEntry, FolderId, FolderView, FoldersDecoded, FoldersDocument, FoldersDocumentProblem, FoldersEdit, FoldersItem, FoldersItemInput, FoldersItemView, FoldersLabelRename, FoldersLeafItem, FoldersMoveApplication, FoldersMoveEntry, FoldersMoveIssue, FoldersMoveOutcome, FoldersMovePlan, FoldersMovePlanResult, FoldersPlacement, FoldersProjectInput, FoldersProjectView, FoldersRemoval, FoldersRemovalIssue, FoldersRemovalOutcome, FoldersRemovalPlanResult, FoldersStoredDocument, FoldersTemplateInput, FoldersTemplateView, FoldersView, FoldersViolation, applyFoldersMove, applyFoldersRemoval, asFolderId, commitFoldersMove, commitFoldersRemoval, decodeFoldersDocument, decodeStoredFoldersDocument, emptyFoldersDocument, encodeFoldersDocument, foldersChildren, foldersDocumentFromView, foldersMovePlansEqual, foldersNameBlank, foldersNameTaken, foldersRemovalsEqual, foldersSiblingNames, foldersSubtree, foldersUniqueName, foldersViewFromDocument, formatFoldersViolation, healFolders, inheritedFolder, planFoldersMove, planFoldersRemoval, validateFoldersDocument };
@@ -0,0 +1,10 @@
1
+ import { FOLDERS_SCHEMA_VERSION, FolderEntry, FoldersDocument, asFolderId, emptyFoldersDocument } from "./document.js";
2
+ import { decodeFoldersDocument, decodeStoredFoldersDocument, encodeFoldersDocument } from "./decode.js";
3
+ import { foldersChildren, foldersDocumentFromView, foldersSiblingNames, foldersSubtree, foldersViewFromDocument, healFolders } from "./view.js";
4
+ import { foldersNameBlank, foldersNameTaken, foldersUniqueName } from "./naming.js";
5
+ import { formatFoldersViolation, validateFoldersDocument } from "./invariants.js";
6
+ import { applyFoldersMove, foldersMovePlansEqual, planFoldersMove } from "./planner.js";
7
+ import { applyFoldersRemoval, foldersRemovalsEqual, planFoldersRemoval } from "./removal.js";
8
+ import { commitFoldersMove, commitFoldersRemoval } from "./commit.js";
9
+ import { inheritedFolder } from "./inheritance.js";
10
+ export { FOLDERS_SCHEMA_VERSION, FolderEntry, FoldersDocument, applyFoldersMove, applyFoldersRemoval, asFolderId, commitFoldersMove, commitFoldersRemoval, decodeFoldersDocument, decodeStoredFoldersDocument, emptyFoldersDocument, encodeFoldersDocument, foldersChildren, foldersDocumentFromView, foldersMovePlansEqual, foldersNameBlank, foldersNameTaken, foldersRemovalsEqual, foldersSiblingNames, foldersSubtree, foldersUniqueName, foldersViewFromDocument, formatFoldersViolation, healFolders, inheritedFolder, planFoldersMove, planFoldersRemoval, validateFoldersDocument };
@@ -0,0 +1,23 @@
1
+ const require_view = require("./view.cjs");
2
+ const require_naming = require("./naming.cjs");
3
+ //#region src/folders/inheritance.ts
4
+ /**
5
+ * The folder a newly created item should land in, given the project it was made from.
6
+ *
7
+ * A copy of a project, and a template taken from one, belong beside their source; landing at the
8
+ * top level reads as the app having lost them. `undefined` means leave it at the top level:
9
+ *
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.
14
+ */
15
+ function inheritedFolder(view, source, created, name) {
16
+ const folder = view.projects.find((candidate) => candidate.id === source)?.folder;
17
+ if (folder === void 0) return void 0;
18
+ return require_naming.foldersNameTaken(name, require_view.foldersSiblingNames(view, folder, [created.id])) ? void 0 : folder;
19
+ }
20
+ //#endregion
21
+ exports.inheritedFolder = inheritedFolder;
22
+
23
+ //# sourceMappingURL=inheritance.cjs.map
@@ -0,0 +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"}
@@ -0,0 +1,19 @@
1
+ import { FolderId } from "./document.js";
2
+ import { FoldersView } from "./view.js";
3
+ import { FoldersLeafItem } from "./planner.js";
4
+ import { ProjectId } from "@milaboratories/pl-model-common";
5
+ //#region src/folders/inheritance.d.ts
6
+ /**
7
+ * The folder a newly created item should land in, given the project it was made from.
8
+ *
9
+ * A copy of a project, and a template taken from one, belong beside their source; landing at the
10
+ * top level reads as the app having lost them. `undefined` means leave it at the top level:
11
+ *
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.
16
+ */
17
+ export declare function inheritedFolder(view: FoldersView, source: ProjectId, created: FoldersLeafItem, name: string): FolderId | undefined;
18
+ //#endregion
19
+ //# sourceMappingURL=inheritance.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"inheritance.d.ts","names":[],"sources":["../../src/folders/inheritance.ts"],"mappings":";;;;;;;;;;;;;;;;wBAkBgB,gBACd,MAAM,aACN,QAAQ,WACR,SAAS,iBACT,eACC"}
@@ -0,0 +1,23 @@
1
+ import { foldersSiblingNames } from "./view.js";
2
+ import { foldersNameTaken } from "./naming.js";
3
+ //#region src/folders/inheritance.ts
4
+ /**
5
+ * The folder a newly created item should land in, given the project it was made from.
6
+ *
7
+ * A copy of a project, and a template taken from one, belong beside their source; landing at the
8
+ * top level reads as the app having lost them. `undefined` means leave it at the top level:
9
+ *
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.
14
+ */
15
+ function inheritedFolder(view, source, created, name) {
16
+ const folder = view.projects.find((candidate) => candidate.id === source)?.folder;
17
+ if (folder === void 0) return void 0;
18
+ return foldersNameTaken(name, foldersSiblingNames(view, folder, [created.id])) ? void 0 : folder;
19
+ }
20
+ //#endregion
21
+ export { inheritedFolder };
22
+
23
+ //# sourceMappingURL=inheritance.js.map
@@ -0,0 +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"}
@@ -0,0 +1,115 @@
1
+ const require_naming = require("./naming.cjs");
2
+ let _milaboratories_pl_model_common = require("@milaboratories/pl-model-common");
3
+ //#region src/folders/invariants.ts
4
+ /**
5
+ * Everything wrong with a document, in one pass.
6
+ *
7
+ * This is the write path's check: a read heals the same problems instead of reporting them, but a
8
+ * write that would persist any of them is refused, because a document no reader can render as
9
+ * stored is a document someone has to repair by hand.
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
14
+ * prunes — from one naming a folder that does not exist, which is a broken document.
15
+ */
16
+ function validateFoldersDocument(document, projects, templates) {
17
+ const violations = [];
18
+ const byId = /* @__PURE__ */ new Map();
19
+ for (const folder of document.folders) {
20
+ if (byId.has(folder.id)) {
21
+ violations.push({
22
+ kind: "duplicate-folder-id",
23
+ folder: folder.id
24
+ });
25
+ continue;
26
+ }
27
+ if (require_naming.foldersNameBlank(folder.name)) violations.push({
28
+ kind: "empty-folder-name",
29
+ folder: folder.id
30
+ });
31
+ byId.set(folder.id, {
32
+ name: folder.name,
33
+ parent: folder.parent
34
+ });
35
+ }
36
+ for (const [id, folder] of byId) {
37
+ if (folder.parent !== void 0 && !byId.has(folder.parent)) {
38
+ violations.push({
39
+ kind: "missing-parent",
40
+ folder: id,
41
+ parent: folder.parent
42
+ });
43
+ continue;
44
+ }
45
+ if (onCycle(id, byId)) violations.push({
46
+ kind: "cycle",
47
+ folder: id
48
+ });
49
+ }
50
+ const namesByParent = /* @__PURE__ */ new Map();
51
+ const claim = (parent, name) => {
52
+ const key = parent ?? "";
53
+ const names = namesByParent.get(key) ?? [];
54
+ namesByParent.set(key, names);
55
+ if (require_naming.foldersNameTaken(name, names)) violations.push({
56
+ kind: "duplicate-name",
57
+ parent,
58
+ name
59
+ });
60
+ names.push(name);
61
+ };
62
+ for (const folder of byId.values()) claim(folder.parent, folder.name);
63
+ const knownProjects = new Set((projects ?? []).map((project) => project.id));
64
+ for (const project of projects ?? []) {
65
+ const parent = document.assignments[project.id];
66
+ if (parent === void 0 || byId.has(parent)) claim(parent, project.name);
67
+ }
68
+ for (const [project, folder] of Object.entries(document.assignments)) if (!byId.has(folder) && (projects === void 0 || knownProjects.has(project))) violations.push({
69
+ kind: "unknown-assignment-folder",
70
+ project: (0, _milaboratories_pl_model_common.asProjectId)(project),
71
+ folder
72
+ });
73
+ for (const template of templates ?? []) {
74
+ const parent = document.templateAssignments[template.id];
75
+ if (parent === void 0 || byId.has(parent)) claim(parent, template.name);
76
+ }
77
+ const knownTemplates = new Set((templates ?? []).map((template) => template.id));
78
+ for (const [template, folder] of Object.entries(document.templateAssignments)) if (!byId.has(folder) && (templates === void 0 || knownTemplates.has(template))) violations.push({
79
+ kind: "unknown-template-assignment-folder",
80
+ template: (0, _milaboratories_pl_model_common.asTemplateId)(template),
81
+ folder
82
+ });
83
+ return violations;
84
+ }
85
+ /** A one-line description of a violation, for an error a user or a log will read. */
86
+ function formatFoldersViolation(violation) {
87
+ switch (violation.kind) {
88
+ case "duplicate-folder-id": return `folder id ${violation.folder} appears more than once`;
89
+ case "empty-folder-name": return `folder ${violation.folder} has an empty name`;
90
+ case "missing-parent": return `folder ${violation.folder} names a parent that does not exist (${violation.parent})`;
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}`;
93
+ case "unknown-assignment-folder": return `project ${violation.project} is assigned to a folder that does not exist (${violation.folder})`;
94
+ case "unknown-template-assignment-folder": return `template ${violation.template} is assigned to a folder that does not exist (${violation.folder})`;
95
+ }
96
+ }
97
+ /**
98
+ * True when following parents from this folder leads back to it. A folder that merely hangs below
99
+ * a cycle is not on it.
100
+ */
101
+ function onCycle(id, byId) {
102
+ const seen = /* @__PURE__ */ new Set();
103
+ let current = byId.get(id)?.parent;
104
+ while (current !== void 0 && !seen.has(current)) {
105
+ if (current === id) return true;
106
+ seen.add(current);
107
+ current = byId.get(current)?.parent;
108
+ }
109
+ return false;
110
+ }
111
+ //#endregion
112
+ exports.formatFoldersViolation = formatFoldersViolation;
113
+ exports.validateFoldersDocument = validateFoldersDocument;
114
+
115
+ //# sourceMappingURL=invariants.cjs.map
@@ -0,0 +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;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AK,IAAI,OAAO,CAAC,EAAE;CAC/B;CACA,OAAO;AACT"}
@@ -0,0 +1,48 @@
1
+ import { FolderId, FoldersDocument } from "./document.js";
2
+ import { FoldersProjectInput, FoldersTemplateInput } from "./view.js";
3
+ import { ProjectId, TemplateId } from "@milaboratories/pl-model-common";
4
+ //#region src/folders/invariants.d.ts
5
+ /** One way a document breaks the rules the write path enforces. */
6
+ export type FoldersViolation = {
7
+ readonly kind: "duplicate-folder-id";
8
+ readonly folder: FolderId;
9
+ } | {
10
+ readonly kind: "empty-folder-name";
11
+ readonly folder: FolderId;
12
+ } | {
13
+ readonly kind: "missing-parent";
14
+ readonly folder: FolderId;
15
+ readonly parent: FolderId;
16
+ } | {
17
+ readonly kind: "cycle";
18
+ readonly folder: FolderId;
19
+ } | {
20
+ readonly kind: "duplicate-name";
21
+ readonly parent?: FolderId;
22
+ readonly name: string;
23
+ } | {
24
+ readonly kind: "unknown-assignment-folder";
25
+ readonly project: ProjectId;
26
+ readonly folder: FolderId;
27
+ } | {
28
+ readonly kind: "unknown-template-assignment-folder";
29
+ readonly template: TemplateId;
30
+ readonly folder: FolderId;
31
+ };
32
+ /**
33
+ * Everything wrong with a document, in one pass.
34
+ *
35
+ * This is the write path's check: a read heals the same problems instead of reporting them, but a
36
+ * write that would persist any of them is refused, because a document no reader can render as
37
+ * stored is a document someone has to repair by hand.
38
+ *
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
+ * prunes — from one naming a folder that does not exist, which is a broken document.
43
+ */
44
+ export declare function validateFoldersDocument(document: FoldersDocument, projects?: readonly FoldersProjectInput[], templates?: readonly FoldersTemplateInput[]): readonly FoldersViolation[];
45
+ /** A one-line description of a violation, for an error a user or a log will read. */
46
+ export declare function formatFoldersViolation(violation: FoldersViolation): string;
47
+ //#endregion
48
+ //# sourceMappingURL=invariants.d.ts.map
@@ -0,0 +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"}
@@ -0,0 +1,114 @@
1
+ import { foldersNameBlank, foldersNameTaken } from "./naming.js";
2
+ import { asProjectId, asTemplateId } from "@milaboratories/pl-model-common";
3
+ //#region src/folders/invariants.ts
4
+ /**
5
+ * Everything wrong with a document, in one pass.
6
+ *
7
+ * This is the write path's check: a read heals the same problems instead of reporting them, but a
8
+ * write that would persist any of them is refused, because a document no reader can render as
9
+ * stored is a document someone has to repair by hand.
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
14
+ * prunes — from one naming a folder that does not exist, which is a broken document.
15
+ */
16
+ function validateFoldersDocument(document, projects, templates) {
17
+ const violations = [];
18
+ const byId = /* @__PURE__ */ new Map();
19
+ for (const folder of document.folders) {
20
+ if (byId.has(folder.id)) {
21
+ violations.push({
22
+ kind: "duplicate-folder-id",
23
+ folder: folder.id
24
+ });
25
+ continue;
26
+ }
27
+ if (foldersNameBlank(folder.name)) violations.push({
28
+ kind: "empty-folder-name",
29
+ folder: folder.id
30
+ });
31
+ byId.set(folder.id, {
32
+ name: folder.name,
33
+ parent: folder.parent
34
+ });
35
+ }
36
+ for (const [id, folder] of byId) {
37
+ if (folder.parent !== void 0 && !byId.has(folder.parent)) {
38
+ violations.push({
39
+ kind: "missing-parent",
40
+ folder: id,
41
+ parent: folder.parent
42
+ });
43
+ continue;
44
+ }
45
+ if (onCycle(id, byId)) violations.push({
46
+ kind: "cycle",
47
+ folder: id
48
+ });
49
+ }
50
+ const namesByParent = /* @__PURE__ */ new Map();
51
+ const claim = (parent, name) => {
52
+ const key = parent ?? "";
53
+ const names = namesByParent.get(key) ?? [];
54
+ namesByParent.set(key, names);
55
+ if (foldersNameTaken(name, names)) violations.push({
56
+ kind: "duplicate-name",
57
+ parent,
58
+ name
59
+ });
60
+ names.push(name);
61
+ };
62
+ for (const folder of byId.values()) claim(folder.parent, folder.name);
63
+ const knownProjects = new Set((projects ?? []).map((project) => project.id));
64
+ for (const project of projects ?? []) {
65
+ const parent = document.assignments[project.id];
66
+ if (parent === void 0 || byId.has(parent)) claim(parent, project.name);
67
+ }
68
+ for (const [project, folder] of Object.entries(document.assignments)) if (!byId.has(folder) && (projects === void 0 || knownProjects.has(project))) violations.push({
69
+ kind: "unknown-assignment-folder",
70
+ project: asProjectId(project),
71
+ folder
72
+ });
73
+ for (const template of templates ?? []) {
74
+ const parent = document.templateAssignments[template.id];
75
+ if (parent === void 0 || byId.has(parent)) claim(parent, template.name);
76
+ }
77
+ const knownTemplates = new Set((templates ?? []).map((template) => template.id));
78
+ for (const [template, folder] of Object.entries(document.templateAssignments)) if (!byId.has(folder) && (templates === void 0 || knownTemplates.has(template))) violations.push({
79
+ kind: "unknown-template-assignment-folder",
80
+ template: asTemplateId(template),
81
+ folder
82
+ });
83
+ return violations;
84
+ }
85
+ /** A one-line description of a violation, for an error a user or a log will read. */
86
+ function formatFoldersViolation(violation) {
87
+ switch (violation.kind) {
88
+ case "duplicate-folder-id": return `folder id ${violation.folder} appears more than once`;
89
+ case "empty-folder-name": return `folder ${violation.folder} has an empty name`;
90
+ case "missing-parent": return `folder ${violation.folder} names a parent that does not exist (${violation.parent})`;
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}`;
93
+ case "unknown-assignment-folder": return `project ${violation.project} is assigned to a folder that does not exist (${violation.folder})`;
94
+ case "unknown-template-assignment-folder": return `template ${violation.template} is assigned to a folder that does not exist (${violation.folder})`;
95
+ }
96
+ }
97
+ /**
98
+ * True when following parents from this folder leads back to it. A folder that merely hangs below
99
+ * a cycle is not on it.
100
+ */
101
+ function onCycle(id, byId) {
102
+ const seen = /* @__PURE__ */ new Set();
103
+ let current = byId.get(id)?.parent;
104
+ while (current !== void 0 && !seen.has(current)) {
105
+ if (current === id) return true;
106
+ seen.add(current);
107
+ current = byId.get(current)?.parent;
108
+ }
109
+ return false;
110
+ }
111
+ //#endregion
112
+ export { formatFoldersViolation, validateFoldersDocument };
113
+
114
+ //# sourceMappingURL=invariants.js.map
@@ -0,0 +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;IAA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5B,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"}
@@ -0,0 +1,53 @@
1
+ //#region src/folders/naming.ts
2
+ /**
3
+ * The one naming rule for folders, projects and templates.
4
+ *
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.
10
+ *
11
+ * The rule auto-renames only where a machine chose the name — a move and a duplicate. Where a
12
+ * human typed the name, a caller uses {@link foldersNameTaken} and rejects the name with
13
+ * an error instead: a text field that quietly disagrees with what was typed is worse than one
14
+ * that says no.
15
+ *
16
+ * Names already stored are never rewritten. Accounts predating folders may hold collisions,
17
+ * because nothing enforced uniqueness before.
18
+ */
19
+ /** True when a sibling already carries this name, compared as the rule above compares names. */
20
+ function foldersNameTaken(name, siblingNames) {
21
+ const wanted = normalizeName(name);
22
+ for (const sibling of siblingNames) if (normalizeName(sibling) === wanted) return true;
23
+ return false;
24
+ }
25
+ /** True when a name has nothing in it but whitespace; a folder cannot be called that. */
26
+ function foldersNameBlank(name) {
27
+ return name.trim() === "";
28
+ }
29
+ /**
30
+ * The name to use for an item landing beside `siblingNames`.
31
+ *
32
+ * A free name is returned untouched. A taken one gains an `X (Copy)`, `X (Copy 2)` … suffix, and
33
+ * an existing copy suffix is replaced rather than stacked, so repeated copies stay readable.
34
+ */
35
+ function foldersUniqueName(desiredName, siblingNames) {
36
+ const taken = /* @__PURE__ */ new Set();
37
+ for (const sibling of siblingNames) taken.add(normalizeName(sibling));
38
+ if (!taken.has(normalizeName(desiredName))) return desiredName;
39
+ const base = desiredName.replace(COPY_SUFFIX, "");
40
+ let candidate = `${base} (Copy)`;
41
+ for (let index = 2; taken.has(normalizeName(candidate)); index++) candidate = `${base} (Copy ${index})`;
42
+ return candidate;
43
+ }
44
+ const COPY_SUFFIX = / \(Copy(?: \d+)?\)$/;
45
+ function normalizeName(name) {
46
+ return name.trim().toLowerCase();
47
+ }
48
+ //#endregion
49
+ exports.foldersNameBlank = foldersNameBlank;
50
+ exports.foldersNameTaken = foldersNameTaken;
51
+ exports.foldersUniqueName = foldersUniqueName;
52
+
53
+ //# sourceMappingURL=naming.cjs.map
@@ -0,0 +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"}
@@ -0,0 +1,31 @@
1
+ //#region src/folders/naming.d.ts
2
+ /**
3
+ * The one naming rule for folders, projects and templates.
4
+ *
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.
10
+ *
11
+ * The rule auto-renames only where a machine chose the name — a move and a duplicate. Where a
12
+ * human typed the name, a caller uses {@link foldersNameTaken} and rejects the name with
13
+ * an error instead: a text field that quietly disagrees with what was typed is worse than one
14
+ * that says no.
15
+ *
16
+ * Names already stored are never rewritten. Accounts predating folders may hold collisions,
17
+ * because nothing enforced uniqueness before.
18
+ */
19
+ /** True when a sibling already carries this name, compared as the rule above compares names. */
20
+ export declare function foldersNameTaken(name: string, siblingNames: Iterable<string>): boolean;
21
+ /** True when a name has nothing in it but whitespace; a folder cannot be called that. */
22
+ export declare function foldersNameBlank(name: string): boolean;
23
+ /**
24
+ * The name to use for an item landing beside `siblingNames`.
25
+ *
26
+ * A free name is returned untouched. A taken one gains an `X (Copy)`, `X (Copy 2)` … suffix, and
27
+ * an existing copy suffix is replaced rather than stacked, so repeated copies stay readable.
28
+ */
29
+ export declare function foldersUniqueName(desiredName: string, siblingNames: Iterable<string>): string;
30
+ //#endregion
31
+ //# sourceMappingURL=naming.d.ts.map
@@ -0,0 +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"}