Skip to content
Myr

validate.js

This file

Path

lib/stdio/validate.js

30 lines · 1.1 KiB · JavaScript

Last changed by

Stricter values for the `encoding` option (#928)ehmickybefore adoption477bbc4

History of this file

Take it

Use `Uint8Array` instead of `Buffer` (#915)ehmickybefore adoption410bac3

line 1why, one step back
1import {Buffer} from 'node:buffer';

Stricter values for the `encoding` option (#928)ehmickybefore adoption477bbc4

line 2why, one step back
2import {isUint8Array} from '../utils.js';

Split files (#837)ehmickybefore adoption672c2d7

lines 3–15why, one step back
3
4export const getValidateTransformReturn = (readableObjectMode, optionName) => readableObjectMode5 ? validateObjectTransformReturn.bind(undefined, optionName)6 : validateStringTransformReturn.bind(undefined, optionName);78const validateObjectTransformReturn = function * (optionName, chunk) {9 validateEmptyReturn(optionName, chunk);10 yield chunk;11};1213const validateStringTransformReturn = function * (optionName, chunk) {14 validateEmptyReturn(optionName, chunk);15

Use `Uint8Array` instead of `Buffer` (#915)ehmickybefore adoption410bac3

lines 16–18why, one step back
16 if (typeof chunk !== 'string' && !isUint8Array(chunk)) {
17 const typeName = Buffer.isBuffer(chunk) ? 'a buffer' : typeof chunk;18 throw new TypeError(`The \`${optionName}\` option's function must yield a string or an Uint8Array, not ${typeName}.`);

Split files (#837)ehmickybefore adoption672c2d7

lines 19–30why, one step back
19 }
2021 yield chunk;22};2324const validateEmptyReturn = (optionName, chunk) => {25 if (chunk === null || chunk === undefined) {26 throw new TypeError(`The \`${optionName}\` option's function must not call \`yield ${chunk}\`.27Instead, \`yield\` should either be called with a value, or not be called at all. For example:28 if (condition) { yield value; }`);29 }30};