virtue-cli
Version:
Personal character development CLI tool for tracking virtues and philosophical alignment
63 lines • 2.26 kB
JavaScript
import { format } from 'date-fns';
export function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
export function formatDate(date = new Date()) {
return format(date, 'yyyy-MM-dd');
}
export function formatMonth(date = new Date()) {
return format(date, 'yyyy-MM');
}
export function formatDateDisplay(date = new Date()) {
return format(date, 'MMMM d, yyyy');
}
export function calculateAlignment(want, pull) {
// Handle zero cases: if both are 0, perfect alignment
if (want === 0 && pull === 0)
return 1;
// Calculate alignment as 1 minus the normalized difference
// This gives us a value between 0 and 1 where:
// - 1 means perfect alignment (want === pull)
// - 0 means maximum misalignment (want = 10, pull = 0 or vice versa)
const maxPossibleDifference = 10; // Since scores are 0-10
const actualDifference = Math.abs(want - pull);
const alignment = 1 - (actualDifference / maxPossibleDifference);
return alignment;
}
export function calculateCoherence(scores, weights) {
const virtueIds = Object.keys(scores);
if (virtueIds.length === 0)
return 0;
let totalWeightedAlignment = 0;
let totalWeight = 0;
for (const virtueId of virtueIds) {
const score = scores[virtueId];
const weight = weights?.[virtueId] || 1;
const alignment = calculateAlignment(score.want, score.pull);
totalWeightedAlignment += alignment * weight;
totalWeight += weight;
}
return totalWeight > 0 ? totalWeightedAlignment / totalWeight : 0;
}
export function getCoherenceLevel(score) {
if (score >= 0.85) {
return { level: 'Excellent', color: 'green', icon: '🟢' };
}
else if (score >= 0.70) {
return { level: 'Good', color: 'yellow', icon: '🟡' };
}
else if (score >= 0.50) {
return { level: 'Fair', color: 'yellow', icon: '🟠' };
}
else {
return { level: 'Needs Attention', color: 'red', icon: '🔴' };
}
}
export function validateScore(value) {
const num = parseInt(value);
if (isNaN(num) || num < 0 || num > 10) {
return 'Please enter a number between 0 and 10';
}
return undefined;
}
//# sourceMappingURL=helpers.js.map