From 27099965f79ae5c11508f3622411e6af63ed49ab Mon Sep 17 00:00:00 2001 From: mark-tate <143323+mark-tate@users.noreply.github.com> Date: Tue, 24 Dec 2024 16:40:14 +0000 Subject: [PATCH] Split date adapters into sub packages to help with peerDependencies resolution ```diff - import { AdapterDateFns } from "@salt-ds/date-adapters"; - import { AdapterDayjs } from "@salt-ds/date-adapters"; - import { AdapterLuxon } from "@salt-ds/date-adapters"; - import { AdapterMoment } from "@salt-ds/date-adapters"; + import { AdapterDateFns } from "@salt-ds/date-adapters/date-fns"; + import { AdapterDayjs } from "@salt-ds/date-adapters/dayjs"; + import { AdapterLuxon } from "@salt-ds/date-adapters/luxon"; + import { AdapterMoment } from "@salt-ds/date-adapters/moment"; ``` --- docs/decorators/withLocalization.tsx | 8 +- package.json | 3 +- packages/date-adapters/package.json | 2 +- packages/date-adapters/scripts/build.mjs | 138 +++++++++ .../src/__tests__/date-fns.spec.d.ts | 1 + .../src/__tests__/dayjs.spec.d.ts | 1 + .../src/__tests__/luxon.spec.d.ts | 1 + .../src/__tests__/moment.spec.d.ts | 1 + .../src/date-fns-adapter/index.d.ts | 208 +++++++++++++ .../{date-fns => date-fns-adapter}/index.ts | 2 +- .../src/dayjs-adapter/index.d.ts | 237 ++++++++++++++ .../src/{dayjs => dayjs-adapter}/index.ts | 2 +- packages/date-adapters/src/index.d.ts | 5 + packages/date-adapters/src/index.ts | 21 +- .../src/luxon-adapter/index.d.ts | 229 ++++++++++++++ .../src/{luxon => luxon-adapter}/index.ts | 2 +- .../src/moment-adapter/index.d.ts | 230 ++++++++++++++ .../src/{moment => moment-adapter}/index.ts | 2 +- .../src/types/DateFrameworkTypeMap.d.ts | 2 + .../src/types/DateFrameworkTypeMap.ts | 11 + packages/date-adapters/src/types/index.d.ts | 291 ++++++++++++++++++ .../src/{types.ts => types/index.ts} | 1 + packages/date-adapters/tsconfig.json | 16 + .../__e2e__/calendar/Calendar.a11y.cy.tsx | 8 +- .../__e2e__/calendar/Calendar.cy.tsx | 8 +- .../calendar/Calendar.multiselect.cy.tsx | 8 +- .../__e2e__/calendar/Calendar.offset.cy.tsx | 8 +- .../__e2e__/calendar/Calendar.range.cy.tsx | 14 +- .../__e2e__/calendar/Calendar.single.cy.tsx | 8 +- .../__e2e__/date-input/DateInput.cy.tsx | 8 +- .../__e2e__/date-input/DateInputRange.cy.tsx | 8 +- .../__e2e__/date-input/DateInputSingle.cy.tsx | 8 +- .../__e2e__/date-picker/DatePicker.cy.tsx | 8 +- .../date-picker/DatePicker.range.cy.tsx | 8 +- .../date-picker/DatePicker.single.cy.tsx | 8 +- .../lab/src/calendar/useCalendarSelection.ts | 2 +- scripts/makeTypings.mjs | 5 +- scripts/utils.mjs | 4 +- .../src/components/components/LivePreview.tsx | 2 +- site/src/examples/calendar/WithLocale.tsx | 2 +- .../examples/localization-provider/Locale.tsx | 8 +- .../examples/localization-provider/MinMax.tsx | 3 +- site/src/pages/_app.tsx | 2 +- tsconfig.json | 15 + vite.config.ts | 2 + 45 files changed, 1474 insertions(+), 87 deletions(-) create mode 100644 packages/date-adapters/scripts/build.mjs create mode 100644 packages/date-adapters/src/__tests__/date-fns.spec.d.ts create mode 100644 packages/date-adapters/src/__tests__/dayjs.spec.d.ts create mode 100644 packages/date-adapters/src/__tests__/luxon.spec.d.ts create mode 100644 packages/date-adapters/src/__tests__/moment.spec.d.ts create mode 100644 packages/date-adapters/src/date-fns-adapter/index.d.ts rename packages/date-adapters/src/{date-fns => date-fns-adapter}/index.ts (99%) create mode 100644 packages/date-adapters/src/dayjs-adapter/index.d.ts rename packages/date-adapters/src/{dayjs => dayjs-adapter}/index.ts (99%) create mode 100644 packages/date-adapters/src/index.d.ts create mode 100644 packages/date-adapters/src/luxon-adapter/index.d.ts rename packages/date-adapters/src/{luxon => luxon-adapter}/index.ts (99%) create mode 100644 packages/date-adapters/src/moment-adapter/index.d.ts rename packages/date-adapters/src/{moment => moment-adapter}/index.ts (99%) create mode 100644 packages/date-adapters/src/types/DateFrameworkTypeMap.d.ts create mode 100644 packages/date-adapters/src/types/DateFrameworkTypeMap.ts create mode 100644 packages/date-adapters/src/types/index.d.ts rename packages/date-adapters/src/{types.ts => types/index.ts} (99%) create mode 100644 packages/date-adapters/tsconfig.json diff --git a/docs/decorators/withLocalization.tsx b/docs/decorators/withLocalization.tsx index 0a086d603cb..20099c07ade 100644 --- a/docs/decorators/withLocalization.tsx +++ b/docs/decorators/withLocalization.tsx @@ -1,9 +1,9 @@ import type { Decorator } from "@storybook/react"; import "dayjs/locale/en"; -import { AdapterDateFns } from "@salt-ds/date-adapters"; -import { AdapterDayjs } from "@salt-ds/date-adapters"; -import { AdapterLuxon } from "@salt-ds/date-adapters"; -import { AdapterMoment } from "@salt-ds/date-adapters"; +import { AdapterDateFns } from "@salt-ds/date-adapters/date-fns"; +import { AdapterDayjs } from "@salt-ds/date-adapters/dayjs"; +import { AdapterLuxon } from "@salt-ds/date-adapters/luxon"; +import { AdapterMoment } from "@salt-ds/date-adapters/moment"; import { LocalizationProvider } from "@salt-ds/lab"; import { enUS as dateFnsEnUs } from "date-fns/locale"; diff --git a/package.json b/package.json index 3a64f0edaed..aa3917b404a 100644 --- a/package.json +++ b/package.json @@ -140,6 +140,7 @@ "get-tsconfig": "^4.7.5", "rollup": "^4.24.2", "rollup-plugin-esbuild": "^6.1.1", - "rollup-plugin-postcss": "^4.0.2" + "rollup-plugin-postcss": "^4.0.2", + "vite-tsconfig-paths": "^4.2.0" } } diff --git a/packages/date-adapters/package.json b/packages/date-adapters/package.json index 7fb6f523a8d..001de7c202f 100644 --- a/packages/date-adapters/package.json +++ b/packages/date-adapters/package.json @@ -38,6 +38,6 @@ "provenance": true }, "scripts": { - "build": "yarn node ../../scripts/build.mjs" + "build": "yarn node ./scripts/build.mjs" } } diff --git a/packages/date-adapters/scripts/build.mjs b/packages/date-adapters/scripts/build.mjs new file mode 100644 index 00000000000..93d942e8bb8 --- /dev/null +++ b/packages/date-adapters/scripts/build.mjs @@ -0,0 +1,138 @@ +import path from "node:path"; +import commonjs from "@rollup/plugin-commonjs"; +import json from "@rollup/plugin-json"; +import { nodeResolve } from "@rollup/plugin-node-resolve"; +import browserslistToEsbuild from "browserslist-to-esbuild"; +import fs from "fs-extra"; +import { rollup } from "rollup"; +import esbuild from "rollup-plugin-esbuild"; +import { makeTypings } from "./../../../scripts/makeTypings.mjs"; +import { transformWorkspaceDeps } from "./../../../scripts/transformWorkspaceDeps.mjs"; +import { distinct } from "./../../../scripts/utils.mjs"; + +const cwd = process.cwd(); + +const packageJson = ( + await import(path.join("file://", cwd, "package.json"), { + with: { type: "json" }, + }) +).default; + +const FILES_TO_COPY = ["README.md", "LICENSE", "CHANGELOG.md"].concat( + packageJson.files ?? [], +); + +const packageName = packageJson.name; +const outputDir = path.join(packageJson.publishConfig.directory); + +console.log(`Building ${packageName}`); + +await fs.mkdirp(outputDir); +await fs.emptyDir(outputDir); + +// Define entry points for each adapter +const entryPoints = { + types: path.join(cwd, "src/types/index.ts"), + moment: path.join(cwd, "src/moment-adapter/index.ts"), + luxon: path.join(cwd, "src/luxon-adapter/index.ts"), + dayjs: path.join(cwd, "src/dayjs-adapter/index.ts"), + "date-fns": path.join(cwd, "src/date-fns-adapter/index.ts"), +}; + +for (const [adapterName, inputPath] of Object.entries(entryPoints)) { + + const entryFolder = path.basename(path.dirname(inputPath)); + + await makeTypings(outputDir, path.dirname(inputPath)); + + const bundle = await rollup({ + input: inputPath, + external: (id) => { + if (id === "babel-plugin-transform-async-to-promises/helpers") { + return false; + } + return !id.startsWith(".") && !path.isAbsolute(id); + }, + treeshake: { + propertyReadSideEffects: false, + }, + plugins: [ + nodeResolve({ + extensions: [".ts", ".tsx", ".js", ".jsx"], + browser: true, + mainFields: ["module", "main", "browser"], + }), + commonjs({ include: /\/node_modules\// }), + esbuild({ + target: browserslistToEsbuild(), + minify: false, + sourceMap: true, + }), + json(), + ], + }); + + const transformSourceMap = (relativeSourcePath, sourceMapPath) => { + const absoluteSourcepath = path.resolve( + path.dirname(sourceMapPath), + relativeSourcePath, + ); + const packageRelativeSourcePath = path.relative(cwd, absoluteSourcepath); + + return `../${packageRelativeSourcePath}`; + }; + + await bundle.write({ + freeze: false, + sourcemap: true, + preserveModules: false, + dir: path.join(outputDir, `dist-cjs/${adapterName}`), + format: "cjs", + exports: "auto", + sourcemapPathTransform: transformSourceMap, + }); + + await bundle.write({ + freeze: false, + sourcemap: true, + preserveModules: false, + dir: path.join(outputDir, `dist-es/${adapterName}`), + format: "es", + exports: "auto", + sourcemapPathTransform: transformSourceMap, + }); + + await bundle.close(); +} + +await fs.writeJSON( + path.join(outputDir, "package.json"), + { + ...packageJson, + dependencies: await transformWorkspaceDeps(packageJson.dependencies), + main: "dist-cjs/index.js", + module: "dist-es/index.js", + typings: "dist-types/types/index.d.ts", + files: distinct([ + ...(packageJson.files ?? []), + "dist-cjs", + "dist-es", + "dist-types", + "CHANGELOG.md", + ]), + }, + { spaces: 2 }, +); + +for (const file of FILES_TO_COPY) { + const filePath = path.join(cwd, file); + try { + await fs.copy(filePath, path.join(outputDir, file)); + } catch (error) { + if (error.code !== "ENOENT") { + throw error; + } + } +} + +console.log(`Built ${packageName} into ${outputDir}`); diff --git a/packages/date-adapters/src/__tests__/date-fns.spec.d.ts b/packages/date-adapters/src/__tests__/date-fns.spec.d.ts new file mode 100644 index 00000000000..cb0ff5c3b54 --- /dev/null +++ b/packages/date-adapters/src/__tests__/date-fns.spec.d.ts @@ -0,0 +1 @@ +export {}; diff --git a/packages/date-adapters/src/__tests__/dayjs.spec.d.ts b/packages/date-adapters/src/__tests__/dayjs.spec.d.ts new file mode 100644 index 00000000000..cb0ff5c3b54 --- /dev/null +++ b/packages/date-adapters/src/__tests__/dayjs.spec.d.ts @@ -0,0 +1 @@ +export {}; diff --git a/packages/date-adapters/src/__tests__/luxon.spec.d.ts b/packages/date-adapters/src/__tests__/luxon.spec.d.ts new file mode 100644 index 00000000000..cb0ff5c3b54 --- /dev/null +++ b/packages/date-adapters/src/__tests__/luxon.spec.d.ts @@ -0,0 +1 @@ +export {}; diff --git a/packages/date-adapters/src/__tests__/moment.spec.d.ts b/packages/date-adapters/src/__tests__/moment.spec.d.ts new file mode 100644 index 00000000000..e66ba94cc75 --- /dev/null +++ b/packages/date-adapters/src/__tests__/moment.spec.d.ts @@ -0,0 +1 @@ +import "moment-timezone"; diff --git a/packages/date-adapters/src/date-fns-adapter/index.d.ts b/packages/date-adapters/src/date-fns-adapter/index.d.ts new file mode 100644 index 00000000000..f8d798a2cc4 --- /dev/null +++ b/packages/date-adapters/src/date-fns-adapter/index.d.ts @@ -0,0 +1,208 @@ +import { type Locale } from "date-fns"; +import { type AdapterOptions, type ParserResult, type RecommendedFormats, type SaltDateAdapter, type TimeFields, type Timezone } from "../types"; +declare module "@salt-ds/date-adapters" { + interface DateFrameworkTypeMap { + "date-fns": Date; + } +} +/** + * Adapter for date-fns library, implementing the SaltDateAdapter interface. + * Provides methods for date manipulation and formatting using date-fns. + */ +export declare class AdapterDateFns implements SaltDateAdapter { + /** + * The locale used for date formatting. + */ + locale: Locale; + /** + * The name of the date library. + */ + lib: string; + /** + * Mapping of format tokens from other libraries to date-fns format tokens. + */ + formats: { + [key: string]: string; + }; + /** + * Creates an instance of AdapterDateFns. + * @param options - Adapter options including locale. + */ + constructor({ locale }?: AdapterOptions); + /** + * Maps format tokens from other libraries to date-fns format tokens. + * @param adapterFormat - The format string to map. + * @returns The mapped format string. + */ + private mapToDateFnsFormat; + /** + * Creates a Date object from a string or returns an invalid date. + * @param value - The date string to parse. + * @param timezone - The timezone to use (default is "default"). + * @param locale - The locale to use for parsing. + * @returns The parsed Date object or an invalid date object. + */ + date: (value?: T | undefined, _timezone?: Timezone, _locale?: Locale | undefined) => Date; + /** + * Formats a Date object using the specified format string. + * Returns an empty string when null or undefined date is given. + * @param date - The Date object to format. + * @param format - The format string to use. + * @param locale - The locale to use for formatting. + * @returns The formatted date string. + */ + format(date: Date | null | undefined, format?: RecommendedFormats, locale?: Locale): string; + /** + * Compares two Date objects. + * @param dateA - The first Date object. + * @param dateB - The second Date object. + * @returns 0 if equal, 1 if dateA is after dateB, -1 if dateA is before dateB. + */ + compare(dateA: Date, dateB: Date): number; + /** + * Parses a date string using the specified format. + * @param value - The date string to parse. + * @param format - The format string to use. + * @param locale - The locale to use for parsing. + * @returns A DateDetail object containing the parsed date and any errors. + */ + parse(value: string, format: string, locale?: Locale): ParserResult; + /** + * Checks if a Date object is valid. + * @param date - The Date object to check. + * @returns True if the date is valid date object, false otherwise. + */ + isValid(date: any): date is Date; + /** + * Subtracts time from a Date object. + * @param date - The Date object to subtract from. + * @param duration - The duration to subtract. + * @returns The resulting Date object. + */ + subtract(date: Date, { days, weeks, months, years, hours, minutes, seconds, milliseconds, }: { + days?: number; + weeks?: number; + months?: number; + years?: number; + hours?: number; + minutes?: number; + seconds?: number; + milliseconds?: number; + }): Date; + /** + * Adds time to a Date object. + * @param date - The Date object to add to. + * @param duration - The duration to add. + * @returns The resulting Date object. + */ + add(date: Date, { days, weeks, months, years, hours, minutes, seconds, milliseconds, }: { + days?: number; + weeks?: number; + months?: number; + years?: number; + hours?: number; + minutes?: number; + seconds?: number; + milliseconds?: number; + }): Date; + /** + * Sets specific components of a Date object. + * @param date - The Date object to modify. + * @param components - The components to set, the month is a number (1-12). + * @returns The resulting Date object. + */ + set(date: Date, { day, month, year, hour, minute, second, millisecond, }: { + day?: number; + month?: number; + year?: number; + hour?: number; + minute?: number; + second?: number; + millisecond?: number; + }): Date; + /** + * Checks if two Date objects are the same based on the specified granularity. + * @param dateA - The first Date object. + * @param dateB - The second Date object. + * @param granularity - The granularity to compare by ("day", "month", "year"). + * @returns True if the dates are the same, false otherwise. + */ + isSame(dateA: Date, dateB: Date, granularity?: "day" | "month" | "year"): boolean; + /** + * Gets the start of a specified time period for a Date object. + * @param date - The Date object. + * @param offset - The time period ("day", "week", "month", "year"). + * @param locale - The locale to use. + * @returns The Date object representing the start of the period. + */ + startOf(date: Date, offset: "day" | "week" | "month" | "year", locale?: Locale): Date; + /** + * Gets the end of a specified time period for a Date object. + * @param date - The Date object. + * @param offset - The time period ("day", "week", "month", "year"). + * @param locale - The locale to use. + * @returns The Date object representing the end of the period. + */ + endOf(date: Date, offset: "day" | "week" | "month" | "year", locale?: Locale): Date; + /** + * Gets the current date with the time set to the start of the day. + * @param _locale - The locale to use. + * @returns The current date at the start of the day. + */ + today(_locale?: Locale): Date; + /** + * Gets the current date and time. + * @param locale - The locale to use. + * @returns The current date and time. + */ + now(locale?: Locale): Date; + /** + * Gets the day of the week for a Date object. + * @param date - The Date object. + * @param locale - The locale to use. + * @returns The day of the week as a number (0-6). + */ + getDayOfWeek(date: Date, locale?: Locale): number; + /** + * Gets the name of the day of the week. + * @param dow - The day of the week as a number (0-6). + * @param format - The format for the day name ("long", "short", "narrow"). + * @param locale - The locale to use. + * @returns The name of the day of the week. + */ + getDayOfWeekName(dow: number, format: "long" | "short" | "narrow", locale: Locale): string; + /** + * Gets the day of the month for a Date object. + * @param date - The Date object. + * @returns The day of the month as a number (1-31). + */ + getDay(date: Date): number; + /** + * Gets the month for a Date object. + * @param date - The Date object. + * @returns The month as a number (1-12). + */ + getMonth(date: Date): number; + /** + * Gets the year for a Date object. + * @param date - The Date object. + * @returns The year as a number. + */ + getYear(date: Date): number; + /** + * Gets the time components for a Date object. + * @param date - The Date object. + * @returns An object containing the hour, minute, second, and millisecond. + */ + getTime(date: Date): TimeFields; + /** + * Validate date string so it can be parsed + * @param value + */ + isValidDateString(value: string): boolean; + /** + * Clone the date object + * @param date + */ + clone(date: Date): Date; +} diff --git a/packages/date-adapters/src/date-fns/index.ts b/packages/date-adapters/src/date-fns-adapter/index.ts similarity index 99% rename from packages/date-adapters/src/date-fns/index.ts rename to packages/date-adapters/src/date-fns-adapter/index.ts index 2765e96a406..c64b9c85e73 100644 --- a/packages/date-adapters/src/date-fns/index.ts +++ b/packages/date-adapters/src/date-fns-adapter/index.ts @@ -41,7 +41,7 @@ import { type Timezone, } from "../types"; -declare module "../types" { +declare module "@salt-ds/date-adapters" { interface DateFrameworkTypeMap { "date-fns": Date; } diff --git a/packages/date-adapters/src/dayjs-adapter/index.d.ts b/packages/date-adapters/src/dayjs-adapter/index.d.ts new file mode 100644 index 00000000000..3d78dbee827 --- /dev/null +++ b/packages/date-adapters/src/dayjs-adapter/index.d.ts @@ -0,0 +1,237 @@ +import defaultDayjs, { type Dayjs } from "dayjs"; +import { type AdapterOptions, type ParserResult, type RecommendedFormats, type SaltDateAdapter, type TimeFields, type Timezone } from "../types"; +declare type Constructor = { + (...args: Parameters): Dayjs; + tz?: (value: Parameters[0], timezone: string) => Dayjs; + utc?: (value?: Parameters[0]) => Dayjs; +}; +declare module "@salt-ds/date-adapters" { + interface DateFrameworkTypeMap { + dayjs: Dayjs; + } +} +/** + * Adapter for Day.js library, implementing the SaltDateAdapter interface. + * Provides methods for date manipulation and formatting using Day.js. + */ +export declare class AdapterDayjs implements SaltDateAdapter { + /** + * The Day.js instance used for date operations. + */ + dayjs: Constructor; + /** + * The locale used for date formatting. + */ + locale: string; + /** + * The name of the date library. + */ + lib: string; + /** + * Creates an instance of AdapterDayjs. + * @param options - Adapter options including locale. + * @param instance - The Day.js instance to use. + */ + constructor({ locale }?: AdapterOptions, instance?: Constructor); + /** + * Type guard for Dayjs object + * @param date + * @private + */ + private isDayjs; + /** + * Creates a Day.js date object in the system timezone. + * @param value - The date string to parse. + * @param locale - The locale to use for parsing. + * @returns The parsed Day.js date object. + */ + private createSystemDate; + /** + * Creates a Day.js date object in UTC. + * @param value - The date string to parse. + * @param locale - The locale to use for parsing. + * @returns The parsed Day.js date object. + * @throws Error if the UTC plugin is missing. + */ + private createUTCDate; + /** + * Creates a Day.js date object in a specified timezone. + * @param value - The date string to parse. + * @param timezone - The timezone to use. + * @param locale - The locale to use for parsing. + * @returns The parsed Day.js date object. + * @throws Error if the timezone plugin is missing. + */ + private createTZDate; + /** + * Creates a Day.js date object from a string or returns an invalid date. + * @param value - The date string to parse. + * @param timezone - The timezone to use (default is "default"). + * @param locale - The locale to use for parsing. + * @returns The parsed Day.js date object or an invalid date object. + */ + date: (value?: T | undefined, timezone?: Timezone, locale?: string | undefined) => Dayjs; + /** + * Formats a Day.js date object using the specified format string. + * Returns an empty string when null or undefined date is given. + * @param date - The Day.js date object to format. + * @param format - The format string to use. + * @param locale - The locale to use for formatting. + * @returns The formatted date string. + */ + format(date: Dayjs | null | undefined, format?: RecommendedFormats, locale?: string): string; + /** + * Compares two Day.js date objects. + * @param dateA - The first Day.js date object. + * @param dateB - The second Day.js date object. + * @returns 0 if equal, 1 if dateA is after dateB, -1 if dateA is before dateB. + */ + compare(dateA: Dayjs, dateB: Dayjs): number; + /** + * Parses a date string using the specified format. + * @param value - The date string to parse. + * @param format - The format string to use. + * @param locale - The locale to use for parsing. + * @returns A DateDetail object containing the parsed date and any errors. + */ + parse(value: string, format: string, locale?: string): ParserResult; + /** + * Checks if a Day.js date object is valid. + * @param date - The Day.js date object to check. + * @returns True if the date is valid date object, false otherwise. + */ + isValid(date: any): date is Dayjs; + /** + * Subtracts time from a Day.js date object. + * @param date - The Day.js date object to subtract from. + * @param duration - The duration to subtract. + * @returns The resulting Day.js date object. + */ + subtract(date: Dayjs, { days, weeks, months, years, hours, minutes, seconds, milliseconds, }: { + days?: number; + weeks?: number; + months?: number; + years?: number; + hours?: number; + minutes?: number; + seconds?: number; + milliseconds?: number; + }): Dayjs; + /** + * Adds time to a Day.js date object. + * @param date - The Day.js date object to add to. + * @param duration - The duration to add. + * @returns The resulting Day.js date object. + */ + add(date: Dayjs, { days, weeks, months, years, hours, minutes, seconds, milliseconds, }: { + days?: number; + weeks?: number; + months?: number; + years?: number; + hours?: number; + minutes?: number; + seconds?: number; + milliseconds?: number; + }): Dayjs; + /** + * Sets specific components of a Day.js date object. + * @param date - The Day.js date object to modify. + * @param components - The components to set, the month is a number (1-12). + * @returns The resulting Day.js date object. + */ + set(date: Dayjs, { day, month, year, hour, minute, second, millisecond, }: { + day?: number; + month?: number; + year?: number; + hour?: number; + minute?: number; + second?: number; + millisecond?: number; + }): Dayjs; + /** + * Checks if two Day.js date objects are the same based on the specified granularity. + * @param dateA - The first Day.js date object. + * @param dateB - The second Day.js date object. + * @param granularity - The granularity to compare by ("day", "month", "year"). + * @returns True if the dates are the same, false otherwise. + */ + isSame(dateA: Dayjs, dateB: Dayjs, granularity?: "day" | "month" | "year"): boolean; + /** + * Gets the start of a specified time period for a Day.js date object. + * @param date - The Day.js date object. + * @param offset - The time period ("day", "week", "month", "year"). + * @param locale - The locale to use. + * @returns The Day.js date object representing the start of the period. + */ + startOf(date: Dayjs, offset: "day" | "week" | "month" | "year", locale?: string): Dayjs; + /** + * Gets the end of a specified time period for a Day.js date object. + * @param date - The Day.js date object. + * @param offset - The time period ("day", "week", "month", "year"). + * @param locale - The locale to use. + * @returns The Day.js date object representing the end of the period. + */ + endOf(date: Dayjs, offset: "day" | "week" | "month" | "year", locale?: string): Dayjs; + /** + * Gets the current date with the time set to the start of the day. + * @param locale - The locale to use. + * @returns The current date at the start of the day. + */ + today(locale?: string): Dayjs; + /** + * Gets the current date and time. + * @param locale - The locale to use. + * @returns The current date and time. + */ + now(locale?: string): Dayjs; + /** + * Gets the day of the week for a Day.js date object. + * @param date - The Day.js date object. + * @param locale - The locale to use. + * @returns The day of the week as a number (0-6). + */ + getDayOfWeek(date: Dayjs, locale?: string): number; + /** + * Gets the name of the day of the week. + * @param dow - The day of the week as a number (0-6). + * @param format - The format for the day name ("long", "short", "narrow"). + * @param locale - The locale to use + * @returns The name of the day of the week. + */ + getDayOfWeekName(dow: number, format: "long" | "short" | "narrow", locale?: string): string; + /** + * Gets the day of the month for a Day.js date object. + * @param date - The Day.js date object. + * @returns The day of the month as a number (1-31). + */ + getDay(date: Dayjs): number; + /** + * Gets the month for a Day.js date object. + * @param date - The Day.js date object. + * @returns The month as a number (0-11). + */ + getMonth(date: Dayjs): number; + /** + * Gets the year for a Day.js date object. + * @param date - The Day.js date object. + * @returns The year as a number. + */ + getYear(date: Dayjs): number; + /** + * Gets the time components for a Day.js date object. + * @param date - The Day.js date object. + * @returns An object containing the hour, minute, second, and millisecond. + */ + getTime(date: Dayjs): TimeFields; + /** + * Validate date string so it can be parsed + * @param value + */ + isValidDateString(value: string): boolean; + /** + * Clone the date object + * @param date + */ + clone(date: Dayjs): Dayjs; +} +export {}; diff --git a/packages/date-adapters/src/dayjs/index.ts b/packages/date-adapters/src/dayjs-adapter/index.ts similarity index 99% rename from packages/date-adapters/src/dayjs/index.ts rename to packages/date-adapters/src/dayjs-adapter/index.ts index 17200410876..4102245dc5a 100644 --- a/packages/date-adapters/src/dayjs/index.ts +++ b/packages/date-adapters/src/dayjs-adapter/index.ts @@ -20,7 +20,7 @@ type Constructor = { utc?: (value?: Parameters[0]) => Dayjs; }; -declare module "../types" { +declare module "@salt-ds/date-adapters" { interface DateFrameworkTypeMap { dayjs: Dayjs; } diff --git a/packages/date-adapters/src/index.d.ts b/packages/date-adapters/src/index.d.ts new file mode 100644 index 00000000000..b219142d83f --- /dev/null +++ b/packages/date-adapters/src/index.d.ts @@ -0,0 +1,5 @@ +export * from "./date-fns-adapter"; +export * from "./dayjs-adapter"; +export * from "./luxon-adapter"; +export * from "./moment-adapter"; +export * from "./types"; diff --git a/packages/date-adapters/src/index.ts b/packages/date-adapters/src/index.ts index 126225dc310..b219142d83f 100644 --- a/packages/date-adapters/src/index.ts +++ b/packages/date-adapters/src/index.ts @@ -1,18 +1,5 @@ -/** - * To add a new adapter, then, add the adapter's date object to `DateFrameworkTypeMap` interface - * - * declare module "./types" { - * interface DateFrameworkTypeMap { - * luxon: DateTime; - * } - * } - */ -// biome-ignore lint/complexity/noBannedTypes: type augmented by configured adapters -export type DateFrameworkTypeMap = {}; - -export * from "./date-fns"; -export * from "./dayjs"; -export * from "./luxon"; -export * from "./moment"; - +export * from "./date-fns-adapter"; +export * from "./dayjs-adapter"; +export * from "./luxon-adapter"; +export * from "./moment-adapter"; export * from "./types"; diff --git a/packages/date-adapters/src/luxon-adapter/index.d.ts b/packages/date-adapters/src/luxon-adapter/index.d.ts new file mode 100644 index 00000000000..8371664ab40 --- /dev/null +++ b/packages/date-adapters/src/luxon-adapter/index.d.ts @@ -0,0 +1,229 @@ +import { DateTime } from "luxon"; +import { type AdapterOptions, type ParserResult, type RecommendedFormats, type SaltDateAdapter, type TimeFields, type Timezone } from "../types"; +declare module "@salt-ds/date-adapters" { + interface DateFrameworkTypeMap { + luxon: DateTime; + } +} +/** + * Adapter for Luxon library, implementing the SaltDateAdapter interface. + * Provides methods for date manipulation and formatting using Luxon. + */ +export declare class AdapterLuxon implements SaltDateAdapter { + /** + * The locale used for date formatting. + */ + locale: string; + /** + * The name of the date library. + */ + lib: string; + /** + * Mapping of format tokens from other libraries to Luxon format tokens. + */ + formats: { + [key: string]: string; + }; + /** + * Creates an instance of AdapterLuxon. + * @param options - Adapter options including locale. + */ + constructor({ locale }?: AdapterOptions); + /** + * Maps format tokens from other libraries to Luxon format tokens. + * @param adapterFormat - The format string to map. + * @returns The mapped format string. + */ + private mapToLuxonFormat; + /** + * Creates a Luxon DateTime object in the system timezone. + * @param value - The date string to parse. + * @param locale - The locale to use for parsing. + * @returns The parsed Luxon DateTime object. + */ + private createSystemDate; + /** + * Creates a Luxon DateTime object in UTC. + * @param value - The date string to parse. + * @param locale - The locale to use for parsing. + * @returns The parsed Luxon DateTime object. + */ + private createUTCDate; + /** + * Creates a Luxon DateTime object in a specified timezone. + * @param value - The date string to parse. + * @param timezone - The timezone to use. + * @param locale - The locale to use for parsing. + * @returns The parsed Luxon DateTime object. + */ + private createTZDate; + /** + * Creates a Luxon DateTime object from a string or returns an invalid date. + * @param value - The date string to parse. + * @param timezone - The timezone to use (default is "default"). + * @param locale - The locale to use for parsing. + * @returns The parsed Luxon DateTime object or an invalid date object. + */ + date: (value?: T | undefined, timezone?: Timezone, locale?: string | undefined) => DateTime; + /** + * Formats a Luxon DateTime object using the specified format string. + * Returns an empty string when null or undefined date is given. + * @param date - The Luxon DateTime object to format. + * @param format - The format string to use. + * @param locale - The locale to use for formatting. + * @returns The formatted date string. + */ + format(date: DateTime | null | undefined, format?: RecommendedFormats, locale?: string): string; + /** + * Compares two Luxon DateTime objects. + * @param dateA - The first Luxon DateTime object. + * @param dateB - The second Luxon DateTime object. + * @returns 0 if equal, 1 if dateA is after dateB, -1 if dateA is before dateB. + */ + compare(dateA: DateTime, dateB: DateTime): number; + /** + * Parses a date string using the specified format. + * @param value - The date string to parse. + * @param format - The format string to use. + * @param locale - The locale to use for parsing. + * @returns A DateDetail object containing the parsed date and any errors. + */ + parse(value: string, format: string, locale?: string): ParserResult; + /** + * Checks if a Luxon DateTime object is valid. + * @param date - The Luxon DateTime object to check. + * @returns True if the date is valid date object, false otherwise. + */ + isValid(date: any): date is DateTime; + /** + * Subtracts time from a Luxon DateTime object. + * @param date - The Luxon DateTime object to subtract from. + * @param duration - The duration to subtract. + * @returns The resulting Luxon DateTime object. + */ + subtract(date: DateTime, { days, weeks, months, years, hours, minutes, seconds, milliseconds, }: { + days?: number; + weeks?: number; + months?: number; + years?: number; + hours?: number; + minutes?: number; + seconds?: number; + milliseconds?: number; + }): DateTime; + /** + * Adds time to a Luxon DateTime object. + * @param date - The Luxon DateTime object to add to. + * @param duration - The duration to add. + * @returns The resulting Luxon DateTime object. + */ + add(date: DateTime, { days, weeks, months, years, hours, minutes, seconds, milliseconds, }: { + days?: number; + weeks?: number; + months?: number; + years?: number; + hours?: number; + minutes?: number; + seconds?: number; + milliseconds?: number; + }): DateTime; + /** + * Sets specific components of a Luxon DateTime object. + * @param date - The Luxon DateTime object to modify. + * @param components - The components to set, the month is a number (1-12). + * @returns The resulting Luxon DateTime object. + */ + set(date: DateTime, { day, month, year, hour, minute, second, millisecond, }: { + day?: number; + month?: number; + year?: number; + hour?: number; + minute?: number; + second?: number; + millisecond?: number; + }): DateTime; + /** + * Checks if two Luxon DateTime objects are the same based on the specified granularity. + * @param dateA - The first Luxon DateTime object. + * @param dateB - The second Luxon DateTime object. + * @param granularity - The granularity to compare by ("day", "month", "year"). + * @returns True if the dates are the same, false otherwise. + */ + isSame(dateA: DateTime, dateB: DateTime, granularity?: "day" | "month" | "year"): boolean; + /** + * Gets the start of a specified time period for a Luxon DateTime object. + * @param date - The Luxon DateTime object. + * @param offset - The time period ("day", "week", "month", "year"). + * @param locale - The locale to use. + * @returns The Luxon DateTime object representing the start of the period. + */ + startOf(date: DateTime, offset: "day" | "week" | "month" | "year", locale?: string): DateTime; + /** + * Gets the end of a specified time period for a Luxon DateTime object. + * @param date - The Luxon DateTime object. + * @param offset - The time period ("day", "week", "month", "year"). + * @param locale - The locale to use. + * @returns The Luxon DateTime object representing the end of the period. + */ + endOf(date: DateTime, offset: "day" | "week" | "month" | "year", locale?: string): DateTime; + /** + * Gets the current date with the time set to the start of the day. + * @param locale - The locale to use. + * @returns The current date at the start of the day. + */ + today(locale?: string): DateTime; + /** + * Gets the current date and time. + * @param locale - The locale to use. + * @returns The current date and time. + */ + now(locale?: string): DateTime; + /** + * Gets the name of the day of the week. + * @param dow - The day of the week as a number (0-6). + * @param format - The format for the day name ("long", "short", "narrow"). + * @param locale - The locale to use. + * @returns The name of the day of the week. + */ + getDayOfWeekName(dow: number, format: "long" | "short" | "narrow", locale?: string): string; + /** + * Gets the day of the week for a Luxon DateTime object. + * @param date - The Luxon DateTime object. + * @returns The day of the week as a number (1-7). + */ + getDayOfWeek(date: DateTime): number; + /** + * Gets the day of the month for a Luxon DateTime object. + * @param date - The Luxon DateTime object. + * @returns The day of the month as a number (1-31). + */ + getDay(date: DateTime): number; + /** + * Gets the month for a Luxon DateTime object. + * @param date - The Luxon DateTime object. + * @returns The month as a number (1-12). + */ + getMonth(date: DateTime): number; + /** + * Gets the year for a Luxon DateTime object. + * @param date - The Luxon DateTime object. + * @returns The year as a number. + */ + getYear(date: DateTime): number; + /** + * Gets the time components for a Luxon DateTime object. + * @param date - The Luxon DateTime object. + * @returns An object containing the hour, minute, second, and millisecond. + */ + getTime(date: DateTime): TimeFields; + /** + * Validate date string so it can be parsed + * @param value + */ + isValidDateString(value: string): boolean; + /** + * Clone the date object + * @param date + */ + clone(date: DateTime): DateTime; +} diff --git a/packages/date-adapters/src/luxon/index.ts b/packages/date-adapters/src/luxon-adapter/index.ts similarity index 99% rename from packages/date-adapters/src/luxon/index.ts rename to packages/date-adapters/src/luxon-adapter/index.ts index 00d78cf1100..35d9c74b87d 100644 --- a/packages/date-adapters/src/luxon/index.ts +++ b/packages/date-adapters/src/luxon-adapter/index.ts @@ -9,7 +9,7 @@ import { type Timezone, } from "../types"; -declare module "../types" { +declare module "@salt-ds/date-adapters" { interface DateFrameworkTypeMap { luxon: DateTime; } diff --git a/packages/date-adapters/src/moment-adapter/index.d.ts b/packages/date-adapters/src/moment-adapter/index.d.ts new file mode 100644 index 00000000000..284587dd038 --- /dev/null +++ b/packages/date-adapters/src/moment-adapter/index.d.ts @@ -0,0 +1,230 @@ +import defaultMoment, { type Moment } from "moment"; +import { type AdapterOptions, type ParserResult, type RecommendedFormats, type SaltDateAdapter, type TimeFields, type Timezone } from "../types"; +declare module "@salt-ds/date-adapters" { + interface DateFrameworkTypeMap { + moment: Moment; + } +} +/** + * Adapter for Moment.js library, implementing the SaltDateAdapter interface. + * Provides methods for date manipulation and formatting using Moment.js. + * Salt provides a Moment adapter to aid migration to a maintained library. + * + * @deprecated Moment date library has been deprecated by its maintainers since September 2020, consider migration to a maintained OSS library. + */ +export declare class AdapterMoment implements SaltDateAdapter { + /** + * The Moment.js instance used for date operations. + */ + moment: typeof defaultMoment; + /** + * The locale used for date formatting. + */ + locale: string; + /** + * The name of the date library. + */ + lib: string; + /** + * Creates an instance of AdapterMoment. + * @param options - Adapter options including locale and instance. + */ + constructor({ locale, instance, }?: AdapterOptions); + /** + * Checks if the timezone plugin is available. + * @returns True if the timezone plugin is available, false otherwise. + */ + private hasTimezonePlugin; + /** + * Creates a Moment.js date object in the system timezone. + * @param value - The date string to parse. + * @param locale - The locale to use for parsing. + * @returns The parsed Moment.js date object. + */ + private createSystemDate; + /** + * Creates a Moment.js date object in UTC. + * @param value - The date string to parse. + * @param locale - The locale to use for parsing. + * @returns The parsed Moment.js date object. + */ + private createUTCDate; + /** + * Creates a Moment.js date object in a specified timezone. + * @param value - The date string to parse. + * @param timezone - The timezone to use. + * @param locale - The locale to use for parsing. + * @returns The parsed Moment.js date object. + * @throws Error if the timezone plugin is missing. + */ + private createTZDate; + /** + * Creates a Moment.js date object from a string or returns an invalid date. + * @param value - The date string to parse. + * @param timezone - The timezone to use (default is "default"). + * @param locale - The locale to use for parsing. + * @returns The parsed Moment.js date object or an invalid date object. + */ + date: (value?: T | undefined, timezone?: Timezone, locale?: string | undefined) => Moment; + /** + * Formats a Moment.js date object using the specified format string. + * Returns an empty string when null or undefined date is given. + * @param date - The Moment.js date object to format. + * @param format - The format string to use. + * @param locale - The locale to use for formatting. + * @returns The formatted date string. + */ + format(date: Moment | null | undefined, format?: RecommendedFormats, locale?: string): string; + /** + * Compares two Moment.js date objects. + * @param dateA - The first Moment.js date object. + * @param dateB - The second Moment.js date object. + * @returns 0 if equal, 1 if dateA is after dateB, -1 if dateA is before dateB. + */ + compare(dateA: Moment, dateB: Moment): number; + /** + * Parses a date string using the specified format. + * @param value - The date string to parse. + * @param format - The format string to use. + * @param locale - The locale to use for parsing. + * @returns A DateDetail object containing the parsed date and any errors. + */ + parse(value: string, format: string, locale?: string): ParserResult; + /** + * Checks if a Moment.js date object is valid. + * @param date - The Moment.js date object to check. + * @returns True if the date is valid date object, false otherwise. + */ + isValid(date: any): date is Moment; + /** + * Subtracts time from a Moment.js date object. + * @param date - The Moment.js date object to subtract from. + * @param duration - The duration to subtract. + * @returns The resulting Moment.js date object. + */ + subtract(date: Moment, { days, weeks, months, years, hours, minutes, seconds, milliseconds, }: { + days?: number; + weeks?: number; + months?: number; + years?: number; + hours?: number; + minutes?: number; + seconds?: number; + milliseconds?: number; + }): Moment; + /** + * Adds time to a Moment.js date object. + * @param date - The Moment.js date object to add to. + * @param duration - The duration to add. + * @returns The resulting Moment.js date object. + */ + add(date: Moment, { days, weeks, months, years, hours, minutes, seconds, milliseconds, }: { + days?: number; + weeks?: number; + months?: number; + years?: number; + hours?: number; + minutes?: number; + seconds?: number; + milliseconds?: number; + }): Moment; + /** + * Sets specific components of a Moment.js date object. + * @param date - The Moment.js date object to modify. + * @param components - The components to set, the month is a number (1-12). + * @returns The resulting Moment.js date object. + */ + set(date: Moment, { day, month, year, hour, minute, second, millisecond, }: { + day?: number; + month?: number; + year?: number; + hour?: number; + minute?: number; + second?: number; + millisecond?: number; + }): Moment; + /** + * Checks if two Moment.js date objects are the same based on the specified granularity. + * @param dateA - The first Moment.js date object. + * @param dateB - The second Moment.js date object. + * @param granularity - The granularity to compare by ("day", "month", "year"). + * @returns True if the dates are the same, false otherwise. + */ + isSame(dateA: Moment, dateB: Moment, granularity?: "day" | "month" | "year"): boolean; + /** + * Gets the start of a specified time period for a Moment.js date object. + * @param date - The Moment.js date object. + * @param offset - The time period ("day", "week", "month", "year"). + * @param locale - The locale to use. + * @returns The Moment.js date object representing the start of the period. + */ + startOf(date: Moment, offset: "day" | "week" | "month" | "year", locale?: string): Moment; + /** + * Gets the end of a specified time period for a Moment.js date object. + * @param date - The Moment.js date object. + * @param offset - The time period ("day", "week", "month", "year"). + * @param locale - The locale to use. + * @returns The Moment.js date object representing the end of the period. + */ + endOf(date: Moment, offset: "day" | "week" | "month" | "year", locale?: string): Moment; + /** + * Gets the current date with the time set to the start of the day. + * @param locale - The locale to use. + * @returns The current date at the start of the day. + */ + today(locale?: string): Moment; + /** + * Gets the current date and time. + * @param locale - The locale to use. + * @returns The current date and time. + */ + now(locale?: string): Moment; + /** + * Gets the name of the day of the week. + * @param dow - The day of the week as a number (0-6). + * @param format - The format for the day name ("long", "short", "narrow"). + * @param locale - The locale to use. + * @returns The name of the day of the week. + */ + getDayOfWeekName(dow: number, format: "long" | "short" | "narrow", locale?: string): string; + /** + * Gets the day of the week for a Moment.js date object. + * @param date - The Moment.js date object. + * @returns The day of the week as a number (0-6). + */ + getDayOfWeek(date: Moment): number; + /** + * Gets the day of the month for a Moment.js date object. + * @param date - The Moment.js date object. + * @returns The day of the month as a number (1-31). + */ + getDay(date: Moment): number; + /** + * Gets the month for a Moment.js date object. + * @param date - The Moment.js date object. + * @returns The month as a number (1-12). + */ + getMonth(date: Moment): number; + /** + * Gets the year for a Moment.js date object. + * @param date - The Moment.js date object. + * @returns The year as a number. + */ + getYear(date: Moment): number; + /** + * Gets the time components for a Moment.js date object. + * @param date - The Moment.js date object. + * @returns An object containing the hour, minute, second, and millisecond. + */ + getTime(date: Moment): TimeFields; + /** + * Validate date string so it can be parsed + * @param value + */ + isValidDateString(value: string): boolean; + /** + * Clone the date object + * @param date + */ + clone(date: Moment): Moment; +} diff --git a/packages/date-adapters/src/moment/index.ts b/packages/date-adapters/src/moment-adapter/index.ts similarity index 99% rename from packages/date-adapters/src/moment/index.ts rename to packages/date-adapters/src/moment-adapter/index.ts index 9d20ca5d2ae..489cd198232 100644 --- a/packages/date-adapters/src/moment/index.ts +++ b/packages/date-adapters/src/moment-adapter/index.ts @@ -9,7 +9,7 @@ import { type Timezone, } from "../types"; -declare module "../types" { +declare module "@salt-ds/date-adapters" { export interface DateFrameworkTypeMap { moment: Moment; } diff --git a/packages/date-adapters/src/types/DateFrameworkTypeMap.d.ts b/packages/date-adapters/src/types/DateFrameworkTypeMap.d.ts new file mode 100644 index 00000000000..b541fb400a6 --- /dev/null +++ b/packages/date-adapters/src/types/DateFrameworkTypeMap.d.ts @@ -0,0 +1,2 @@ +export interface DateFrameworkTypeMap { +} diff --git a/packages/date-adapters/src/types/DateFrameworkTypeMap.ts b/packages/date-adapters/src/types/DateFrameworkTypeMap.ts new file mode 100644 index 00000000000..8ea63201955 --- /dev/null +++ b/packages/date-adapters/src/types/DateFrameworkTypeMap.ts @@ -0,0 +1,11 @@ +/** + * To add a new adapter, then, add the adapter's date object to `DateFrameworkTypeMap` interface + * + * declare module "./types" { + * interface DateFrameworkTypeMap { + * luxon: DateTime; + * } + * } + */ +// biome-ignore lint/complexity/noBannedTypes: type augmented by configured adapters +export interface DateFrameworkTypeMap {}; diff --git a/packages/date-adapters/src/types/index.d.ts b/packages/date-adapters/src/types/index.d.ts new file mode 100644 index 00000000000..a5a4b102d57 --- /dev/null +++ b/packages/date-adapters/src/types/index.d.ts @@ -0,0 +1,291 @@ +import { DateFrameworkTypeMap } from "./augmentations"; +/** + * To add a new adapter, then, add the adapter's date object to `DateFrameworkTypeMap` interface + * + * declare module "./types" { + * interface DateFrameworkTypeMap { + * luxon: DateTime; + * } + * } + */ +/** + * Represents the date object of a date framework. + * + * If the DateFrameworkTypeMap is empty, it defaults to any; otherwise, it is a union of all types in the map. + */ +export declare type DateFrameworkType = keyof DateFrameworkTypeMap extends never ? any : DateFrameworkTypeMap[keyof DateFrameworkTypeMap]; +/** + * Enum representing possible date detail error types. + */ +export declare enum DateDetailErrorEnum { + /** Error type for unset values */ + UNSET = "unset", + /** Error type for values that are not a date */ + NOT_A_DATE = "not-a-date", + /** Error type for invalid date values */ + INVALID_DATE = "date", + /** Error type for invalid month values */ + INVALID_MONTH = "month", + /** Error type for invalid day values */ + INVALID_DAY = "day", + /** Error type for invalid year values */ + INVALID_YEAR = "year" +} +/** + * Represents an error detail for a date. + */ +export declare type DateDetailError = { + /** The error code */ + type: DateDetailErrorEnum | string; + /** The error message */ + message: string; +}; +/** + * Provides a way to return date errors in a uniform way. + */ +export declare type DateDetail = { + /** The original entered value, if applicable */ + value?: string; + /** The errors found by the parser */ + errors?: DateDetailError[]; +}; +/** + * Represents the time components of a date. + */ +export declare type TimeFields = { + /** The hour component */ + hour: number; + /** The minute component */ + minute: number; + /** The second component */ + second: number; + /** The millisecond component */ + millisecond: number; +}; +/** + * Options for configuring a date adapter. + * + * @template TLocale - The type of the locale. + * @template TInstance - The type of the date library instance. + */ +export declare type AdapterOptions = { + /** + * The locale to be used by the date adapter. + */ + locale?: TLocale; + /** + * The instance of the date library to be used by the adapter. + */ + instance?: TInstance; +}; +/** + * Recommended date formats for formatting date strings. + */ +export declare type RecommendedFormats = "dd" | "dddd" | "D" | "DD" | "DD MMM YYYY" | "DD MMMM YYYY" | "MMMM YYYY" | "YYYY" | string; +/** + * Timezone options for date operations. + */ +export declare type Timezone = "default" | "system" | "UTC" | string; +export declare type ParserResult = { + date: TDate; +} & DateDetail; +/** + * Interface for a date adapter, providing methods for date manipulation and formatting. + * + * @template TDate - The type of the date object used by the adapter. + * @template TLocale - The type of the locale, defaulting to any. + */ +export interface SaltDateAdapter { + /** + * The locale used by the date adapter. + */ + locale?: TLocale; + /** + * The name of the date library. + */ + lib: string; + /** + * Creates a date object from a string or returns an invalid date. + * + * @param value - The date string to parse. + * @param timezone - The timezone to use. + * @param locale - The locale to use for parsing. + * @returns The parsed Date object or an invalid date object. + */ + date(value?: T, timezone?: string, locale?: TLocale): TDate; + /** + * Formats a date object using the specified format string. + * + * @param date - The date object to format. + * @param format - The format string to use. + * @param locale - The locale to use for formatting. + * Returns an empty string when null or undefined date is given. + */ + format(date: TDate | null | undefined, format?: RecommendedFormats, locale?: TLocale): string; + /** + * Compares two date objects. + * + * @param dateA - The first date object. + * @param dateB - The second date object. + * @returns 0 if equal, 1 if dateA is after dateB, -1 if dateA is before dateB. + */ + compare(dateA: TDate, dateB: TDate): number; + /** + * Parses a date string using the specified format. + * + * @param value - The date string to parse. + * @param format - The format string to use. + * @param locale - The locale to use for parsing. + * @returns A ParserResult object containing the parsed date and any errors. + */ + parse(value: string, format: string, locale?: TLocale): ParserResult; + /** + * Checks if a date object is valid. + * + * @param date - The date object to check. + * @returns True if the date is valid, false otherwise. + */ + isValid(date: any): date is TDate; + /** + * Adds time to a date object. + * + * @param date - The date object to add to. + * @param duration - The duration to add. + * @returns The resulting date object. + */ + add(date: TDate, { days, weeks, months, years, hours, minutes, seconds, milliseconds, }: { + days?: number; + weeks?: number; + months?: number; + years?: number; + hours?: number; + minutes?: number; + seconds?: number; + milliseconds?: number; + }): TDate; + /** + * Subtracts time from a date object. + * + * @param date - The date object to subtract from. + * @param duration - The duration to subtract. + * @returns The resulting date object. + */ + subtract(date: TDate, { days, weeks, months, years, hours, minutes, seconds, milliseconds, }: { + days?: number; + weeks?: number; + months?: number; + years?: number; + hours?: number; + minutes?: number; + seconds?: number; + milliseconds?: number; + }): TDate; + /** + * Sets specific components of a date object. + * + * @param date - The date object to modify. + * @param components - The components to set. + * @returns The resulting date object. + */ + set(date: TDate, { day, month, year, hour, minute, second, millisecond, }: { + day?: number; + month?: number; + year?: number; + hour?: number; + minute?: number; + second?: number; + millisecond?: number; + }): TDate; + /** + * Checks if two date objects are the same based on the specified granularity. + * + * @param dateA - The first date object. + * @param dateB - The second date object. + * @param granularity - The granularity to compare by ("day", "month", "year"). + * @returns True if the dates are the same, false otherwise. + */ + isSame(dateA: TDate, dateB: TDate, granularity: "day" | "month" | "year"): boolean; + /** + * Gets the start of a specified time period for a date object. + * + * @param date - The date object. + * @param granularity - The time period ("day", "week", "month", "year"). + * @param locale - The locale to use. + * @returns The date object representing the start of the period. + */ + startOf(date: TDate, granularity: "day" | "week" | "month" | "year", locale?: TLocale): TDate; + /** + * Gets the end of a specified time period for a date object. + * + * @param date - The date object. + * @param granularity - The time period ("day", "week", "month", "year"). + * @param locale - The locale to use. + * @returns The date object representing the end of the period. + */ + endOf(date: TDate, granularity: "day" | "week" | "month" | "year", locale?: TLocale): TDate; + /** + * Gets the current date with the time set to the start of the day. + * + * @param locale - The locale to use. + * @returns The current date at the start of the day. + */ + today(locale?: TLocale): TDate; + /** + * Gets the current date and time. + * + * @param locale - The locale to use. + * @returns The current date and time. + */ + now(locale?: TLocale): TDate; + /** + * Gets the day of the week for a date object. + * + * @param date - The date object. + * @param locale - The locale to use. + * @returns The day of the week as a number (0-6). + */ + getDayOfWeek(date: TDate, locale?: TLocale): number; + /** + * Gets the day of the month for a date object. + * + * @param date - The date object. + * @returns The day of the month as a number (1-31). + */ + getDay(date: TDate): number; + /** + * Gets the month for a date object. + * + * @param date - The date object. + * @returns The month as a number (0-11). + */ + getMonth(date: TDate): number; + /** + * Gets the year for a date object. + * + * @param date - The date object. + * @returns The year as a number. + */ + getYear(date: TDate): number; + /** + * Gets the time components for a date object. + * + * @param date - The date object. + * @returns An object containing the hour, minute, second, and millisecond. + */ + getTime(date: TDate): TimeFields; + /** + * Gets the name of the day of the week. + * + * @param dow - The day of the week as a number (0-6). + * @param format - The format for the day name ("long", "short", "narrow"). + * @param locale - The locale to use. + * @returns The name of the day of the week. + */ + getDayOfWeekName(dow: number, format: "long" | "short" | "narrow", locale?: any): string; + /** + * Clone the date + * + * @param date + */ + clone(date: TDate): TDate; +} diff --git a/packages/date-adapters/src/types.ts b/packages/date-adapters/src/types/index.ts similarity index 99% rename from packages/date-adapters/src/types.ts rename to packages/date-adapters/src/types/index.ts index 857a978bd75..9ddaa6c6771 100644 --- a/packages/date-adapters/src/types.ts +++ b/packages/date-adapters/src/types/index.ts @@ -1,3 +1,4 @@ +import { DateFrameworkTypeMap } from "./DateFrameworkTypeMap"; /** * Represents the date object of a date framework. * diff --git a/packages/date-adapters/tsconfig.json b/packages/date-adapters/tsconfig.json new file mode 100644 index 00000000000..03d45b9c6f8 --- /dev/null +++ b/packages/date-adapters/tsconfig.json @@ -0,0 +1,16 @@ +{ + "compilerOptions": { + "baseUrl": "./src", + "paths": { + "@salt-ds/date-adapters": ["./types/index.ts"], + }, + "outDir": "./dist", + "module": "commonjs", + "target": "es2016", + "moduleResolution": "node", + "strict": true, + "skipLibCheck": true, + "esModuleInterop": true + }, + "include": ["src/**/*"] +} diff --git a/packages/lab/src/__tests__/__e2e__/calendar/Calendar.a11y.cy.tsx b/packages/lab/src/__tests__/__e2e__/calendar/Calendar.a11y.cy.tsx index 1fa9c6d77e9..87dfe4aa1af 100644 --- a/packages/lab/src/__tests__/__e2e__/calendar/Calendar.a11y.cy.tsx +++ b/packages/lab/src/__tests__/__e2e__/calendar/Calendar.a11y.cy.tsx @@ -1,7 +1,7 @@ -import { AdapterDateFns } from "@salt-ds/date-adapters"; -import { AdapterDayjs } from "@salt-ds/date-adapters"; -import { AdapterLuxon } from "@salt-ds/date-adapters"; -import { AdapterMoment } from "@salt-ds/date-adapters"; +import { AdapterDateFns } from "@salt-ds/date-adapters/date-fns"; +import { AdapterDayjs } from "@salt-ds/date-adapters/dayjs"; +import { AdapterLuxon } from "@salt-ds/date-adapters/luxon"; +import { AdapterMoment } from "@salt-ds/date-adapters/moment"; import * as calendarStories from "@stories/calendar/calendar.stories"; import { composeStories } from "@storybook/react"; diff --git a/packages/lab/src/__tests__/__e2e__/calendar/Calendar.cy.tsx b/packages/lab/src/__tests__/__e2e__/calendar/Calendar.cy.tsx index 20532957bea..6d2d11e0553 100644 --- a/packages/lab/src/__tests__/__e2e__/calendar/Calendar.cy.tsx +++ b/packages/lab/src/__tests__/__e2e__/calendar/Calendar.cy.tsx @@ -2,10 +2,10 @@ import type { DateFrameworkType, SaltDateAdapter, } from "@salt-ds/date-adapters"; -import { AdapterDateFns } from "@salt-ds/date-adapters"; -import { AdapterDayjs } from "@salt-ds/date-adapters"; -import { AdapterLuxon } from "@salt-ds/date-adapters"; -import { AdapterMoment } from "@salt-ds/date-adapters"; +import { AdapterDateFns } from "@salt-ds/date-adapters/date-fns"; +import { AdapterDayjs } from "@salt-ds/date-adapters/dayjs"; +import { AdapterLuxon } from "@salt-ds/date-adapters/luxon"; +import { AdapterMoment } from "@salt-ds/date-adapters/moment"; import { Calendar, CalendarGrid, diff --git a/packages/lab/src/__tests__/__e2e__/calendar/Calendar.multiselect.cy.tsx b/packages/lab/src/__tests__/__e2e__/calendar/Calendar.multiselect.cy.tsx index 89560972b2f..8cc9feaf8fd 100644 --- a/packages/lab/src/__tests__/__e2e__/calendar/Calendar.multiselect.cy.tsx +++ b/packages/lab/src/__tests__/__e2e__/calendar/Calendar.multiselect.cy.tsx @@ -2,10 +2,10 @@ import type { DateFrameworkType, SaltDateAdapter, } from "@salt-ds/date-adapters"; -import { AdapterDateFns } from "@salt-ds/date-adapters"; -import { AdapterDayjs } from "@salt-ds/date-adapters"; -import { AdapterLuxon } from "@salt-ds/date-adapters"; -import { AdapterMoment } from "@salt-ds/date-adapters"; +import { AdapterDateFns } from "@salt-ds/date-adapters/date-fns"; +import { AdapterDayjs } from "@salt-ds/date-adapters/dayjs"; +import { AdapterLuxon } from "@salt-ds/date-adapters/luxon"; +import { AdapterMoment } from "@salt-ds/date-adapters/moment"; import { Calendar, CalendarGrid, diff --git a/packages/lab/src/__tests__/__e2e__/calendar/Calendar.offset.cy.tsx b/packages/lab/src/__tests__/__e2e__/calendar/Calendar.offset.cy.tsx index c4439f3d5a5..ef9ad5bd2cf 100644 --- a/packages/lab/src/__tests__/__e2e__/calendar/Calendar.offset.cy.tsx +++ b/packages/lab/src/__tests__/__e2e__/calendar/Calendar.offset.cy.tsx @@ -2,10 +2,10 @@ import type { DateFrameworkType, SaltDateAdapter, } from "@salt-ds/date-adapters"; -import { AdapterDateFns } from "@salt-ds/date-adapters"; -import { AdapterDayjs } from "@salt-ds/date-adapters"; -import { AdapterLuxon } from "@salt-ds/date-adapters"; -import { AdapterMoment } from "@salt-ds/date-adapters"; +import { AdapterDateFns } from "@salt-ds/date-adapters/date-fns"; +import { AdapterDayjs } from "@salt-ds/date-adapters/dayjs"; +import { AdapterLuxon } from "@salt-ds/date-adapters/luxon"; +import { AdapterMoment } from "@salt-ds/date-adapters/moment"; import { Calendar, CalendarGrid, diff --git a/packages/lab/src/__tests__/__e2e__/calendar/Calendar.range.cy.tsx b/packages/lab/src/__tests__/__e2e__/calendar/Calendar.range.cy.tsx index c7b1143b43a..6626853e5e4 100644 --- a/packages/lab/src/__tests__/__e2e__/calendar/Calendar.range.cy.tsx +++ b/packages/lab/src/__tests__/__e2e__/calendar/Calendar.range.cy.tsx @@ -1,11 +1,19 @@ +import { + type DateFrameworkType, + type SaltDateAdapter, +} from "@salt-ds/date-adapters"; import { AdapterDateFns, +} from "@salt-ds/date-adapters/date-fns"; +import { AdapterDayjs, +} from "@salt-ds/date-adapters/dayjs"; +import { AdapterLuxon, +} from "@salt-ds/date-adapters/luxon"; +import { AdapterMoment, - type DateFrameworkType, - type SaltDateAdapter, -} from "@salt-ds/date-adapters"; +} from "@salt-ds/date-adapters/moment"; import { Calendar, CalendarGrid, diff --git a/packages/lab/src/__tests__/__e2e__/calendar/Calendar.single.cy.tsx b/packages/lab/src/__tests__/__e2e__/calendar/Calendar.single.cy.tsx index a6327fec61b..c06773f8af3 100644 --- a/packages/lab/src/__tests__/__e2e__/calendar/Calendar.single.cy.tsx +++ b/packages/lab/src/__tests__/__e2e__/calendar/Calendar.single.cy.tsx @@ -2,10 +2,10 @@ import type { DateFrameworkType, SaltDateAdapter, } from "@salt-ds/date-adapters"; -import { AdapterDateFns } from "@salt-ds/date-adapters"; -import { AdapterDayjs } from "@salt-ds/date-adapters"; -import { AdapterLuxon } from "@salt-ds/date-adapters"; -import { AdapterMoment } from "@salt-ds/date-adapters"; +import { AdapterDateFns } from "@salt-ds/date-adapters/date-fns"; +import { AdapterDayjs } from "@salt-ds/date-adapters/dayjs"; +import { AdapterLuxon } from "@salt-ds/date-adapters/luxon"; +import { AdapterMoment } from "@salt-ds/date-adapters/moment"; import { Calendar, CalendarGrid, diff --git a/packages/lab/src/__tests__/__e2e__/date-input/DateInput.cy.tsx b/packages/lab/src/__tests__/__e2e__/date-input/DateInput.cy.tsx index 6fa1bc3a234..33856596a0a 100644 --- a/packages/lab/src/__tests__/__e2e__/date-input/DateInput.cy.tsx +++ b/packages/lab/src/__tests__/__e2e__/date-input/DateInput.cy.tsx @@ -1,7 +1,7 @@ -import { AdapterDateFns } from "@salt-ds/date-adapters"; -import { AdapterDayjs } from "@salt-ds/date-adapters"; -import { AdapterLuxon } from "@salt-ds/date-adapters"; -import { AdapterMoment } from "@salt-ds/date-adapters"; +import { AdapterDateFns } from "@salt-ds/date-adapters/date-fns"; +import { AdapterDayjs } from "@salt-ds/date-adapters/dayjs"; +import { AdapterLuxon } from "@salt-ds/date-adapters/luxon"; +import { AdapterMoment } from "@salt-ds/date-adapters/moment"; import * as dateInputStories from "@stories/date-input/date-input.stories"; import { composeStories } from "@storybook/react"; import { checkAccessibility } from "../../../../../../cypress/tests/checkAccessibility"; diff --git a/packages/lab/src/__tests__/__e2e__/date-input/DateInputRange.cy.tsx b/packages/lab/src/__tests__/__e2e__/date-input/DateInputRange.cy.tsx index d4634c8301c..cab0d0bba47 100644 --- a/packages/lab/src/__tests__/__e2e__/date-input/DateInputRange.cy.tsx +++ b/packages/lab/src/__tests__/__e2e__/date-input/DateInputRange.cy.tsx @@ -1,7 +1,7 @@ -import { AdapterDateFns } from "@salt-ds/date-adapters"; -import { AdapterDayjs } from "@salt-ds/date-adapters"; -import { AdapterLuxon } from "@salt-ds/date-adapters"; -import { AdapterMoment } from "@salt-ds/date-adapters"; +import { AdapterDateFns } from "@salt-ds/date-adapters/date-fns"; +import { AdapterDayjs } from "@salt-ds/date-adapters/dayjs"; +import { AdapterLuxon } from "@salt-ds/date-adapters/luxon"; +import { AdapterMoment } from "@salt-ds/date-adapters/moment"; import { DateDetailErrorEnum, type DateFrameworkType, diff --git a/packages/lab/src/__tests__/__e2e__/date-input/DateInputSingle.cy.tsx b/packages/lab/src/__tests__/__e2e__/date-input/DateInputSingle.cy.tsx index 7b723f82129..b0d6e78e8f1 100644 --- a/packages/lab/src/__tests__/__e2e__/date-input/DateInputSingle.cy.tsx +++ b/packages/lab/src/__tests__/__e2e__/date-input/DateInputSingle.cy.tsx @@ -4,10 +4,10 @@ import { type ParserResult, type SaltDateAdapter, } from "@salt-ds/date-adapters"; -import { AdapterDateFns } from "@salt-ds/date-adapters"; -import { AdapterDayjs } from "@salt-ds/date-adapters"; -import { AdapterLuxon } from "@salt-ds/date-adapters"; -import { AdapterMoment } from "@salt-ds/date-adapters"; +import { AdapterDateFns } from "@salt-ds/date-adapters/date-fns"; +import { AdapterDayjs } from "@salt-ds/date-adapters/dayjs"; +import { AdapterLuxon } from "@salt-ds/date-adapters/luxon"; +import { AdapterMoment } from "@salt-ds/date-adapters/moment"; import { DateInputSingle } from "@salt-ds/lab"; import { es as dateFnsEs } from "date-fns/locale"; diff --git a/packages/lab/src/__tests__/__e2e__/date-picker/DatePicker.cy.tsx b/packages/lab/src/__tests__/__e2e__/date-picker/DatePicker.cy.tsx index ec1d88c6b68..fc4b15aeafc 100644 --- a/packages/lab/src/__tests__/__e2e__/date-picker/DatePicker.cy.tsx +++ b/packages/lab/src/__tests__/__e2e__/date-picker/DatePicker.cy.tsx @@ -1,7 +1,7 @@ -import { AdapterDateFns } from "@salt-ds/date-adapters"; -import { AdapterDayjs } from "@salt-ds/date-adapters"; -import { AdapterLuxon } from "@salt-ds/date-adapters"; -import { AdapterMoment } from "@salt-ds/date-adapters"; +import { AdapterDateFns } from "@salt-ds/date-adapters/date-fns"; +import { AdapterDayjs } from "@salt-ds/date-adapters/dayjs"; +import { AdapterLuxon } from "@salt-ds/date-adapters/luxon"; +import { AdapterMoment } from "@salt-ds/date-adapters/moment"; import * as datePickerStories from "@stories/date-picker/date-picker.stories"; import { composeStories } from "@storybook/react"; import { checkAccessibility } from "../../../../../../cypress/tests/checkAccessibility"; diff --git a/packages/lab/src/__tests__/__e2e__/date-picker/DatePicker.range.cy.tsx b/packages/lab/src/__tests__/__e2e__/date-picker/DatePicker.range.cy.tsx index 86d2e0c0d0a..2acb3491667 100644 --- a/packages/lab/src/__tests__/__e2e__/date-picker/DatePicker.range.cy.tsx +++ b/packages/lab/src/__tests__/__e2e__/date-picker/DatePicker.range.cy.tsx @@ -3,10 +3,10 @@ import { type DateFrameworkType, type SaltDateAdapter, } from "@salt-ds/date-adapters"; -import { AdapterDateFns } from "@salt-ds/date-adapters"; -import { AdapterDayjs } from "@salt-ds/date-adapters"; -import { AdapterLuxon } from "@salt-ds/date-adapters"; -import { AdapterMoment } from "@salt-ds/date-adapters"; +import { AdapterDateFns } from "@salt-ds/date-adapters/date-fns"; +import { AdapterDayjs } from "@salt-ds/date-adapters/dayjs"; +import { AdapterLuxon } from "@salt-ds/date-adapters/luxon"; +import { AdapterMoment } from "@salt-ds/date-adapters/moment"; import { DatePicker, DatePickerOverlay, diff --git a/packages/lab/src/__tests__/__e2e__/date-picker/DatePicker.single.cy.tsx b/packages/lab/src/__tests__/__e2e__/date-picker/DatePicker.single.cy.tsx index 7759c061a3a..d3ce2f46f3e 100644 --- a/packages/lab/src/__tests__/__e2e__/date-picker/DatePicker.single.cy.tsx +++ b/packages/lab/src/__tests__/__e2e__/date-picker/DatePicker.single.cy.tsx @@ -3,10 +3,10 @@ import { type DateFrameworkType, type SaltDateAdapter, } from "@salt-ds/date-adapters"; -import { AdapterDateFns } from "@salt-ds/date-adapters"; -import { AdapterDayjs } from "@salt-ds/date-adapters"; -import { AdapterLuxon } from "@salt-ds/date-adapters"; -import { AdapterMoment } from "@salt-ds/date-adapters"; +import { AdapterDateFns } from "@salt-ds/date-adapters/date-fns"; +import { AdapterDayjs } from "@salt-ds/date-adapters/dayjs"; +import { AdapterLuxon } from "@salt-ds/date-adapters/luxon"; +import { AdapterMoment } from "@salt-ds/date-adapters/moment"; import { DatePicker, DatePickerOverlay, diff --git a/packages/lab/src/calendar/useCalendarSelection.ts b/packages/lab/src/calendar/useCalendarSelection.ts index 4d776731258..83eadff1d06 100644 --- a/packages/lab/src/calendar/useCalendarSelection.ts +++ b/packages/lab/src/calendar/useCalendarSelection.ts @@ -400,7 +400,7 @@ export function useCalendarSelection( case "single": return ( isSingleSelectionValueType(selectedDate) && - dateAdapter.isSame(selectedDate, date, "day") + dateAdapter.isSame(selectedDate as TDate, date, "day") ); case "multiselect": return ( diff --git a/scripts/makeTypings.mjs b/scripts/makeTypings.mjs index 240329f1c0a..09cf242c2cf 100644 --- a/scripts/makeTypings.mjs +++ b/scripts/makeTypings.mjs @@ -24,8 +24,8 @@ export function reportTSDiagnostics(diagnostics) { } } -export async function makeTypings(outDir) { - const typescriptConfig = await getTypescriptConfig(cwd); +export async function makeTypings(outDir, srcDir = path.join(cwd, "src")) { + const typescriptConfig = await getTypescriptConfig(cwd, srcDir); console.log("generating .d.ts files"); @@ -37,6 +37,7 @@ export async function makeTypings(outDir) { }, }; + // then add our custom stuff // Only include src files from the package to prevent already built // files from interferring with the compile diff --git a/scripts/utils.mjs b/scripts/utils.mjs index 68d03c3fb8b..2fc27fce73e 100644 --- a/scripts/utils.mjs +++ b/scripts/utils.mjs @@ -5,13 +5,13 @@ export function distinct(arr) { return [...new Set(arr)]; } -export async function getTypescriptConfig(cwd) { +export async function getTypescriptConfig(cwd, srcDir) { const typescriptConfig = {}; const result = getTsconfig(cwd); Object.assign(typescriptConfig, result.config, { - include: [path.join(cwd, "src")], + include: [srcDir], exclude: distinct( [ // all TS test files, regardless whether co-located or in test/ etc diff --git a/site/src/components/components/LivePreview.tsx b/site/src/components/components/LivePreview.tsx index 1e4520ccef1..d1c08db6e79 100644 --- a/site/src/components/components/LivePreview.tsx +++ b/site/src/components/components/LivePreview.tsx @@ -14,7 +14,7 @@ import useIsMobileView from "../../utils/useIsMobileView"; import { Pre } from "../mdx/pre"; import { useLivePreviewControls } from "./useLivePreviewControls"; -import { AdapterDateFns } from "@salt-ds/date-adapters"; +import { AdapterDateFns } from "@salt-ds/date-adapters/date-fns"; import { LocalizationProvider } from "@salt-ds/lab"; import styles from "./LivePreview.module.css"; diff --git a/site/src/examples/calendar/WithLocale.tsx b/site/src/examples/calendar/WithLocale.tsx index c10bd40a986..81030819829 100644 --- a/site/src/examples/calendar/WithLocale.tsx +++ b/site/src/examples/calendar/WithLocale.tsx @@ -1,4 +1,4 @@ -import { AdapterDateFns } from "@salt-ds/date-adapters"; +import { AdapterDateFns } from "@salt-ds/date-adapters/date-fns"; import { Calendar, CalendarGrid, diff --git a/site/src/examples/localization-provider/Locale.tsx b/site/src/examples/localization-provider/Locale.tsx index f173748c36e..b4d3983ea82 100644 --- a/site/src/examples/localization-provider/Locale.tsx +++ b/site/src/examples/localization-provider/Locale.tsx @@ -13,10 +13,10 @@ import { } from "@salt-ds/lab"; import { type ChangeEventHandler, type ReactElement, useState } from "react"; import "dayjs/locale/en"; -import { AdapterDateFns } from "@salt-ds/date-adapters"; -import { AdapterDayjs } from "@salt-ds/date-adapters"; -import { AdapterLuxon } from "@salt-ds/date-adapters"; -import { AdapterMoment } from "@salt-ds/date-adapters"; +import { AdapterDateFns } from "@salt-ds/date-adapters/date-fns"; +import { AdapterDayjs } from "@salt-ds/date-adapters/dayjs"; +import { AdapterLuxon } from "@salt-ds/date-adapters/luxon"; +import { AdapterMoment } from "@salt-ds/date-adapters/moment"; import moment from "moment"; import "moment/locale/zh-cn"; // Import the Chinese locale import { enUS as dateFnsEnUs } from "date-fns/locale"; diff --git a/site/src/examples/localization-provider/MinMax.tsx b/site/src/examples/localization-provider/MinMax.tsx index 1751da85710..f121dee1367 100644 --- a/site/src/examples/localization-provider/MinMax.tsx +++ b/site/src/examples/localization-provider/MinMax.tsx @@ -1,5 +1,6 @@ import { FormField, FormFieldHelperText, FormFieldLabel } from "@salt-ds/core"; -import { AdapterDateFns, type DateFrameworkType } from "@salt-ds/date-adapters"; +import { AdapterDateFns } from "@salt-ds/date-adapters/date-fns"; +import { type DateFrameworkType } from "@salt-ds/date-adapters"; import { Calendar, CalendarGrid, diff --git a/site/src/pages/_app.tsx b/site/src/pages/_app.tsx index 9b9dbd66d75..0ae007c77eb 100755 --- a/site/src/pages/_app.tsx +++ b/site/src/pages/_app.tsx @@ -25,7 +25,7 @@ import { SaltProviderNext, useCurrentBreakpoint, } from "@salt-ds/core"; -import { AdapterDateFns } from "@salt-ds/date-adapters"; +import { AdapterDateFns } from "@salt-ds/date-adapters/date-fns"; import { LocalizationProvider } from "@salt-ds/lab"; import clsx from "clsx"; import { SessionProvider } from "next-auth/react"; diff --git a/tsconfig.json b/tsconfig.json index 3b48ae806c5..885c372b3bd 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -20,6 +20,21 @@ "jsx": "react-jsx", "baseUrl": ".", "paths": { + "@salt-ds/date-adapters/date-fns": [ + "packages/date-adapters/src/date-fns-adapter/index.ts", + ], + "@salt-ds/date-adapters/dayjs": [ + "packages/date-adapters/src/dayjs-adapter/index.ts", + ], + "@salt-ds/date-adapters/luxon": [ + "packages/date-adapters/src/luxon-adapter/index.ts", + ], + "@salt-ds/date-adapters/moment": [ + "packages/date-adapters/src/moment-adapter/index.ts", + ], + "@salt-ds/date-adapters": [ + "packages/date-adapters/src/types", + ], "@stories/*": [ "packages/ag-grid-theme/stories/*", "packages/core/stories/*", diff --git a/vite.config.ts b/vite.config.ts index f25730da82c..ef0bfef07b0 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -1,6 +1,8 @@ import { defineConfig } from "vitest/config"; +import tsconfigPaths from 'vite-tsconfig-paths'; export default defineConfig({ + plugins: [tsconfigPaths() as any], test: { include: ["**/*.spec.[jt]s?(x)"], },