diff --git a/src/datetime.js b/src/datetime.js index 4d37ba6ad..d57945e80 100644 --- a/src/datetime.js +++ b/src/datetime.js @@ -22,7 +22,7 @@ import { import { normalizeZone } from "./impl/zoneUtil.js"; import diff from "./impl/diff.js"; import { parseRFC2822Date, parseISODate, parseHTTPDate, parseSQL } from "./impl/regexParser.js"; -import { parseFromTokens, explainFromTokens } from "./impl/tokenParser.js"; +import { parseFromTokens, explainFromTokens, formatOptsToTokens } from "./impl/tokenParser.js"; import { gregorianToWeek, weekToGregorian, @@ -864,7 +864,7 @@ export default class DateTime { /** * Create an invalid DateTime. - * @param {string} reason - simple string of why this DateTime is invalid. Should not contain parameters or anything else data-dependent + * @param {DateTime} reason - simple string of why this DateTime is invalid. Should not contain parameters or anything else data-dependent * @param {string} [explanation=null] - longer explanation, may include parameters and other useful debugging information * @return {DateTime} */ @@ -891,6 +891,21 @@ export default class DateTime { return (o && o.isLuxonDateTime) || false; } + /** + * @param formatOpts + * @param localeOpts + * @returns {string} + */ + static parseFormatForOpts(formatOpts, localeOpts = {}) { + const tokenList = formatOptsToTokens(formatOpts, Locale.fromObject(localeOpts)); + return !tokenList + ? null + : tokenList + .map(t => (t ? t.val : null)) + .join("") + .trim(); + } + // INFO /** diff --git a/src/impl/tokenParser.js b/src/impl/tokenParser.js index 4ca0401c1..434320170 100644 --- a/src/impl/tokenParser.js +++ b/src/impl/tokenParser.js @@ -372,17 +372,9 @@ function maybeExpandMacroToken(token, locale) { } const formatOpts = Formatter.macroTokenToFormatOpts(token.val); + const tokens = formatOptsToTokens(formatOpts, locale); - if (!formatOpts) { - return token; - } - - const formatter = Formatter.create(locale, formatOpts); - const parts = formatter.formatDateTimeParts(getDummyDateTime()); - - const tokens = parts.map(p => tokenForPart(p, locale, formatOpts)); - - if (tokens.includes(undefined)) { + if (tokens == null || tokens.includes(undefined)) { return token; } @@ -422,3 +414,14 @@ export function parseFromTokens(locale, input, format) { const { result, zone, invalidReason } = explainFromTokens(locale, input, format); return [result, zone, invalidReason]; } + +export function formatOptsToTokens(formatOpts, locale) { + if (!formatOpts) { + return null; + } + + const formatter = Formatter.create(locale, formatOpts); + const parts = formatter.formatDateTimeParts(getDummyDateTime()); + + return parts.map(p => tokenForPart(p, locale, formatOpts)); +} diff --git a/tasks/build.js b/tasks/build.js index 775bc52a2..d4b3124fd 100644 --- a/tasks/build.js +++ b/tasks/build.js @@ -175,7 +175,8 @@ async function cjsBrowser() { async function es6() { await buildLibrary("es6", { format: "es", - compile: false + compile: false, + minify: true }); } diff --git a/test/datetime/tokenParse.test.js b/test/datetime/tokenParse.test.js index 969621aa5..8e3811f80 100644 --- a/test/datetime/tokenParse.test.js +++ b/test/datetime/tokenParse.test.js @@ -949,7 +949,7 @@ test("DateTime.fromFormatExplain() takes the same options as fromFormat", () => //------- test("DateTime.fromStringExplain is an alias for DateTime.fromFormatExplain", () => { const ff = DateTime.fromFormatExplain("1982/05/25 09:10:11.445", "yyyy/MM/dd HH:mm:ss.SSS"), - fs = DateTime.fromStringExplain("1982/05/25 09:10:11.445", "yyyy/MM/dd HH:mm:ss.SSS"); + fs = DateTime.fromFormatExplain("1982/05/25 09:10:11.445", "yyyy/MM/dd HH:mm:ss.SSS"); expect(ff).toEqual(fs); }); @@ -960,7 +960,16 @@ test("DateTime.fromStringExplain is an alias for DateTime.fromFormatExplain", () test("DateTime.fromString is an alias for DateTime.fromFormat", () => { const ff = DateTime.fromFormat("1982/05/25 09:10:11.445", "yyyy/MM/dd HH:mm:ss.SSS"), - fs = DateTime.fromString("1982/05/25 09:10:11.445", "yyyy/MM/dd HH:mm:ss.SSS"); + fs = DateTime.fromFormat("1982/05/25 09:10:11.445", "yyyy/MM/dd HH:mm:ss.SSS"); expect(ff).toEqual(fs); }); + +//------ +// .parseFormatForOpts +//------- + +test("DateTime.parseFormatFromOpts returns a parsing format", () => { + const format = DateTime.parseFormatForOpts(DateTime.DATETIME_FULL); + expect(format).toEqual("MMMM d, yyyyy, h:m a"); +});