Skip to content

Commit

Permalink
Added luxonDateAdapter support
Browse files Browse the repository at this point in the history
  • Loading branch information
angelaki committed Apr 11, 2024
1 parent 4b34012 commit c426278
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 3 deletions.
24 changes: 23 additions & 1 deletion projects/angular-calendar/schematics/ng-add/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import {
import { Schema } from './schema';
import {
dateFnsVersion,
luxonVersion,
momentVersion,
angularCalendarVersion,
} from './version-names';
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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';

Expand Down Expand Up @@ -156,6 +158,26 @@ function addModuleToImports(options: Schema): Rule {
);
}

if (options.dateAdapter === 'luxon') {
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);
Expand Down
6 changes: 5 additions & 1 deletion projects/angular-calendar/schematics/ng-add/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"description": "Which date adapter to use",
"type": "string",
"default": "date-fns",
"enum": ["moment", "date-fns"],
"enum": ["moment", "date-fns", "luxon"],
"x-prompt": {
"message": "What date adapter would you like to use?",
"type": "list",
Expand All @@ -20,6 +20,10 @@
{
"value": "moment",
"label": "moment [ https://momentjs.com/ ]"
},
{
"value": "luxon",
"label": "luxon [ https://moment.github.io/luxon/ ]"
}
]
}
Expand Down
2 changes: 1 addition & 1 deletion projects/angular-calendar/schematics/ng-add/schema.ts
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
Expand Up @@ -3,3 +3,4 @@ const packageJson = require('./package.json'); // eslint-disable-line @typescri
export const angularCalendarVersion = `^${packageJson.version}`;
export const momentVersion = packageJson.devDependencies.moment;
export const dateFnsVersion = packageJson.devDependencies['date-fns'];
export const luxonVersion = packageJson.devDependencies.luxon;
55 changes: 55 additions & 0 deletions projects/angular-calendar/src/date-adapters/luxon/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { adapterFactory as baseAdapterFactory } from 'calendar-utils/date-adapters/luxon';
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"
}

0 comments on commit c426278

Please sign in to comment.