UNPKG

@wordpress/components

Version:
53 lines (52 loc) • 1.45 kB
// packages/components/src/utils/values.ts /** * Determines if a value is null or undefined. * * @param value The value to check. * @return Whether value is not null or undefined. */ function isValueDefined(value) { return value !== void 0 && value !== null; } /** * Determines if a value is empty, null, or undefined. * * @param value The value to check. * @return Whether value is empty. */ function isValueEmpty(value) { const isEmptyString = value === ""; return !isValueDefined(value) || isEmptyString; } /** * Get the first defined/non-null value from an array. * * @param values Values to derive from. * @param fallbackValue Fallback value if there are no defined values. * @return A defined value or the fallback value. */ function getDefinedValue(values = [], fallbackValue) { return values.find(isValueDefined) ?? fallbackValue; } /** * Converts a string to a number. * * @param value * @return String as a number. */ const stringToNumber = (value) => { return parseFloat(value); }; /** * Regardless of the input being a string or a number, returns a number. * * Returns `undefined` in case the string is `undefined` or not a valid numeric value. * * @param value * @return The parsed number. */ const ensureNumber = (value) => { return typeof value === "string" ? stringToNumber(value) : value; }; export { ensureNumber, getDefinedValue, isValueDefined, isValueEmpty, stringToNumber }; //# sourceMappingURL=values.mjs.map