@mitre/hdf-utilities 3.0.1 → 3.1.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.
- package/LICENSE.md +55 -0
- package/dist/csv/index.d.ts +27 -29
- package/dist/csv/index.d.ts.map +1 -1
- package/dist/csv/index.js +179 -218
- package/dist/csv/index.js.map +1 -1
- package/dist/hash/index.d.ts +15 -12
- package/dist/hash/index.d.ts.map +1 -1
- package/dist/hash/index.js +90 -98
- package/dist/hash/index.js.map +1 -1
- package/dist/index.d.ts +36 -9
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +55 -17
- package/dist/index.js.map +1 -1
- package/dist/json/index.d.ts +13 -9
- package/dist/json/index.d.ts.map +1 -1
- package/dist/json/index.js +45 -54
- package/dist/json/index.js.map +1 -1
- package/dist/object/index.d.ts +6 -3
- package/dist/object/index.d.ts.map +1 -1
- package/dist/object/index.js +54 -60
- package/dist/object/index.js.map +1 -1
- package/dist/string/index.d.ts +5 -2
- package/dist/string/index.d.ts.map +1 -1
- package/dist/string/index.js +55 -56
- package/dist/string/index.js.map +1 -1
- package/dist/xml/index.d.ts +13 -13
- package/dist/xml/index.d.ts.map +1 -1
- package/dist/xml/index.js +136 -166
- package/dist/xml/index.js.map +1 -1
- package/package.json +30 -30
- package/dist/severity/index.d.ts +0 -29
- package/dist/severity/index.d.ts.map +0 -1
- package/dist/severity/index.js +0 -52
- package/dist/severity/index.js.map +0 -1
package/dist/json/index.js
CHANGED
|
@@ -1,61 +1,52 @@
|
|
|
1
|
-
|
|
1
|
+
import { findValuesByKey } from "../object/index.js";
|
|
2
|
+
//#region src/json/index.ts
|
|
2
3
|
/**
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
return JSON.parse(input);
|
|
17
|
-
}
|
|
18
|
-
catch (error) {
|
|
19
|
-
throw new Error(`Invalid JSON: ${error instanceof Error ? error.message : String(error)}`);
|
|
20
|
-
}
|
|
4
|
+
* Safely parse a JSON string
|
|
5
|
+
* @param input - JSON string to parse
|
|
6
|
+
* @returns Parsed JSON value
|
|
7
|
+
* @throws Error if input is not valid JSON
|
|
8
|
+
*/
|
|
9
|
+
function parseJSON(input) {
|
|
10
|
+
if (typeof input !== "string") throw new Error("Input must be a string");
|
|
11
|
+
if (input.trim() === "") throw new Error("Input cannot be empty");
|
|
12
|
+
try {
|
|
13
|
+
return JSON.parse(input);
|
|
14
|
+
} catch (error) {
|
|
15
|
+
throw new Error(`Invalid JSON: ${error instanceof Error ? error.message : String(error)}`);
|
|
16
|
+
}
|
|
21
17
|
}
|
|
22
18
|
/**
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
catch (error) {
|
|
38
|
-
throw new Error(`Failed to stringify JSON: ${error instanceof Error ? error.message : String(error)}`);
|
|
39
|
-
}
|
|
19
|
+
* Safely stringify a value to JSON
|
|
20
|
+
* @param value - Value to stringify
|
|
21
|
+
* @param options - Stringification options
|
|
22
|
+
* @returns JSON string
|
|
23
|
+
* @throws Error if value contains circular references or cannot be stringified
|
|
24
|
+
*/
|
|
25
|
+
function stringifyJSON(value, options = {}) {
|
|
26
|
+
const { pretty = false, indent = 2 } = options;
|
|
27
|
+
try {
|
|
28
|
+
if (pretty) return JSON.stringify(value, null, indent);
|
|
29
|
+
return JSON.stringify(value);
|
|
30
|
+
} catch (error) {
|
|
31
|
+
throw new Error(`Failed to stringify JSON: ${error instanceof Error ? error.message : String(error)}`);
|
|
32
|
+
}
|
|
40
33
|
}
|
|
41
34
|
/**
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
return true;
|
|
56
|
-
}
|
|
57
|
-
catch {
|
|
58
|
-
return false;
|
|
59
|
-
}
|
|
35
|
+
* Check if a string is valid JSON
|
|
36
|
+
* @param input - String to validate
|
|
37
|
+
* @returns true if input is valid JSON, false otherwise
|
|
38
|
+
*/
|
|
39
|
+
function isValidJSON(input) {
|
|
40
|
+
if (typeof input !== "string") return false;
|
|
41
|
+
if (input.trim() === "") return false;
|
|
42
|
+
try {
|
|
43
|
+
JSON.parse(input);
|
|
44
|
+
return true;
|
|
45
|
+
} catch {
|
|
46
|
+
return false;
|
|
47
|
+
}
|
|
60
48
|
}
|
|
49
|
+
//#endregion
|
|
50
|
+
export { findValuesByKey as findJsonValues, isValidJSON, parseJSON, stringifyJSON };
|
|
51
|
+
|
|
61
52
|
//# sourceMappingURL=index.js.map
|
package/dist/json/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../../src/json/index.ts"],"sourcesContent":["export { findValuesByKey as findJsonValues } from '../object/index.js';\n\n/**\n * Options for JSON stringification\n */\nexport interface StringifyOptions {\n /** Enable pretty printing with indentation */\n pretty?: boolean;\n /** Number of spaces for indentation (default: 2) */\n indent?: number;\n}\n\n/**\n * Safely parse a JSON string\n * @param input - JSON string to parse\n * @returns Parsed JSON value\n * @throws Error if input is not valid JSON\n */\nexport function parseJSON<T = unknown>(input: string): T {\n if (typeof input !== 'string') {\n throw new Error('Input must be a string');\n }\n\n if (input.trim() === '') {\n throw new Error('Input cannot be empty');\n }\n\n try {\n return JSON.parse(input) as T;\n } catch (error) {\n throw new Error(`Invalid JSON: ${error instanceof Error ? error.message : String(error)}`);\n }\n}\n\n/**\n * Safely stringify a value to JSON\n * @param value - Value to stringify\n * @param options - Stringification options\n * @returns JSON string\n * @throws Error if value contains circular references or cannot be stringified\n */\nexport function stringifyJSON(value: unknown, options: StringifyOptions = {}): string {\n const { pretty = false, indent = 2 } = options;\n\n try {\n if (pretty) {\n return JSON.stringify(value, null, indent);\n }\n return JSON.stringify(value);\n } catch (error) {\n throw new Error(`Failed to stringify JSON: ${error instanceof Error ? error.message : String(error)}`);\n }\n}\n\n/**\n * Check if a string is valid JSON\n * @param input - String to validate\n * @returns true if input is valid JSON, false otherwise\n */\nexport function isValidJSON(input: unknown): boolean {\n if (typeof input !== 'string') {\n return false;\n }\n\n if (input.trim() === '') {\n return false;\n }\n\n try {\n JSON.parse(input);\n return true;\n } catch {\n return false;\n }\n}\n"],"mappings":";;;;;;;;AAkBA,SAAgB,UAAuB,OAAkB;AACvD,KAAI,OAAO,UAAU,SACnB,OAAM,IAAI,MAAM,yBAAyB;AAG3C,KAAI,MAAM,MAAM,KAAK,GACnB,OAAM,IAAI,MAAM,wBAAwB;AAG1C,KAAI;AACF,SAAO,KAAK,MAAM,MAAM;UACjB,OAAO;AACd,QAAM,IAAI,MAAM,iBAAiB,iBAAiB,QAAQ,MAAM,UAAU,OAAO,MAAM,GAAG;;;;;;;;;;AAW9F,SAAgB,cAAc,OAAgB,UAA4B,EAAE,EAAU;CACpF,MAAM,EAAE,SAAS,OAAO,SAAS,MAAM;AAEvC,KAAI;AACF,MAAI,OACF,QAAO,KAAK,UAAU,OAAO,MAAM,OAAO;AAE5C,SAAO,KAAK,UAAU,MAAM;UACrB,OAAO;AACd,QAAM,IAAI,MAAM,6BAA6B,iBAAiB,QAAQ,MAAM,UAAU,OAAO,MAAM,GAAG;;;;;;;;AAS1G,SAAgB,YAAY,OAAyB;AACnD,KAAI,OAAO,UAAU,SACnB,QAAO;AAGT,KAAI,MAAM,MAAM,KAAK,GACnB,QAAO;AAGT,KAAI;AACF,OAAK,MAAM,MAAM;AACjB,SAAO;SACD;AACN,SAAO"}
|
package/dist/object/index.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
//#region src/object/index.d.ts
|
|
1
2
|
/**
|
|
2
3
|
* Generic object query utilities for traversing parsed data structures.
|
|
3
4
|
*
|
|
@@ -16,7 +17,7 @@
|
|
|
16
17
|
* @param maxDepth - Maximum recursion depth (default 100). Nodes beyond this depth are silently skipped.
|
|
17
18
|
* @returns Flat array of all matched values
|
|
18
19
|
*/
|
|
19
|
-
|
|
20
|
+
declare function findValuesByKey(obj: unknown, key: string, maxDepth?: number): unknown[];
|
|
20
21
|
/**
|
|
21
22
|
* Extract all values for a named field from an array of objects.
|
|
22
23
|
*
|
|
@@ -26,7 +27,7 @@ export declare function findValuesByKey(obj: unknown, key: string, maxDepth?: nu
|
|
|
26
27
|
* @param column - Property name to extract
|
|
27
28
|
* @returns Array of values for that column
|
|
28
29
|
*/
|
|
29
|
-
|
|
30
|
+
declare function extractColumn(rows: Record<string, unknown>[], column: string): unknown[];
|
|
30
31
|
/**
|
|
31
32
|
* Filter an array of objects to rows where a column matches a value.
|
|
32
33
|
*
|
|
@@ -37,5 +38,7 @@ export declare function extractColumn(rows: Record<string, unknown>[], column: s
|
|
|
37
38
|
* @param value - Value to match against
|
|
38
39
|
* @returns Filtered array of matching rows
|
|
39
40
|
*/
|
|
40
|
-
|
|
41
|
+
declare function findRows(rows: Record<string, unknown>[], column: string, value: unknown): Record<string, unknown>[];
|
|
42
|
+
//#endregion
|
|
43
|
+
export { extractColumn, findRows, findValuesByKey };
|
|
41
44
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","
|
|
1
|
+
{"version":3,"file":"index.d.ts","names":[],"sources":["../../src/object/index.ts"],"mappings":";;AAmBA;;;;;;;;;AAkDA;;;;;;;;iBAlDgB,eAAA,CACd,GAAA,WACA,GAAA,UACA,QAAA;AAgEF;;;;;;;;;AAAA,iBAjBgB,aAAA,CACd,IAAA,EAAM,MAAA,qBACN,MAAA;;;;;;;;;;;iBAec,QAAA,CACd,IAAA,EAAM,MAAA,qBACN,MAAA,UACA,KAAA,YACC,MAAA"}
|
package/dist/object/index.js
CHANGED
|
@@ -1,70 +1,64 @@
|
|
|
1
|
+
//#region src/object/index.ts
|
|
1
2
|
/**
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
3
|
+
* Generic object query utilities for traversing parsed data structures.
|
|
4
|
+
*
|
|
5
|
+
* These functions work on any plain JS objects — parsed XML, JSON, or CSV rows
|
|
6
|
+
* are all objects by the time converters work with them.
|
|
7
|
+
*/
|
|
7
8
|
/**
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
9
|
+
* Recursively find all values for a given key anywhere in a nested object tree.
|
|
10
|
+
*
|
|
11
|
+
* Walks the entire tree depth-first. When a property matching `key` is found,
|
|
12
|
+
* its value is collected. Recurse continues into all child values (objects and
|
|
13
|
+
* arrays) regardless of whether the current level matched.
|
|
14
|
+
*
|
|
15
|
+
* @param obj - Object tree to search (parsed XML, JSON, etc.)
|
|
16
|
+
* @param key - Property name to search for
|
|
17
|
+
* @param maxDepth - Maximum recursion depth (default 100). Nodes beyond this depth are silently skipped.
|
|
18
|
+
* @returns Flat array of all matched values
|
|
19
|
+
*/
|
|
20
|
+
function findValuesByKey(obj, key, maxDepth = 100) {
|
|
21
|
+
const results = [];
|
|
22
|
+
walk(obj, key, results, 0, maxDepth);
|
|
23
|
+
return results;
|
|
23
24
|
}
|
|
24
25
|
function walk(node, key, results, depth, maxDepth) {
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
}
|
|
35
|
-
return;
|
|
36
|
-
}
|
|
37
|
-
const record = node;
|
|
38
|
-
if (key in record) {
|
|
39
|
-
results.push(record[key]);
|
|
40
|
-
}
|
|
41
|
-
for (const value of Object.values(record)) {
|
|
42
|
-
walk(value, key, results, depth + 1, maxDepth);
|
|
43
|
-
}
|
|
26
|
+
if (depth > maxDepth) return;
|
|
27
|
+
if (node === null || node === void 0 || typeof node !== "object") return;
|
|
28
|
+
if (Array.isArray(node)) {
|
|
29
|
+
for (const item of node) walk(item, key, results, depth + 1, maxDepth);
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
const record = node;
|
|
33
|
+
if (key in record) results.push(record[key]);
|
|
34
|
+
for (const value of Object.values(record)) walk(value, key, results, depth + 1, maxDepth);
|
|
44
35
|
}
|
|
45
36
|
/**
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
37
|
+
* Extract all values for a named field from an array of objects.
|
|
38
|
+
*
|
|
39
|
+
* Rows where the column is `undefined` are skipped.
|
|
40
|
+
*
|
|
41
|
+
* @param rows - Array of objects (e.g. parsed CSV rows)
|
|
42
|
+
* @param column - Property name to extract
|
|
43
|
+
* @returns Array of values for that column
|
|
44
|
+
*/
|
|
45
|
+
function extractColumn(rows, column) {
|
|
46
|
+
return rows.map((r) => r[column]).filter((v) => v !== void 0);
|
|
56
47
|
}
|
|
57
48
|
/**
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
49
|
+
* Filter an array of objects to rows where a column matches a value.
|
|
50
|
+
*
|
|
51
|
+
* Uses strict equality (`===`).
|
|
52
|
+
*
|
|
53
|
+
* @param rows - Array of objects to filter
|
|
54
|
+
* @param column - Property name to check
|
|
55
|
+
* @param value - Value to match against
|
|
56
|
+
* @returns Filtered array of matching rows
|
|
57
|
+
*/
|
|
58
|
+
function findRows(rows, column, value) {
|
|
59
|
+
return rows.filter((r) => r[column] === value);
|
|
69
60
|
}
|
|
61
|
+
//#endregion
|
|
62
|
+
export { extractColumn, findRows, findValuesByKey };
|
|
63
|
+
|
|
70
64
|
//# sourceMappingURL=index.js.map
|
package/dist/object/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../../src/object/index.ts"],"sourcesContent":["/**\n * Generic object query utilities for traversing parsed data structures.\n *\n * These functions work on any plain JS objects — parsed XML, JSON, or CSV rows\n * are all objects by the time converters work with them.\n */\n\n/**\n * Recursively find all values for a given key anywhere in a nested object tree.\n *\n * Walks the entire tree depth-first. When a property matching `key` is found,\n * its value is collected. Recurse continues into all child values (objects and\n * arrays) regardless of whether the current level matched.\n *\n * @param obj - Object tree to search (parsed XML, JSON, etc.)\n * @param key - Property name to search for\n * @param maxDepth - Maximum recursion depth (default 100). Nodes beyond this depth are silently skipped.\n * @returns Flat array of all matched values\n */\nexport function findValuesByKey(\n obj: unknown,\n key: string,\n maxDepth = 100,\n): unknown[] {\n const results: unknown[] = [];\n walk(obj, key, results, 0, maxDepth);\n return results;\n}\n\nfunction walk(\n node: unknown,\n key: string,\n results: unknown[],\n depth: number,\n maxDepth: number,\n): void {\n if (depth > maxDepth) {\n return;\n }\n\n if (node === null || node === undefined || typeof node !== 'object') {\n return;\n }\n\n if (Array.isArray(node)) {\n for (const item of node) {\n walk(item, key, results, depth + 1, maxDepth);\n }\n return;\n }\n\n const record = node as Record<string, unknown>;\n if (key in record) {\n results.push(record[key]);\n }\n for (const value of Object.values(record)) {\n walk(value, key, results, depth + 1, maxDepth);\n }\n}\n\n/**\n * Extract all values for a named field from an array of objects.\n *\n * Rows where the column is `undefined` are skipped.\n *\n * @param rows - Array of objects (e.g. parsed CSV rows)\n * @param column - Property name to extract\n * @returns Array of values for that column\n */\nexport function extractColumn(\n rows: Record<string, unknown>[],\n column: string,\n): unknown[] {\n return rows.map((r) => r[column]).filter((v) => v !== undefined);\n}\n\n/**\n * Filter an array of objects to rows where a column matches a value.\n *\n * Uses strict equality (`===`).\n *\n * @param rows - Array of objects to filter\n * @param column - Property name to check\n * @param value - Value to match against\n * @returns Filtered array of matching rows\n */\nexport function findRows(\n rows: Record<string, unknown>[],\n column: string,\n value: unknown,\n): Record<string, unknown>[] {\n return rows.filter((r) => r[column] === value);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;AAmBA,SAAgB,gBACd,KACA,KACA,WAAW,KACA;CACX,MAAM,UAAqB,EAAE;AAC7B,MAAK,KAAK,KAAK,SAAS,GAAG,SAAS;AACpC,QAAO;;AAGT,SAAS,KACP,MACA,KACA,SACA,OACA,UACM;AACN,KAAI,QAAQ,SACV;AAGF,KAAI,SAAS,QAAQ,SAAS,KAAA,KAAa,OAAO,SAAS,SACzD;AAGF,KAAI,MAAM,QAAQ,KAAK,EAAE;AACvB,OAAK,MAAM,QAAQ,KACjB,MAAK,MAAM,KAAK,SAAS,QAAQ,GAAG,SAAS;AAE/C;;CAGF,MAAM,SAAS;AACf,KAAI,OAAO,OACT,SAAQ,KAAK,OAAO,KAAK;AAE3B,MAAK,MAAM,SAAS,OAAO,OAAO,OAAO,CACvC,MAAK,OAAO,KAAK,SAAS,QAAQ,GAAG,SAAS;;;;;;;;;;;AAalD,SAAgB,cACd,MACA,QACW;AACX,QAAO,KAAK,KAAK,MAAM,EAAE,QAAQ,CAAC,QAAQ,MAAM,MAAM,KAAA,EAAU;;;;;;;;;;;;AAalE,SAAgB,SACd,MACA,QACA,OAC2B;AAC3B,QAAO,KAAK,QAAQ,MAAM,EAAE,YAAY,MAAM"}
|
package/dist/string/index.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
//#region src/string/index.d.ts
|
|
1
2
|
/**
|
|
2
3
|
* String manipulation utilities for HDF converters.
|
|
3
4
|
*
|
|
@@ -24,7 +25,7 @@
|
|
|
24
25
|
* stripHtml('no tags here'); // 'no tags here'
|
|
25
26
|
* ```
|
|
26
27
|
*/
|
|
27
|
-
|
|
28
|
+
declare function stripHtml(html: string): string;
|
|
28
29
|
/**
|
|
29
30
|
* Parse a timestamp string in various common formats.
|
|
30
31
|
*
|
|
@@ -46,5 +47,7 @@ export declare function stripHtml(html: string): string;
|
|
|
46
47
|
* parseTimestamp(''); // null
|
|
47
48
|
* ```
|
|
48
49
|
*/
|
|
49
|
-
|
|
50
|
+
declare function parseTimestamp(s: string): Date | null;
|
|
51
|
+
//#endregion
|
|
52
|
+
export { parseTimestamp, stripHtml };
|
|
50
53
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","
|
|
1
|
+
{"version":3,"file":"index.d.ts","names":[],"sources":["../../src/string/index.ts"],"mappings":";;AA2BA;;;;;AA0BA;;;;;;;;;;;;;;;;;;;;iBA1BgB,SAAA,CAAU,IAAA;;;;;;;;;;;;;;;;;;;;;;iBA0BV,cAAA,CAAe,CAAA,WAAY,IAAA"}
|
package/dist/string/index.js
CHANGED
|
@@ -1,62 +1,61 @@
|
|
|
1
|
+
//#region src/string/index.ts
|
|
1
2
|
/**
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
3
|
+
* String manipulation utilities for HDF converters.
|
|
4
|
+
*
|
|
5
|
+
* These handle common operations on security tool output strings:
|
|
6
|
+
* HTML stripping for STIG descriptions and SonarQube messages,
|
|
7
|
+
* and multi-format timestamp parsing for tool output dates.
|
|
8
|
+
*/
|
|
8
9
|
/**
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
return stripped.replace(/\s+/g, ' ').trim();
|
|
10
|
+
* Remove HTML tags from a string and normalize whitespace.
|
|
11
|
+
*
|
|
12
|
+
* Strips all HTML/XML tags, collapses multiple whitespace characters
|
|
13
|
+
* into single spaces, and trims leading/trailing whitespace.
|
|
14
|
+
*
|
|
15
|
+
* For XML documents, use {@link extractTextFromXml} from the xml module instead.
|
|
16
|
+
* This function is for inline HTML in non-XML strings (STIG descriptions,
|
|
17
|
+
* SonarQube messages, etc.).
|
|
18
|
+
*
|
|
19
|
+
* @param html - String potentially containing HTML tags
|
|
20
|
+
* @returns Plain text with tags removed and whitespace normalized
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* ```typescript
|
|
24
|
+
* stripHtml('<p>hello</p> <b>world</b>'); // 'hello world'
|
|
25
|
+
* stripHtml('no tags here'); // 'no tags here'
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
function stripHtml(html) {
|
|
29
|
+
return html.replace(/<[^>]*>/g, " ").replace(/\s+/g, " ").trim();
|
|
30
30
|
}
|
|
31
31
|
/**
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
if (isNaN(parsed.getTime())) {
|
|
58
|
-
return null;
|
|
59
|
-
}
|
|
60
|
-
return parsed;
|
|
32
|
+
* Parse a timestamp string in various common formats.
|
|
33
|
+
*
|
|
34
|
+
* Attempts to parse the input using the JavaScript Date constructor, which
|
|
35
|
+
* supports ISO 8601, RFC 2822, and several other formats natively.
|
|
36
|
+
*
|
|
37
|
+
* Returns `null` if the input is empty or cannot be parsed.
|
|
38
|
+
*
|
|
39
|
+
* Equivalent to the Go `ParseTimestamp` in shared/go/converterutil.go.
|
|
40
|
+
*
|
|
41
|
+
* @param s - Timestamp string to parse
|
|
42
|
+
* @returns Parsed Date or null if unparseable
|
|
43
|
+
*
|
|
44
|
+
* @example
|
|
45
|
+
* ```typescript
|
|
46
|
+
* parseTimestamp('2024-01-15T10:30:00Z'); // Date object
|
|
47
|
+
* parseTimestamp('Mon, 15 Jan 2024 10:30:00 UTC'); // Date object
|
|
48
|
+
* parseTimestamp('not a date'); // null
|
|
49
|
+
* parseTimestamp(''); // null
|
|
50
|
+
* ```
|
|
51
|
+
*/
|
|
52
|
+
function parseTimestamp(s) {
|
|
53
|
+
if (!s || s.trim().length === 0) return null;
|
|
54
|
+
const parsed = new Date(s);
|
|
55
|
+
if (isNaN(parsed.getTime())) return null;
|
|
56
|
+
return parsed;
|
|
61
57
|
}
|
|
58
|
+
//#endregion
|
|
59
|
+
export { parseTimestamp, stripHtml };
|
|
60
|
+
|
|
62
61
|
//# sourceMappingURL=index.js.map
|
package/dist/string/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../../src/string/index.ts"],"sourcesContent":["/**\n * String manipulation utilities for HDF converters.\n *\n * These handle common operations on security tool output strings:\n * HTML stripping for STIG descriptions and SonarQube messages,\n * and multi-format timestamp parsing for tool output dates.\n */\n\n/**\n * Remove HTML tags from a string and normalize whitespace.\n *\n * Strips all HTML/XML tags, collapses multiple whitespace characters\n * into single spaces, and trims leading/trailing whitespace.\n *\n * For XML documents, use {@link extractTextFromXml} from the xml module instead.\n * This function is for inline HTML in non-XML strings (STIG descriptions,\n * SonarQube messages, etc.).\n *\n * @param html - String potentially containing HTML tags\n * @returns Plain text with tags removed and whitespace normalized\n *\n * @example\n * ```typescript\n * stripHtml('<p>hello</p> <b>world</b>'); // 'hello world'\n * stripHtml('no tags here'); // 'no tags here'\n * ```\n */\nexport function stripHtml(html: string): string {\n const stripped = html.replace(/<[^>]*>/g, ' ');\n return stripped.replace(/\\s+/g, ' ').trim();\n}\n\n/**\n * Parse a timestamp string in various common formats.\n *\n * Attempts to parse the input using the JavaScript Date constructor, which\n * supports ISO 8601, RFC 2822, and several other formats natively.\n *\n * Returns `null` if the input is empty or cannot be parsed.\n *\n * Equivalent to the Go `ParseTimestamp` in shared/go/converterutil.go.\n *\n * @param s - Timestamp string to parse\n * @returns Parsed Date or null if unparseable\n *\n * @example\n * ```typescript\n * parseTimestamp('2024-01-15T10:30:00Z'); // Date object\n * parseTimestamp('Mon, 15 Jan 2024 10:30:00 UTC'); // Date object\n * parseTimestamp('not a date'); // null\n * parseTimestamp(''); // null\n * ```\n */\nexport function parseTimestamp(s: string): Date | null {\n if (!s || s.trim().length === 0) {\n return null;\n }\n\n const parsed = new Date(s);\n if (isNaN(parsed.getTime())) {\n return null;\n }\n\n return parsed;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;AA2BA,SAAgB,UAAU,MAAsB;AAE9C,QADiB,KAAK,QAAQ,YAAY,IAAI,CAC9B,QAAQ,QAAQ,IAAI,CAAC,MAAM;;;;;;;;;;;;;;;;;;;;;;;AAwB7C,SAAgB,eAAe,GAAwB;AACrD,KAAI,CAAC,KAAK,EAAE,MAAM,CAAC,WAAW,EAC5B,QAAO;CAGT,MAAM,SAAS,IAAI,KAAK,EAAE;AAC1B,KAAI,MAAM,OAAO,SAAS,CAAC,CACzB,QAAO;AAGT,QAAO"}
|
package/dist/xml/index.d.ts
CHANGED
|
@@ -1,9 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
import type { X2jOptions, XmlBuilderOptions } from 'fast-xml-parser';
|
|
6
|
-
export { findValuesByKey as findXmlValues } from '../object/index.js';
|
|
1
|
+
import { findValuesByKey } from "../object/index.js";
|
|
2
|
+
import { X2jOptions, XmlBuilderOptions } from "fast-xml-parser";
|
|
3
|
+
|
|
4
|
+
//#region src/xml/index.d.ts
|
|
7
5
|
/**
|
|
8
6
|
* Parse XML string to JavaScript object
|
|
9
7
|
*
|
|
@@ -18,8 +16,8 @@ export { findValuesByKey as findXmlValues } from '../object/index.js';
|
|
|
18
16
|
* // Returns: { root: { item: { id: '1', '#text': 'Test' } } }
|
|
19
17
|
* ```
|
|
20
18
|
*/
|
|
21
|
-
|
|
22
|
-
|
|
19
|
+
declare function parseXml(xml: string, options?: Partial<X2jOptions> & {
|
|
20
|
+
maxSize?: number;
|
|
23
21
|
}): Record<string, unknown>;
|
|
24
22
|
/**
|
|
25
23
|
* Build XML string from JavaScript object
|
|
@@ -35,7 +33,7 @@ export declare function parseXml(xml: string, options?: Partial<X2jOptions> & {
|
|
|
35
33
|
* // Returns formatted XML string
|
|
36
34
|
* ```
|
|
37
35
|
*/
|
|
38
|
-
|
|
36
|
+
declare function buildXml(obj: Record<string, unknown>, options?: Partial<XmlBuilderOptions>): string;
|
|
39
37
|
/**
|
|
40
38
|
* Validate if a string is well-formed XML
|
|
41
39
|
*
|
|
@@ -49,7 +47,7 @@ export declare function buildXml(obj: Record<string, unknown>, options?: Partial
|
|
|
49
47
|
* isValidXml('not xml'); // false
|
|
50
48
|
* ```
|
|
51
49
|
*/
|
|
52
|
-
|
|
50
|
+
declare function isValidXml(xml: string): boolean;
|
|
53
51
|
/**
|
|
54
52
|
* Parse XML with array handling for repeated elements
|
|
55
53
|
* Forces specified tag names to always be arrays, even if only one element exists
|
|
@@ -66,8 +64,8 @@ export declare function isValidXml(xml: string): boolean;
|
|
|
66
64
|
* // Without arrayTags, would return: { root: { item: 'Test' } }
|
|
67
65
|
* ```
|
|
68
66
|
*/
|
|
69
|
-
|
|
70
|
-
|
|
67
|
+
declare function parseXmlWithArrays(xml: string, arrayTags: string[], options?: Partial<X2jOptions> & {
|
|
68
|
+
maxSize?: number;
|
|
71
69
|
}): Record<string, unknown>;
|
|
72
70
|
/**
|
|
73
71
|
* Extract text content from XML, stripping all tags
|
|
@@ -82,5 +80,7 @@ export declare function parseXmlWithArrays(xml: string, arrayTags: string[], opt
|
|
|
82
80
|
* // Returns: 'Bold and italic'
|
|
83
81
|
* ```
|
|
84
82
|
*/
|
|
85
|
-
|
|
83
|
+
declare function extractTextFromXml(xml: string): string;
|
|
84
|
+
//#endregion
|
|
85
|
+
export { buildXml, extractTextFromXml, findValuesByKey as findXmlValues, isValidXml, parseXml, parseXmlWithArrays };
|
|
86
86
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/xml/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","
|
|
1
|
+
{"version":3,"file":"index.d.ts","names":[],"sources":["../../src/xml/index.ts"],"mappings":";;;;;;;;;;;;;;;;;;iBAuDgB,QAAA,CACd,GAAA,UACA,OAAA,GAAU,OAAA,CAAQ,UAAA;EAAgB,OAAA;AAAA,IACjC,MAAA;;;;;;;;;;;;;AA8DH;;iBA1BgB,QAAA,CACd,GAAA,EAAK,MAAA,mBACL,OAAA,GAAU,OAAA,CAAQ,iBAAA;;;AAiDpB;;;;;;;;;;;iBAzBgB,UAAA,CAAW,GAAA;;;;;;AAiE3B;;;;;;;;;;;iBAxCgB,kBAAA,CACd,GAAA,UACA,SAAA,YACA,OAAA,GAAU,OAAA,CAAQ,UAAA;EAAgB,OAAA;AAAA,IACjC,MAAA;;;;;;;;;;;;;;iBAoCa,kBAAA,CAAmB,GAAA"}
|