validate.js
This file
Path
lib/transform/validate.js
44 lines · 1.7 KiB · JavaScript
Last changed by
Add more code comments (#984)ehmickybefore adoptionfdd8a9f
History of this fileTake it
Rename files (#977)ehmickybefore adoption4b2e316
lines 1–3why, one step back1import {Buffer} from 'node:buffer';2import {isUint8Array} from '../utils/uint-array.js';3Add more code comments (#984)ehmickybefore adoptionfdd8a9f
line 4why, one step back4// Validate the type of chunk argument passed to transform generatorsRename files (#977)ehmickybefore adoption4b2e316
lines 5–16why, one step back5export const getValidateTransformInput = (writableObjectMode, optionName) => writableObjectMode6 ? undefined7 : validateStringTransformInput.bind(undefined, optionName);89const validateStringTransformInput = function * (optionName, chunk) {10 if (typeof chunk !== 'string' && !isUint8Array(chunk) && !Buffer.isBuffer(chunk)) {11 throw new TypeError(`The \`${optionName}\` option's transform must use "objectMode: true" to receive as input: ${typeof chunk}.`);12 }1314 yield chunk;15};16Add more code comments (#984)ehmickybefore adoptionfdd8a9f
line 17why, one step back17// Validate the type of the value returned by transform generatorsRename files (#977)ehmickybefore adoption4b2e316
lines 18–44why, one step back18export const getValidateTransformReturn = (readableObjectMode, optionName) => readableObjectMode19 ? validateObjectTransformReturn.bind(undefined, optionName)20 : validateStringTransformReturn.bind(undefined, optionName);2122const validateObjectTransformReturn = function * (optionName, chunk) {23 validateEmptyReturn(optionName, chunk);24 yield chunk;25};2627const validateStringTransformReturn = function * (optionName, chunk) {28 validateEmptyReturn(optionName, chunk);2930 if (typeof chunk !== 'string' && !isUint8Array(chunk)) {31 const typeName = Buffer.isBuffer(chunk) ? 'a buffer' : typeof chunk;32 throw new TypeError(`The \`${optionName}\` option's function must yield a string or an Uint8Array, not ${typeName}.`);33 }3435 yield chunk;36};3738const validateEmptyReturn = (optionName, chunk) => {39 if (chunk === null || chunk === undefined) {40 throw new TypeError(`The \`${optionName}\` option's function must not call \`yield ${chunk}\`.41Instead, \`yield\` should either be called with a value, or not be called at all. For example:42 if (condition) { yield value; }`);43 }44};