package file→ .shtukbase/packages/tasks/checks/lib/worklib.ts
checks/lib/worklib.ts
62 lines, 3.0 KB.
- // The shared part of the rules of the package "Tasks" (w07, w08, w16).
- // A copy of the start of tools/checks/spec-checks/worklib.py of the standard, without the part about git and settings
- // (only w04–w06 read it, and the package has none of them). It does not change speclib.ts: on import it adds to its
- // type map the collections of the layer "Work" (Initiative, AgentTask, Milestone, Direction, OwnerTask). Collections
- // are found by the itemType of the collection page, a relation field by the collection it leads to. If the schema has
- // no relation field, the field is taken by name from the fallback list (FALLBACK). speclib is from the package
- // collections (tasks -> norms-core -> contours -> frame -> collections).
- import { type Project, type Rec, SchemaError, TYPES, TYPE_LABEL, pystr, truthy } from '../../../collections/checks/lib/speclib.ts';
- Object.assign(TYPES, {
- INIT: ['Initiative'],
- TASK: ['AgentTask', 'Task'],
- MS: ['Milestone'],
- DIR: ['Direction'],
- OWNER: ['OwnerTask'],
- AREA: ['Area'],
- });
- Object.assign(TYPE_LABEL, {
- INIT: 'initiatives', TASK: 'agent tasks', MS: 'milestones', DIR: 'directions',
- OWNER: 'owner tasks', AREA: 'product areas',
- });
- /** Field name when the schema gives none (a schema without a relation field, or no target collection). */
- const FALLBACK: Record<string, string> = {
- 'INIT>DIR': 'direction', 'INIT>CAP': 'realizes', 'INIT>AREA': 'area', 'INIT>MS': 'milestone',
- 'TASK>INIT': 'initiative', 'MS>AC': 'criteria', 'MS>REQ': 'implements', 'MS>CAP': 'realizes', 'TASK>REQ': 'implements',
- };
- /** The relation field src → dst by the schema; if none, the fallback name. Several fields: the hint or the fallback. */
- export function fld(p: Project, src: string, dst: string, hint?: string | null): string | null {
- let f: string | null = null;
- try {
- f = p.field(src, dst, hint);
- } catch (e) {
- if (!(e instanceof SchemaError)) throw e;
- }
- return f || hint || FALLBACK[`${src}>${dst}`] || null;
- }
- /** Is the field filled: a list with a non-blank item, or a value that is not null, false or blank. */
- export function has(rec: Rec, field: string | null | undefined): boolean {
- const v = field ? rec.fm[field] : null;
- if (Array.isArray(v)) return v.some((x) => pystr(x).trim());
- return v !== null && v !== undefined && v !== false && pystr(v).trim() !== '';
- }
- /** Supporting work without its own value for the product: `enabler: true` in the head. */
- export function isEnabler(rec: Rec): boolean {
- const v = rec.fm.enabler;
- return v === true || pystr(v).trim().toLowerCase() === 'true';
- }
- /** State category: arriving / working / done / dropped; '?' or null: cannot tell. */
- export function cat(p: Project, rec: Rec, key: string): string | null {
- return p.category(rec, key);
- }
- /** The status of a record: the state name without the folder and the `.md` end. */
- export function status(rec: Rec): string {
- const s = pystr(truthy(rec.fm.status) ? rec.fm.status : '').trim();
- return s.endsWith('.md') ? s.split('/').pop()!.slice(0, -3) : s;
- }