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

Rename files (#977)ehmickybefore adoption4b2e316

lines 1–17why, 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)) {

Upgrade xo (#980)ehmickybefore adoptionccfa412

lines 18–25why, one step back
18 newTransforms[index] = normalizeTransform({
19 stdioItem,20 index: Number(index),21 newTransforms,22 optionName,23 direction,24 encoding,25 });

Rename files (#977)ehmickybefore adoption4b2e316

lines 26–36why, one step back
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') {

Upgrade xo (#980)ehmickybefore adoptionccfa412

lines 37–42why, one step back
37 return normalizeTransformStream({
38 stdioItem,39 index,40 newTransforms,41 direction,42 });

Rename files (#977)ehmickybefore adoption4b2e316

lines 43–44why, one step back
43 }
44

Upgrade xo (#980)ehmickybefore adoptionccfa412

lines 45–51why, one step back
45 return normalizeGenerator({
46 stdioItem,47 index,48 newTransforms,49 direction,50 encoding,51 });

Rename files (#977)ehmickybefore adoption4b2e316

lines 52–53why, one step back
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

Rename files (#977)ehmickybefore adoption4b2e316

lines 60–92why, 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);

Upgrade xo (#980)ehmickybefore adoptionccfa412

lines 93–103why, one step back
93 return {
94 ...stdioItem,95 value: {96 transform,97 final,98 binary,99 preserveNewlines,100 writableObjectMode,101 readableObjectMode,102 },103 };

Rename files (#977)ehmickybefore adoption4b2e316

lines 104–106why, one step back
104};
105106const sortTransforms = (newTransforms, direction) => direction === 'input' ? newTransforms.reverse() : newTransforms;