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

Add registerDateLibrary to allow register every date library & allow ember-truth-helpers v4 #335

Merged
merged 4 commits into from
Nov 15, 2023
Merged
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
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
## 0.20.0
# master
- [BREAKING] `ember-power-calendar-moment`, `ember-power-calendar-luxon`... must be registered in `app/app.js` (more details in https://ember-power-calendar.com/docs/installation => Choose your preferred date library) (#335)

# 0.20.0
- [BREAKING] Drop support for node < 16 (#328)
- [BREAKING] Drop support for Ember < 3.28 (#328)
- [BREAKING] Update components to glimmer (#328)
Expand Down
3 changes: 3 additions & 0 deletions addon/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { registerDateLibrary } from './utils';

export { registerDateLibrary };
78 changes: 37 additions & 41 deletions addon/utils.js
Original file line number Diff line number Diff line change
@@ -1,119 +1,115 @@
import {
dependencySatisfies,
macroCondition,
importSync,
} from '@embroider/macros';

const DateLibrary = (() => {
if (macroCondition(dependencySatisfies('ember-power-calendar-moment', '*'))) {
return importSync('ember-power-calendar-moment');
} else if (
macroCondition(dependencySatisfies('ember-power-calendar-luxon', '*'))
) {
return importSync('ember-power-calendar-luxon');
} else {
let dateLib;

export function registerDateLibrary(dateLibrary) {
dateLib = dateLibrary;
}

function getDateLibrary() {
if (!dateLib) {
throw new Error(
`You have installed "ember-power-calendar" but you don't have any of the required meta-addons to make it work, like 'ember-power-calendar-moment' or 'ember-power-calendar-luxon'. Please add to your app.`,
`You have installed "ember-power-calendar" but you don't have registered any of the required meta-addons to make it work, like 'ember-power-calendar-moment' or 'ember-power-calendar-luxon'. Please add to your app and register the meta package in app.js.`,
);
}
})();

return dateLib;
}

export function add(date, quantity, unit) {
return DateLibrary.add(date, quantity, unit);
return getDateLibrary().add(date, quantity, unit);
}

export function formatDate(date, format, locale = null) {
return DateLibrary.formatDate(date, format, locale);
return getDateLibrary().formatDate(date, format, locale);
}

export function startOf(date, unit) {
return DateLibrary.startOf(date, unit);
return getDateLibrary().startOf(date, unit);
}

export function endOf(date, unit) {
return DateLibrary.endOf(date, unit);
return getDateLibrary().endOf(date, unit);
}

export function weekday(date) {
return DateLibrary.weekday(date);
return getDateLibrary().weekday(date);
}

export function isoWeekday(date) {
return DateLibrary.isoWeekday(date);
return getDateLibrary().isoWeekday(date);
}

export function getWeekdaysShort() {
return DateLibrary.getWeekdaysShort();
return getDateLibrary().getWeekdaysShort();
}

export function getWeekdaysMin() {
return DateLibrary.getWeekdaysMin();
return getDateLibrary().getWeekdaysMin();
}

export function getWeekdays() {
return DateLibrary.getWeekdays();
return getDateLibrary().getWeekdays();
}

export function isAfter(date1, date2) {
return DateLibrary.isAfter(date1, date2);
return getDateLibrary().isAfter(date1, date2);
}

export function isBefore(date1, date2) {
return DateLibrary.isBefore(date1, date2);
return getDateLibrary().isBefore(date1, date2);
}

export function isSame(date1, date2, unit) {
return DateLibrary.isSame(date1, date2, unit);
return getDateLibrary().isSame(date1, date2, unit);
}

export function isBetween(date, start, end, unit, inclusivity) {
return DateLibrary.isBetween(date, start, end, unit, inclusivity);
return getDateLibrary().isBetween(date, start, end, unit, inclusivity);
}

export function diff(date1, date2) {
return DateLibrary.diff(date1, date2);
return getDateLibrary().diff(date1, date2);
}

export function normalizeDate(date) {
return DateLibrary.normalizeDate(date);
return getDateLibrary().normalizeDate(date);
}

export function normalizeRangeActionValue(val) {
return DateLibrary.normalizeRangeActionValue(val);
return getDateLibrary().normalizeRangeActionValue(val);
}

export function normalizeMultipleActionValue(val) {
return DateLibrary.normalizeMultipleActionValue(val);
return getDateLibrary().normalizeMultipleActionValue(val);
}

export function normalizeCalendarDay(day) {
return DateLibrary.normalizeCalendarDay(day);
return getDateLibrary().normalizeCalendarDay(day);
}

export function withLocale(locale, fn) {
return DateLibrary.withLocale(locale, fn);
return getDateLibrary().withLocale(locale, fn);
}

export function normalizeCalendarValue(value) {
return DateLibrary.normalizeCalendarValue(value);
return getDateLibrary().normalizeCalendarValue(value);
}

export function normalizeDuration(value) {
return DateLibrary.normalizeDuration(value);
return getDateLibrary().normalizeDuration(value);
}

export function getDefaultLocale() {
return DateLibrary.getDefaultLocale();
return getDateLibrary().getDefaultLocale();
}

export function localeStartOfWeek(locale) {
return DateLibrary.localeStartOfWeek(locale);
return getDateLibrary().localeStartOfWeek(locale);
}

export function startOfWeek(day, startOfWeek) {
return DateLibrary.startOfWeek(day, startOfWeek);
return getDateLibrary().startOfWeek(day, startOfWeek);
}

export function endOfWeek(day, startOfWeek) {
return DateLibrary.endOfWeek(day, startOfWeek);
return getDateLibrary().endOfWeek(day, startOfWeek);
}
Loading