Skip to content

Commit

Permalink
fix: parsableStrictIsoDateZ case insensitivity issues
Browse files Browse the repository at this point in the history
  • Loading branch information
belgattitude committed May 24, 2024
1 parent 16e7dce commit 066e527
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 15 deletions.
5 changes: 5 additions & 0 deletions .changeset/smart-seals-study.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@httpx/assert": patch
---

make assertParsableStrictIsoDateZ case insensitive
2 changes: 1 addition & 1 deletion packages/assert/src/__tests__/string.guard.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
} from '../string.guards';

describe('Typeguards string tests', () => {
describe('isParsableIsoStrictDateZ', () => {
describe('isParsableStrictIsoDateZ', () => {
it.each([
[true, new Date().toISOString()],
[true, '2023-12-29T23:37:31.653Z'],
Expand Down
8 changes: 2 additions & 6 deletions packages/assert/src/string.asserts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,10 @@ export function assertParsableStrictIsoDateZ(
let check: 'INVALID_FORMAT' | 'INVALID_DATE' | true | null = null;
if (typeof v !== 'string') {
check = null;
} else if (
v.length === 24 &&
isoDateTimeZRegexp.test(v.toLocaleUpperCase())
) {
} else if (v.length === 24 && isoDateTimeZRegexp.test(v)) {
try {
const d = new Date(v);
check =
d.toISOString().toLocaleLowerCase() === v.toLocaleLowerCase()
new Date(v).toISOString().toUpperCase() === v.toUpperCase()
? true
: 'INVALID_DATE';
} catch {
Expand Down
9 changes: 2 additions & 7 deletions packages/assert/src/string.guards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,11 @@ export const isParsableSafeInt = (v: unknown): v is ParsableSafeInt => {
export const isParsableStrictIsoDateZ = (
v: unknown
): v is ParsableStrictIsoDateZ => {
if (
!isStringNonEmpty(v) ||
v.length !== 24 ||
!isoDateTimeZRegexp.test(v.toLocaleUpperCase())
) {
if (!isStringNonEmpty(v) || v.length !== 24 || !isoDateTimeZRegexp.test(v)) {
return false;
}
try {
const d = new Date(v);
return d.toISOString().toLowerCase() === v.toLowerCase();
return new Date(v).toISOString().toUpperCase() === v.toUpperCase();
} catch {
return false;
}
Expand Down
3 changes: 2 additions & 1 deletion packages/assert/src/string.utils.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export const isoDateTimeZRegexp =
/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z$/;
// eslint-disable-next-line unicorn/better-regex
/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z$/i;

0 comments on commit 066e527

Please sign in to comment.