* Generate lorem ipsum text using the new streams API.
( targetBytes: number, seed: number )
| 212 | * Generate lorem ipsum text using the new streams API. |
| 213 | */ |
| 214 | async function* createLoremStreamNewAPI( |
| 215 | targetBytes: number, |
| 216 | seed: number |
| 217 | ): AsyncGenerator<string> { |
| 218 | const random = createRandom(seed); |
| 219 | let bytesGenerated = 0; |
| 220 | |
| 221 | while (bytesGenerated < targetBytes) { |
| 222 | // Generate a paragraph |
| 223 | const sentenceCount = Math.floor(random() * 5) + 3; |
| 224 | const sentences: string[] = []; |
| 225 | |
| 226 | for (let s = 0; s < sentenceCount; s++) { |
| 227 | const wordCount = Math.floor(random() * 10) + 5; |
| 228 | const words: string[] = []; |
| 229 | |
| 230 | for (let w = 0; w < wordCount; w++) { |
| 231 | const wordIndex = Math.floor(random() * LOREM_WORDS.length); |
| 232 | let word = LOREM_WORDS[wordIndex]; |
| 233 | if (w === 0) { |
| 234 | word = word.charAt(0).toUpperCase() + word.slice(1); |
| 235 | } |
| 236 | words.push(word); |
| 237 | } |
| 238 | |
| 239 | sentences.push(words.join(' ') + '.'); |
| 240 | } |
| 241 | |
| 242 | const paragraph = sentences.join(' ') + '\n\n'; |
| 243 | bytesGenerated += encoder.encode(paragraph).byteLength; |
| 244 | yield paragraph; |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | /** |
| 249 | * Transform that batches input into 16KB chunks (New Streams API). |
no test coverage detected