validate.js
This file
Path
lib/stdio/validate.js
28 lines · 1.0 KiB · JavaScript
Last changed by
Split files (#837)ehmickybefore adoption672c2d7
History of this fileTake it
Split files (#837)ehmickybefore adoption672c2d7
lines 1–28why, one step back1import {isBinary} from '../utils.js';23export const getValidateTransformReturn = (readableObjectMode, optionName) => readableObjectMode4 ? validateObjectTransformReturn.bind(undefined, optionName)5 : validateStringTransformReturn.bind(undefined, optionName);67const validateObjectTransformReturn = function * (optionName, chunk) {8 validateEmptyReturn(optionName, chunk);9 yield chunk;10};1112const validateStringTransformReturn = function * (optionName, chunk) {13 validateEmptyReturn(optionName, chunk);1415 if (typeof chunk !== 'string' && !isBinary(chunk)) {16 throw new TypeError(`The \`${optionName}\` option's function must yield a string or an Uint8Array, not ${typeof chunk}.`);17 }1819 yield chunk;20};2122const validateEmptyReturn = (optionName, chunk) => {23 if (chunk === null || chunk === undefined) {24 throw new TypeError(`The \`${optionName}\` option's function must not call \`yield ${chunk}\`.25Instead, \`yield\` should either be called with a value, or not be called at all. For example:26 if (condition) { yield value; }`);27 }28};