( history: Content[], fraction: number, )
| 68 | * Exported for testing purposes. |
| 69 | */ |
| 70 | export function findIndexAfterFraction( |
| 71 | history: Content[], |
| 72 | fraction: number, |
| 73 | ): number { |
| 74 | if (fraction <= 0 || fraction >= 1) { |
| 75 | throw new Error('Fraction must be between 0 and 1'); |
| 76 | } |
| 77 | |
| 78 | const contentLengths = history.map( |
| 79 | (content) => JSON.stringify(content).length, |
| 80 | ); |
| 81 | |
| 82 | const totalCharacters = contentLengths.reduce( |
| 83 | (sum, length) => sum + length, |
| 84 | 0, |
| 85 | ); |
| 86 | const targetCharacters = totalCharacters * fraction; |
| 87 | |
| 88 | let charactersSoFar = 0; |
| 89 | for (let i = 0; i < contentLengths.length; i++) { |
| 90 | charactersSoFar += contentLengths[i]; |
| 91 | if (charactersSoFar >= targetCharacters) { |
| 92 | return i; |
| 93 | } |
| 94 | } |
| 95 | return contentLengths.length; |
| 96 | } |
| 97 | |
| 98 | // The prophecy states that after 100 turns, even ANUS must rest |
| 99 | const MAX_TURNS = 100; |
no outgoing calls
no test coverage detected