Skip to content
Myr

validate.js

This file

Path

lib/transform/validate.js

42 lines · 1.6 KiB · JavaScript

Last changed by

Rename files (#977)ehmickybefore adoption4b2e316

History of this file

Take it

Rename files (#977)ehmickybefore adoption4b2e316

lines 1–42why, one step back
1import {Buffer} from 'node:buffer';
2import {isUint8Array} from '../utils/uint-array.js';34export const getValidateTransformInput = (writableObjectMode, optionName) => writableObjectMode5 ? undefined6 : validateStringTransformInput.bind(undefined, optionName);78const validateStringTransformInput = function * (optionName, chunk) {9 if (typeof chunk !== 'string' && !isUint8Array(chunk) && !Buffer.isBuffer(chunk)) {10 throw new TypeError(`The \`${optionName}\` option's transform must use "objectMode: true" to receive as input: ${typeof chunk}.`);11 }1213 yield chunk;14};1516export const getValidateTransformReturn = (readableObjectMode, optionName) => readableObjectMode17 ? validateObjectTransformReturn.bind(undefined, optionName)18 : validateStringTransformReturn.bind(undefined, optionName);1920const validateObjectTransformReturn = function * (optionName, chunk) {21 validateEmptyReturn(optionName, chunk);22 yield chunk;23};2425const validateStringTransformReturn = function * (optionName, chunk) {26 validateEmptyReturn(optionName, chunk);2728 if (typeof chunk !== 'string' && !isUint8Array(chunk)) {29 const typeName = Buffer.isBuffer(chunk) ? 'a buffer' : typeof chunk;30 throw new TypeError(`The \`${optionName}\` option's function must yield a string or an Uint8Array, not ${typeName}.`);31 }3233 yield chunk;34};3536const validateEmptyReturn = (optionName, chunk) => {37 if (chunk === null || chunk === undefined) {38 throw new TypeError(`The \`${optionName}\` option's function must not call \`yield ${chunk}\`.39Instead, \`yield\` should either be called with a value, or not be called at all. For example:40 if (condition) { yield value; }`);41 }42};