UNPKG

836 BJavaScriptView Raw
1const dayOfYearTokenRE = /^D+$/;
2const weekYearTokenRE = /^Y+$/;
3
4const throwTokens = ["D", "DD", "YY", "YYYY"];
5
6export function isProtectedDayOfYearToken(token) {
7 return dayOfYearTokenRE.test(token);
8}
9
10export function isProtectedWeekYearToken(token) {
11 return weekYearTokenRE.test(token);
12}
13
14export function warnOrThrowProtectedError(token, format, input) {
15 const _message = message(token, format, input);
16 console.warn(_message);
17 if (throwTokens.includes(token)) throw new RangeError(_message);
18}
19
20function message(token, format, input) {
21 const subject = token[0] === "Y" ? "years" : "days of the month";
22 return `Use \`${token.toLowerCase()}\` instead of \`${token}\` (in \`${format}\`) for formatting ${subject} to the input \`${input}\`; see: https://github.com/date-fns/date-fns/blob/master/docs/unicodeTokens.md`;
23}