• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

atinc / ngx-tethys / d9ae709b-3c27-4b69-b125-b8b80b54f90b

pending completion
d9ae709b-3c27-4b69-b125-b8b80b54f90b

Pull #2757

circleci

mengshuicmq
fix: fix code review
Pull Request #2757: feat(color-picker): color-picker support disabled (#INFR-8645)

98 of 6315 branches covered (1.55%)

Branch coverage included in aggregate %.

1 of 1 new or added line in 1 file covered. (100.0%)

2392 of 13661 relevant lines covered (17.51%)

83.12 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/date-picker/picker.util.ts
1
import { CompatibleDate, DateEntry, ThyDateRangeEntry, ThyPanelMode, ThyDateGranularity, ThyShortcutValue } from './standard-types';
2

3
import { fromUnixTime } from 'date-fns';
4
import { helpers, TinyDate } from 'ngx-tethys/util';
×
5
import { CompatibleValue, RangeAdvancedValue } from './inner-types';
×
6

7
export function transformDateValue(value: CompatibleDate | CompatibleValue | number | DateEntry | ThyDateRangeEntry | RangeAdvancedValue): {
8
    value: CompatibleDate;
×
9
    withTime?: boolean;
×
10
    flexibleDateGranularity?: ThyDateGranularity;
11
} {
×
12
    if (!value) {
×
13
        return { value: null };
×
14
    }
15
    let withTime, flexibleDateGranularity: ThyDateGranularity;
16
    if (value && typeof value === 'number') {
×
17
        value = convertDate(value);
×
18
    }
19
    if (value && instanceOfCompatibleValue(value as CompatibleValue)) {
20
        if (value instanceof TinyDate) {
×
21
            value = convertDate(value.nativeDate);
×
22
        } else {
×
23
            value[0] = convertDate(value[0].nativeDate);
×
24
            value[1] = convertDate(value[1].nativeDate);
25
        }
×
26
    }
×
27
    if (value && instanceOfDateEntry(value as DateEntry)) {
×
28
        const { date, with_time } = value as DateEntry;
×
29
        value = date ? convertDate(date) : null;
×
30
        withTime = !!with_time;
×
31
    }
32
    if (value && instanceOfRangeEntry(value as ThyDateRangeEntry)) {
×
33
        const rangeValue = value as ThyDateRangeEntry;
×
34
        value = [];
35
        if (rangeValue.begin && rangeValue.end) {
36
            value[0] = convertDate(rangeValue.begin);
×
37
            value[1] = convertDate(rangeValue.end);
×
38
        }
×
39
        if (rangeValue.granularity) {
×
40
            flexibleDateGranularity = rangeValue.granularity;
41
        }
×
42
    }
×
43

×
44
    if (value && instanceOfRangeAdvancedValue(value as RangeAdvancedValue)) {
×
45
        const rangeValue = value as RangeAdvancedValue;
46
        if (rangeValue.dateGranularity) {
47
            flexibleDateGranularity = rangeValue.dateGranularity;
×
48
        }
49
        value = [];
50
        if (rangeValue.begin && rangeValue.end) {
×
51
            value[0] = convertDate(rangeValue.begin.nativeDate);
×
52
            value[1] = convertDate(rangeValue.end.nativeDate);
×
53
        }
54
    }
×
55
    return { value: value as CompatibleDate, withTime, flexibleDateGranularity };
56
}
×
57

×
58
export function getFlexibleAdvancedReadableValue(tinyDates: TinyDate[], flexibleDateGranularity: ThyDateGranularity) {
59
    let value = '';
60
    if (!tinyDates[0] || !tinyDates[1]) {
×
61
        return value;
62
    }
×
63
    switch (flexibleDateGranularity) {
64
        case 'year':
×
65
            if (tinyDates[0].isSameYear(tinyDates[1])) {
×
66
                value = `${tinyDates[0].getYear()}年`;
67
            } else {
68
                value = `${tinyDates[0].getYear()}年 ~ ${tinyDates[1].getYear()}年`;
×
69
            }
70
            break;
×
71
        case 'quarter':
72
            if (tinyDates[0].isSameQuarter(tinyDates[1])) {
×
73
                value = `${tinyDates[0].getYear()}年 Q${tinyDates[0].getQuarter()}`;
×
74
            } else {
75
                value = `${tinyDates[0].getYear()}年 Q${tinyDates[0].getQuarter()} ~ ${tinyDates[1].getYear()}年 Q${tinyDates[1].getQuarter()}`;
76
            }
×
77
            break;
78
        case 'month':
×
79
            if (tinyDates[0].isSameMonth(tinyDates[1])) {
80
                value = `${tinyDates[0].getYear()}年 ${tinyDates[0].getMonth() + 1}月`;
×
81
            } else {
82
                value = `${tinyDates[0].getYear()}年 ${tinyDates[0].getMonth() + 1}月 ~ ${tinyDates[1].getYear()}年 ${
83
                    tinyDates[1].getMonth() + 1
×
84
                }月`;
×
85
            }
×
86
            break;
87
    }
88
    return value;
×
89
}
90

91
export function convertDate(date: Date | number): Date {
92
    if (typeof date === 'number') {
×
93
        if (date.toString().length < 13) {
94
            return fromUnixTime(date);
95
        } else {
96
            return new Date(date);
×
97
        }
×
98
    } else {
99
        return date;
100
    }
×
101
}
102

103
export function hasValue(value: CompatibleValue): boolean {
×
104
    if (Array.isArray(value)) {
×
105
        return !!value[0] && !!value[1];
×
106
    } else {
107
        return !!value;
108
    }
×
109
}
110

111
export function makeValue(value: CompatibleDate | null, isRange: boolean = false): CompatibleValue {
112
    if (isRange) {
113
        return value ? (value as Date[]).map(val => new TinyDate(val)) : [];
×
114
    } else {
115
        return value ? new TinyDate(value as Date) : null;
×
116
    }
×
117
}
118

×
119
export function dateAddAmount(value: TinyDate, amount: number, mode: ThyPanelMode): TinyDate {
×
120
    let date: TinyDate;
121
    switch (mode) {
×
122
        case 'decade':
×
123
            date = value.addYears(amount * 10);
124
            break;
×
125
        case 'year':
×
126
            date = value.addYears(amount);
127
            break;
×
128
        case 'month':
129
            date = value.addMonths(amount);
130
            break;
131
        default:
×
132
            date = value.addMonths(amount);
×
133
            break;
×
134
    }
×
135
    return date;
136
}
×
137

×
138
// rightDate 超过 leftDate 一个月
139
export function isAfterMoreThanOneMonth(rightDate: TinyDate, leftDate: TinyDate) {
140
    rightDate = rightDate ? rightDate : leftDate ? leftDate : new TinyDate();
×
141
    leftDate = leftDate ? leftDate : rightDate;
142
    if (rightDate.getYear() < leftDate.getYear()) {
143
        return false;
144
    }
×
145

×
146
    if (rightDate.getYear() === leftDate.getYear() && leftDate.getMonth() + 1 >= rightDate.getMonth()) {
×
147
        return false;
×
148
    }
149

150
    // 处理rightDate(2020,1,1) 为leftDate(2020,12,1)后一年1月,同时leftDate日期为12月的特殊情况
×
151
    return !(rightDate.getYear() - leftDate.getYear() === 1 && rightDate.getMonth() === 0 && leftDate.getMonth() === 11);
152
}
153

154
// rightDate 超过 leftDate 不到一年
×
155
export function isAfterMoreThanLessOneYear(rightDate: TinyDate, leftDate: TinyDate) {
×
156
    rightDate = rightDate ? rightDate : leftDate ? leftDate : new TinyDate();
×
157
    leftDate = leftDate ? leftDate : rightDate;
×
158
    if (rightDate.getYear() <= leftDate.getYear()) {
159
        return false;
160
    }
×
161
    // 处理rightDate(2021,1,1)日期比leftDate(2020,12,1)日期大1年,同时rightDate日期月份小于leftDate日期月份的情况
162
    return !(rightDate.getYear() - leftDate.getYear() === 1 && rightDate.getMonth() <= leftDate.getMonth());
163
}
164

×
165
// rightDate 超过 leftDate 一年
×
166
export function isAfterMoreThanOneYear(rightDate: TinyDate, leftDate: TinyDate) {
×
167
    rightDate = rightDate ? rightDate : leftDate ? leftDate : new TinyDate();
168
    leftDate = leftDate ? leftDate : rightDate;
169
    if (leftDate.getYear() + 1 >= rightDate.getYear()) {
×
170
        return false;
171
    } else {
172
        return true;
×
173
    }
174
}
175

×
176
export function isAfterMoreThanOneDecade(rightDate: TinyDate, leftDate: TinyDate) {
177
    rightDate = rightDate ? rightDate : leftDate ? leftDate : new TinyDate();
178
    leftDate = leftDate ? leftDate : rightDate;
×
179
    return rightDate.getYear() - leftDate.getYear() >= 20;
180
}
181

×
182
export function instanceOfDateEntry(object: DateEntry): object is DateEntry {
183
    return isSupportDateType(object, 'date') && typeof object.with_time === 'number';
184
}
×
185

186
export function instanceOfRangeEntry(object: ThyDateRangeEntry): object is ThyDateRangeEntry {
187
    return isSupportDateType(object, 'begin') && isSupportDateType(object, 'end');
188
}
189

190
export function instanceOfCompatibleValue(object: CompatibleValue): object is CompatibleValue {
191
    return object instanceof TinyDate || object[0] instanceof TinyDate;
192
}
193

194
export function instanceOfRangeAdvancedValue(object: RangeAdvancedValue): object is RangeAdvancedValue {
195
    return object['begin'] instanceof TinyDate && object['end'] instanceof TinyDate;
196
}
197

198
export function isSupportDateType(object: DateEntry | ThyDateRangeEntry, key: string) {
199
    return typeof object[key] === 'number' || object[key] === null || object[key] instanceof Date;
200
}
201

202
export function getShortcutValue(value: ThyShortcutValue): number | Date {
203
    return helpers.isFunction(value) ? value() : value;
204
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc