You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
71 lines
2.1 KiB
JavaScript
71 lines
2.1 KiB
JavaScript
(function (root, factory) {
|
|
'use strict';
|
|
const api = factory();
|
|
if (typeof module === 'object' && module.exports) module.exports = api;
|
|
if (root) root.VenixAdminConfigUnits = api;
|
|
})(typeof globalThis !== 'undefined' ? globalThis : this, function () {
|
|
'use strict';
|
|
|
|
const definitions = Object.freeze({
|
|
minutes: Object.freeze({ multiplier: 1, max: 10080 }),
|
|
hours: Object.freeze({ multiplier: 60, max: 168 }),
|
|
days: Object.freeze({ multiplier: 1440, max: 7 }),
|
|
});
|
|
|
|
function definition(unit) {
|
|
if (!Object.prototype.hasOwnProperty.call(definitions, unit)) {
|
|
throw new RangeError('账单支付期限单位无效');
|
|
}
|
|
return definitions[unit];
|
|
}
|
|
|
|
function positiveInteger(value) {
|
|
const raw = String(value == null ? '' : value).trim();
|
|
if (!/^[1-9][0-9]*$/.test(raw)) {
|
|
throw new RangeError('账单支付期限请输入正整数');
|
|
}
|
|
const amount = Number(raw);
|
|
if (!Number.isSafeInteger(amount)) {
|
|
throw new RangeError('账单支付期限超出允许范围');
|
|
}
|
|
return amount;
|
|
}
|
|
|
|
function maxValue(unit) {
|
|
return definition(unit).max;
|
|
}
|
|
|
|
function toMinutes(value, unit) {
|
|
const amount = positiveInteger(value);
|
|
const selected = definition(unit);
|
|
if (amount > selected.max) {
|
|
throw new RangeError('账单支付期限超出允许范围');
|
|
}
|
|
return amount * selected.multiplier;
|
|
}
|
|
|
|
function fromMinutes(value) {
|
|
const minutes = positiveInteger(value);
|
|
if (minutes > definitions.minutes.max) {
|
|
throw new RangeError('账单支付期限超出允许范围');
|
|
}
|
|
if (minutes % definitions.days.multiplier === 0) {
|
|
return Object.freeze({
|
|
value: minutes / definitions.days.multiplier,
|
|
unit: 'days',
|
|
minutes,
|
|
});
|
|
}
|
|
if (minutes % definitions.hours.multiplier === 0) {
|
|
return Object.freeze({
|
|
value: minutes / definitions.hours.multiplier,
|
|
unit: 'hours',
|
|
minutes,
|
|
});
|
|
}
|
|
return Object.freeze({ value: minutes, unit: 'minutes', minutes });
|
|
}
|
|
|
|
return Object.freeze({ fromMinutes, maxValue, toMinutes });
|
|
});
|