UNPKG

@clerk/shared

Version:

Internal package utils used by the Clerk SDKs

1 lines 3.69 kB
{"version":3,"file":"date.mjs","names":[],"sources":["../src/date.ts"],"sourcesContent":["const MILLISECONDS_IN_DAY = 86400000;\n\n/**\n *\n */\nexport function dateTo12HourTime(date: Date): string {\n if (!date) {\n return '';\n }\n return date.toLocaleString('en-US', {\n hour: '2-digit',\n minute: 'numeric',\n hour12: true,\n });\n}\n\n/**\n *\n */\nexport function differenceInCalendarDays(a: Date, b: Date, { absolute = true } = {}): number {\n if (!a || !b) {\n return 0;\n }\n const utcA = Date.UTC(a.getFullYear(), a.getMonth(), a.getDate());\n const utcB = Date.UTC(b.getFullYear(), b.getMonth(), b.getDate());\n const diff = Math.floor((utcB - utcA) / MILLISECONDS_IN_DAY);\n return absolute ? Math.abs(diff) : diff;\n}\n\n/**\n *\n */\nexport function normalizeDate(d: Date | string | number): Date {\n try {\n return new Date(d || new Date());\n } catch {\n return new Date();\n }\n}\n\ntype DateFormatRelativeParams = {\n date: Date | string | number;\n relativeTo: Date | string | number;\n};\n\nexport type RelativeDateCase = 'previous6Days' | 'lastDay' | 'sameDay' | 'nextDay' | 'next6Days' | 'other';\ntype RelativeDateReturn = { relativeDateCase: RelativeDateCase; date: Date } | null;\n\n/**\n *\n */\nexport function formatRelative(props: DateFormatRelativeParams): RelativeDateReturn {\n const { date, relativeTo } = props;\n if (!date || !relativeTo) {\n return null;\n }\n const a = normalizeDate(date);\n const b = normalizeDate(relativeTo);\n const differenceInDays = differenceInCalendarDays(b, a, { absolute: false });\n\n if (differenceInDays < -6) {\n return { relativeDateCase: 'other', date: a };\n }\n if (differenceInDays < -1) {\n return { relativeDateCase: 'previous6Days', date: a };\n }\n if (differenceInDays === -1) {\n return { relativeDateCase: 'lastDay', date: a };\n }\n if (differenceInDays === 0) {\n return { relativeDateCase: 'sameDay', date: a };\n }\n if (differenceInDays === 1) {\n return { relativeDateCase: 'nextDay', date: a };\n }\n if (differenceInDays < 7) {\n return { relativeDateCase: 'next6Days', date: a };\n }\n return { relativeDateCase: 'other', date: a };\n}\n\n/**\n *\n */\nexport function addYears(initialDate: Date | number | string, yearsToAdd: number): Date {\n const date = normalizeDate(initialDate);\n date.setFullYear(date.getFullYear() + yearsToAdd);\n return date;\n}\n"],"mappings":";AAAA,MAAM,sBAAsB;;;;AAK5B,SAAgB,iBAAiB,MAAoB;CACnD,IAAI,CAAC,MACH,OAAO;CAET,OAAO,KAAK,eAAe,SAAS;EAClC,MAAM;EACN,QAAQ;EACR,QAAQ;CACV,CAAC;AACH;;;;AAKA,SAAgB,yBAAyB,GAAS,GAAS,EAAE,WAAW,SAAS,CAAC,GAAW;CAC3F,IAAI,CAAC,KAAK,CAAC,GACT,OAAO;CAET,MAAM,OAAO,KAAK,IAAI,EAAE,YAAY,GAAG,EAAE,SAAS,GAAG,EAAE,QAAQ,CAAC;CAChE,MAAM,OAAO,KAAK,IAAI,EAAE,YAAY,GAAG,EAAE,SAAS,GAAG,EAAE,QAAQ,CAAC;CAChE,MAAM,OAAO,KAAK,OAAO,OAAO,QAAQ,mBAAmB;CAC3D,OAAO,WAAW,KAAK,IAAI,IAAI,IAAI;AACrC;;;;AAKA,SAAgB,cAAc,GAAiC;CAC7D,IAAI;EACF,OAAO,IAAI,KAAK,qBAAK,IAAI,KAAK,CAAC;CACjC,QAAQ;EACN,uBAAO,IAAI,KAAK;CAClB;AACF;;;;AAaA,SAAgB,eAAe,OAAqD;CAClF,MAAM,EAAE,MAAM,eAAe;CAC7B,IAAI,CAAC,QAAQ,CAAC,YACZ,OAAO;CAET,MAAM,IAAI,cAAc,IAAI;CAE5B,MAAM,mBAAmB,yBADf,cAAc,UAC0B,GAAG,GAAG,EAAE,UAAU,MAAM,CAAC;CAE3E,IAAI,mBAAmB,IACrB,OAAO;EAAE,kBAAkB;EAAS,MAAM;CAAE;CAE9C,IAAI,mBAAmB,IACrB,OAAO;EAAE,kBAAkB;EAAiB,MAAM;CAAE;CAEtD,IAAI,qBAAqB,IACvB,OA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