UNPKG

timezonecomplete

Version:

DateTime, TimeZone, Duration and Period library aimed at providing a consistent and complete date-time interface, away from the original JavaScript Date class.

211 lines 8.4 kB
/** * Functionality to parse a DateTime object to a string */ "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.tokenize = exports.TokenType = void 0; /** * Different types of tokens, each for a DateTime "period type" (like year, month, hour etc.) */ var TokenType; (function (TokenType) { /** * Raw text */ TokenType[TokenType["IDENTITY"] = 0] = "IDENTITY"; TokenType[TokenType["ERA"] = 1] = "ERA"; TokenType[TokenType["YEAR"] = 2] = "YEAR"; TokenType[TokenType["QUARTER"] = 3] = "QUARTER"; TokenType[TokenType["MONTH"] = 4] = "MONTH"; TokenType[TokenType["WEEK"] = 5] = "WEEK"; TokenType[TokenType["DAY"] = 6] = "DAY"; TokenType[TokenType["WEEKDAY"] = 7] = "WEEKDAY"; TokenType[TokenType["DAYPERIOD"] = 8] = "DAYPERIOD"; TokenType[TokenType["HOUR"] = 9] = "HOUR"; TokenType[TokenType["MINUTE"] = 10] = "MINUTE"; TokenType[TokenType["SECOND"] = 11] = "SECOND"; TokenType[TokenType["ZONE"] = 12] = "ZONE"; })(TokenType || (exports.TokenType = TokenType = {})); /** * Tokenize an LDML date/time format string * @param formatString the string to tokenize * @throws nothing */ function tokenize(formatString) { if (!formatString) { return []; } var result = []; var appendToken = function (tokenString, raw) { // The tokenString may be longer than supported for a tokentype, e.g. "hhhh" which would be TWO hour specs. // We greedily consume LDML specs while possible while (tokenString !== "") { if (raw || !SYMBOL_MAPPING.hasOwnProperty(tokenString[0])) { var token = { length: tokenString.length, raw: tokenString, symbol: tokenString[0], type: TokenType.IDENTITY }; result.push(token); tokenString = ""; } else { // depending on the type of token, different lengths may be supported var info = SYMBOL_MAPPING[tokenString[0]]; var length_1 = void 0; if (info.maxLength === undefined && (!Array.isArray(info.lengths) || info.lengths.length === 0)) { // everything is allowed length_1 = tokenString.length; } else if (info.maxLength !== undefined) { // greedily gobble up length_1 = Math.min(tokenString.length, info.maxLength); } else /* istanbul ignore else */ if (Array.isArray(info.lengths) && info.lengths.length > 0) { // find maximum allowed length for (var _i = 0, _a = info.lengths; _i < _a.length; _i++) { var l = _a[_i]; if (l <= tokenString.length && (length_1 === undefined || length_1 < l)) { length_1 = l; } } } /* istanbul ignore if */ if (length_1 === undefined) { // no allowed length found (not possible with current symbol mapping since length 1 is always allowed) var token = { length: tokenString.length, raw: tokenString, symbol: tokenString[0], type: TokenType.IDENTITY }; result.push(token); tokenString = ""; } else { // prefix found var token = { length: length_1, raw: tokenString.slice(0, length_1), symbol: tokenString[0], type: info.type }; result.push(token); tokenString = tokenString.slice(length_1); } } } }; var currentToken = ""; var previousChar = ""; var quoting = false; var possibleEscaping = false; for (var _i = 0, formatString_1 = formatString; _i < formatString_1.length; _i++) { var currentChar = formatString_1[_i]; // Hanlde escaping and quoting if (currentChar === "'") { if (!quoting) { if (possibleEscaping) { // Escaped a single ' character without quoting if (currentChar !== previousChar) { appendToken(currentToken); currentToken = ""; } currentToken += "'"; possibleEscaping = false; } else { possibleEscaping = true; } } else { // Two possibilities: Were are done quoting, or we are escaping a ' character if (possibleEscaping) { // Escaping, add ' to the token currentToken += currentChar; possibleEscaping = false; } else { // Maybe escaping, wait for next token if we are escaping possibleEscaping = true; } } if (!possibleEscaping) { // Current character is relevant, so save it for inspecting next round previousChar = currentChar; } continue; } else if (possibleEscaping) { quoting = !quoting; possibleEscaping = false; // Flush current token appendToken(currentToken, !quoting); currentToken = ""; } if (quoting) { // Quoting mode, add character to token. currentToken += currentChar; previousChar = currentChar; continue; } if (currentChar !== previousChar) { // We stumbled upon a new token! appendToken(currentToken); currentToken = currentChar; } else { // We are repeating the token with more characters currentToken += currentChar; } previousChar = currentChar; } // Don't forget to add the last token to the result! appendToken(currentToken, quoting); return result; } exports.tokenize = tokenize; var SYMBOL_MAPPING = { G: { type: TokenType.ERA, maxLength: 5 }, y: { type: TokenType.YEAR }, Y: { type: TokenType.YEAR }, u: { type: TokenType.YEAR }, U: { type: TokenType.YEAR, maxLength: 5 }, r: { type: TokenType.YEAR }, Q: { type: TokenType.QUARTER, maxLength: 5 }, q: { type: TokenType.QUARTER, maxLength: 5 }, M: { type: TokenType.MONTH, maxLength: 5 }, L: { type: TokenType.MONTH, maxLength: 5 }, l: { type: TokenType.MONTH, maxLength: 1 }, w: { type: TokenType.WEEK, maxLength: 2 }, W: { type: TokenType.WEEK, maxLength: 1 }, d: { type: TokenType.DAY, maxLength: 2 }, D: { type: TokenType.DAY, maxLength: 3 }, F: { type: TokenType.DAY, maxLength: 1 }, g: { type: TokenType.DAY }, E: { type: TokenType.WEEKDAY, maxLength: 6 }, e: { type: TokenType.WEEKDAY, maxLength: 6 }, c: { type: TokenType.WEEKDAY, maxLength: 6 }, a: { type: TokenType.DAYPERIOD, maxLength: 5 }, b: { type: TokenType.DAYPERIOD, maxLength: 5 }, B: { type: TokenType.DAYPERIOD, maxLength: 5 }, h: { type: TokenType.HOUR, maxLength: 2 }, H: { type: TokenType.HOUR, maxLength: 2 }, k: { type: TokenType.HOUR, maxLength: 2 }, K: { type: TokenType.HOUR, maxLength: 2 }, j: { type: TokenType.HOUR, maxLength: 6 }, J: { type: TokenType.HOUR, maxLength: 2 }, m: { type: TokenType.MINUTE, maxLength: 2 }, s: { type: TokenType.SECOND, maxLength: 2 }, S: { type: TokenType.SECOND }, A: { type: TokenType.SECOND }, z: { type: TokenType.ZONE, maxLength: 4 }, Z: { type: TokenType.ZONE, maxLength: 5 }, O: { type: TokenType.ZONE, lengths: [1, 4] }, v: { type: TokenType.ZONE, lengths: [1, 4] }, V: { type: TokenType.ZONE, maxLength: 4 }, X: { type: TokenType.ZONE, maxLength: 5 }, x: { type: TokenType.ZONE, maxLength: 5 }, }; //# sourceMappingURL=token.js.map