Skip to content

Commit 549165e

Browse files
committed
add full test-coverage for DateRange; export definitions;
1 parent 8f6809c commit 549165e

File tree

5 files changed

+41
-7
lines changed

5 files changed

+41
-7
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@mbauer83/ts-utils",
3-
"version": "v0.2.3",
3+
"version": "v0.2.4",
44
"type": "module",
55
"types": "src/main.d.ts",
66
"repository": "https://github.com/mbauer83/ts-utils.git",

src/date/DateRange.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,11 @@ export class DateRange {
4141
}
4242

4343
hasLowerBound(): boolean {
44-
return this.fromDate !== null;
44+
return this.fromDate !== undefined;
4545
}
4646

4747
hasUpperBound(): boolean {
48-
return this.toDate !== null;
48+
return this.toDate !== undefined;
4949
}
5050

5151
isInRange(dateOrMillisecondTimestamp: Date | number): boolean {
@@ -55,9 +55,9 @@ export class DateRange {
5555

5656
toString(): string {
5757
const lowerBoundDelimiter = this.lowerBoundIsInclusive ? '[' : '(';
58-
const lowerBoundPart = lowerBoundDelimiter + (this.hasLowerBound() ? this.fromDate!.toString() : '...') + ', ';
58+
const lowerBoundPart = lowerBoundDelimiter + (this.hasLowerBound() ? this.fromDate!.toISOString() : '...') + ', ';
5959
const upperBoundDelimiter = this.upperBoundIsInclusive ? ']' : ')';
60-
const upperBoundPart = (this.hasUpperBound() ? this.toDate!.toString() : '...') + upperBoundDelimiter;
60+
const upperBoundPart = (this.hasUpperBound() ? this.toDate!.toISOString() : '...') + upperBoundDelimiter;
6161
return lowerBoundPart + upperBoundPart;
6262
}
6363
}

tests/unit/dateRange.test.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,4 +140,37 @@ describe('test DateRange', () => {
140140
expect(DateRange.empty.fromDate).toBe(undefined);
141141
expect(DateRange.empty.toDate).toBe(undefined);
142142
})
143+
144+
test('hasLowerBound reports correctly', () => {
145+
expect(new DateRange().hasLowerBound()).toBe(false);
146+
expect(new DateRange(new Date()).hasLowerBound()).toBe(true);
147+
expect(new DateRange(undefined, new Date()).hasLowerBound()).toBe(false);
148+
expect(new DateRange(new Date(), new Date()).hasLowerBound()).toBe(true);
149+
})
150+
151+
test('hasUpperBound reports correctly', () => {
152+
expect(new DateRange().hasUpperBound()).toBe(false);
153+
expect(new DateRange(undefined, new Date()).hasUpperBound()).toBe(true);
154+
expect(new DateRange(new Date(), undefined).hasUpperBound()).toBe(false);
155+
expect(new DateRange(new Date(), new Date()).hasUpperBound()).toBe(true);
156+
})
157+
158+
test('toString returns correct string', () => {
159+
// Should use '(' / ')' for exclusive bounds and '[' / ']' for inclusive bounds.
160+
// Should use '...' for undefined bounds and ISO 8601 for defined bounds.
161+
expect(new DateRange().toString()).toBe('(..., ...)');
162+
expect(new DateRange(undefined, undefined, true, true).toString()).toBe('[..., ...]');
163+
const currDate = new Date();
164+
const expectedDateString = currDate.toISOString();
165+
expect(new DateRange(currDate).toString()).toBe(`(${expectedDateString}, ...)`);
166+
expect(new DateRange(undefined, currDate).toString()).toBe(`(..., ${expectedDateString})`);
167+
expect(new DateRange(currDate, undefined, true, true).toString()).toBe(`[${expectedDateString}, ...]`);
168+
expect(new DateRange(undefined, currDate, true, true).toString()).toBe(`[..., ${expectedDateString}]`);
169+
expect(new DateRange(currDate, undefined, true, false).toString()).toBe(`[${expectedDateString}, ...)`);
170+
expect(new DateRange(currDate, undefined, false, true).toString()).toBe(`(${expectedDateString}, ...]`);
171+
expect(new DateRange(undefined, currDate, true, false).toString()).toBe(`[..., ${expectedDateString})`);
172+
expect(new DateRange(undefined, currDate, false, true).toString()).toBe(`(..., ${expectedDateString}]`);
173+
expect(new DateRange(currDate, currDate).toString()).toBe(`(${expectedDateString}, ${expectedDateString})`);
174+
expect(new DateRange(currDate, currDate, true, true).toString()).toBe(`[${expectedDateString}, ${expectedDateString}]`);
175+
});
143176
});

tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"allowJs": true,
77
"checkJs": false,
88
"declaration": true,
9+
"declarationDir": "./dist/types",
910
"declarationMap": true,
1011
"outDir": "./dist",
1112
"sourceMap": true,

0 commit comments

Comments
 (0)