@claudemini/shit-cli 1.8.2 → 1.9.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.
@@ -0,0 +1,100 @@
1
+ import { existsSync, readFileSync } from 'fs';
2
+
3
+ export function loadJsonIfExists(filePath, fallback = null) {
4
+ if (!existsSync(filePath)) {
5
+ return fallback;
6
+ }
7
+
8
+ try {
9
+ return JSON.parse(readFileSync(filePath, 'utf8'));
10
+ } catch {
11
+ return fallback;
12
+ }
13
+ }
14
+
15
+ export function normalizeLocation(location) {
16
+ if (!location) {
17
+ return null;
18
+ }
19
+
20
+ if (typeof location === 'string') {
21
+ return normalizeLocationString(location);
22
+ }
23
+
24
+ if (typeof location !== 'object') {
25
+ return null;
26
+ }
27
+
28
+ const file = String(location.file || '').trim();
29
+ if (!file) {
30
+ return null;
31
+ }
32
+
33
+ const line = Number(location.line);
34
+ const column = Number(location.column);
35
+
36
+ return {
37
+ file,
38
+ ...(Number.isFinite(line) && line > 0 ? { line: Math.floor(line) } : {}),
39
+ ...(Number.isFinite(column) && column > 0 ? { column: Math.floor(column) } : {}),
40
+ };
41
+ }
42
+
43
+ function normalizeLocationString(rawLocation) {
44
+ const raw = String(rawLocation || '').trim();
45
+ if (!raw) {
46
+ return null;
47
+ }
48
+ if (/^[a-z][a-z0-9+.-]*:\/\//i.test(raw)) {
49
+ return { file: raw };
50
+ }
51
+
52
+ const parts = raw.split(':');
53
+ let file = raw;
54
+ let line;
55
+ let column;
56
+
57
+ if (parts.length >= 3 && /^\d+$/.test(parts[parts.length - 1]) && /^\d+$/.test(parts[parts.length - 2])) {
58
+ file = parts.slice(0, -2).join(':');
59
+ line = Number(parts[parts.length - 2]);
60
+ column = Number(parts[parts.length - 1]);
61
+ } else if (parts.length >= 2 && /^\d+$/.test(parts[parts.length - 1])) {
62
+ file = parts.slice(0, -1).join(':');
63
+ line = Number(parts[parts.length - 1]);
64
+ }
65
+
66
+ file = file.trim();
67
+ if (!file) {
68
+ return null;
69
+ }
70
+
71
+ return {
72
+ file,
73
+ ...(Number.isFinite(line) && line > 0 ? { line: Math.floor(line) } : {}),
74
+ ...(Number.isFinite(column) && column > 0 ? { column: Math.floor(column) } : {}),
75
+ };
76
+ }
77
+
78
+ export function normalizeLocationKey(location) {
79
+ const normalized = normalizeLocation(location);
80
+ if (!normalized) {
81
+ return '';
82
+ }
83
+ return `${normalized.file}:${normalized.line || ''}:${normalized.column || ''}`;
84
+ }
85
+
86
+ export function normalizeEvidence(evidence, options = {}) {
87
+ const values = Array.isArray(evidence) ? evidence : [evidence];
88
+ const normalized = values
89
+ .map(item => String(item || '').trim())
90
+ .filter(Boolean);
91
+
92
+ if (normalized.length > 0) {
93
+ return [...new Set(normalized)];
94
+ }
95
+
96
+ const fallbackValues = Array.isArray(options.fallbackItems) ? options.fallbackItems : [options.fallbackItems];
97
+ return fallbackValues
98
+ .map(item => String(item || '').trim())
99
+ .filter(Boolean);
100
+ }