-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: added matchId check and timestamp conversion (#2709)
* fix: added matchId check and timestamp conversion * fix: added matchId check and timestamp conversion * fix: timestamp logic to function * fix: lint fixes * fix: move to utility * fix: move to utility
- Loading branch information
1 parent
d681d5e
commit f49244d
Showing
7 changed files
with
377 additions
and
50 deletions.
There are no files selected for viewing
This file contains 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 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 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,33 @@ | ||
function convertToMicroseconds(input) { | ||
const timestamp = Date.parse(input); | ||
|
||
if (!Number.isNaN(timestamp)) { | ||
// If the input is a valid date string, timestamp will be a number | ||
if (input.includes('Z')) { | ||
// ISO 8601 date string with milliseconds | ||
return timestamp * 1000; | ||
} | ||
// to handle case of "2022-11-17T00:22:02.903+05:30" strings | ||
return timestamp.toString().length === 13 ? timestamp * 1000 : timestamp * 1000000; | ||
} | ||
|
||
if (/^\d+$/.test(input)) { | ||
// If the input is a numeric string (assume microseconds or milliseconds) | ||
if (input.length === 13) { | ||
// equal to 13 indicates milliseconds | ||
return parseInt(input, 10) * 1000; | ||
} | ||
|
||
if (input.length === 10) { | ||
// equal to 10 indicates seconds | ||
return parseInt(input, 10) * 1000000; | ||
} | ||
// Otherwise, assume microseconds | ||
return parseInt(input, 10); | ||
} | ||
return timestamp; | ||
} | ||
|
||
module.exports = { | ||
convertToMicroseconds, | ||
}; |
This file contains 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,23 @@ | ||
const { convertToMicroseconds } = require('./util'); | ||
|
||
describe('convertToMicroseconds utility test', () => { | ||
it('ISO 8601 input', () => { | ||
expect(convertToMicroseconds('2021-01-04T08:25:04.780Z')).toEqual(1609748704780000); | ||
}); | ||
|
||
it('unix microseconds input', () => { | ||
expect(convertToMicroseconds('1668624722903333')).toEqual(1668624722903333); | ||
}); | ||
|
||
it('non numeric time input', () => { | ||
expect(convertToMicroseconds('2022-11-17T00:22:02.903+05:30')).toEqual(1668624722903000); | ||
}); | ||
|
||
it('unix seconds input', () => { | ||
expect(convertToMicroseconds('1697013935')).toEqual(1697013935000000); | ||
}); | ||
|
||
it('unix miliseconds input', () => { | ||
expect(convertToMicroseconds('1697013935000')).toEqual(1697013935000000); | ||
}); | ||
}); |
Oops, something went wrong.