INNER CODE UNIT · JavaScript

shortenCodeFences

nadimtuhin/claude-token-optimizer · src/commands/compress.js:18

export function shortenCodeFences(content) {
  return content.replace(/^(```)(javascript|typescript|python|ruby|golang|shell|dockerfile)$/gm,
    (_, fence, lang) => fence + (LANG_SHORT[lang] ?? lang));
}

export function truncateLongLists(content, maxItems = 3) {
  const changes = [];
  const result = content.replace(
    /((?:^[ \t]*[-*] .+\n){6,})/gm,
    (block) => {
      const lines = block.split('\n').filter(Boolean);
      if (lines.length <= maxItems) return block;
      const kept = lines.slice(0, maxItems);
      const dropped = lines.length - maxItems;
      changes.push(`Truncated list (kept ${maxItems} of ${lines.length} items)`);
      return kept.join('\n') + `\n- # ... ${dropped} more\n`;
    },
  );

View source record →

📰 Research Paper
Loading…
⏳ Fetching content…