@wordpress/components
Version:
UI components for WordPress.
115 lines (99 loc) • 2.9 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.getAllValue = getAllValue;
exports.isValuesMixed = isValuesMixed;
exports.isValuesDefined = isValuesDefined;
exports.DEFAULT_VISUALIZER_VALUES = exports.DEFAULT_VALUES = exports.LABELS = void 0;
var _lodash = require("lodash");
var _i18n = require("@wordpress/i18n");
var _utils = require("../unit-control/utils");
/**
* External dependencies
*/
/**
* WordPress dependencies
*/
/**
* Internal dependencies
*/
const LABELS = {
all: (0, _i18n.__)('All'),
top: (0, _i18n.__)('Top'),
bottom: (0, _i18n.__)('Bottom'),
left: (0, _i18n.__)('Left'),
right: (0, _i18n.__)('Right'),
mixed: (0, _i18n.__)('Mixed')
};
exports.LABELS = LABELS;
const DEFAULT_VALUES = {
top: null,
right: null,
bottom: null,
left: null
};
exports.DEFAULT_VALUES = DEFAULT_VALUES;
const DEFAULT_VISUALIZER_VALUES = {
top: false,
right: false,
bottom: false,
left: false
};
/**
* Gets an items with the most occurance within an array
* https://stackoverflow.com/a/20762713
*
* @param {Array<any>} arr Array of items to check.
* @return {any} The item with the most occurances.
*/
exports.DEFAULT_VISUALIZER_VALUES = DEFAULT_VISUALIZER_VALUES;
function mode(arr) {
return arr.sort((a, b) => arr.filter(v => v === a).length - arr.filter(v => v === b).length).pop();
}
/**
* Gets the 'all' input value and unit from values data.
*
* @param {Object} values Box values.
* @return {string} A value + unit for the 'all' input.
*/
function getAllValue(values = {}) {
const parsedValues = Object.values(values).map(value => (0, _utils.parseUnit)(value));
const allValues = parsedValues.map(value => value[0]);
const allUnits = parsedValues.map(value => value[1]);
const value = allValues.every(v => v === allValues[0]) ? allValues[0] : '';
const unit = mode(allUnits);
/**
* The isNumber check is important. On reset actions, the incoming value
* may be null or an empty string.
*
* Also, the value may also be zero (0), which is considered a valid unit value.
*
* isNumber() is more specific for these cases, rather than relying on a
* simple truthy check.
*/
const allValue = (0, _lodash.isNumber)(value) ? `${value}${unit}` : null;
return allValue;
}
/**
* Checks to determine if values are mixed.
*
* @param {Object} values Box values.
* @return {boolean} Whether values are mixed.
*/
function isValuesMixed(values = {}) {
const allValue = getAllValue(values);
const isMixed = isNaN(parseFloat(allValue));
return isMixed;
}
/**
* Checks to determine if values are defined.
*
* @param {Object} values Box values.
*
* @return {boolean} Whether values are mixed.
*/
function isValuesDefined(values) {
return values !== undefined && !(0, _lodash.isEmpty)(Object.values(values).filter(Boolean));
}
//# sourceMappingURL=utils.js.map