UNPKG

@eimen/token-saver

Version:

Strip polite fluff from AI prompts to cut token use and cost.

3 lines (2 loc) 1.44 kB
"use strict";class e{static DEFAULT_CONFIG={removePoliteness:!0,removeFillers:!0,removeRedundantIntros:!0,tokenCharRatio:4};static PATTERNS={politeness:/\b(?:please|kindly)\b[,.!?]?\s*|\b(?:thank(?:\s*you)?|thanks)\b[,.!?]?\s*|\b(?:if you don['’]t mind|if you could|if possible)\b[,.!?]?\s*|\bappreciate\s*(?:it|your help)\b[,.!?]?\s*/gi,fillers:/\b(?:um|uh|er|hmm|like|you know|actually|basically|literally|so|well|anyway|anyhow)\b[,.!?]?\s*/gi,redundantIntros:/^(?:i (?:was (?:wondering|hoping|thinking) if you (?:could|would|can)|(?:am|'m) (?:trying|looking|hoping) to)|can you|could you|would you be able to|(?:i'd|i would) like you to|i want you to|i need you to|i'm looking for|i'm trying to find|could i get|can i get)\s+/i};constructor(o={}){this.config={...e.DEFAULT_CONFIG,...o}}clean(o=""){let t=`${o}`;return this.config.removePoliteness&&(t=t.replace(e.PATTERNS.politeness," ")),this.config.removeFillers&&(t=t.replace(e.PATTERNS.fillers," ")),this.config.removeRedundantIntros&&(t=t.replace(e.PATTERNS.redundantIntros,"")),t=t.trim().replace(/\s+/g," "),t=t.replace(/\s([.,!?;:])/g,"$1"),t=t.replace(/\s[!?]+$/,""),t&&(t=t[0].toUpperCase()+t.slice(1)),t}_tokens(e){return Math.ceil(e.length/this.config.tokenCharRatio)}process(e){const o=this.clean(e);return{original:e,cleaned:o,charsSaved:e.length-o.length,estimatedTokensSaved:this._tokens(e)-this._tokens(o)}}}module.exports=e; //# sourceMappingURL=token-saver.cjs.map