From: Jukka Kurkela Date: Sun, 21 Mar 2021 15:05:20 +0000 (+0200) Subject: Generalize toTRBL and toTRBLCorners (#8686) X-Git-Tag: v3.0.0-rc.2~3 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=7e8e7f01374d2c3e7633d4acf1d1e38928ba2a07;p=thirdparty%2FChart.js.git Generalize toTRBL and toTRBLCorners (#8686) --- diff --git a/src/helpers/helpers.options.js b/src/helpers/helpers.options.js index 829583183..6286b3e32 100644 --- a/src/helpers/helpers.options.js +++ b/src/helpers/helpers.options.js @@ -38,7 +38,22 @@ export function toLineHeight(value, size) { } const numberOrZero = v => +v || 0; -const numberOrZero2 = (v1, v2) => numberOrZero(valueOrDefault(v1, v2)); + +function readValueToProps(value, props) { + const ret = {}; + const objProps = isObject(props); + const keys = objProps ? Object.keys(props) : props; + const read = isObject(value) + ? objProps + ? prop => valueOrDefault(value[prop], value[props[prop]]) + : prop => value[prop] + : () => value; + + for (const prop of keys) { + ret[prop] = numberOrZero(read(prop)); + } + return ret; +} /** * Converts the given value into a TRBL object. @@ -49,24 +64,7 @@ const numberOrZero2 = (v1, v2) => numberOrZero(valueOrDefault(v1, v2)); * @since 3.0.0 */ export function toTRBL(value) { - let t, r, b, l; - - if (isObject(value)) { - const {x, y} = value; - t = numberOrZero2(value.top, y); - r = numberOrZero2(value.right, x); - b = numberOrZero2(value.bottom, y); - l = numberOrZero2(value.left, x); - } else { - t = r = b = l = numberOrZero(value); - } - - return { - top: t, - right: r, - bottom: b, - left: l - }; + return readValueToProps(value, {top: 'y', right: 'x', bottom: 'y', left: 'x'}); } /** @@ -77,23 +75,7 @@ export function toTRBL(value) { * @since 3.0.0 */ export function toTRBLCorners(value) { - let tl, tr, bl, br; - - if (isObject(value)) { - tl = numberOrZero(value.topLeft); - tr = numberOrZero(value.topRight); - bl = numberOrZero(value.bottomLeft); - br = numberOrZero(value.bottomRight); - } else { - tl = tr = bl = br = numberOrZero(value); - } - - return { - topLeft: tl, - topRight: tr, - bottomLeft: bl, - bottomRight: br - }; + return readValueToProps(value, ['topLeft', 'topRight', 'bottomLeft', 'bottomRight']); } /**