protectedTokens.cjs 1016 B

123456789101112131415161718192021222324252627
  1. "use strict";
  2. exports.isProtectedDayOfYearToken = isProtectedDayOfYearToken;
  3. exports.isProtectedWeekYearToken = isProtectedWeekYearToken;
  4. exports.warnOrThrowProtectedError = warnOrThrowProtectedError;
  5. const dayOfYearTokenRE = /^D+$/;
  6. const weekYearTokenRE = /^Y+$/;
  7. const throwTokens = ["D", "DD", "YY", "YYYY"];
  8. function isProtectedDayOfYearToken(token) {
  9. return dayOfYearTokenRE.test(token);
  10. }
  11. function isProtectedWeekYearToken(token) {
  12. return weekYearTokenRE.test(token);
  13. }
  14. 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. function message(token, format, input) {
  20. const subject = token[0] === "Y" ? "years" : "days of the month";
  21. 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`;
  22. }