@gleif-it/did-webs-ts
Version:
did-webs typescript library
27 lines (26 loc) • 1.05 kB
JavaScript
const isValidThreshold = (threshold) => {
if (isNumericThreshold(threshold)) {
// Check if the string is a valid integer
const num = parseInt(threshold, 10);
return !isNaN(num) && num >= 1;
}
else if (isFractionalThreshold(threshold)) {
// Check if the array is a valid fractional threshold
return threshold.every((fraction) => {
const parts = fraction.split('/');
if (parts.length !== 2)
return false;
const numerator = parseInt(parts[0], 10);
const denominator = parseInt(parts[1], 10);
return !isNaN(numerator) && !isNaN(denominator) && denominator > 0;
});
}
return false;
};
export const isNumericThreshold = (threshold) => typeof threshold === 'string';
export const isFractionalThreshold = (threshold) => Array.isArray(threshold);
export const createThreshold = (threshold) => isValidThreshold(threshold)
? threshold
: (() => {
throw new Error('Invalid threshold format');
})();