Setter.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import { constructFrom } from "../../constructFrom.js";
  2. import { transpose } from "../../transpose.js";
  3. const TIMEZONE_UNIT_PRIORITY = 10;
  4. export class Setter {
  5. subPriority = 0;
  6. validate(_utcDate, _options) {
  7. return true;
  8. }
  9. }
  10. export class ValueSetter extends Setter {
  11. constructor(
  12. value,
  13. validateValue,
  14. setValue,
  15. priority,
  16. subPriority,
  17. ) {
  18. super();
  19. this.value = value;
  20. this.validateValue = validateValue;
  21. this.setValue = setValue;
  22. this.priority = priority;
  23. if (subPriority) {
  24. this.subPriority = subPriority;
  25. }
  26. }
  27. validate(date, options) {
  28. return this.validateValue(date, this.value, options);
  29. }
  30. set(date, flags, options) {
  31. return this.setValue(date, flags, this.value, options);
  32. }
  33. }
  34. export class DateTimezoneSetter extends Setter {
  35. priority = TIMEZONE_UNIT_PRIORITY;
  36. subPriority = -1;
  37. constructor(context, reference) {
  38. super();
  39. this.context = context || ((date) => constructFrom(reference, date));
  40. }
  41. set(date, flags) {
  42. if (flags.timestampIsSet) return date;
  43. return constructFrom(date, transpose(date, this.context));
  44. }
  45. }