-
-
Notifications
You must be signed in to change notification settings - Fork 868
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
feat: add luxon date adapter #1709
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,6 +32,7 @@ import { | |
import { Schema } from './schema'; | ||
import { | ||
dateFnsVersion, | ||
luxonVersion, | ||
momentVersion, | ||
angularCalendarVersion, | ||
} from './version-names'; | ||
|
@@ -59,6 +60,7 @@ function addPackageJsonDependencies(options: Schema): Rule { | |
const dateAdapters: { [key: string]: string } = { | ||
moment: momentVersion, | ||
'date-fns': dateFnsVersion, | ||
'luxon': luxonVersion, | ||
}; | ||
|
||
const angularCalendarDependency: NodeDependency = nodeDependencyFactory( | ||
|
@@ -113,7 +115,7 @@ function addModuleToImports(options: Schema): Rule { | |
const moduleName = `CalendarModule.forRoot({ provide: DateAdapter, useFactory: ${ | ||
options.dateAdapter === 'moment' | ||
? 'momentAdapterFactory' | ||
: 'adapterFactory' | ||
: (options.dateAdapter === 'luxon' ? 'luxonAdapterFactory' : 'adapterFactory') | ||
} })`; | ||
const moduleCalendarSrc = 'angular-calendar'; | ||
|
||
|
@@ -156,6 +158,26 @@ function addModuleToImports(options: Schema): Rule { | |
); | ||
} | ||
|
||
if (options.dateAdapter === 'luxon') { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's add some tests for this based on the moment ones: |
||
updates.push( | ||
insertWildcardImport( | ||
moduleSource as ts.SourceFile, | ||
appModulePath, | ||
'luxon', | ||
'luxon' | ||
) as InsertChange | ||
); | ||
updates.push( | ||
insertAfterImports( | ||
moduleSource as ts.SourceFile, | ||
appModulePath, | ||
`;\n\nexport function luxonAdapterFactory() { | ||
return adapterFactory(); | ||
}` | ||
) as InsertChange | ||
); | ||
} | ||
|
||
const recorder = host.beginUpdate(appModulePath); | ||
updates.forEach((update) => { | ||
recorder.insertLeft(update.pos, update.toAdd); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
export interface Schema { | ||
dateAdapter: 'date-fns' | 'moment'; | ||
dateAdapter: 'date-fns' | 'moment' | 'luxon'; | ||
module?: string; | ||
projectName?: string; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { adapterFactory as baseAdapterFactory } from 'calendar-utils/date-adapters/luxon'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To make this work you'll need to add a new date adapter here: and add it to the tests here: |
||
import { DateTime } from 'luxon'; | ||
import { DateAdapter } from '../date-adapter'; | ||
|
||
export function adapterFactory(): DateAdapter { | ||
let coerceDateTime = (date: Date | number) => typeof date === 'number' ? DateTime.fromMillis(date) : DateTime.fromJSDate(date) | ||
|
||
return { | ||
...baseAdapterFactory(), | ||
|
||
addWeeks(date: Date | number, amount: number): Date { | ||
return coerceDateTime(date).plus({ weeks: amount }).toJSDate(); | ||
}, | ||
|
||
addMonths(date: Date | number, amount: number): Date { | ||
return coerceDateTime(date).plus({ months: amount }).toJSDate(); | ||
}, | ||
|
||
subDays(date: Date | number, amount: number): Date { | ||
return coerceDateTime(date).minus({ days: amount }).toJSDate(); | ||
}, | ||
|
||
subWeeks(date: Date | number, amount: number): Date { | ||
return coerceDateTime(date).minus({ weeks: amount }).toJSDate(); | ||
}, | ||
|
||
subMonths(date: Date | number, amount: number): Date { | ||
return coerceDateTime(date).minus({ months: amount }).toJSDate(); | ||
}, | ||
|
||
getISOWeek(date: Date | number): number { | ||
return coerceDateTime(date).weekNumber; | ||
}, | ||
|
||
setDate(date: Date | number, dayOfMonth: number): Date { | ||
return coerceDateTime(date).set({ day: dayOfMonth }).toJSDate(); | ||
}, | ||
|
||
setMonth(date: Date | number, month: number): Date { | ||
return coerceDateTime(date).set({ month: month + 1 }).toJSDate(); | ||
}, | ||
|
||
setYear(date: Date | number, year: number): Date { | ||
return coerceDateTime(date).set({ year: year }).toJSDate(); | ||
}, | ||
|
||
getDate(date: Date | number): number { | ||
return coerceDateTime(date).day; | ||
}, | ||
|
||
getYear(date: Date | number): number { | ||
return coerceDateTime(date).year; | ||
}, | ||
}; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"main": "./index.js", | ||
"module": "../esm/luxon/index.js", | ||
"typings": "./index.d.ts" | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's bake this into the
dateAdapters
object and avoid the branching logic e.g.Then here we can just pass dateAdapters[options.dateAdapter].adapterName;