@ma.vu/appdate 0.2.0 → 0.2.1
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 +32 -2
- package/dist/index.d.ts +32 -2
- package/dist/index.js +1 -1
- package/package.json +9 -1
package/dist/index.cjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
"use client";
|
|
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=(
|
|
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=(e,t)=>()=>(e&&(t=e(e=0)),t);var l=(e,t)=>{for(var a in t)c(e,a,{get:t[a],enumerable:!0})},u=(e,t,a,n)=>{if(t&&typeof t=="object"||typeof t=="function")for(let o of b(t))!_.call(e,o)&&o!==a&&c(e,o,{get:()=>t[o],enumerable:!(n=k(t,o))||n.enumerable});return e};var s=(e,t,a)=>(a=e!=null?Y(w(e)):{},u(t||!e||!e.__esModule?c(a,"default",{value:e,enumerable:!0}):a,e)),O=e=>u(c({},"__esModule",{value:!0}),e);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:e=>`${e}.`,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 E={};l(E,{AppDate:()=>d,extendAppDate:()=>P,formatLocalTime:()=>I,getLocalizedDateString:()=>H,isDateString:()=>T,setAppDateLanguage:()=>z,setTimezone:()=>F});module.exports=O(E);var r=s(require("dayjs"),1),$=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(e){switch(e){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 i="Europe/Zurich";function F(e){i=e}var f="HH:mm",y="HH:mm:ssZ",d=class e{timezone;dayjsDate;static INVALID_DATE=(0,r.default)("");constructor(t,a,{invalid:n}={invalid:!1}){if(this.timezone=t,n){this.dayjsDate=e.INVALID_DATE;return}try{if(typeof a=="string"&&!T(a))throw new Error("Invalid Date string, we expect YYYY-DD-MM");this.dayjsDate=r.default.tz(a,t)}catch{console.warn("Could not parse date:",a),this.dayjsDate=e.INVALID_DATE}}static invalid(){return new e(i,"",{invalid:!0})}static now(){return new e(i,(0,r.default)())}static fromDateString(t){return new e(i,t)}static fromEpochSeconds(t){let a=r.default.unix(t);return new e(i,a)}static fromEpochMillis(t){let a=(0,r.default)(t);return new e(i,a)}static fromLocalTime(t){try{let a=r.default.tz(t,f,i);return new e(i,a)}catch(a){return a instanceof Error&&console.warn(`fromLocalTime(): ${a.message}`),e.invalid()}}static fromUtcString(t){let a=r.default.utc(t);return new e(i,a)}static fromUtcTime(t){let a=r.default.utc(t,y);return new e(i,a)}static minDate(){return e.fromDateString("1900-01-01")}static maxDate(){return e.fromDateString("2200-12-31")}add(t,a){let n=this.dayjsDate.add(t,a);return new e(i,n)}subtract(t,a){let n=this.dayjsDate.subtract(t,a);return new e(i,n)}startOf(t){let a=this.dayjsDate.startOf(t);return new e(i,a)}endOf(t){let a=this.dayjsDate.endOf(t);return new e(i,a)}tomorrow(){return this.add(1,"day")}isValid(){return this.dayjsDate.isValid()}isBefore(t,a){return this.dayjsDate.isBefore(t.dayjsDate,a)}isSame(t,a){return this.dayjsDate.isSame(t.dayjsDate,a)}isToday(){return this.dayjsDate.endOf("d").isSame((0,r.default)().endOf("d"))}isAfter(t,a){return this.dayjsDate.isAfter(t.dayjsDate,a)}isBetween(t=e.minDate(),a=e.maxDate(),n,o){return this.dayjsDate.isBetween(t.dayjsDate,a.dayjsDate,n,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 a=this.dayjsDate.format("L");return t?this.dayjsDate.format("dd, ")+a:a}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 a=this.toLocalizedDateString({includeDayOfWeek:t}),n=this.toLocalTime();return`${a}, ${n}`}toRelative(t){let a=(0,r.default)(),n=Math.abs(this.dayjsDate.diff(a,"day"));return t?.fallbackAfterDays&&n>=t.fallbackAfterDays?(t.fallback??(m=>m.toLocalizedDateString()))(this):t?.cap&&n>=t.cap?(this.dayjsDate.isBefore(a)?a.subtract(t.cap,"day"):a.add(t.cap,"day")).fromNow().replace(String(t.cap),`${t.cap}+`):this.dayjsDate.fromNow()}},U=[1,2,3,4,5];function H(e,t){return d.fromDateString(e).toLocalizedDateString(t)}function I(e){return d.fromLocalTime(e).toLocalTime()}function T(e){return e?(0,r.default)(e,"YYYY-MM-DD",!0).isValid():!1}function P(e){Object.assign(d.prototype,e)}0&&(module.exports={AppDate,extendAppDate,formatLocalTime,getLocalizedDateString,isDateString,setAppDateLanguage,setTimezone});
|
package/dist/index.d.cts
CHANGED
|
@@ -201,7 +201,7 @@ declare class AppDate {
|
|
|
201
201
|
|
|
202
202
|
* ```
|
|
203
203
|
*/
|
|
204
|
-
toLocalizedDateString({ includeDayOfWeek
|
|
204
|
+
toLocalizedDateString({ includeDayOfWeek }?: LocalizedFormatOptions): string;
|
|
205
205
|
toUtcDateString(): DateString;
|
|
206
206
|
toUtcString(): string;
|
|
207
207
|
/**
|
|
@@ -233,6 +233,9 @@ declare class AppDate {
|
|
|
233
233
|
*
|
|
234
234
|
* @see {@link https://day.js.org/docs/en/display/format|Day.js format documentation}
|
|
235
235
|
*
|
|
236
|
+
* TODO: Support date-fns/Unicode format tokens (e.g. EEEE for day of week)
|
|
237
|
+
* or warn when unrecognized tokens are used instead of printing them literally.
|
|
238
|
+
* Currently uses dayjs tokens: dddd = day of week, not EEEE.
|
|
236
239
|
*/
|
|
237
240
|
format(template?: FormatTemplate): string;
|
|
238
241
|
/**
|
|
@@ -300,6 +303,33 @@ declare function formatLocalTime(time: string): string;
|
|
|
300
303
|
* otherwise false
|
|
301
304
|
*/
|
|
302
305
|
declare function isDateString(date: string | undefined | null | Dayjs): date is DateString;
|
|
306
|
+
/**
|
|
307
|
+
* Extend AppDate prototype with custom methods.
|
|
308
|
+
* Use TypeScript module augmentation to add type definitions.
|
|
309
|
+
*
|
|
310
|
+
* @example
|
|
311
|
+
* ```typescript
|
|
312
|
+
* // In your app
|
|
313
|
+
* import { AppDate, extendAppDate } from "@ma.vu/appdate";
|
|
314
|
+
*
|
|
315
|
+
* extendAppDate({
|
|
316
|
+
* formatLong(this: AppDate) {
|
|
317
|
+
* return this.format("dddd, DD. MMMM");
|
|
318
|
+
* },
|
|
319
|
+
* });
|
|
320
|
+
*
|
|
321
|
+
* // Augment types
|
|
322
|
+
* declare module "@ma.vu/appdate" {
|
|
323
|
+
* interface AppDate {
|
|
324
|
+
* formatLong(): string;
|
|
325
|
+
* }
|
|
326
|
+
* }
|
|
327
|
+
*
|
|
328
|
+
* // Now you can use it
|
|
329
|
+
* AppDate.now().formatLong(); // "ponedjeljak, 06. januar"
|
|
330
|
+
* ```
|
|
331
|
+
*/
|
|
332
|
+
declare function extendAppDate<T extends Record<string, unknown>>(methods: T): void;
|
|
303
333
|
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 & {});
|
|
304
334
|
|
|
305
|
-
export { AppDate, type GetNDaysOptions, type LocalizedFormatOptions, type RelativeTimeOptions, formatLocalTime, getLocalizedDateString, isDateString, setAppDateLanguage, setTimezone };
|
|
335
|
+
export { AppDate, type GetNDaysOptions, type LocalizedFormatOptions, type RelativeTimeOptions, extendAppDate, formatLocalTime, getLocalizedDateString, isDateString, setAppDateLanguage, setTimezone };
|
package/dist/index.d.ts
CHANGED
|
@@ -201,7 +201,7 @@ declare class AppDate {
|
|
|
201
201
|
|
|
202
202
|
* ```
|
|
203
203
|
*/
|
|
204
|
-
toLocalizedDateString({ includeDayOfWeek
|
|
204
|
+
toLocalizedDateString({ includeDayOfWeek }?: LocalizedFormatOptions): string;
|
|
205
205
|
toUtcDateString(): DateString;
|
|
206
206
|
toUtcString(): string;
|
|
207
207
|
/**
|
|
@@ -233,6 +233,9 @@ declare class AppDate {
|
|
|
233
233
|
*
|
|
234
234
|
* @see {@link https://day.js.org/docs/en/display/format|Day.js format documentation}
|
|
235
235
|
*
|
|
236
|
+
* TODO: Support date-fns/Unicode format tokens (e.g. EEEE for day of week)
|
|
237
|
+
* or warn when unrecognized tokens are used instead of printing them literally.
|
|
238
|
+
* Currently uses dayjs tokens: dddd = day of week, not EEEE.
|
|
236
239
|
*/
|
|
237
240
|
format(template?: FormatTemplate): string;
|
|
238
241
|
/**
|
|
@@ -300,6 +303,33 @@ declare function formatLocalTime(time: string): string;
|
|
|
300
303
|
* otherwise false
|
|
301
304
|
*/
|
|
302
305
|
declare function isDateString(date: string | undefined | null | Dayjs): date is DateString;
|
|
306
|
+
/**
|
|
307
|
+
* Extend AppDate prototype with custom methods.
|
|
308
|
+
* Use TypeScript module augmentation to add type definitions.
|
|
309
|
+
*
|
|
310
|
+
* @example
|
|
311
|
+
* ```typescript
|
|
312
|
+
* // In your app
|
|
313
|
+
* import { AppDate, extendAppDate } from "@ma.vu/appdate";
|
|
314
|
+
*
|
|
315
|
+
* extendAppDate({
|
|
316
|
+
* formatLong(this: AppDate) {
|
|
317
|
+
* return this.format("dddd, DD. MMMM");
|
|
318
|
+
* },
|
|
319
|
+
* });
|
|
320
|
+
*
|
|
321
|
+
* // Augment types
|
|
322
|
+
* declare module "@ma.vu/appdate" {
|
|
323
|
+
* interface AppDate {
|
|
324
|
+
* formatLong(): string;
|
|
325
|
+
* }
|
|
326
|
+
* }
|
|
327
|
+
*
|
|
328
|
+
* // Now you can use it
|
|
329
|
+
* AppDate.now().formatLong(); // "ponedjeljak, 06. januar"
|
|
330
|
+
* ```
|
|
331
|
+
*/
|
|
332
|
+
declare function extendAppDate<T extends Record<string, unknown>>(methods: T): void;
|
|
303
333
|
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 & {});
|
|
304
334
|
|
|
305
|
-
export { AppDate, type GetNDaysOptions, type LocalizedFormatOptions, type RelativeTimeOptions, formatLocalTime, getLocalizedDateString, isDateString, setAppDateLanguage, setTimezone };
|
|
335
|
+
export { AppDate, type GetNDaysOptions, type LocalizedFormatOptions, type RelativeTimeOptions, extendAppDate, 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 l from"dayjs/plugin/customParseFormat.js";import u from"dayjs/plugin/isBetween.js";import m from"dayjs/plugin/localizedFormat.js";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 p from"dayjs/plugin/relativeTime.js";import f from"dayjs/plugin/timezone.js";import y from"dayjs/plugin/utc.js";r.extend(y);r.extend(f);r.extend(l);r.extend(u);r.extend(m);r.extend(p);async function x(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",s=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,o){return this.dayjsDate.isBetween(t.dayjsDate,e.dayjsDate,n,o??"[)")}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 Y(a,t){return s.fromDateString(a).toLocalizedDateString(t)}function z(a){return s.fromLocalTime(a).toLocalTime()}function h(a){return a?r(a,"YYYY-MM-DD",!0).isValid():!1}function v(a){Object.assign(s.prototype,a)}export{s as AppDate,v as extendAppDate,z as formatLocalTime,Y as getLocalizedDateString,h as isDateString,x as setAppDateLanguage,k as setTimezone};
|
package/package.json
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
{
|
|
2
|
-
"version": "0.2.
|
|
2
|
+
"version": "0.2.1",
|
|
3
3
|
"name": "@ma.vu/appdate",
|
|
4
4
|
"private": false,
|
|
5
5
|
"main": "dist/index.js",
|
|
@@ -22,12 +22,20 @@
|
|
|
22
22
|
"scripts": {
|
|
23
23
|
"build": "tsup",
|
|
24
24
|
"test": "bun test",
|
|
25
|
+
"lint": "eslint src/",
|
|
26
|
+
"format": "prettier --check src/",
|
|
27
|
+
"format:fix": "prettier --write src/",
|
|
28
|
+
"validate": "bun run lint && bun run test && bun run format",
|
|
25
29
|
"docs": "typedoc src/index.ts",
|
|
26
30
|
"bump": "npm version patch",
|
|
27
31
|
"prepublishOnly": "bun run build && bun run docs"
|
|
28
32
|
},
|
|
29
33
|
"devDependencies": {
|
|
34
|
+
"@typescript-eslint/eslint-plugin": "^8.52.0",
|
|
35
|
+
"@typescript-eslint/parser": "^8.52.0",
|
|
30
36
|
"bun-types": "latest",
|
|
37
|
+
"eslint": "^9.39.2",
|
|
38
|
+
"prettier": "^3.7.4",
|
|
31
39
|
"tsup": "~8.2.4",
|
|
32
40
|
"typedoc": "~0.28.15",
|
|
33
41
|
"vite": "~5.3.5",
|