virtue-tracker
Version:
Personal character development CLI tool for tracking virtues and philosophical alignment
60 lines • 2.02 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 either is 0, alignment is 0 unless both are 0
if (want === 0 && pull === 0)
return 1;
if (want === 0 || pull === 0)
return 0;
const dotProduct = want * pull;
const magnitudes = Math.sqrt(want * want) * Math.sqrt(pull * pull);
return dotProduct / magnitudes;
}
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