@tak-ps/node-cot 14.60.1 → 14.61.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.
@@ -467,7 +467,7 @@ export const SpatialAttitudeAttributes = Type.Object({
467
467
  });
468
468
 
469
469
  export const SpatialAttitude = Type.Object({
470
- _attributes: SpatialAttitudeAttributes
470
+ _attributes: Type.Optional(SpatialAttitudeAttributes)
471
471
  });
472
472
 
473
473
  export const SpatialSpinAttributes = Type.Object({
@@ -492,7 +492,7 @@ export const SpatialSpinAttributes = Type.Object({
492
492
  });
493
493
 
494
494
  export const SpatialSpin = Type.Object({
495
- _attributes: SpatialSpinAttributes
495
+ _attributes: Type.Optional(SpatialSpinAttributes)
496
496
  });
497
497
 
498
498
  export const Spatial = Type.Object({
@@ -501,8 +501,8 @@ export const Spatial = Type.Object({
501
501
  description: 'Version tag for this sub schema.'
502
502
  }))
503
503
  })),
504
- attitude: SpatialAttitude,
505
- spin: SpatialSpin
504
+ attitude: Type.Optional(SpatialAttitude),
505
+ spin: Type.Optional(SpatialSpin)
506
506
  });
507
507
 
508
508
  export const VideoAttributes = Type.Object({
@@ -582,9 +582,11 @@ export const Chat = Type.Object({
582
582
  messageId: Type.Optional(Type.String()),
583
583
  chatroom: Type.String(),
584
584
  id: Type.Optional(Type.String()),
585
- senderCallsign: Type.String()
585
+ senderCallsign: Type.String(),
586
+ deleteChild: Type.Optional(Type.String())
586
587
  }),
587
- chatgrp: Type.Any()
588
+ chatgrp: Type.Any(),
589
+ hierarchy: Type.Optional(Type.Any())
588
590
  })
589
591
 
590
592
  export const TakVersionAttributes = Type.Object({
@@ -0,0 +1,123 @@
1
+ import type { ChatHierarchyNode } from '../types/feature.js';
2
+
3
+ type RawHierarchyNode = {
4
+ _attributes?: { uid?: unknown; name?: unknown };
5
+ group?: unknown;
6
+ contact?: unknown;
7
+ };
8
+
9
+ function isRawNode(raw: unknown): raw is RawHierarchyNode {
10
+ return typeof raw === 'object' && raw !== null;
11
+ }
12
+
13
+ function rawNodes(raw: unknown): Array<RawHierarchyNode> {
14
+ if (Array.isArray(raw)) return raw.filter((item) => isRawNode(item));
15
+ return isRawNode(raw) ? [raw] : [];
16
+ }
17
+
18
+ function toNode(raw: RawHierarchyNode, type: 'group' | 'contact'): ChatHierarchyNode | undefined {
19
+ const uid = raw._attributes?.uid;
20
+ const name = raw._attributes?.name;
21
+
22
+ if (typeof uid !== 'string' || typeof name !== 'string') return undefined;
23
+
24
+ const node: ChatHierarchyNode = { uid, name, type };
25
+
26
+ if (type === 'group') {
27
+ node.children = [
28
+ ...rawNodes(raw.group).map((child) => toNode(child, 'group')),
29
+ ...rawNodes(raw.contact).map((child) => toNode(child, 'contact'))
30
+ ].filter((child) => child !== undefined);
31
+ }
32
+
33
+ return node;
34
+ }
35
+
36
+ /**
37
+ * Convert the `<hierarchy>` of a `__chat` detail into a tree of groups & contacts
38
+ */
39
+ export function chatHierarchy(raw: unknown): ChatHierarchyNode | undefined {
40
+ if (!isRawNode(raw)) return undefined;
41
+
42
+ const roots = rawNodes(raw.group);
43
+ if (roots.length === 0) return undefined;
44
+
45
+ const root = roots[0];
46
+
47
+ return toNode(root, 'group');
48
+ }
49
+
50
+ type HierarchyDetail = {
51
+ _attributes: { uid: string; name: string };
52
+ group?: Array<HierarchyDetail>;
53
+ contact?: Array<HierarchyDetail>;
54
+ };
55
+
56
+ function toDetail(node: ChatHierarchyNode): HierarchyDetail {
57
+ const detail: HierarchyDetail = {
58
+ _attributes: { uid: node.uid, name: node.name }
59
+ };
60
+
61
+ const children = node.children ?? [];
62
+ const groups = children.filter((child) => child.type === 'group');
63
+ const contacts = children.filter((child) => child.type === 'contact');
64
+
65
+ if (groups.length > 0) detail.group = groups.map((group) => toDetail(group));
66
+ if (contacts.length > 0) detail.contact = contacts.map((contact) => toDetail(contact));
67
+
68
+ return detail;
69
+ }
70
+
71
+ /**
72
+ * Convert a tree of groups & contacts into the `<hierarchy>` of a `__chat` detail
73
+ */
74
+ export function chatHierarchyDetail(root: ChatHierarchyNode): { group: HierarchyDetail } {
75
+ return { group: toDetail(root) };
76
+ }
77
+
78
+ /**
79
+ * Every contact below a node of a hierarchy, regardless of how deeply it is nested
80
+ */
81
+ export function chatHierarchyContacts(node: ChatHierarchyNode): Array<{ uid: string; callsign: string }> {
82
+ const contacts = new Map<string, { uid: string; callsign: string }>();
83
+
84
+ const walk = (current: ChatHierarchyNode): void => {
85
+ if (current.type === 'contact') {
86
+ contacts.set(current.uid, { uid: current.uid, callsign: current.name });
87
+ }
88
+
89
+ for (const child of current.children ?? []) walk(child);
90
+ };
91
+
92
+ walk(node);
93
+
94
+ return [...contacts.values()];
95
+ }
96
+
97
+ /**
98
+ * Find a group by UID anywhere in a hierarchy
99
+ */
100
+ export function chatHierarchyGroup(node: ChatHierarchyNode, uid: string): ChatHierarchyNode | undefined {
101
+ if (node.type === 'group' && node.uid === uid) return node;
102
+
103
+ for (const child of node.children ?? []) {
104
+ const found = chatHierarchyGroup(child, uid);
105
+ if (found) return found;
106
+ }
107
+
108
+ return undefined;
109
+ }
110
+
111
+ /**
112
+ * UID of the group that directly contains the group with the given UID
113
+ */
114
+ export function chatHierarchyParent(node: ChatHierarchyNode, uid: string): string | undefined {
115
+ for (const child of node.children ?? []) {
116
+ if (child.type === 'group' && child.uid === uid) return node.uid;
117
+
118
+ const found = chatHierarchyParent(child, uid);
119
+ if (found) return found;
120
+ }
121
+
122
+ return undefined;
123
+ }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@tak-ps/node-cot",
3
3
  "type": "module",
4
- "version": "14.60.1",
4
+ "version": "14.61.1",
5
5
  "description": "Lightweight JavaScript library for parsing and manipulating TAK messages",
6
6
  "author": "Nick Ingalls <nick@ingalls.ca>",
7
7
  "types": "index.ts",