Skip to content
Myr

normalize.js

This file

Path

lib/transform/normalize.js

106 lines · 3.2 KiB · JavaScript

Last changed by

Require Node.js 22 (#1243)Sindre Sorhusbefore adoption04b4454

History of this file

Take it

The walk stopped after 200 commits: lines older than bd3b478 point at it.

Refactor logic and rename some variables (#983)ehmickybefore adoptionbd3b478

lines 1–53why, one step back
1import isPlainObj from 'is-plain-obj';
2import {BINARY_ENCODINGS} from '../arguments/encoding-option.js';3import {TRANSFORM_TYPES} from '../stdio/type.js';4import {getTransformObjectModes} from './object-mode.js';56// Transforms generators/duplex/TransformStream can have multiple shapes.7// This normalizes it and applies default values.8export const normalizeTransforms = (stdioItems, optionName, direction, options) => [9 ...stdioItems.filter(({type}) => !TRANSFORM_TYPES.has(type)),10 ...getTransforms(stdioItems, optionName, direction, options),11];1213const getTransforms = (stdioItems, optionName, direction, {encoding}) => {14 const transforms = stdioItems.filter(({type}) => TRANSFORM_TYPES.has(type));15 const newTransforms = Array.from({length: transforms.length});1617 for (const [index, stdioItem] of Object.entries(transforms)) {18 newTransforms[index] = normalizeTransform({19 stdioItem,20 index: Number(index),21 newTransforms,22 optionName,23 direction,24 encoding,25 });26 }2728 return sortTransforms(newTransforms, direction);29};3031const normalizeTransform = ({stdioItem, stdioItem: {type}, index, newTransforms, optionName, direction, encoding}) => {32 if (type === 'duplex') {33 return normalizeDuplex({stdioItem, optionName});34 }3536 if (type === 'webTransform') {37 return normalizeTransformStream({38 stdioItem,39 index,40 newTransforms,41 direction,42 });43 }4445 return normalizeGenerator({46 stdioItem,47 index,48 newTransforms,49 direction,50 encoding,51 });52};53

Require Node.js 22 (#1243)Sindre Sorhusbefore adoption04b4454

lines 54–59why, one step back
54const normalizeDuplex = ({stdioItem, optionName}) => {
55 const {value} = stdioItem;56 const {transform} = value;57 const {writableObjectMode, readableObjectMode} = transform;58 const {objectMode = readableObjectMode} = value;59

Refactor logic and rename some variables (#983)ehmickybefore adoptionbd3b478

lines 60–106why, one step back
60 if (objectMode && !readableObjectMode) {
61 throw new TypeError(`The \`${optionName}.objectMode\` option can only be \`true\` if \`new Duplex({objectMode: true})\` is used.`);62 }6364 if (!objectMode && readableObjectMode) {65 throw new TypeError(`The \`${optionName}.objectMode\` option cannot be \`false\` if \`new Duplex({objectMode: true})\` is used.`);66 }6768 return {69 ...stdioItem,70 value: {transform, writableObjectMode, readableObjectMode},71 };72};7374const normalizeTransformStream = ({stdioItem, stdioItem: {value}, index, newTransforms, direction}) => {75 const {transform, objectMode} = isPlainObj(value) ? value : {transform: value};76 const {writableObjectMode, readableObjectMode} = getTransformObjectModes(objectMode, index, newTransforms, direction);77 return ({78 ...stdioItem,79 value: {transform, writableObjectMode, readableObjectMode},80 });81};8283const normalizeGenerator = ({stdioItem, stdioItem: {value}, index, newTransforms, direction, encoding}) => {84 const {85 transform,86 final,87 binary: binaryOption = false,88 preserveNewlines = false,89 objectMode,90 } = isPlainObj(value) ? value : {transform: value};91 const binary = binaryOption || BINARY_ENCODINGS.has(encoding);92 const {writableObjectMode, readableObjectMode} = getTransformObjectModes(objectMode, index, newTransforms, direction);93 return {94 ...stdioItem,95 value: {96 transform,97 final,98 binary,99 preserveNewlines,100 writableObjectMode,101 readableObjectMode,102 },103 };104};105106const sortTransforms = (newTransforms, direction) => direction === 'input' ? newTransforms.reverse() : newTransforms;