@gleif-it/did-webs-ts
Version:
did-webs typescript library
33 lines (32 loc) • 1.37 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.createThreshold = exports.isFractionalThreshold = exports.isNumericThreshold = void 0;
const isValidThreshold = (threshold) => {
if ((0, exports.isNumericThreshold)(threshold)) {
// Check if the string is a valid integer
const num = parseInt(threshold, 10);
return !isNaN(num) && num >= 1;
}
else if ((0, exports.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;
};
const isNumericThreshold = (threshold) => typeof threshold === 'string';
exports.isNumericThreshold = isNumericThreshold;
const isFractionalThreshold = (threshold) => Array.isArray(threshold);
exports.isFractionalThreshold = isFractionalThreshold;
const createThreshold = (threshold) => isValidThreshold(threshold)
? threshold
: (() => {
throw new Error('Invalid threshold format');
})();
exports.createThreshold = createThreshold;