-
Notifications
You must be signed in to change notification settings - Fork 639
chore: Port the ISO 8601 time structs to snaps-utils
#3287
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| { | ||
| "branches": 99.74, | ||
| "functions": 98.93, | ||
| "lines": 99.61, | ||
| "statements": 96.95 | ||
| "branches": 99.75, | ||
| "functions": 98.94, | ||
| "lines": 99.62, | ||
| "statements": 96.99 | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| import { create, is } from '@metamask/superstruct'; | ||
| import { DateTime } from 'luxon'; | ||
|
|
||
| import { Iso8601DateStruct, Iso8601DurationStruct } from './time'; | ||
|
|
||
| describe('Iso8601dateStruct', () => { | ||
| it('should return true for a valid ISO 8601 date', () => { | ||
| const value = DateTime.now().toISO(); | ||
| expect(is(value, Iso8601DateStruct)).toBe(true); | ||
| }); | ||
|
|
||
| it('should return false for an invalid ISO 8601 date', () => { | ||
| const value = 'Mon Mar 31 2025'; | ||
| expect(is(value, Iso8601DateStruct)).toBe(false); | ||
| }); | ||
|
|
||
| it('should return false for an ISO 8601 date without timezone information', () => { | ||
| const value = '2025-03-31T12:00:00'; | ||
| expect(is(value, Iso8601DateStruct)).toBe(false); | ||
| }); | ||
|
|
||
| it('should return an error message for invalid ISO 8601 date', () => { | ||
| const value = 'Mon Mar 31 2025'; | ||
| expect(() => create(value, Iso8601DateStruct)).toThrow( | ||
| 'Not a valid ISO 8601 date', | ||
| ); | ||
| }); | ||
|
|
||
| it('should return an error message for ISO 8601 date without timezone information', () => { | ||
| const value = '2025-03-31T12:00:00'; | ||
| expect(() => create(value, Iso8601DateStruct)).toThrow( | ||
| 'ISO 8601 date must have timezone information', | ||
| ); | ||
| }); | ||
| }); | ||
|
|
||
| describe('Iso8601DurationStruct', () => { | ||
| it('should return true for a valid ISO 8601 duration', () => { | ||
| const value = 'P3Y6M4DT12H30M5S'; | ||
| expect(is(value, Iso8601DurationStruct)).toBe(true); | ||
| }); | ||
|
|
||
| it('should return false for an invalid ISO 8601 duration', () => { | ||
| const value = 'Millisecond'; | ||
| expect(is(value, Iso8601DurationStruct)).toBe(false); | ||
| }); | ||
|
|
||
| it('should return an error message for invalid ISO 8601 duration', () => { | ||
| const value = '1Millisecond'; | ||
| expect(() => create(value, Iso8601DurationStruct)).toThrow( | ||
| 'Not a valid ISO 8601 duration', | ||
| ); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| import { refine, string } from '@metamask/superstruct'; | ||
| import { DateTime, Duration } from 'luxon'; | ||
|
|
||
| /** | ||
| * Refines a string as an ISO 8601 duration. | ||
| */ | ||
| export const Iso8601DurationStruct = refine( | ||
| string(), | ||
| 'ISO 8601 duration', | ||
| (value) => { | ||
| const parsedDuration = Duration.fromISO(value); | ||
| if (!parsedDuration.isValid) { | ||
| return 'Not a valid ISO 8601 duration'; | ||
| } | ||
| return true; | ||
| }, | ||
| ); | ||
|
|
||
| /** | ||
| * Regex to match the offset part of an ISO 8601 date. | ||
| */ | ||
| const offsetRegex = /Z|([+-]\d{2}:?\d{2})$/u; | ||
|
|
||
| /** | ||
| * Refines a string as an ISO 8601 date. | ||
| */ | ||
| export const Iso8601DateStruct = refine(string(), 'ISO 8601 date', (value) => { | ||
GuillaumeRx marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| const parsedDate = DateTime.fromISO(value); | ||
|
|
||
| if (!parsedDate.isValid) { | ||
| return 'Not a valid ISO 8601 date'; | ||
| } | ||
|
|
||
| if (!offsetRegex.test(value)) { | ||
| // Luxon doesn't have a reliable way to check if timezone info was not provided | ||
| return 'ISO 8601 date must have timezone information'; | ||
| } | ||
|
|
||
| return true; | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.