Javascript - 泛化一个可以在相同上下文中以两种不同方式使用的函数
Javascript - Generalize a function that can be used in two different ways but in the same context
代码
我实现了一个“responsify”维度(响应维度)的功能。
这是我目前的方法:
export default (originalDimensions, finalWidth = WINDOW_WIDTH) => {
const responsiveWidth = finalWidth;
const responsiveHeight = ruleOfThree(
originalDimensions.height,
originalDimensions.width,
responsiveWidth
);
return {
width: responsiveWidth,
height: responsiveHeight,
};
};
/* Helper */
function ruleOfThree (originalX, originalY, finalY, direct = true) => {
if (direct) {
return (originalX * finalY) / originalY;
}
return (originalY * finalY) / originalX;
};
问题
如您所见,它只是 returns 具有响应尺寸的对象。我遇到的主要问题是它只使给定的尺寸适应给定的“finalWidth”。但是,如果我收到的不是 finalWidth,而是最终高度(以便该方法将原始宽度调整为最终高度)怎么办?
如何为该方法增加灵活性,以便能够解决该情况而无需创建另一种不同的方法?
有什么设计模式或想法吗?
备注
如果我按如下方式传递最终高度(而不是最终宽度):responsifyDimensions(photo.dimensions, /*finalHeight = 100*/)
那么,该方法必须做:
export default (originalDimensions, finalHeight = WINDOW_HEIGHT) => {
const responsiveHeight = finalHeight; <----
const responsiveWidth = ruleOfThree(
originalDimensions.width,
originalDimensions.height, <-----
responsiveHeight <-----
);
return {
width: responsiveWidth,
height: responsiveHeight,
};
}
代码看起来非常相似...所以我想可能有一种简单的方法可以使原始方法更加“灵活”
我会在最后一个参数中进行对象解构,然后,根据设置的值,我会切换 width/height,如下所示:
export default (originalDimensions, { finalWidth, finalHeight } = {}) => {
if (finalHeight) {
return {
width: ruleOfThree(
originalDimensions.height,
originalDimensions.width,
finalHeight
),
height: finalHeight,
};
}
const width = finalWidth ?? WINDOW_WIDTH;
return {
width,
height: ruleOfThree(
originalDimensions.width,
originalDimensions.height,
width
),
};
};
/* Helper */
function ruleOfThree (originalX, originalY, finalY, direct = true) => {
if (direct) {
return (originalX * finalY) / originalY;
}
return (originalY * finalY) / originalX;
};
/* usage */
// Fit width
const dimsWidth = responsifyDimensions(photo.dimensions, { finalWidth: 100 });
// Fit height
const dimsHeight = responsifyDimensions(photo.dimensions, { finalHeight: 100 });
// Fit default (WINDOW_WIDTH as width)
const dimsDefault = responsifyDimensions(photo.dimensions);
代码
我实现了一个“responsify”维度(响应维度)的功能。
这是我目前的方法:
export default (originalDimensions, finalWidth = WINDOW_WIDTH) => {
const responsiveWidth = finalWidth;
const responsiveHeight = ruleOfThree(
originalDimensions.height,
originalDimensions.width,
responsiveWidth
);
return {
width: responsiveWidth,
height: responsiveHeight,
};
};
/* Helper */
function ruleOfThree (originalX, originalY, finalY, direct = true) => {
if (direct) {
return (originalX * finalY) / originalY;
}
return (originalY * finalY) / originalX;
};
问题
如您所见,它只是 returns 具有响应尺寸的对象。我遇到的主要问题是它只使给定的尺寸适应给定的“finalWidth”。但是,如果我收到的不是 finalWidth,而是最终高度(以便该方法将原始宽度调整为最终高度)怎么办?
如何为该方法增加灵活性,以便能够解决该情况而无需创建另一种不同的方法?
有什么设计模式或想法吗?
备注
如果我按如下方式传递最终高度(而不是最终宽度):responsifyDimensions(photo.dimensions, /*finalHeight = 100*/)
那么,该方法必须做:
export default (originalDimensions, finalHeight = WINDOW_HEIGHT) => {
const responsiveHeight = finalHeight; <----
const responsiveWidth = ruleOfThree(
originalDimensions.width,
originalDimensions.height, <-----
responsiveHeight <-----
);
return {
width: responsiveWidth,
height: responsiveHeight,
};
}
代码看起来非常相似...所以我想可能有一种简单的方法可以使原始方法更加“灵活”
我会在最后一个参数中进行对象解构,然后,根据设置的值,我会切换 width/height,如下所示:
export default (originalDimensions, { finalWidth, finalHeight } = {}) => {
if (finalHeight) {
return {
width: ruleOfThree(
originalDimensions.height,
originalDimensions.width,
finalHeight
),
height: finalHeight,
};
}
const width = finalWidth ?? WINDOW_WIDTH;
return {
width,
height: ruleOfThree(
originalDimensions.width,
originalDimensions.height,
width
),
};
};
/* Helper */
function ruleOfThree (originalX, originalY, finalY, direct = true) => {
if (direct) {
return (originalX * finalY) / originalY;
}
return (originalY * finalY) / originalX;
};
/* usage */
// Fit width
const dimsWidth = responsifyDimensions(photo.dimensions, { finalWidth: 100 });
// Fit height
const dimsHeight = responsifyDimensions(photo.dimensions, { finalHeight: 100 });
// Fit default (WINDOW_WIDTH as width)
const dimsDefault = responsifyDimensions(photo.dimensions);