Blame view

天文台pc/tianwentai-ui/node_modules/date-fns/parse/_lib/parsers/LocalDayParser.mjs 2.3 KB
bc518174   王天杨   提交两个项目文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
  import { setDay } from "../../../setDay.mjs";
  import { Parser } from "../Parser.mjs";
  import { mapValue, parseNDigits } from "../utils.mjs";
  
  // Local day of week
  export class LocalDayParser extends Parser {
    priority = 90;
    parse(dateString, token, match, options) {
      const valueCallback = (value) => {
        // We want here floor instead of trunc, so we get -7 for value 0 instead of 0
        const wholeWeekDays = Math.floor((value - 1) / 7) * 7;
        return ((value + options.weekStartsOn + 6) % 7) + wholeWeekDays;
      };
  
      switch (token) {
        // 3
        case "e":
        case "ee": // 03
          return mapValue(parseNDigits(token.length, dateString), valueCallback);
        // 3rd
        case "eo":
          return mapValue(
            match.ordinalNumber(dateString, {
              unit: "day",
            }),
            valueCallback,
          );
        // Tue
        case "eee":
          return (
            match.day(dateString, {
              width: "abbreviated",
              context: "formatting",
            }) ||
            match.day(dateString, { width: "short", context: "formatting" }) ||
            match.day(dateString, { width: "narrow", context: "formatting" })
          );
  
        // T
        case "eeeee":
          return match.day(dateString, {
            width: "narrow",
            context: "formatting",
          });
        // Tu
        case "eeeeee":
          return (
            match.day(dateString, { width: "short", context: "formatting" }) ||
            match.day(dateString, { width: "narrow", context: "formatting" })
          );
  
        // Tuesday
        case "eeee":
        default:
          return (
            match.day(dateString, { width: "wide", context: "formatting" }) ||
            match.day(dateString, {
              width: "abbreviated",
              context: "formatting",
            }) ||
            match.day(dateString, { width: "short", context: "formatting" }) ||
            match.day(dateString, { width: "narrow", context: "formatting" })
          );
      }
    }
  
    validate(_date, value) {
      return value >= 0 && value <= 6;
    }
  
    set(date, _flags, value, options) {
      date = setDay(date, value, options);
      date.setHours(0, 0, 0, 0);
      return date;
    }
  
    incompatibleTokens = [
      "y",
      "R",
      "u",
      "q",
      "Q",
      "M",
      "L",
      "I",
      "d",
      "D",
      "E",
      "i",
      "c",
      "t",
      "T",
    ];
  }