@awsui/components-react
Version:
On July 19th, 2022, we launched [Cloudscape Design System](https://cloudscape.design). Cloudscape is an evolution of AWS-UI. It consists of user interface guidelines, front-end components, design resources, and development tools for building intuitive, en
46 lines • 1.81 kB
JavaScript
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
export function getPercent(value, range) {
return ((value - range[0]) / (range[1] - range[0])) * 100;
}
function countDecimals(value) {
var _a;
if (Math.floor(value) === value) {
return 0;
}
const str = Math.abs(value).toString();
// very small numbers, e.g. 1e-9
if (str.indexOf('-') !== -1) {
return parseInt(str.split('-')[1], 10) || 0;
}
return ((_a = str.split('.')[1]) === null || _a === void 0 ? void 0 : _a.length) || 0;
}
export const getStepArray = (step, [min, max]) => {
const steps = [min];
// JS struggles with rounding errors when using decimals, so include a multiplier
// to make step calculations integer-based
const multiplier = Math.pow(10, countDecimals(step));
let currentStep = min;
while (currentStep < max) {
currentStep = (multiplier * currentStep + multiplier * step) / multiplier;
if (currentStep <= max) {
steps.push(currentStep);
}
}
return steps;
};
export const findLowerAndHigherValues = (array, value) => {
let sortedArray = [...array];
sortedArray = sortedArray.sort((a, b) => a - b);
const index = sortedArray.indexOf(value) || 0;
const lower = sortedArray[index - 1] || undefined;
const higher = sortedArray[index + 1] || undefined;
return { lower, higher };
};
export const valuesAreValid = (referenceValues) => {
const valuesWithDecimals = referenceValues === null || referenceValues === void 0 ? void 0 : referenceValues.filter(value => !Number.isInteger(value));
return valuesWithDecimals.length === 0;
};
export const THUMB_SIZE = 16;
export const THUMB_READONLY_SIZE = 12;
//# sourceMappingURL=utils.js.map