UNPKG

devextreme

Version:

JavaScript/TypeScript Component Suite for Responsive Web Development

181 lines (180 loc) • 5.26 kB
/** * DevExtreme (cjs/__internal/ui/date_box/date_box.mask.parts.js) * Version: 25.2.8 * Build date: Mon Jun 08 2026 * * Copyright (c) 2012 - 2026 Developer Express Inc. ALL RIGHTS RESERVED * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/ */ "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.renderDateParts = exports.getDatePartIndexByPosition = void 0; var _date = require("../../../common/core/localization/ldml/date.parser"); var _common = require("../../../core/utils/common"); var _extend = require("../../../core/utils/extend"); var _math = require("../../../core/utils/math"); const getLimits = (pattern, date, forcedPattern) => { const limits = { y: { min: 0, max: 9999 }, M: { min: 1, max: 12 }, L: { min: 1, max: 12 }, d: { min: 1, max: 31 }, dM: { min: 1, max: new Date(date.getFullYear(), date.getMonth() + 1, 0).getDate() }, E: { min: 0, max: 6 }, H: { min: 0, max: 23 }, h: { min: 1, max: 12 }, m: { min: 0, max: 59 }, s: { min: 0, max: 59 }, S: { min: 0, max: 999 }, a: { min: 0, max: 1 }, x: { min: 0, max: 0 } }; return limits[forcedPattern ?? pattern] ?? { min: 0, max: 0 } }; const monthGetter = date => date.getMonth() + 1; const monthSetter = (date, value) => { const day = date.getDate(); const monthLimits = getLimits("M", date); const newValue = (0, _math.fitIntoRange)(+value, monthLimits.min, monthLimits.max); date.setMonth(newValue - 1, 1); const { min: min, max: max } = getLimits("dM", date); const newDay = (0, _math.fitIntoRange)(day, min, max); date.setDate(newDay) }; const PATTERN_GETTERS = { a: date => date.getHours() < 12 ? 0 : 1, E: "getDay", y: "getFullYear", M: monthGetter, L: monthGetter, d: "getDate", H: "getHours", h: "getHours", m: "getMinutes", s: "getSeconds", S: "getMilliseconds", x: "getTimezoneOffset" }; const PATTERN_SETTERS = (0, _extend.extend)({}, (0, _date.getPatternSetters)(), { a: (date, value) => { const hours = date.getHours(); const current = hours >= 12; if (current === !!+value) { return } date.setHours((hours + 12) % 24) }, d: (date, value) => { const lastDayInMonth = getLimits("dM", date).max; if (+value > lastDayInMonth) { date.setMonth(date.getMonth() + 1) } date.setDate(+value) }, h: (date, value) => { const isPM = date.getHours() >= 12; date.setHours(+value % 12 + (isPM ? 12 : 0)) }, M: monthSetter, L: monthSetter, E: (date, value) => { if (+value < 0) { return } date.setDate(date.getDate() - date.getDay() + +value) }, y: (date, value) => { const currentYear = date.getFullYear(); const valueLength = String(value).length; const maxLimitLength = String(getLimits("y", date).max).length; const yearPrefix = String(currentYear).substr(0, maxLimitLength - valueLength); const newValue = parseInt(yearPrefix + value, 10); date.setFullYear(newValue) }, x: date => date }); const getPatternGetter = patternChar => PATTERN_GETTERS[patternChar] ?? (() => patternChar); const renderDateParts = (text, regExpInfo) => { const result = regExpInfo.regexp.exec(text) ?? []; let start = 0; let end = 0; const sections = []; for (let i = 1; i < result.length; i += 1) { start = end; end = start + result[i].length; const pattern = regExpInfo.patterns[i - 1].replace(/^'|'$/g, ""); const getter = getPatternGetter(pattern[0]); sections.push({ index: i - 1, isStub: pattern === result[i], caret: { start: start, end: end }, pattern: pattern, text: result[i], limits: (date, forcedPattern) => getLimits(pattern[0], date, forcedPattern), setter: PATTERN_SETTERS[pattern[0]] ?? _common.noop, getter: getter }) } return sections }; exports.renderDateParts = renderDateParts; const getDatePartIndexByPosition = (dateParts, position) => { for (let i = 0; i < dateParts.length; i += 1) { const caretInGroup = dateParts[i].caret.end >= position; if (!dateParts[i].isStub && caretInGroup) { return i } } return null }; exports.getDatePartIndexByPosition = getDatePartIndexByPosition;