@ma.vu/appdate 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +38 -1
- package/dist/index.d.ts +38 -1
- package/dist/index.js +1 -1
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
"use client";
|
|
2
|
-
"use strict";var Y=Object.create;var
|
|
2
|
+
"use strict";var Y=Object.create;var c=Object.defineProperty;var k=Object.getOwnPropertyDescriptor;var b=Object.getOwnPropertyNames;var w=Object.getPrototypeOf,_=Object.prototype.hasOwnProperty;var A=(a,t)=>()=>(a&&(t=a(a=0)),t);var l=(a,t)=>{for(var e in t)c(a,e,{get:t[e],enumerable:!0})},u=(a,t,e,i)=>{if(t&&typeof t=="object"||typeof t=="function")for(let o of b(t))!_.call(a,o)&&o!==e&&c(a,o,{get:()=>t[o],enumerable:!(i=k(t,o))||i.enumerable});return a};var s=(a,t,e)=>(e=a!=null?Y(w(a)):{},u(t||!a||!a.__esModule?c(e,"default",{value:a,enumerable:!0}):e,a)),O=a=>u(c({},"__esModule",{value:!0}),a);var D={};l(D,{default:()=>v});var x,v,p=A(()=>{"use strict";x={name:"sr-ije",weekdays:"Nedjelja_Ponedjeljak_Utorak_Srijeda_\u010Cetvrtak_Petak_Subota".split("_"),weekdaysShort:"Ned._Pon._Uto._Sri._\u010Cet._Pet._Sub.".split("_"),weekdaysMin:"ne_po_ut_sr_\u010De_pe_su".split("_"),months:"Januar_Februar_Mart_April_Maj_Jun_Jul_Avgust_Septembar_Oktobar_Novembar_Decembar".split("_"),monthsShort:"Jan._Feb._Mar._Apr._Maj_Jun_Jul_Avg._Sep._Okt._Nov._Dec.".split("_"),weekStart:1,ordinal:a=>`${a}.`,formats:{LT:"H:mm",LTS:"H:mm:ss",L:"DD.MM.YYYY",LL:"D. MMMM YYYY.",LLL:"D. MMMM YYYY. H:mm",LLLL:"dddd, D. MMMM YYYY. H:mm"},relativeTime:{future:"za %s",past:"prije %s",s:"nekoliko sekundi",m:"jedan minut",mm:"%d minuta",h:"jedan sat",hh:"%d sati",d:"jedan dan",dd:"%d dana",M:"jedan mjesec",MM:"%d mjeseci",y:"jednu godinu",yy:"%d godina"}},v=x});var P={};l(P,{AppDate:()=>d,formatLocalTime:()=>I,getLocalizedDateString:()=>H,isDateString:()=>T,setAppDateLanguage:()=>z,setTimezone:()=>F});module.exports=O(P);var r=s(require("dayjs"),1),N=require("dayjs/locale/de"),g=s(require("dayjs/plugin/customParseFormat.js"),1),j=s(require("dayjs/plugin/isBetween.js"),1),h=s(require("dayjs/plugin/localizedFormat.js"),1),M=s(require("dayjs/plugin/relativeTime.js"),1),L=s(require("dayjs/plugin/timezone.js"),1),S=s(require("dayjs/plugin/utc.js"),1);r.default.extend(S.default);r.default.extend(L.default);r.default.extend(g.default);r.default.extend(j.default);r.default.extend(h.default);r.default.extend(M.default);async function z(a){switch(a){case"de":{let t=await import("dayjs/locale/de-ch");r.default.locale(t.default);break}case"fr":{let t=await import("dayjs/locale/fr-ch");r.default.locale(t.default);break}case"sr":{let t=await import("dayjs/locale/sr");r.default.locale(t.default);break}case"sr-ije":{let t=await Promise.resolve().then(()=>(p(),D));r.default.locale(t.default,void 0,!0),r.default.locale("sr-ije");break}case"en":default:{let t=await import("dayjs/locale/en");r.default.locale(t.default);break}}}var n="Europe/Zurich";function F(a){n=a}var f="HH:mm",y="HH:mm:ssZ",d=class a{timezone;dayjsDate;static INVALID_DATE=(0,r.default)("");constructor(t,e,{invalid:i}={invalid:!1}){if(this.timezone=t,i){this.dayjsDate=a.INVALID_DATE;return}try{if(typeof e=="string"&&!T(e))throw new Error("Invalid Date string, we expect YYYY-DD-MM");this.dayjsDate=r.default.tz(e,t)}catch{console.warn("Could not parse date:",e),this.dayjsDate=a.INVALID_DATE}}static invalid(){return new a(n,"",{invalid:!0})}static now(){return new a(n,(0,r.default)())}static fromDateString(t){return new a(n,t)}static fromEpochSeconds(t){let e=r.default.unix(t);return new a(n,e)}static fromEpochMillis(t){let e=(0,r.default)(t);return new a(n,e)}static fromLocalTime(t){try{let e=r.default.tz(t,f,n);return new a(n,e)}catch(e){return e instanceof Error&&console.warn(`fromLocalTime(): ${e.message}`),a.invalid()}}static fromUtcString(t){let e=r.default.utc(t);return new a(n,e)}static fromUtcTime(t){let e=r.default.utc(t,y);return new a(n,e)}static minDate(){return a.fromDateString("1900-01-01")}static maxDate(){return a.fromDateString("2200-12-31")}add(t,e){let i=this.dayjsDate.add(t,e);return new a(n,i)}subtract(t,e){let i=this.dayjsDate.subtract(t,e);return new a(n,i)}startOf(t){let e=this.dayjsDate.startOf(t);return new a(n,e)}endOf(t){let e=this.dayjsDate.endOf(t);return new a(n,e)}tomorrow(){return this.add(1,"day")}isValid(){return this.dayjsDate.isValid()}isBefore(t,e){return this.dayjsDate.isBefore(t.dayjsDate,e)}isSame(t,e){return this.dayjsDate.isSame(t.dayjsDate,e)}isToday(){return this.dayjsDate.endOf("d").isSame((0,r.default)().endOf("d"))}isAfter(t,e){return this.dayjsDate.isAfter(t.dayjsDate,e)}isBetween(t=a.minDate(),e=a.maxDate(),i,o){return this.dayjsDate.isBetween(t.dayjsDate,e.dayjsDate,i,o??"[)")}isFirstDayOfWeek(){return this.dayjsDate.day()===1}isWorkingDay(){return U.includes(this.dayjsDate.day())}nextWorkingDay(){let t=this.add(1,"day");return t.isWorkingDay()?t:t.nextWorkingDay()}previousWorkingDay(){let t=this.subtract(1,"day");return t.isWorkingDay()?t:t.previousWorkingDay()}addWorkingDays(t){return t<=0||!Number.isInteger(t)?this:this.nextWorkingDay().addWorkingDays(t-1)}toLocalTime(){return this.format(f)}toUtcTime(){return this.dayjsDate.utc().format(y)}toDateString(){return this.format("YYYY-MM-DD")}toLocalizedDateString({includeDayOfWeek:t=!1}={}){let e=this.dayjsDate.format("L");return t?this.dayjsDate.format("dd, ")+e:e}toUtcDateString(){return this.dayjsDate.utc().format("YYYY-MM-DD")}toUtcString(){return this.dayjsDate.utc().format()}toEpochSeconds(){return this.dayjsDate.unix()}toEpochMillis(){return this.dayjsDate.valueOf()}format(t="YYYY-MM-DDTHH:mm:ssZ[Z]"){return this.dayjsDate.format(t)}formatShort({includeDayOfWeek:t=!0}={}){return t?this.format("dd, DD.MM."):this.format("DD.MM.")}formatDateTime({includeDayOfWeek:t=!0}={}){let e=this.toLocalizedDateString({includeDayOfWeek:t}),i=this.toLocalTime();return`${e}, ${i}`}toRelative(t){let e=(0,r.default)(),i=Math.abs(this.dayjsDate.diff(e,"day"));return t?.fallbackAfterDays&&i>=t.fallbackAfterDays?(t.fallback??(m=>m.toLocalizedDateString()))(this):t?.cap&&i>=t.cap?(this.dayjsDate.isBefore(e)?e.subtract(t.cap,"day"):e.add(t.cap,"day")).fromNow().replace(String(t.cap),`${t.cap}+`):this.dayjsDate.fromNow()}},U=[1,2,3,4,5];function H(a,t){return d.fromDateString(a).toLocalizedDateString(t)}function I(a){return d.fromLocalTime(a).toLocalTime()}function T(a){return a?(0,r.default)(a,"YYYY-MM-DD",!0).isValid():!1}0&&(module.exports={AppDate,formatLocalTime,getLocalizedDateString,isDateString,setAppDateLanguage,setTimezone});
|
package/dist/index.d.cts
CHANGED
|
@@ -248,6 +248,43 @@ declare class AppDate {
|
|
|
248
248
|
*/
|
|
249
249
|
formatShort({ includeDayOfWeek }?: LocalizedFormatOptions): string;
|
|
250
250
|
formatDateTime({ includeDayOfWeek }?: LocalizedFormatOptions): string;
|
|
251
|
+
/**
|
|
252
|
+
* Returns a localized string representing the relative time from now.
|
|
253
|
+
* Automatically handles past and future dates.
|
|
254
|
+
*
|
|
255
|
+
* @param options - Optional settings for capping and fallback behavior
|
|
256
|
+
* @param options.cap - Cap the relative time at this many days (e.g., 9 shows "9+ days ago")
|
|
257
|
+
* @param options.fallbackAfterDays - After this many days, use the fallback formatter
|
|
258
|
+
* @param options.fallback - Custom formatter function when fallbackAfterDays is exceeded
|
|
259
|
+
*
|
|
260
|
+
* @returns A localized string like "2 days ago" or "in 3 hours"
|
|
261
|
+
*
|
|
262
|
+
* @example
|
|
263
|
+
* ```typescript
|
|
264
|
+
* await setAppDateLanguage('sr');
|
|
265
|
+
* AppDate.now().subtract(2, 'day').toRelative(); // "pre 2 dana"
|
|
266
|
+
* AppDate.now().add(3, 'hour').toRelative(); // "za 3 sata"
|
|
267
|
+
*
|
|
268
|
+
* // With cap at 9 days
|
|
269
|
+
* AppDate.now().subtract(15, 'day').toRelative({ cap: 9 }); // "pre 9+ dana"
|
|
270
|
+
*
|
|
271
|
+
* // With fallback to date after 14 days
|
|
272
|
+
* AppDate.now().subtract(20, 'day').toRelative({
|
|
273
|
+
* cap: 9,
|
|
274
|
+
* fallbackAfterDays: 14,
|
|
275
|
+
* fallback: (d) => d.toLocalizedDateString()
|
|
276
|
+
* }); // "20.12.2025"
|
|
277
|
+
* ```
|
|
278
|
+
*/
|
|
279
|
+
toRelative(options?: RelativeTimeOptions): string;
|
|
280
|
+
}
|
|
281
|
+
interface RelativeTimeOptions {
|
|
282
|
+
/** Cap the relative time at this many days. Shows "X+" format after this threshold. */
|
|
283
|
+
cap?: number;
|
|
284
|
+
/** After this many days, use the fallback formatter instead of relative time. */
|
|
285
|
+
fallbackAfterDays?: number;
|
|
286
|
+
/** Custom formatter function called when fallbackAfterDays is exceeded. Defaults to toLocalizedDateString(). */
|
|
287
|
+
fallback?: (date: AppDate) => string;
|
|
251
288
|
}
|
|
252
289
|
interface LocalizedFormatOptions {
|
|
253
290
|
includeDayOfWeek?: boolean;
|
|
@@ -265,4 +302,4 @@ declare function formatLocalTime(time: string): string;
|
|
|
265
302
|
declare function isDateString(date: string | undefined | null | Dayjs): date is DateString;
|
|
266
303
|
type FormatTemplate = "YY" | "YYYY" | "M" | "MM" | "MMM" | "MMMM" | "D" | "DD" | "d" | "dd" | "ddd" | "dddd" | "H" | "HH" | "h" | "hh" | "m" | "mm" | "s" | "ss" | "SSS" | "Z" | "ZZ" | "A" | "a" | "LT" | "LTS" | /* en: 10/10/2020, de: 10.10.2020 */ "L" | "LL" | "LLL" | "LLLL" | "l" | "ll" | "lll" | "llll" | (string & {});
|
|
267
304
|
|
|
268
|
-
export { AppDate, type GetNDaysOptions, type LocalizedFormatOptions, formatLocalTime, getLocalizedDateString, isDateString, setAppDateLanguage, setTimezone };
|
|
305
|
+
export { AppDate, type GetNDaysOptions, type LocalizedFormatOptions, type RelativeTimeOptions, formatLocalTime, getLocalizedDateString, isDateString, setAppDateLanguage, setTimezone };
|
package/dist/index.d.ts
CHANGED
|
@@ -248,6 +248,43 @@ declare class AppDate {
|
|
|
248
248
|
*/
|
|
249
249
|
formatShort({ includeDayOfWeek }?: LocalizedFormatOptions): string;
|
|
250
250
|
formatDateTime({ includeDayOfWeek }?: LocalizedFormatOptions): string;
|
|
251
|
+
/**
|
|
252
|
+
* Returns a localized string representing the relative time from now.
|
|
253
|
+
* Automatically handles past and future dates.
|
|
254
|
+
*
|
|
255
|
+
* @param options - Optional settings for capping and fallback behavior
|
|
256
|
+
* @param options.cap - Cap the relative time at this many days (e.g., 9 shows "9+ days ago")
|
|
257
|
+
* @param options.fallbackAfterDays - After this many days, use the fallback formatter
|
|
258
|
+
* @param options.fallback - Custom formatter function when fallbackAfterDays is exceeded
|
|
259
|
+
*
|
|
260
|
+
* @returns A localized string like "2 days ago" or "in 3 hours"
|
|
261
|
+
*
|
|
262
|
+
* @example
|
|
263
|
+
* ```typescript
|
|
264
|
+
* await setAppDateLanguage('sr');
|
|
265
|
+
* AppDate.now().subtract(2, 'day').toRelative(); // "pre 2 dana"
|
|
266
|
+
* AppDate.now().add(3, 'hour').toRelative(); // "za 3 sata"
|
|
267
|
+
*
|
|
268
|
+
* // With cap at 9 days
|
|
269
|
+
* AppDate.now().subtract(15, 'day').toRelative({ cap: 9 }); // "pre 9+ dana"
|
|
270
|
+
*
|
|
271
|
+
* // With fallback to date after 14 days
|
|
272
|
+
* AppDate.now().subtract(20, 'day').toRelative({
|
|
273
|
+
* cap: 9,
|
|
274
|
+
* fallbackAfterDays: 14,
|
|
275
|
+
* fallback: (d) => d.toLocalizedDateString()
|
|
276
|
+
* }); // "20.12.2025"
|
|
277
|
+
* ```
|
|
278
|
+
*/
|
|
279
|
+
toRelative(options?: RelativeTimeOptions): string;
|
|
280
|
+
}
|
|
281
|
+
interface RelativeTimeOptions {
|
|
282
|
+
/** Cap the relative time at this many days. Shows "X+" format after this threshold. */
|
|
283
|
+
cap?: number;
|
|
284
|
+
/** After this many days, use the fallback formatter instead of relative time. */
|
|
285
|
+
fallbackAfterDays?: number;
|
|
286
|
+
/** Custom formatter function called when fallbackAfterDays is exceeded. Defaults to toLocalizedDateString(). */
|
|
287
|
+
fallback?: (date: AppDate) => string;
|
|
251
288
|
}
|
|
252
289
|
interface LocalizedFormatOptions {
|
|
253
290
|
includeDayOfWeek?: boolean;
|
|
@@ -265,4 +302,4 @@ declare function formatLocalTime(time: string): string;
|
|
|
265
302
|
declare function isDateString(date: string | undefined | null | Dayjs): date is DateString;
|
|
266
303
|
type FormatTemplate = "YY" | "YYYY" | "M" | "MM" | "MMM" | "MMMM" | "D" | "DD" | "d" | "dd" | "ddd" | "dddd" | "H" | "HH" | "h" | "hh" | "m" | "mm" | "s" | "ss" | "SSS" | "Z" | "ZZ" | "A" | "a" | "LT" | "LTS" | /* en: 10/10/2020, de: 10.10.2020 */ "L" | "LL" | "LLL" | "LLLL" | "l" | "ll" | "lll" | "llll" | (string & {});
|
|
267
304
|
|
|
268
|
-
export { AppDate, type GetNDaysOptions, type LocalizedFormatOptions, formatLocalTime, getLocalizedDateString, isDateString, setAppDateLanguage, setTimezone };
|
|
305
|
+
export { AppDate, type GetNDaysOptions, type LocalizedFormatOptions, type RelativeTimeOptions, formatLocalTime, getLocalizedDateString, isDateString, setAppDateLanguage, setTimezone };
|
package/dist/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
"use client";
|
|
2
|
-
import r from"dayjs";import"dayjs/locale/de";import
|
|
2
|
+
import r from"dayjs";import"dayjs/locale/de";import l from"dayjs/plugin/customParseFormat.js";import u from"dayjs/plugin/isBetween.js";import m from"dayjs/plugin/localizedFormat.js";import f from"dayjs/plugin/relativeTime.js";import p from"dayjs/plugin/timezone.js";import y from"dayjs/plugin/utc.js";r.extend(y);r.extend(p);r.extend(l);r.extend(u);r.extend(m);r.extend(f);async function Y(a){switch(a){case"de":{let t=await import("dayjs/locale/de-ch");r.locale(t.default);break}case"fr":{let t=await import("dayjs/locale/fr-ch");r.locale(t.default);break}case"sr":{let t=await import("dayjs/locale/sr");r.locale(t.default);break}case"sr-ije":{let t=await import("./sr-ijekavian-6CKRA277.js");r.locale(t.default,void 0,!0),r.locale("sr-ije");break}case"en":default:{let t=await import("dayjs/locale/en");r.locale(t.default);break}}}var i="Europe/Zurich";function k(a){i=a}var c="HH:mm",D="HH:mm:ssZ",o=class a{timezone;dayjsDate;static INVALID_DATE=r("");constructor(t,e,{invalid:n}={invalid:!1}){if(this.timezone=t,n){this.dayjsDate=a.INVALID_DATE;return}try{if(typeof e=="string"&&!h(e))throw new Error("Invalid Date string, we expect YYYY-DD-MM");this.dayjsDate=r.tz(e,t)}catch{console.warn("Could not parse date:",e),this.dayjsDate=a.INVALID_DATE}}static invalid(){return new a(i,"",{invalid:!0})}static now(){return new a(i,r())}static fromDateString(t){return new a(i,t)}static fromEpochSeconds(t){let e=r.unix(t);return new a(i,e)}static fromEpochMillis(t){let e=r(t);return new a(i,e)}static fromLocalTime(t){try{let e=r.tz(t,c,i);return new a(i,e)}catch(e){return e instanceof Error&&console.warn(`fromLocalTime(): ${e.message}`),a.invalid()}}static fromUtcString(t){let e=r.utc(t);return new a(i,e)}static fromUtcTime(t){let e=r.utc(t,D);return new a(i,e)}static minDate(){return a.fromDateString("1900-01-01")}static maxDate(){return a.fromDateString("2200-12-31")}add(t,e){let n=this.dayjsDate.add(t,e);return new a(i,n)}subtract(t,e){let n=this.dayjsDate.subtract(t,e);return new a(i,n)}startOf(t){let e=this.dayjsDate.startOf(t);return new a(i,e)}endOf(t){let e=this.dayjsDate.endOf(t);return new a(i,e)}tomorrow(){return this.add(1,"day")}isValid(){return this.dayjsDate.isValid()}isBefore(t,e){return this.dayjsDate.isBefore(t.dayjsDate,e)}isSame(t,e){return this.dayjsDate.isSame(t.dayjsDate,e)}isToday(){return this.dayjsDate.endOf("d").isSame(r().endOf("d"))}isAfter(t,e){return this.dayjsDate.isAfter(t.dayjsDate,e)}isBetween(t=a.minDate(),e=a.maxDate(),n,s){return this.dayjsDate.isBetween(t.dayjsDate,e.dayjsDate,n,s??"[)")}isFirstDayOfWeek(){return this.dayjsDate.day()===1}isWorkingDay(){return g.includes(this.dayjsDate.day())}nextWorkingDay(){let t=this.add(1,"day");return t.isWorkingDay()?t:t.nextWorkingDay()}previousWorkingDay(){let t=this.subtract(1,"day");return t.isWorkingDay()?t:t.previousWorkingDay()}addWorkingDays(t){return t<=0||!Number.isInteger(t)?this:this.nextWorkingDay().addWorkingDays(t-1)}toLocalTime(){return this.format(c)}toUtcTime(){return this.dayjsDate.utc().format(D)}toDateString(){return this.format("YYYY-MM-DD")}toLocalizedDateString({includeDayOfWeek:t=!1}={}){let e=this.dayjsDate.format("L");return t?this.dayjsDate.format("dd, ")+e:e}toUtcDateString(){return this.dayjsDate.utc().format("YYYY-MM-DD")}toUtcString(){return this.dayjsDate.utc().format()}toEpochSeconds(){return this.dayjsDate.unix()}toEpochMillis(){return this.dayjsDate.valueOf()}format(t="YYYY-MM-DDTHH:mm:ssZ[Z]"){return this.dayjsDate.format(t)}formatShort({includeDayOfWeek:t=!0}={}){return t?this.format("dd, DD.MM."):this.format("DD.MM.")}formatDateTime({includeDayOfWeek:t=!0}={}){let e=this.toLocalizedDateString({includeDayOfWeek:t}),n=this.toLocalTime();return`${e}, ${n}`}toRelative(t){let e=r(),n=Math.abs(this.dayjsDate.diff(e,"day"));return t?.fallbackAfterDays&&n>=t.fallbackAfterDays?(t.fallback??(d=>d.toLocalizedDateString()))(this):t?.cap&&n>=t.cap?(this.dayjsDate.isBefore(e)?e.subtract(t.cap,"day"):e.add(t.cap,"day")).fromNow().replace(String(t.cap),`${t.cap}+`):this.dayjsDate.fromNow()}},g=[1,2,3,4,5];function x(a,t){return o.fromDateString(a).toLocalizedDateString(t)}function z(a){return o.fromLocalTime(a).toLocalTime()}function h(a){return a?r(a,"YYYY-MM-DD",!0).isValid():!1}export{o as AppDate,z as formatLocalTime,x as getLocalizedDateString,h as isDateString,Y as setAppDateLanguage,k as setTimezone};
|
package/package.json
CHANGED