Skip to content
Myr

normalize.js

This file

Path

lib/transform/normalize.js

111 lines · 3.2 KiB · JavaScript

Last changed by

Upgrade xo (#980)ehmickybefore adoptionccfa412

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–97why, one step back
52};
5354const normalizeDuplex = ({55 stdioItem,56 stdioItem: {57 value: {58 transform,59 transform: {writableObjectMode, readableObjectMode},60 objectMode = readableObjectMode,61 },62 },63 optionName,64}) => {65 if (objectMode && !readableObjectMode) {66 throw new TypeError(`The \`${optionName}.objectMode\` option can only be \`true\` if \`new Duplex({objectMode: true})\` is used.`);67 }6869 if (!objectMode && readableObjectMode) {70 throw new TypeError(`The \`${optionName}.objectMode\` option cannot be \`false\` if \`new Duplex({objectMode: true})\` is used.`);71 }7273 return {74 ...stdioItem,75 value: {transform, writableObjectMode, readableObjectMode},76 };77};7879const normalizeTransformStream = ({stdioItem, stdioItem: {value}, index, newTransforms, direction}) => {80 const {transform, objectMode} = isPlainObj(value) ? value : {transform: value};81 const {writableObjectMode, readableObjectMode} = getTransformObjectModes(objectMode, index, newTransforms, direction);82 return ({83 ...stdioItem,84 value: {transform, writableObjectMode, readableObjectMode},85 });86};8788const normalizeGenerator = ({stdioItem, stdioItem: {value}, index, newTransforms, direction, encoding}) => {89 const {90 transform,91 final,92 binary: binaryOption = false,93 preserveNewlines = false,94 objectMode,95 } = isPlainObj(value) ? value : {transform: value};96 const binary = binaryOption || BINARY_ENCODINGS.has(encoding);97 const {writableObjectMode, readableObjectMode} = getTransformObjectModes(objectMode, index, newTransforms, direction);

Upgrade xo (#980)ehmickybefore adoptionccfa412

lines 98–108why, one step back
98 return {
99 ...stdioItem,100 value: {101 transform,102 final,103 binary,104 preserveNewlines,105 writableObjectMode,106 readableObjectMode,107 },108 };

Rename files (#977)ehmickybefore adoption4b2e316

lines 109–111why, one step back
109};
110111const sortTransforms = (newTransforms, direction) => direction === 'input' ? newTransforms.reverse() : newTransforms;