generator.js
This file
Path
test/helpers/generator.js
118 lines · 3.0 KiB · JavaScript
Last changed by
Not known.
History of this fileTake it
The walk stopped after 200 commits: lines older than 9f65b86 point at it.
100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 1–118why, one step back1import {2 setImmediate,3 setInterval,4 setTimeout,5 scheduler,6} from 'node:timers/promises';7import {foobarObject, foobarString} from './input.js';89const getGenerator = transform => (objectMode, binary, preserveNewlines) => ({10 transform,11 objectMode,12 binary,13 preserveNewlines,14});1516export const addNoopGenerator = (transform, addNoopTransform, objectMode, binary) => addNoopTransform17 ? [transform, noopGenerator(objectMode, binary)]18 : [transform];1920export const noopGenerator = getGenerator(function * (value) {21 yield value;22});2324export const noopAsyncGenerator = getGenerator(async function * (value) {25 yield value;26});2728export const serializeGenerator = getGenerator(function * (object) {29 yield JSON.stringify(object);30});3132export const getOutputGenerator = input => getGenerator(function * () {33 yield input;34});3536export const outputObjectGenerator = () => getOutputGenerator(foobarObject)(true);3738export const getOutputAsyncGenerator = input => getGenerator(async function * () {39 yield input;40});4142export const getOutputsGenerator = inputs => getGenerator(function * () {43 yield * inputs;44});4546export const getOutputsAsyncGenerator = inputs => getGenerator(async function * () {47 for (const input of inputs) {48 yield input;49 // eslint-disable-next-line no-await-in-loop50 await setImmediate();51 }52});5354const noYieldTransform = function * () {};5556export const noYieldGenerator = getGenerator(noYieldTransform);5758export const prefix = '> ';59export const suffix = ' <';6061export const multipleYieldGenerator = getGenerator(async function * (line = foobarString) {62 yield prefix;63 await scheduler.yield();64 yield line;65 await scheduler.yield();66 yield suffix;67});6869export const convertTransformToFinal = (transform, final) => {70 if (!final) {71 return transform;72 }7374 const generatorOptions = typeof transform === 'function' ? {transform} : transform;75 return ({...generatorOptions, transform: noYieldTransform, final: generatorOptions.transform});76};7778export const infiniteGenerator = getGenerator(async function * () {79 for await (const value of setInterval(100, foobarString)) {80 yield value;81 }82});8384const textDecoder = new TextDecoder();85const textEncoder = new TextEncoder();8687export const uppercaseBufferGenerator = getGenerator(function * (buffer) {88 yield textEncoder.encode(textDecoder.decode(buffer).toUpperCase());89});9091export const uppercaseGenerator = getGenerator(function * (string) {92 yield string.toUpperCase();93});9495// eslint-disable-next-line require-yield96export const throwingGenerator = error => getGenerator(function * () {97 throw error;98});99100export const appendGenerator = getGenerator(function * (string) {101 yield `${string}${casedSuffix}`;102});103104export const appendAsyncGenerator = getGenerator(async function * (string) {105 yield `${string}${casedSuffix}`;106});107108export const casedSuffix = 'k';109110export const resultGenerator = inputs => getGenerator(function * (input) {111 inputs.push(input);112 yield input;113});114115export const timeoutGenerator = timeout => getGenerator(async function * () {116 await setTimeout(timeout);117 yield foobarString;118});