Skip to content

Commit c5eda83

Browse files
committed
🐛 Fix minor type error in types utility
1 parent b850ef9 commit c5eda83

File tree

4 files changed

+26
-10
lines changed

4 files changed

+26
-10
lines changed

src/print-value.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ describe('printValue', () => {
2222
expect(printValue(new Date('2001-02-03T04:05:06.789Z'))).toEqual(
2323
'Date 2001-02-03T04:05:06.789Z',
2424
);
25+
expect(printValue(new Date('invalid date'))).toEqual('Invalid Date');
2526
});
2627

2728
it('prints functions', () => {

src/print-value.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
export function printValue(value: unknown, refs?: unknown[]) {
22
if (typeof value === 'string') return `'${value}'`;
33
if (Array.isArray(value)) return printArray(value, refs);
4-
if (value instanceof Date) return `Date ${value.toISOString()}`;
4+
if (value instanceof Date) {
5+
try {
6+
return `Date ${value.toISOString()}`;
7+
} catch {
8+
return `Invalid Date`;
9+
}
10+
}
511
if (value instanceof Function) {
612
return value.name ? `function ${value.name}` : 'anonymous function';
713
}

src/types.test.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ describe('readNumber', () => {
288288
});
289289
});
290290

291-
describe('asDate', () => {
291+
describe('readDate', () => {
292292
it('returns date objects', () => {
293293
const date = new Date('2000-04-01T12:13:14.000Z');
294294

@@ -305,6 +305,12 @@ describe('asDate', () => {
305305
expect(() => readDate(23)).toThrow(new TypeError('Expected Date, got: 23'));
306306
expect(() => readDate(null)).toThrow(new TypeError('Expected Date, got: null'));
307307
});
308+
309+
it('throws for invalid Date objects', () => {
310+
expect(() => readDate(new Date('invalid date'))).toThrow(
311+
new TypeError('Expected valid Date, got: Invalid Date'),
312+
);
313+
});
308314
});
309315

310316
describe('readArray', () => {

src/types.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,13 @@ export function readNumber(input: unknown, options?: NumberOptions) {
159159
}
160160

161161
export function readDate(value: unknown): Date {
162-
if (value instanceof Date) return value as Date;
162+
if (value instanceof Date) {
163+
if (Number.isNaN(value.getTime())) {
164+
throw typeError('valid Date', value);
165+
}
166+
return value as Date;
167+
}
168+
163169
throw typeError('Date', value);
164170
}
165171

@@ -214,25 +220,22 @@ export function readObject<T extends Record<string, TypeDef<unknown>>>(
214220
if (options?.maxProperties != null && Object.keys(input).length > options.maxProperties) {
215221
throw typeError(`object with max. ${options.maxProperties} properties`, input);
216222
}
217-
return properties ? mapObject(input, properties) : (input as any);
218-
}
219-
220-
function mapObject(obj: Obj, properties: Record<string, TypeDef<unknown>>) {
223+
if (!properties) return input as any;
221224
return pickDefined(
222225
Object.fromEntries(
223226
Object.entries(properties).map(([key, type]) => {
224-
return [key, readFrom(obj, key, type)];
227+
return [key, readFrom(input, key, type)];
225228
}),
226229
),
227-
);
230+
) as Partial<{ [P in keyof T]: ReturnType<T[P]> }>;
228231
}
229232

230233
export function isObject(value: unknown): value is Record<string, unknown> {
231234
return (
232235
value != null &&
233236
typeof value === 'object' &&
234237
!Array.isArray(value) &&
235-
value.toString() === '[object Object]'
238+
Object.prototype.toString.call(value) === '[object Object]'
236239
);
237240
}
238241

0 commit comments

Comments
 (0)