172 lines
5.1 KiB
JavaScript
172 lines
5.1 KiB
JavaScript
import { d as _inherits, e as _createSuper, a as _classCallCheck, _ as _createClass, g as _get, h as _getPrototypeOf, f as _slicedToArray } from '../_rollupPluginBabelHelpers-b054ecd2.js';
|
|
import MaskedPattern from './pattern.js';
|
|
import { normalizePrepare } from '../core/utils.js';
|
|
import IMask from '../core/holder.js';
|
|
import '../core/change-details.js';
|
|
import './base.js';
|
|
import '../core/continuous-tail-details.js';
|
|
import './pattern/input-definition.js';
|
|
import './factory.js';
|
|
import './pattern/fixed-definition.js';
|
|
import './pattern/chunk-tail-details.js';
|
|
import './pattern/cursor.js';
|
|
import './regexp.js';
|
|
|
|
/** Pattern which accepts ranges */
|
|
|
|
var MaskedRange = /*#__PURE__*/function (_MaskedPattern) {
|
|
_inherits(MaskedRange, _MaskedPattern);
|
|
|
|
var _super = _createSuper(MaskedRange);
|
|
|
|
function MaskedRange() {
|
|
_classCallCheck(this, MaskedRange);
|
|
|
|
return _super.apply(this, arguments);
|
|
}
|
|
|
|
_createClass(MaskedRange, [{
|
|
key: "_matchFrom",
|
|
get:
|
|
/**
|
|
Optionally sets max length of pattern.
|
|
Used when pattern length is longer then `to` param length. Pads zeros at start in this case.
|
|
*/
|
|
|
|
/** Min bound */
|
|
|
|
/** Max bound */
|
|
|
|
/** */
|
|
function get() {
|
|
return this.maxLength - String(this.from).length;
|
|
}
|
|
/**
|
|
@override
|
|
*/
|
|
|
|
}, {
|
|
key: "_update",
|
|
value: function _update(opts) {
|
|
// TODO type
|
|
opts = Object.assign({
|
|
to: this.to || 0,
|
|
from: this.from || 0,
|
|
maxLength: this.maxLength || 0
|
|
}, opts);
|
|
var maxLength = String(opts.to).length;
|
|
if (opts.maxLength != null) maxLength = Math.max(maxLength, opts.maxLength);
|
|
opts.maxLength = maxLength;
|
|
var fromStr = String(opts.from).padStart(maxLength, '0');
|
|
var toStr = String(opts.to).padStart(maxLength, '0');
|
|
var sameCharsCount = 0;
|
|
|
|
while (sameCharsCount < toStr.length && toStr[sameCharsCount] === fromStr[sameCharsCount]) {
|
|
++sameCharsCount;
|
|
}
|
|
|
|
opts.mask = toStr.slice(0, sameCharsCount).replace(/0/g, '\\0') + '0'.repeat(maxLength - sameCharsCount);
|
|
|
|
_get(_getPrototypeOf(MaskedRange.prototype), "_update", this).call(this, opts);
|
|
}
|
|
/**
|
|
@override
|
|
*/
|
|
|
|
}, {
|
|
key: "isComplete",
|
|
get: function get() {
|
|
return _get(_getPrototypeOf(MaskedRange.prototype), "isComplete", this) && Boolean(this.value);
|
|
}
|
|
}, {
|
|
key: "boundaries",
|
|
value: function boundaries(str) {
|
|
var minstr = '';
|
|
var maxstr = '';
|
|
|
|
var _ref = str.match(/^(\D*)(\d*)(\D*)/) || [],
|
|
_ref2 = _slicedToArray(_ref, 3),
|
|
placeholder = _ref2[1],
|
|
num = _ref2[2];
|
|
|
|
if (num) {
|
|
minstr = '0'.repeat(placeholder.length) + num;
|
|
maxstr = '9'.repeat(placeholder.length) + num;
|
|
}
|
|
|
|
minstr = minstr.padEnd(this.maxLength, '0');
|
|
maxstr = maxstr.padEnd(this.maxLength, '9');
|
|
return [minstr, maxstr];
|
|
} // TODO str is a single char everytime
|
|
|
|
/**
|
|
@override
|
|
*/
|
|
|
|
}, {
|
|
key: "doPrepare",
|
|
value: function doPrepare(ch) {
|
|
var flags = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
var details;
|
|
|
|
var _normalizePrepare = normalizePrepare(_get(_getPrototypeOf(MaskedRange.prototype), "doPrepare", this).call(this, ch.replace(/\D/g, ''), flags));
|
|
|
|
var _normalizePrepare2 = _slicedToArray(_normalizePrepare, 2);
|
|
|
|
ch = _normalizePrepare2[0];
|
|
details = _normalizePrepare2[1];
|
|
if (!this.autofix || !ch) return ch;
|
|
var fromStr = String(this.from).padStart(this.maxLength, '0');
|
|
var toStr = String(this.to).padStart(this.maxLength, '0');
|
|
var nextVal = this.value + ch;
|
|
if (nextVal.length > this.maxLength) return '';
|
|
|
|
var _this$boundaries = this.boundaries(nextVal),
|
|
_this$boundaries2 = _slicedToArray(_this$boundaries, 2),
|
|
minstr = _this$boundaries2[0],
|
|
maxstr = _this$boundaries2[1];
|
|
|
|
if (Number(maxstr) < this.from) return fromStr[nextVal.length - 1];
|
|
|
|
if (Number(minstr) > this.to) {
|
|
if (this.autofix === 'pad' && nextVal.length < this.maxLength) {
|
|
return ['', details.aggregate(this.append(fromStr[nextVal.length - 1] + ch, flags))];
|
|
}
|
|
|
|
return toStr[nextVal.length - 1];
|
|
}
|
|
|
|
return ch;
|
|
}
|
|
/**
|
|
@override
|
|
*/
|
|
|
|
}, {
|
|
key: "doValidate",
|
|
value: function doValidate() {
|
|
var _get2;
|
|
|
|
var str = this.value;
|
|
var firstNonZero = str.search(/[^0]/);
|
|
if (firstNonZero === -1 && str.length <= this._matchFrom) return true;
|
|
|
|
var _this$boundaries3 = this.boundaries(str),
|
|
_this$boundaries4 = _slicedToArray(_this$boundaries3, 2),
|
|
minstr = _this$boundaries4[0],
|
|
maxstr = _this$boundaries4[1];
|
|
|
|
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
|
|
args[_key] = arguments[_key];
|
|
}
|
|
|
|
return this.from <= Number(maxstr) && Number(minstr) <= this.to && (_get2 = _get(_getPrototypeOf(MaskedRange.prototype), "doValidate", this)).call.apply(_get2, [this].concat(args));
|
|
}
|
|
}]);
|
|
|
|
return MaskedRange;
|
|
}(MaskedPattern);
|
|
IMask.MaskedRange = MaskedRange;
|
|
|
|
export { MaskedRange as default };
|