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

atinc / ngx-tethys / 129e0fd3-b436-4e51-a99e-27a86ab79bed

14 Dec 2023 05:55AM UTC coverage: 90.399% (-0.009%) from 90.408%
129e0fd3-b436-4e51-a99e-27a86ab79bed

Pull #2969

circleci

luxiaobei
feat(property): property-item add thyEditingChange #INFR-10910
Pull Request #2969: feat(property): property-item add thyEditingChange #INFR-10910

5353 of 6582 branches covered (0.0%)

Branch coverage included in aggregate %.

5 of 5 new or added lines in 2 files covered. (100.0%)

20 existing lines in 5 files now uncovered.

13336 of 14092 relevant lines covered (94.64%)

972.82 hits per line

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

94.67
/src/util/date/tiny-date.ts
1
import {
2
    differenceInCalendarDays,
3
    differenceInCalendarMonths,
13✔
4
    differenceInCalendarYears,
12✔
5
    differenceInWeeks,
12✔
6
    differenceInHours,
7
    differenceInMinutes,
1✔
8
    differenceInSeconds,
9
    isSameDay,
10
    isSameHour,
11
    isSameMinute,
39,024✔
12
    isSameMonth,
37,045✔
13
    isSameSecond,
36,785✔
14
    isSameYear,
15
    isSameQuarter,
260✔
16
    isToday,
259✔
17
    isTomorrow,
18
    isValid,
1!
19
    setYear,
1✔
20
    startOfMonth,
21
    startOfWeek,
22
    addMonths,
23
    addYears,
1,979✔
24
    setDay,
25
    setMonth,
26
    getUnixTime,
27
    startOfDay,
1✔
28
    endOfDay,
29
    fromUnixTime,
30
    isWeekend,
31
    getWeek,
826✔
32
    getDaysInMonth,
33
    addSeconds,
34
    addMinutes,
20,630✔
35
    addHours,
36
    addWeeks,
37
    addQuarters,
6,707✔
38
    startOfQuarter,
39
    startOfYear,
40
    endOfWeek,
158✔
41
    endOfMonth,
42
    endOfQuarter,
43
    endOfYear,
1,134✔
44
    format,
45
    getQuarter,
2✔
46
    addDays
2✔
47
} from './functions';
48

49
import { Locale } from 'date-fns';
3✔
50

51
export type TinyDateCompareGrain = 'decade' | 'year' | 'quarter' | 'month' | 'week' | 'day' | 'hour' | 'minute' | 'second';
52

24✔
53
export type WeekDayIndex = 0 | 1 | 2 | 3 | 4 | 5 | 6;
54

55
export type TinyDateType = TinyDate | Date | null;
24✔
56

57
export function sortRangeValue(rangeValue: TinyDate[]): TinyDate[] {
58
    if (Array.isArray(rangeValue)) {
24✔
59
        const [start, end] = rangeValue;
60
        return start && end && start.isAfterSecond(end) ? [end, start] : [start, end];
61
    }
1✔
62
    return rangeValue;
63
}
64

1✔
65
export class TinyDate implements Record<string, any> {
66
    nativeDate: Date;
67

1✔
68
    constructor(date?: Date | string | number) {
69
        if (date) {
70
            if (date instanceof Date) {
71
                this.nativeDate = date;
31✔
72
            } else if (typeof date === 'string' || typeof date === 'number') {
31✔
73
                this.nativeDate = new Date(date);
31✔
74
            } else if (typeof ngDevMode === 'undefined' || ngDevMode) {
75
                throw new Error(
76
                    `The input date type is not supported expect Date | string | number | { date: number; with_time: 0 | 1}, actual ${JSON.stringify(
103✔
77
                        date
103✔
78
                    )}`
103✔
79
                );
80
            }
81
        } else {
458✔
82
            this.nativeDate = new Date();
83
        }
84
    }
1,238✔
85

86
    static fromUnixTime(unixTime: number): TinyDate {
87
        return new TinyDate(fromUnixTime(unixTime));
1✔
88
    }
89

90
    // get
91
    getTime(): number {
376✔
92
        return this.nativeDate.getTime();
93
    }
94

438✔
95
    getDate(): number {
96
        return this.nativeDate.getDate();
97
    }
624✔
98

99
    getYear(): number {
100
        return this.nativeDate.getFullYear();
1✔
101
    }
102

103
    getQuarter(): number {
26,732✔
104
        return getQuarter(this.nativeDate);
105
    }
106

1✔
107
    getMonth(): number {
108
        return this.nativeDate.getMonth();
109
    }
2✔
110

111
    getWeek(options: { locale?: Locale; weekStartsOn?: WeekDayIndex } = { weekStartsOn: 1 }): number {
112
        return getWeek(this.nativeDate, options);
1✔
113
    }
114

115
    getDay(): number {
×
116
        return this.nativeDate.getDay();
117
    }
48,826!
118

UNCOV
119
    getHours(): number {
×
UNCOV
120
        return this.nativeDate.getHours();
×
121
    }
122

530✔
123
    getMinutes(): number {
530✔
124
        return this.nativeDate.getMinutes();
125
    }
1,353✔
126

1,353✔
127
    getSeconds(): number {
128
        return this.nativeDate.getSeconds();
70✔
129
    }
70✔
130

131
    getMilliseconds(): number {
46,866✔
132
        return this.nativeDate.getMilliseconds();
46,866✔
133
    }
134

2✔
135
    getDaysInMonth() {
2✔
136
        return getDaysInMonth(this.nativeDate);
137
    }
2✔
138

2✔
139
    getDaysInQuarter() {
140
        return differenceInCalendarDays(this.endOfQuarter().addSeconds(1).nativeDate, this.startOfQuarter().nativeDate);
2✔
141
    }
2✔
142

143
    // set
1✔
144
    setDate(amount: number): TinyDate {
1✔
145
        const date = new Date(this.nativeDate);
146
        date.setDate(amount);
48,826✔
147
        return new TinyDate(date);
148
    }
149

523✔
150
    setHms(hour: number, minute: number, second: number): TinyDate {
151
        const date = new Date(this.nativeDate);
152
        date.setHours(hour, minute, second);
1,313✔
153
        return new TinyDate(date);
154
    }
155

70✔
156
    setYear(year: number): TinyDate {
157
        return new TinyDate(setYear(this.nativeDate, year));
158
    }
46,865✔
159

160
    setMonth(month: number): TinyDate {
161
        return new TinyDate(setMonth(this.nativeDate, month));
1✔
162
    }
163

164
    setDay(day: number, options?: { weekStartsOn: WeekDayIndex }): TinyDate {
1✔
165
        return new TinyDate(setDay(this.nativeDate, day, options));
166
    }
167

1✔
168
    // add
169
    addYears(amount: number): TinyDate {
170
        return new TinyDate(addYears(this.nativeDate, amount));
171
    }
30✔
172

173
    addQuarters(amount: number): TinyDate {
174
        return new TinyDate(addQuarters(this.nativeDate, amount));
20,557✔
175
    }
176

177
    addMonths(amount: number): TinyDate {
1✔
178
        return new TinyDate(addMonths(this.nativeDate, amount));
179
    }
180

5,622✔
181
    addWeeks(amount: number): TinyDate {
182
        return new TinyDate(addWeeks(this.nativeDate, amount));
183
    }
1✔
184

185
    addDays(amount: number): TinyDate {
186
        return new TinyDate(addDays(this.nativeDate, amount));
1✔
187
    }
188
    addHours(amount: number): TinyDate {
189
        return new TinyDate(addHours(this.nativeDate, amount));
1✔
190
    }
191

192
    addSeconds(amount: number): TinyDate {
2✔
193
        return new TinyDate(addSeconds(this.nativeDate, amount));
194
    }
195

20,413✔
196
    addMinutes(amount: number): TinyDate {
197
        return new TinyDate(addMinutes(this.nativeDate, amount));
198
    }
1✔
199

200
    // isSame
201

3✔
202
    isSame(date: TinyDateType, grain: TinyDateCompareGrain = 'day'): boolean {
203
        let fn;
204
        switch (grain) {
1✔
205
            case 'decade':
206
                fn = (pre: Date, next: Date) => Math.abs(pre.getFullYear() - next.getFullYear()) < 11;
207
                break;
1✔
208
            case 'year':
209
                fn = isSameYear;
210
                break;
13✔
211
            case 'month':
212
                fn = isSameMonth;
213
                break;
214
            case 'quarter':
2✔
215
                fn = isSameQuarter;
216
                break;
217
            case 'day':
40,826✔
218
                fn = isSameDay;
219
                break;
220
            case 'hour':
2✔
221
                fn = isSameHour;
222
                break;
223
            case 'minute':
2✔
224
                fn = isSameMinute;
225
                break;
226
            case 'second':
227
                fn = isSameSecond;
377✔
228
                break;
229
            default:
230
                fn = isSameDay;
201✔
231
                break;
232
        }
233
        return fn(this.nativeDate, this.toNativeDate(date));
236✔
234
    }
235

236
    isSameYear(date: TinyDateType): boolean {
34✔
237
        return this.isSame(date, 'year');
238
    }
239

1,965✔
240
    isSameMonth(date: TinyDateType): boolean {
241
        return this.isSame(date, 'month');
242
    }
111✔
243

244
    isSameQuarter(date: TinyDateType): boolean {
245
        return this.isSame(date, 'quarter');
146✔
246
    }
247

248
    isSameDay(date: TinyDateType): boolean {
180✔
249
        return this.isSame(date, 'day');
250
    }
251

35✔
252
    isSameHour(date: TinyDateType): boolean {
253
        return this.isSame(date, 'hour');
254
    }
1,625✔
255

256
    isSameMinute(date: TinyDateType): boolean {
257
        return this.isSame(date, 'minute');
258
    }
92✔
259

260
    isSameSecond(date: TinyDateType): boolean {
261
        return this.isSame(date, 'second');
973✔
262
    }
263

264
    // isBefore and isAfter
107✔
265
    isBeforeYear(date: TinyDateType): boolean {
266
        return this.compare(date, 'year');
267
    }
101✔
268

269
    isBeforeMonth(date: TinyDateType): boolean {
26,213!
270
        return this.compare(date, 'month');
46,647✔
271
    }
2✔
272

273
    isBeforeWeek(date: TinyDateType): boolean {
274
        return this.compare(date, 'week');
46,645!
275
    }
276

30✔
277
    isBeforeDay(date: TinyDateType): boolean {
30✔
278
        return this.compare(date, 'day');
279
    }
40,970✔
280

40,970✔
281
    isBeforeHour(date: TinyDateType): boolean {
282
        return this.compare(date, 'hour');
5,625✔
283
    }
5,625✔
284

285
    isBeforeMinute(date: TinyDateType): boolean {
2✔
286
        return this.compare(date, 'minute');
2✔
287
    }
288

2✔
289
    isBeforeSecond(date: TinyDateType): boolean {
2✔
290
        return this.compare(date, 'second');
291
    }
2✔
292

2✔
293
    isAfterYear(date: TinyDateType): boolean {
294
        return this.compare(date, 'year', false);
14✔
295
    }
14✔
296

UNCOV
297
    isAfterMonth(date: TinyDateType): boolean {
×
UNCOV
298
        return this.compare(date, 'month', false);
×
299
    }
300

46,645✔
301
    isAfterWeek(date: TinyDateType): boolean {
302
        return this.compare(date, 'week', false);
303
    }
95,471✔
304

305
    isAfterDay(date: TinyDateType): boolean {
306
        return this.compare(date, 'day', false);
307
    }
308

309
    isAfterHour(date: TinyDateType): boolean {
310
        return this.compare(date, 'hour', false);
311
    }
312

313
    isAfterMinute(date: TinyDateType): boolean {
314
        return this.compare(date, 'minute', false);
315
    }
316

317
    isAfterSecond(date: TinyDateType): boolean {
318
        return this.compare(date, 'second', false);
319
    }
320

321
    // is
322
    isWeekend() {
323
        return isWeekend(this.nativeDate);
324
    }
325

326
    isToday(): boolean {
327
        return isToday(this.nativeDate);
328
    }
329

330
    isTomorrow(): boolean {
331
        return isTomorrow(this.nativeDate);
332
    }
333

334
    isValid(): boolean {
335
        return isValid(this.nativeDate);
336
    }
337

338
    // startOf and endOf
339
    startOfYear(): TinyDate {
340
        return new TinyDate(startOfYear(this.nativeDate));
341
    }
342

343
    startOfQuarter(): TinyDate {
344
        return new TinyDate(startOfQuarter(this.nativeDate));
345
    }
346

347
    startOfMonth(): TinyDate {
348
        return new TinyDate(startOfMonth(this.nativeDate));
349
    }
350

351
    startOfWeek(options?: { locale?: Locale; weekStartsOn?: WeekDayIndex }): TinyDate {
352
        return new TinyDate(startOfWeek(this.nativeDate, options));
353
    }
354

355
    startOfDay(): TinyDate {
356
        return new TinyDate(startOfDay(this.nativeDate));
357
    }
358

359
    endOfYear(): TinyDate {
360
        return new TinyDate(endOfYear(this.nativeDate));
361
    }
362

363
    endOfQuarter(): TinyDate {
364
        return new TinyDate(endOfQuarter(this.nativeDate));
365
    }
366

367
    endOfMonth(): TinyDate {
368
        return new TinyDate(endOfMonth(this.nativeDate));
369
    }
370

371
    endOfWeek(options?: { locale?: Locale; weekStartsOn?: WeekDayIndex }): TinyDate {
372
        return new TinyDate(endOfWeek(this.nativeDate, options));
373
    }
374

375
    endOfDay(): TinyDate {
376
        return new TinyDate(endOfDay(this.nativeDate));
377
    }
378

379
    // other
380
    format(
381
        mat: string,
382
        options?: {
383
            locale?: Locale;
384
            weekStartsOn?: WeekDayIndex;
385
            firstWeekContainsDate?: number;
386
            useAdditionalWeekYearTokens?: boolean;
387
            useAdditionalDayOfYearTokens?: boolean;
388
        }
389
    ) {
390
        return format(this.nativeDate, mat, options);
391
    }
392

393
    calendarStart(options?: { weekStartsOn: WeekDayIndex | undefined }): TinyDate {
394
        return new TinyDate(startOfWeek(startOfMonth(this.nativeDate), options));
395
    }
396

397
    clone(): TinyDate {
398
        return new TinyDate(new Date(this.nativeDate));
399
    }
400

401
    getUnixTime(): number {
402
        return getUnixTime(this.nativeDate);
403
    }
404

405
    compare(date: TinyDateType, grain: TinyDateCompareGrain = 'day', isBefore: boolean = true): boolean {
406
        if (date === null) {
407
            return false;
408
        }
409
        let fn;
410
        switch (grain) {
411
            case 'year':
412
                fn = differenceInCalendarYears;
413
                break;
414
            case 'month':
415
                fn = differenceInCalendarMonths;
416
                break;
417
            case 'day':
418
                fn = differenceInCalendarDays;
419
                break;
420
            case 'week':
421
                fn = differenceInWeeks;
422
                break;
423
            case 'hour':
424
                fn = differenceInHours;
425
                break;
426
            case 'minute':
427
                fn = differenceInMinutes;
428
                break;
429
            case 'second':
430
                fn = differenceInSeconds;
431
                break;
432
            default:
433
                fn = differenceInCalendarDays;
434
                break;
435
        }
436
        return isBefore ? fn(this.nativeDate, this.toNativeDate(date)) < 0 : fn(this.nativeDate, this.toNativeDate(date)) > 0;
437
    }
438

439
    private toNativeDate(date: any): Date {
440
        return date instanceof TinyDate ? date.nativeDate : date;
441
    }
442
}
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