-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[docs] Limit
date-fns
package to v2 in codesandbox (#11463)
- Loading branch information
Showing
6 changed files
with
87 additions
and
13 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,38 @@ | ||
import { expect } from 'chai'; | ||
import { DATE_ADAPTER_VERSIONS, ADAPTER_TO_LIBRARY, postProcessImport } from './postProcessImport'; | ||
|
||
describe('postProcessImport', () => { | ||
const ADAPTERS = ['AdapterDateFns', 'AdapterDayjs', 'AdapterLuxon', 'AdapterMoment']; | ||
|
||
describe('@mui/lab imports', () => { | ||
ADAPTERS.forEach((adapter) => { | ||
it('should provide correct adapter', () => { | ||
const resolvedDep = postProcessImport(`@mui/lab/${adapter}`); | ||
|
||
const expectedLibrary = ADAPTER_TO_LIBRARY[adapter]; | ||
expect(resolvedDep).to.deep.equal({ | ||
[expectedLibrary]: DATE_ADAPTER_VERSIONS[expectedLibrary], | ||
}); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('@mui/x-date-pickers imports', () => { | ||
const ALL_ADAPTERS = [ | ||
...ADAPTERS, | ||
'AdapterDateFnsJalali', | ||
'AdapterMomentHijri', | ||
'AdapterMomentJalaali', | ||
]; | ||
ALL_ADAPTERS.forEach((adapter) => { | ||
it('should provide correct adapter', () => { | ||
const resolvedDep = postProcessImport(`@mui/x-date-pickers/${adapter}`); | ||
|
||
const expectedLibrary = ADAPTER_TO_LIBRARY[adapter]; | ||
expect(resolvedDep).to.deep.equal({ | ||
[expectedLibrary]: DATE_ADAPTER_VERSIONS[expectedLibrary], | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |
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,40 @@ | ||
export const DATE_ADAPTER_VERSIONS: Record<string, string> = { | ||
'date-fns': '^2.30.0', | ||
'date-fns-jalali': '^2.30.0-0', | ||
dayjs: '^1.11.10', | ||
luxon: '^3.4.4', | ||
moment: '^2.29.4', | ||
'moment-hijri': '^2.1.2', | ||
'moment-jalaali': '^0.10.0', | ||
} as const; | ||
|
||
export const ADAPTER_TO_LIBRARY: Record<string, string> = { | ||
AdapterDateFns: 'date-fns', | ||
AdapterDateFnsJalali: 'date-fns-jalali', | ||
AdapterDayjs: 'dayjs', | ||
AdapterLuxon: 'luxon', | ||
AdapterMoment: 'moment', | ||
AdapterMomentHijri: 'moment-hijri', | ||
AdapterMomentJalaali: 'moment-jalaali', | ||
}; | ||
|
||
const PICKERS_ADAPTER_REGEX = /^@mui\/(lab|x-date-pickers)\/(?<adapterName>Adapter.*)/; | ||
|
||
export const postProcessImport = (importName: string): Record<string, string> | null => { | ||
// e.g. date-fns | ||
const dateAdapterMatch = PICKERS_ADAPTER_REGEX.exec(importName); | ||
if (dateAdapterMatch !== null) { | ||
/** | ||
* Mapping from the date adapter sub-packages to the npm packages they require. | ||
* @example `@mui/x-date-pickers/AdapterDayjs` has a peer dependency on `dayjs`. | ||
*/ | ||
const packageName = ADAPTER_TO_LIBRARY[dateAdapterMatch.groups?.adapterName || '']; | ||
if (packageName === undefined) { | ||
throw new TypeError( | ||
`Can't determine required npm package for adapter '${dateAdapterMatch[1]}'`, | ||
); | ||
} | ||
return { [packageName]: DATE_ADAPTER_VERSIONS[packageName] ?? 'latest' }; | ||
} | ||
return null; | ||
}; |
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