ShtukbaseStandard
package file→ .shtukbase/packages/tasks/checks/lib/worklib.ts

checks/lib/worklib.ts

62 lines, 3.0 KB.

  1. // The shared part of the rules of the package "Tasks" (w07, w08, w16).
  2. // A copy of the start of tools/checks/spec-checks/worklib.py of the standard, without the part about git and settings
  3. // (only w04–w06 read it, and the package has none of them). It does not change speclib.ts: on import it adds to its
  4. // type map the collections of the layer "Work" (Initiative, AgentTask, Milestone, Direction, OwnerTask). Collections
  5. // are found by the itemType of the collection page, a relation field by the collection it leads to. If the schema has
  6. // no relation field, the field is taken by name from the fallback list (FALLBACK). speclib is from the package
  7. // collections (tasks -> norms-core -> contours -> frame -> collections).
  8. import { type Project, type Rec, SchemaError, TYPES, TYPE_LABEL, pystr, truthy } from '../../../collections/checks/lib/speclib.ts';
  9. Object.assign(TYPES, {
  10. INIT: ['Initiative'],
  11. TASK: ['AgentTask', 'Task'],
  12. MS: ['Milestone'],
  13. DIR: ['Direction'],
  14. OWNER: ['OwnerTask'],
  15. AREA: ['Area'],
  16. });
  17. Object.assign(TYPE_LABEL, {
  18. INIT: 'initiatives', TASK: 'agent tasks', MS: 'milestones', DIR: 'directions',
  19. OWNER: 'owner tasks', AREA: 'product areas',
  20. });
  21. /** Field name when the schema gives none (a schema without a relation field, or no target collection). */
  22. const FALLBACK: Record<string, string> = {
  23. 'INIT>DIR': 'direction', 'INIT>CAP': 'realizes', 'INIT>AREA': 'area', 'INIT>MS': 'milestone',
  24. 'TASK>INIT': 'initiative', 'MS>AC': 'criteria', 'MS>REQ': 'implements', 'MS>CAP': 'realizes', 'TASK>REQ': 'implements',
  25. };
  26. /** The relation field src → dst by the schema; if none, the fallback name. Several fields: the hint or the fallback. */
  27. export function fld(p: Project, src: string, dst: string, hint?: string | null): string | null {
  28. let f: string | null = null;
  29. try {
  30. f = p.field(src, dst, hint);
  31. } catch (e) {
  32. if (!(e instanceof SchemaError)) throw e;
  33. }
  34. return f || hint || FALLBACK[`${src}>${dst}`] || null;
  35. }
  36. /** Is the field filled: a list with a non-blank item, or a value that is not null, false or blank. */
  37. export function has(rec: Rec, field: string | null | undefined): boolean {
  38. const v = field ? rec.fm[field] : null;
  39. if (Array.isArray(v)) return v.some((x) => pystr(x).trim());
  40. return v !== null && v !== undefined && v !== false && pystr(v).trim() !== '';
  41. }
  42. /** Supporting work without its own value for the product: `enabler: true` in the head. */
  43. export function isEnabler(rec: Rec): boolean {
  44. const v = rec.fm.enabler;
  45. return v === true || pystr(v).trim().toLowerCase() === 'true';
  46. }
  47. /** State category: arriving / working / done / dropped; '?' or null: cannot tell. */
  48. export function cat(p: Project, rec: Rec, key: string): string | null {
  49. return p.category(rec, key);
  50. }
  51. /** The status of a record: the state name without the folder and the `.md` end. */
  52. export function status(rec: Rec): string {
  53. const s = pystr(truthy(rec.fm.status) ? rec.fm.status : '').trim();
  54. return s.endsWith('.md') ? s.split('/').pop()!.slice(0, -3) : s;
  55. }