All files / web/src/providers/navigation url-utils.ts

100% Statements 44/44
100% Branches 12/12
100% Functions 6/6
100% Lines 44/44

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 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 58            1x 84x 84x   84x 100x 100x 84x   84x 84x   27x 27x 27x 27x 1x 1x 27x   1x 12x 12x 12x 12x 12x 12x   1x 5x 5x 5x 5x 5x 5x   1x 5x 5x 5x 5x 5x 5x   1x 5x 5x 5x 5x 5x 5x  
import type { Agent, Skill } from '@shared/types/data';
import type { SkillOptimizationArm } from '@shared/types/data/skill-optimization-arm';
import type { SkillOptimizationCluster } from '@shared/types/data/skill-optimization-cluster';
 
// Remove potentially dangerous HTML tags but preserve spacing/case
// Applies repeatedly until no more changes to handle nested tags like <scrip<script>t>
export function sanitizeName(name: string): string {
  let previous: string;
  let current = name;
 
  do {
    previous = current;
    current = current.replace(/<[^>]*>/g, '');
  } while (current !== previous);
 
  return current.trim();
}
 
function safeDecodeURIComponent(value: string): string {
  try {
    return decodeURIComponent(value);
  } catch {
    return value;
  }
}
 
export function getAgentByName(
  agents: Agent[],
  name: string,
): Agent | undefined {
  const target = sanitizeName(safeDecodeURIComponent(name));
  return agents.find((agent) => sanitizeName(agent.name) === target);
}
 
export function getSkillByName(
  skills: Skill[],
  name: string,
): Skill | undefined {
  const target = sanitizeName(safeDecodeURIComponent(name));
  return skills.find((skill) => sanitizeName(skill.name) === target);
}
 
export function getClusterByName(
  clusters: SkillOptimizationCluster[],
  name: string,
): SkillOptimizationCluster | undefined {
  const target = sanitizeName(safeDecodeURIComponent(name));
  return clusters.find((cluster) => sanitizeName(cluster.name) === target);
}
 
export function getArmByName(
  arms: SkillOptimizationArm[],
  name: string,
): SkillOptimizationArm | undefined {
  const target = sanitizeName(safeDecodeURIComponent(name));
  return arms.find((arm) => sanitizeName(arm.name) === target);
}