Spaces:
Running
Running
| /** | |
| * ๊ธด ๋ณธ๋ฌธ ๊ณ๋จ์ ์ถ์ฝ โ PIPC ์๊ฒฐ๋ฌธ(ํ๊ท 1๋ง์+) ํ ํฐ ์ ์ฝ์ฉ. | |
| * ์ 800์ + ์ค๋ต ๋ง์ปค + ๋ค 400์. minSave ๊ฐ๋๋ก ์งง์ ๋ณธ๋ฌธ์ ๊ทธ๋๋ก ์ ์ง. | |
| */ | |
| const HEAD_LIMIT = 800; | |
| const TAIL_LIMIT = 400; | |
| const MIN_LENGTH_TO_COMPACT = 1300; | |
| /** ํ๊ตญ์ด ์ข ๊ฒฐ์ด๋ฏธ โ ๋ฌธ์ฅ ๊ฒฝ๊ณ ํ์ง */ | |
| const SENTENCE_ENDINGS = /(?:๋ค\.|๋ผ\.|์\.|์\.|ํจ\.|์ด๋ค\.|๊ฒ์ด๋ค\.|ํ๋จ๋๋ค\.)\s*/g; | |
| export interface CompactOptions { | |
| /** override head ๊ธ์์ */ | |
| headLimit?: number; | |
| /** override tail ๊ธ์์ */ | |
| tailLimit?: number; | |
| /** override ์ต์ ์์ถ ๊ธธ์ด ์๊ณ๊ฐ */ | |
| minLength?: number; | |
| } | |
| export function compactBody(text: string, options: CompactOptions = {}): string { | |
| const head = options.headLimit ?? HEAD_LIMIT; | |
| const tail = options.tailLimit ?? TAIL_LIMIT; | |
| const minLen = options.minLength ?? MIN_LENGTH_TO_COMPACT; | |
| if (text.length <= minLen) return text; | |
| if (text.length <= head + tail) return text; | |
| const headRaw = text.substring(0, head); | |
| const tailRaw = text.substring(text.length - tail); | |
| const headFinal = trimAtSentenceEnd(headRaw); | |
| const tailFinal = trimAtSentenceStart(tailRaw); | |
| const omitted = text.length - headFinal.length - tailFinal.length; | |
| if (omitted <= 0) return text; | |
| return `${headFinal}\n\nโฏ ์ค๋ต ${omitted}์ (full=true๋ก ์ ๋ฌธ ์กฐํ) โฏ\n\n${tailFinal}`; | |
| } | |
| function trimAtSentenceEnd(text: string): string { | |
| const matches = [...text.matchAll(SENTENCE_ENDINGS)]; | |
| if (matches.length === 0) return text; | |
| const last = matches[matches.length - 1]; | |
| if (!last || last.index === undefined) return text; | |
| return text.substring(0, last.index + last[0].length).trimEnd(); | |
| } | |
| function trimAtSentenceStart(text: string): string { | |
| // ์ฒซ ์ข ๊ฒฐ ๋ค์ ์์์ โ ๋ฌธ์ฅ ์ค๊ฐ ์๋ฆผ ๋ฐฉ์ง | |
| const m = text.match(/[.ใ]\s+([๊ฐ-ํฃA-Z\d])/); | |
| if (!m || m.index === undefined) return text; | |
| return text.substring(m.index + m[0].length - 1); | |
| } | |