Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add DateTime.parseFormatForOpts #840

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions src/datetime.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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}
*/
Expand All @@ -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

/**
Expand Down
23 changes: 13 additions & 10 deletions src/impl/tokenParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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));
}
3 changes: 2 additions & 1 deletion tasks/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,8 @@ async function cjsBrowser() {
async function es6() {
await buildLibrary("es6", {
format: "es",
compile: false
compile: false,
minify: true
});
}

Expand Down
13 changes: 11 additions & 2 deletions test/datetime/tokenParse.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand All @@ -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");
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oops

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(i shouldn't have changed this, just a find-and-replace fail)


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");
});