protectedTokens.js 836 B

1234567891011121314151617181920212223
  1. const dayOfYearTokenRE = /^D+$/;
  2. const weekYearTokenRE = /^Y+$/;
  3. const throwTokens = ["D", "DD", "YY", "YYYY"];
  4. export function isProtectedDayOfYearToken(token) {
  5. return dayOfYearTokenRE.test(token);
  6. }
  7. export function isProtectedWeekYearToken(token) {
  8. return weekYearTokenRE.test(token);
  9. }
  10. export function warnOrThrowProtectedError(token, format, input) {
  11. const _message = message(token, format, input);
  12. console.warn(_message);
  13. if (throwTokens.includes(token)) throw new RangeError(_message);
  14. }
  15. function message(token, format, input) {
  16. const subject = token[0] === "Y" ? "years" : "days of the month";
  17. 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`;
  18. }