UNPKG

@phensley/cldr-core

Version:
127 lines 5.33 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var autogen_calprefs_1 = require("./autogen.calprefs"); var autogen_weekdata_1 = require("./autogen.weekdata"); var formatter_1 = require("./formatter"); var cache_1 = require("../../utils/cache"); var date_1 = require("../../parsing/patterns/date"); var rules_1 = require("./rules"); /** * Framework scoped calendar functions. */ var CalendarInternalsImpl = /** @class */ (function () { function CalendarInternalsImpl(internals, cacheSize) { if (cacheSize === void 0) { cacheSize = 50; } var _this = this; this.internals = internals; this.cacheSize = cacheSize; this.schema = internals.schema; this.dayPeriodRules = new rules_1.DayPeriodRules(cacheSize); this.patternCache = new cache_1.Cache(date_1.parseDatePattern, cacheSize); this.hourPatternCache = new cache_1.Cache(function (raw) { var parts = raw.split(';'); return [_this.patternCache.get(parts[0]), _this.patternCache.get(parts[1])]; }, cacheSize); this.calendarFormatterCache = new cache_1.Cache(function (calendar) { var s; switch (calendar) { case 'buddhist': s = _this.schema.Buddhist; break; case 'japanese': s = _this.schema.Japanese; break; case 'persian': s = _this.schema.Persian; break; case 'iso8601': case 'gregory': default: s = _this.schema.Gregorian; break; } return new formatter_1.CalendarFormatter(_this.internals, s); }, cacheSize); } CalendarInternalsImpl.prototype.flexDayPeriod = function (bundle, minutes) { return this.dayPeriodRules.get(bundle, minutes); }; CalendarInternalsImpl.prototype.getCalendarFormatter = function (type) { return this.calendarFormatterCache.get(type); }; CalendarInternalsImpl.prototype.parseDatePattern = function (raw) { return this.patternCache.get(raw); }; CalendarInternalsImpl.prototype.getHourPattern = function (raw, negative) { var patterns = this.hourPatternCache.get(raw); return patterns[negative ? 0 : 1]; }; CalendarInternalsImpl.prototype.weekFirstDay = function (region) { return autogen_weekdata_1.weekFirstDay[region] || autogen_weekdata_1.weekFirstDay['001']; }; CalendarInternalsImpl.prototype.weekMinDays = function (region) { return autogen_weekdata_1.weekMinDays[region] || autogen_weekdata_1.weekMinDays['001']; }; CalendarInternalsImpl.prototype.formatDateTime = function (calendar, ctx, renderer, date, time, wrapper) { var formatter = this.getCalendarFormatter(calendar); var _date; var _time; if (date) { formatter.format(renderer, ctx, date); _date = renderer.get(); } if (time) { formatter.format(renderer, ctx, time); _time = renderer.get(); } if (_date && _time && wrapper) { var pattern = this.internals.wrapper.parseWrapper(wrapper); renderer.wrap(pattern, [_time, _date]); return renderer.get(); } return _date ? _date : _time ? _time : renderer.empty(); }; CalendarInternalsImpl.prototype.formatInterval = function (calendar, bundle, params, renderer, start, end, pattern) { var idx = date_1.intervalPatternBoundary(pattern); var ctx = { date: start, bundle: bundle, system: params.system, latnSystem: params.latnSystem }; var s = this.formatDateTime(calendar, ctx, renderer, pattern.slice(0, idx)); ctx.date = end; var e = this.formatDateTime(calendar, ctx, renderer, pattern.slice(idx)); return renderer.join(s, e); }; CalendarInternalsImpl.prototype.selectCalendar = function (bundle, ca) { var calendar = this.supportedCalendar(ca) || this.supportedCalendar(bundle.calendarSystem()); if (!calendar) { var prefs = autogen_calprefs_1.calendarPrefData[bundle.region()] || autogen_calprefs_1.calendarPrefData['001']; for (var _i = 0, prefs_1 = prefs; _i < prefs_1.length; _i++) { var id = prefs_1[_i]; calendar = this.supportedCalendar(autogen_calprefs_1.calendarIds[id]); if (calendar) { return calendar; } } return 'gregory'; } return calendar; }; /** * Translates a string into a supported calendar type, or undefined if none match. */ CalendarInternalsImpl.prototype.supportedCalendar = function (c) { switch (c) { case 'buddhist': case 'iso8601': case 'japanese': case 'persian': case 'gregory': return c; case 'gregorian': return 'gregory'; default: return undefined; } }; return CalendarInternalsImpl; }()); exports.CalendarInternalsImpl = CalendarInternalsImpl; //# sourceMappingURL=internal.js.map