Skip to content
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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,17 @@
"fesm2015": "./date-adapters/esm/moment/index.js",
"default": "./date-adapters/esm/moment/index.js"
},
"./date-adapters/luxon": {
"main": "./date-adapters/luxon/index.js",
"types": "./date-adapters/luxon/index.d.ts",
"require": "./date-adapters/luxon/index.js",
"import": "./date-adapters/esm/luxon/index.js",
"es2020": "./date-adapters/esm/luxon/index.js",
"esm2020": "./date-adapters/esm/luxon/index.js",
"fesm2020": "./date-adapters/esm/luxon/index.js",
"fesm2015": "./date-adapters/esm/luxon/index.js",
"default": "./date-adapters/esm/luxon/index.js"
},
"./scss/*": {
"sass": "./scss/*"
}
Expand Down
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')
Copy link
Owner

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.

const dateAdapters: Record<Schema['dateAdapter'], {version: string, adapterName: string}> = {
      moment: {
        version: momentVersion,
        adapterName: 'momentAdapterFactory'
      },
      'date-fns': {
        version: dateFnsVersion,
        adapterName: 'adapterFactory'
      },
      luxon: {
				version: luxonVersion,
				adapterName: 'luxonAdapterFactory'
			},
    };

Then here we can just pass dateAdapters[options.dateAdapter].adapterName;

} })`;
const moduleCalendarSrc = 'angular-calendar';

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

if (options.dateAdapter === 'luxon') {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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';
Copy link
Owner

@mattlewis92 mattlewis92 Apr 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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"
}
Loading