@opra/common
Version:
Opra common package
35 lines (34 loc) • 1.25 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.TimeLiteral = void 0;
const errors_js_1 = require("../../errors.js");
const utils_js_1 = require("../../utils.js");
const literal_js_1 = require("../abstract/literal.js");
const TIME_PATTERN = /^([01]\d|2[0-3]):([0-5]\d)(:[0-5]\d)?(\.(\d+))?$/;
class TimeLiteral extends literal_js_1.Literal {
constructor(value) {
super('');
if (value instanceof Date) {
this.value =
pad(value.getHours()) +
':' +
pad(value.getMinutes()) +
(value.getSeconds() ? ':' + pad(value.getSeconds()) : '') +
(value.getMilliseconds() ? '.' + pad(value.getMilliseconds()) : '');
return;
}
// noinspection SuspiciousTypeOfGuard
if (typeof value === 'string' && TIME_PATTERN.test(value)) {
this.value = value;
return;
}
throw new errors_js_1.FilterValidationError(`Invalid time value "${value}"`);
}
toString() {
return (0, utils_js_1.quoteFilterString)(this.value);
}
}
exports.TimeLiteral = TimeLiteral;
function pad(n) {
return n <= 9 ? '0' + n : '' + n;
}