-
Notifications
You must be signed in to change notification settings - Fork 276
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8701 from google/enhancement/8687-pax-date-range
Enhancement/8687 pax date range
- Loading branch information
Showing
6 changed files
with
225 additions
and
3 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
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,44 @@ | ||
/** | ||
* PAX utility functions. | ||
* | ||
* Site Kit by Google, Copyright 2024 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { stringToDate } from '../../../util'; | ||
|
||
/** | ||
* Returns formatted date object. | ||
* | ||
* @since n.e.x.t | ||
* | ||
* @param {string} dateString Date in 'YYYY-MM-DD' format. | ||
* @return {Date} Date instance. | ||
*/ | ||
export function formatPaxDate( dateString ) { | ||
const dateObject = stringToDate( dateString ); | ||
|
||
return { | ||
year: dateObject.getFullYear(), | ||
// PAX uses a 1-indexed month value (to match the month string value). | ||
// | ||
// Our `stringToDate()` function returns 0-indexed month values, | ||
// so we need to adjust the values for PAX. | ||
month: dateObject.getMonth() + 1, | ||
day: dateObject.getDate(), | ||
}; | ||
} |
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,50 @@ | ||
/** | ||
* PAX utility functions tests. | ||
* | ||
* Site Kit by Google, Copyright 2024 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { INVALID_DATE_STRING_ERROR } from '../../../util'; | ||
import { formatPaxDate } from './utils'; | ||
|
||
describe( 'formatPaxDate', () => { | ||
it.each( [ null, NaN, '', '12345', '1900-00-00', 'not a date string' ] )( | ||
'throws an error when given the invalid date string: %s', | ||
( invalidDateString ) => { | ||
expect( () => formatPaxDate( invalidDateString ) ).toThrow( | ||
INVALID_DATE_STRING_ERROR | ||
); | ||
} | ||
); | ||
|
||
it( 'uses a one-indexed month', () => { | ||
const date = formatPaxDate( '2019-01-31' ); | ||
|
||
expect( date.year ).toBe( 2019 ); | ||
expect( date.month ).toBe( 1 ); | ||
expect( date.day ).toBe( 31 ); | ||
} ); | ||
|
||
it( 'returns a valid date instance for the given date string', () => { | ||
const date = formatPaxDate( '2019-10-31' ); | ||
|
||
expect( date.year ).toBe( 2019 ); | ||
expect( date.month ).toBe( 10 ); // 1-index based month | ||
expect( date.day ).toBe( 31 ); | ||
} ); | ||
} ); |
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