MCPcopy Create free account
hub / github.com/WinterTC55/iter-streams / createLoremStreamNewAPI

Function createLoremStreamNewAPI

samples/cloudflare-worker/src/index.ts:214–246  ·  view source on GitHub ↗

* Generate lorem ipsum text using the new streams API.

(
  targetBytes: number,
  seed: number
)

Source from the content-addressed store, hash-verified

212 * Generate lorem ipsum text using the new streams API.
213 */
214async 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).

Callers 1

handleNewStreamsFunction · 0.85

Calls 2

createRandomFunction · 0.85
pushMethod · 0.65

Tested by

no test coverage detected